public static void AddRange <T>(this DynamicBuffer <T> buffer, T[] array) where T : struct { var a = new NativeArray <T>(array, Allocator.Temp); buffer.AddRange(a); a.Dispose(); }
private void Populate(Entity entity, ref DynamicBuffer <MeshVertex> vertices, ref DynamicBuffer <MeshVertexIndex> triangles, int startIndex) { var textVertices = VerticesFromEntity[entity]; var textIndices = TrianglesFromEntity[entity]; for (int i = 0; i < textIndices.Length; i++) { triangles.Add(new MeshVertexIndex() { Value = textIndices[i].Value + startIndex }); } vertices.AddRange(textVertices.Reinterpret <MeshVertex>().AsNativeArray()); }
private void ShowAdd() { DynamicBuffer <int> buffer = new DynamicBuffer <int>(); DynamicBuffer <int> secondBuffer = new DynamicBuffer <int>(); #region dynamicbuffer.add buffer.Add(5); #endregion #region dynamicbuffer.addrange int[] source = { 1, 2, 3, 4, 5 }; NativeArray <int> newElements = new NativeArray <int>(source, Allocator.Persistent); buffer.AddRange(newElements); #endregion #region dynamicbuffer.asnativearray int[] intArray = { 1, 2, 3, 4, 5 }; NativeArray <int> .Copy(intArray, buffer.AsNativeArray()); #endregion #region dynamicbuffer.capacity #endregion #region dynamicbuffer.clear buffer.Clear(); #endregion #region dynamicbuffer.copyfrom.dynamicbuffer buffer.CopyFrom(secondBuffer); #endregion #region dynamicbuffer.copyfrom.nativearray int[] sourceArray = { 1, 2, 3, 4, 5 }; NativeArray <int> nativeArray = new NativeArray <int>(source, Allocator.Persistent); buffer.CopyFrom(nativeArray); #endregion #region dynamicbuffer.copyfrom.nativeslice NativeSlice <int> nativeSlice = new NativeSlice <int>(nativeArray, 1, 3); buffer.CopyFrom(nativeSlice); #endregion #region dynamicbuffer.copyfrom.array int[] integerArray = { 1, 2, 3, 4, 5 }; buffer.CopyFrom(integerArray); #endregion #region dynamicbuffer.getenumerator foreach (var element in buffer) { //Use element... } #endregion #region dynamicbuffer.getunsafeptr #endregion int insertionIndex = 2; #region dynamicbuffer.insert if (insertionIndex < buffer.Length) { buffer.Insert(insertionIndex, 6); } #endregion #region dynamicbuffer.iscreated #endregion #region dynamicbuffer.length for (int i = 0; i < buffer.Length; i++) { buffer[i] = i * i; } #endregion #region dynamicbuffer.removeat if (insertionIndex < buffer.Length) { buffer.RemoveAt(insertionIndex); } #endregion int start = 1; #region dynamicbuffer.removerange buffer.RemoveRange(start, 5); #endregion #region dynamicbuffer.reserve buffer.EnsureCapacity(buffer.Capacity + 10); #endregion #region dynamicbuffer.resizeuninitialized buffer.ResizeUninitialized(buffer.Length + 10); #endregion #region dynamicbuffer.indexoperator for (int i = 0; i < buffer.Length; i++) { buffer[i] = i * i; } #endregion #region dynamicbuffer.tonativearray NativeArray <int> copy = buffer.ToNativeArray(Allocator.Persistent); #endregion #region dynamicbuffer.trimexcess if (buffer.Capacity > buffer.Length) { buffer.TrimExcess(); } #endregion }
private void AddQuad( float3 v1, float3 v2, float3 v3, float3 v4, int width, int height, Voxel voxel, bool backFace, float scale, DynamicBuffer <Vertex> vertices, DynamicBuffer <Index> indices, DynamicBuffer <UV0> UVs) { NativeArray <Vertex> voxelVertices = new NativeArray <Vertex>(4, Allocator.Temp); NativeArray <Index> voxelIndices = new NativeArray <Index>(6, Allocator.Temp); NativeArray <UV0> voxelUVs = new NativeArray <UV0>(4, Allocator.Temp); voxelVertices[0] = new Vertex { Value = v1 * scale }; voxelVertices[1] = new Vertex { Value = v2 * scale }; voxelVertices[2] = new Vertex { Value = v3 * scale }; voxelVertices[3] = new Vertex { Value = v4 * scale }; int indexOffset = vertices.Length; if (backFace) { voxelIndices[0] = new Index { value = indexOffset + 2 }; voxelIndices[1] = new Index { value = indexOffset + 1 }; voxelIndices[2] = new Index { value = indexOffset }; voxelIndices[3] = new Index { value = indexOffset }; voxelIndices[4] = new Index { value = indexOffset + 3 }; voxelIndices[5] = new Index { value = indexOffset + 2 }; } else { voxelIndices[0] = new Index { value = indexOffset }; voxelIndices[1] = new Index { value = indexOffset + 1 }; voxelIndices[2] = new Index { value = indexOffset + 2 }; voxelIndices[3] = new Index { value = indexOffset + 2 }; voxelIndices[4] = new Index { value = indexOffset + 3 }; voxelIndices[5] = new Index { value = indexOffset }; } VoxelConcurrent currentVoxel = ConcurrentVoxels[voxel.DatabaseIndex]; for (int i = 0; i < voxelUVs.Length; i++) { float2 uv = float2.zero; switch (voxel.Side) { case TOP: uv = currentVoxel.TopUV; break; case BOTTOM: uv = currentVoxel.BottomUV; break; case WEST: uv = currentVoxel.WestUV; break; case EAST: uv = currentVoxel.EastUV; break; case NORTH: uv = currentVoxel.NorthUV; break; case SOUTH: uv = currentVoxel.SouthUV; break; } voxelUVs[i] = new UV0 { Value = uv }; } vertices.AddRange(voxelVertices); indices.AddRange(voxelIndices); UVs.AddRange(voxelUVs); voxelVertices.Dispose(); voxelIndices.Dispose(); voxelUVs.Dispose(); }
void AddDashedLaneLine( NativeArray <RigidTransform> samples, float xOffset, RoadMarking marking, DynamicBuffer <CombinedVertex> vertexBuffer, DynamicBuffer <Triangle> triangleBuffer, DynamicBuffer <SubMesh> subMeshBuffer, DynamicBuffer <RigidTransform> roadMarkingSamples, DynamicBuffer <DashPoint> dashPoints) { DashSamples(samples, marking, out var dashSamples, out var dashCaps); roadMarkingSamples.AddRange(dashSamples); dashPoints.AddRange(dashCaps); var markingWidth = marking.Width / 2; var leftOffsetSpline = SplineUtility.OffsetSpline(dashSamples, new float3(-markingWidth + xOffset, 0f, 0f), Allocator.Temp); var rightOffsetSpline = SplineUtility.OffsetSpline(dashSamples, new float3(markingWidth + xOffset, 0f, 0f), Allocator.Temp); var dashVertices = new NativeList <CombinedVertex>(leftOffsetSpline.Length / 2, Allocator.Temp); var nonDashVertices = new NativeList <CombinedVertex>(leftOffsetSpline.Length / 2, Allocator.Temp); var upVector = new float3(0f, 1f, 0f); for (var i = 0; i < dashSamples.Length - 1; i++) { if (dashCaps[i].Cap == DashCap.Start) { var leftPoint = leftOffsetSpline[i]; dashVertices.Add(new CombinedVertex { Vertex = leftPoint.pos, Normal = math.mul(leftPoint.rot, upVector), Uv = leftPoint.pos.xz }); var rightPoint = rightOffsetSpline[i]; dashVertices.Add(new CombinedVertex { Vertex = rightPoint.pos, Normal = math.mul(rightPoint.rot, upVector), Uv = rightPoint.pos.xz }); leftPoint = leftOffsetSpline[i + 1]; dashVertices.Add(new CombinedVertex { Vertex = leftPoint.pos, Normal = math.mul(leftPoint.rot, upVector), Uv = leftPoint.pos.xz }); rightPoint = rightOffsetSpline[i + 1]; dashVertices.Add(new CombinedVertex { Vertex = rightPoint.pos, Normal = math.mul(rightPoint.rot, upVector), Uv = rightPoint.pos.xz }); } else { var leftPoint = leftOffsetSpline[i]; nonDashVertices.Add(new CombinedVertex { Vertex = leftPoint.pos, Normal = math.mul(leftPoint.rot, upVector), Uv = leftPoint.pos.xz }); var rightPoint = rightOffsetSpline[i]; nonDashVertices.Add(new CombinedVertex { Vertex = rightPoint.pos, Normal = math.mul(rightPoint.rot, upVector), Uv = rightPoint.pos.xz }); leftPoint = leftOffsetSpline[i + 1]; nonDashVertices.Add(new CombinedVertex { Vertex = leftPoint.pos, Normal = math.mul(leftPoint.rot, upVector), Uv = leftPoint.pos.xz }); rightPoint = rightOffsetSpline[i + 1]; nonDashVertices.Add(new CombinedVertex { Vertex = rightPoint.pos, Normal = math.mul(rightPoint.rot, upVector), Uv = rightPoint.pos.xz }); } } leftOffsetSpline.Dispose(); rightOffsetSpline.Dispose(); dashSamples.Dispose(); dashCaps.Dispose(); var dashSubMesh = new SubMesh { VertexCount = dashVertices.Length, VertexStartIndex = vertexBuffer.Length, TriangleCount = (dashVertices.Length * 3) / 2, TriangleStartIndex = triangleBuffer.Length, Material = marking.Material }; var nonDashSubMesh = new SubMesh { VertexCount = nonDashVertices.Length, VertexStartIndex = dashSubMesh.VertexStartIndex + dashSubMesh.VertexCount, TriangleCount = (nonDashVertices.Length * 3) / 2, TriangleStartIndex = dashSubMesh.TriangleStartIndex + dashSubMesh.TriangleCount, Material = RoadMaterial.RoadSurface }; vertexBuffer.AddRange(dashVertices); vertexBuffer.AddRange(nonDashVertices); triangleBuffer.ResizeUninitialized(triangleBuffer.Length + dashSubMesh.TriangleCount + nonDashSubMesh.TriangleCount); for (int i = 0, j = dashSubMesh.TriangleStartIndex; i < dashSubMesh.VertexCount; i += 4) { triangleBuffer[j++] = i; triangleBuffer[j++] = i + 3; triangleBuffer[j++] = i + 1; triangleBuffer[j++] = i; triangleBuffer[j++] = i + 2; triangleBuffer[j++] = i + 3; } for (int i = 0, j = nonDashSubMesh.TriangleStartIndex; i < nonDashSubMesh.VertexCount; i += 4) { triangleBuffer[j++] = i; triangleBuffer[j++] = i + 3; triangleBuffer[j++] = i + 1; triangleBuffer[j++] = i; triangleBuffer[j++] = i + 2; triangleBuffer[j++] = i + 3; } subMeshBuffer.Add(dashSubMesh); subMeshBuffer.Add(nonDashSubMesh); dashVertices.Dispose(); nonDashVertices.Dispose(); }