Skip to content

Commit a1c46c0

Browse files
Merge pull request #29 from Unity-Technologies/return_when_vertex_layout_unexpected
Return when vertex layout unexpected
2 parents 4ba079c + 0d2b295 commit a1c46c0

File tree

2 files changed

+25
-11
lines changed

2 files changed

+25
-11
lines changed

PluginSource/source/RenderingPlugin.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,13 @@ static void ModifyVertexBuffer()
262262
return;
263263
int vertexStride = int(bufferSize / vertexCount);
264264

265+
// Unity should return us a buffer that is the size of `vertexCount * sizeof(MeshVertex)`
266+
// If that's not the case then we should quit to avoid unexpected results.
267+
// This can happen if https://docs.unity3d.com/ScriptReference/Mesh.GetNativeVertexBufferPtr.html returns
268+
// a pointer to a buffer with an unexpected layout.
269+
if (static_cast<unsigned int>(vertexStride) != sizeof(MeshVertex))
270+
return;
271+
265272
const float t = g_Time * 3.0f;
266273

267274
char* bufferPtr = (char*)bufferDataPtr;

UnityProject/Assets/UseRenderingPlugin.cs

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
using System;
33
using System.Collections;
44
using System.Runtime.InteropServices;
5-
5+
using UnityEngine.Rendering;
66

77
public class UseRenderingPlugin : MonoBehaviour
88
{
@@ -80,18 +80,25 @@ private void CreateTextureAndPassToPlugin()
8080
private void SendMeshBuffersToPlugin ()
8181
{
8282
var filter = GetComponent<MeshFilter> ();
83-
var mesh = filter.mesh;
84-
// The plugin will want to modify the vertex buffer -- on many platforms
85-
// for that to work we have to mark mesh as "dynamic" (which makes the buffers CPU writable --
86-
// by default they are immutable and only GPU-readable).
83+
var mesh = filter.mesh;
84+
85+
// This is equivalent to MeshVertex in RenderingPlugin.cpp
86+
var desiredVertexLayout = new[]
87+
{
88+
new VertexAttributeDescriptor(VertexAttribute.Position, VertexAttributeFormat.Float32, 3),
89+
new VertexAttributeDescriptor(VertexAttribute.Normal, VertexAttributeFormat.Float32, 3),
90+
new VertexAttributeDescriptor(VertexAttribute.Color, VertexAttributeFormat.Float32, 4),
91+
new VertexAttributeDescriptor(VertexAttribute.TexCoord0, VertexAttributeFormat.Float32, 2)
92+
};
93+
94+
// Let's be certain we'll get the vertex buffer layout we want in native code
95+
mesh.SetVertexBufferParams(mesh.vertexCount, desiredVertexLayout);
96+
97+
// The plugin will want to modify the vertex buffer -- on many platforms
98+
// for that to work we have to mark mesh as "dynamic" (which makes the buffers CPU writable --
99+
// by default they are immutable and only GPU-readable).
87100
mesh.MarkDynamic ();
88101

89-
// Make sure to have vertex colors so that the plugin can rely on a known
90-
// vertex layout (position+normal+color+UV). Since Unity 2019.3 it's easier
91-
// since there are APIs to query all that info.
92-
var colors = mesh.colors;
93-
mesh.colors = colors;
94-
95102
// However, mesh being dynamic also means that the CPU on most platforms can not
96103
// read from the vertex buffer. Our plugin also wants original mesh data,
97104
// so let's pass it as pointers to regular C# arrays.

0 commit comments

Comments
 (0)