|
2 | 2 | using System; |
3 | 3 | using System.Collections; |
4 | 4 | using System.Runtime.InteropServices; |
5 | | - |
| 5 | +using UnityEngine.Rendering; |
6 | 6 |
|
7 | 7 | public class UseRenderingPlugin : MonoBehaviour |
8 | 8 | { |
@@ -80,18 +80,25 @@ private void CreateTextureAndPassToPlugin() |
80 | 80 | private void SendMeshBuffersToPlugin () |
81 | 81 | { |
82 | 82 | 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). |
87 | 100 | mesh.MarkDynamic (); |
88 | 101 |
|
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 | | - |
95 | 102 | // However, mesh being dynamic also means that the CPU on most platforms can not |
96 | 103 | // read from the vertex buffer. Our plugin also wants original mesh data, |
97 | 104 | // so let's pass it as pointers to regular C# arrays. |
|
0 commit comments