private void InitMeshData(ref MeshData meshData, GraphInfo graphInfo) { var parameters = UIVertexData.AllocateVertexDescriptor(Allocator.Temp); meshData.SetVertexBufferParams(graphInfo.renderBoxCount * 4, parameters); meshData.SetIndexBufferParams(graphInfo.renderBoxCount * 6, UnityEngine.Rendering.IndexFormat.UInt16); meshData.subMeshCount = graphInfo.MeshCount; }
private void Render(NativeArray <UIVertexData> vertices, UIContextData *context, ref MeshData meshData, UIGraphData graph, NativeArray <NodeInfo> layout, GraphInfo graphInfo, NativeArray <UIPassState> stateLayout) { var indices = meshData.GetIndexData <ushort>(); var subMeshes = new NativeArray <SubMeshInfo>(graphInfo.subMeshCount, Allocator.Temp); int submeshIndex = 0; int renderIndex = 0; float4 bounds = float4.zero; RenderMesh(0, -1, 0, vertices, context, indices, graph, graphInfo, layout, stateLayout, subMeshes, true, true, true, ref submeshIndex, ref renderIndex, ref bounds); float4 totalBounds = bounds; int submesh0RenderIndexCount = renderIndex; for (int i = 0; i < subMeshes.Length; i++) { SubMeshInfo current = subMeshes[i]; var initialRenderIndex = renderIndex; RenderMesh(current.nodeIndex, -1, current.nodeIndex, vertices, context, indices, graph, graphInfo, layout, stateLayout, subMeshes, true, false, false, ref submeshIndex, ref renderIndex, ref bounds); totalBounds = new float4(math.min(totalBounds.x, bounds.x), math.min(totalBounds.y, bounds.y), math.max(totalBounds.z, bounds.z), math.max(totalBounds.w, bounds.w)); current.meshIndexStart = initialRenderIndex * 6; current.meshIndexCount = (renderIndex - initialRenderIndex) * 6; current.bounds = bounds; subMeshes[i] = current; for (int j = 0; j < (renderIndex - initialRenderIndex); j++) { for (int k = 0; k < 4; k++) { UIVertexData vertex = vertices[(initialRenderIndex + j) * 4 + k]; vertex.position.z -= 0.001f * (i + 1); vertices[(initialRenderIndex + j) * 4 + k] = vertex; } } } //Center Mesh float3 totalSize = new float3(math.abs(totalBounds.z - totalBounds.x), math.abs(totalBounds.y - totalBounds.w), 0f); var adjust = new float3(totalSize.x / 2f, totalSize.y / 2f, 0); for (int i = 0; i < vertices.Length; i++) { UIVertexData vertex = vertices[i]; vertex.position -= adjust; vertices[i] = vertex; } for (int i = 0; i < subMeshes.Length; i++) { SubMeshInfo current = subMeshes[i]; var size = new float3(math.abs(current.bounds.z - current.bounds.x), math.abs(current.bounds.y - current.bounds.w), 0.00001f); meshData.SetSubMesh(meshData.subMeshCount - (i + 1), new UnityEngine.Rendering.SubMeshDescriptor(current.meshIndexStart, current.meshIndexCount) { //bounds = new Bounds(new float3(current.bounds.x + (size.x / 2f), bounds.y + (size.y / 2f), 0f) - adjust, size), bounds = new Bounds(float3.zero, size), firstVertex = (current.meshIndexStart / 6) * 4, vertexCount = (current.meshIndexCount / 6) * 4 } //,MeshUpdateFlags.DontRecalculateBounds | MeshUpdateFlags.DontValidateIndices ); } meshData.SetSubMesh(0, new UnityEngine.Rendering.SubMeshDescriptor(0, submesh0RenderIndexCount * 6) { bounds = new Bounds(float3.zero, new float3(totalSize.x, totalSize.y, 0.00001f)), firstVertex = 0, vertexCount = submesh0RenderIndexCount * 4 } //,MeshUpdateFlags.DontRecalculateBounds | MeshUpdateFlags.DontValidateIndices ); subMeshes.Dispose(); }