Example #1
0
    public void Increment()
    {
        var counter = new NativeCounter(Allocator.Temp);

        Assert.AreEqual(0, counter.Count);
        counter.Increment();
        Assert.AreEqual(1, counter.Count);
        counter.Increment();
        counter.Increment();
        Assert.AreEqual(3, counter.Count);
        counter.Dispose();
    }
Example #2
0
            public void Execute()
            {
                // Contact constraints
                for (int i = 0; i < CollisionManifoldsArray.Length; i++)
                {
                    float3 comAToContactPoint = (CollisionManifoldsArray[i].ContactPointA - RigidBodyFromEntity[CollisionManifoldsArray[i].RigidBodyEntityA].CenterOfMass);
                    float3 comBToContactPoint = (CollisionManifoldsArray[i].ContactPointB - RigidBodyFromEntity[CollisionManifoldsArray[i].RigidBodyEntityB].CenterOfMass);

                    ConstraintsArray[ConstraintsCounter.Count] = new Constraint
                    {
                        ConstraintJacobianMatrix = new float12
                        {
                            aa = -CollisionManifoldsArray[i].CollisionNormalAToB,
                            ab = -math.cross(comAToContactPoint, CollisionManifoldsArray[i].CollisionNormalAToB),
                            ba = CollisionManifoldsArray[i].CollisionNormalAToB,
                            bb = math.cross(comBToContactPoint, CollisionManifoldsArray[i].CollisionNormalAToB),
                        },
                        RigidbodyA     = CollisionManifoldsArray[i].RigidBodyEntityA,
                        RigidbodyB     = CollisionManifoldsArray[i].RigidBodyEntityB,
                        BaumgarteDepth = CollisionManifoldsArray[i].OverlapDistance,
                    };

                    ConstraintsCounter.Increment();
                }
            }
 public void Execute()
 {
     for (var i = 0; i < 10; i++)
     {
         Kek.Increment();
     }
 }
            public void Execute()
            {
                var prevIndex = -1;

                for (var i = SortedEntities.Length - 1; i >= 0; --i)
                {
                    var entity = SortedEntities[i].Value;
                    if (!EntitiesIndexMap.TryGetValue(entity, out var index))
                    {
                        continue;
                    }
                    var vertexData               = Vertices[entity];
                    var vertexIndexData          = VertexIndices[entity];
                    var sharedComponentIndex     = SharedComponentIndices[index];
                    var prevSharedComponentIndex = prevIndex >= 0 ? SharedComponentIndices[prevIndex] : -1;
                    Offsets[index] = new OffsetInfo {
                        Vertex            = VertexCounter.Value,
                        VertexCount       = vertexData.Length,
                        Indices           = VertexIndexCounter.Value,
                        SubMeshIndex      = sharedComponentIndex != prevSharedComponentIndex ? SubMeshCounter.Value : -1,
                        SubMesh           = sharedComponentIndex != prevSharedComponentIndex ? VertexIndexCounter.Value : -1,
                        SubMeshMaterialId = sharedComponentIndex != prevSharedComponentIndex ? sharedComponentIndex : -1
                    };
                    if (sharedComponentIndex != prevSharedComponentIndex)
                    {
                        SubMeshCounter.Increment(1);
                    }
                    VertexCounter.Increment(vertexData.Length);
                    VertexIndexCounter.Increment(vertexIndexData.Length);
                    prevIndex = index;
                }
            }
Example #5
0
    public void SetCountIncrement()
    {
        int count;
        var counter = new NativeCounter(Allocator.Temp);

        Assert.AreEqual(0, counter.Count);
        count = counter.Increment();
        Assert.AreEqual(1, count);
        Assert.AreEqual(1, counter.Count);
        counter.Count = 40;
        Assert.AreEqual(40, counter.Count);
        count = counter.Increment();
        count = counter.Increment();
        Assert.AreEqual(42, count);
        Assert.AreEqual(42, counter.Count);
        counter.Dispose();
    }
Example #6
0
        void CreateOffsets(
            NativeHashMap <Entity, int> entitiesIndexMap,
            NativeArray <SortedEntity> sortedEntities,
            NativeArray <int> sharedComponentIndices,
            NativeArray <OffsetInfo> offsets,
            NativeCounter vertexCounter,
            NativeCounter vertexIndexCounter,
            NativeCounter subMeshCounter)
        {
            var vertices      = GetBufferFromEntity <Vertex>(true);
            var vertexIndices = GetBufferFromEntity <VertexIndex>(true);

            Job
            .WithReadOnly(entitiesIndexMap)
            .WithReadOnly(sortedEntities)
            .WithReadOnly(sharedComponentIndices)
            .WithReadOnly(vertices)
            .WithReadOnly(vertexIndices)
            .WithCode(() => {
                var prevIndex = -1;
                for (var i = sortedEntities.Length - 1; i >= 0; --i)
                {
                    var entity = sortedEntities[i].Value;
                    if (!entitiesIndexMap.TryGetValue(entity, out var index))
                    {
                        continue;
                    }
                    var vertexData               = vertices[entity];
                    var vertexIndexData          = vertexIndices[entity];
                    var sharedComponentIndex     = sharedComponentIndices[index];
                    var prevSharedComponentIndex = prevIndex >= 0 ? sharedComponentIndices[prevIndex] : -1;
                    offsets[index]               = new OffsetInfo {
                        Vertex            = vertexCounter.Value,
                        VertexCount       = vertexData.Length,
                        Indices           = vertexIndexCounter.Value,
                        SubMeshIndex      = sharedComponentIndex != prevSharedComponentIndex ? subMeshCounter.Value : -1,
                        SubMesh           = sharedComponentIndex != prevSharedComponentIndex ? vertexIndexCounter.Value : -1,
                        SubMeshMaterialId = sharedComponentIndex != prevSharedComponentIndex ? sharedComponentIndex : -1
                    };
                    if (sharedComponentIndex != prevSharedComponentIndex)
                    {
                        subMeshCounter.Increment(1);
                    }
                    vertexCounter.Increment(vertexData.Length);
                    vertexIndexCounter.Increment(vertexIndexData.Length);
                    prevIndex = index;
                }
            }).Schedule();
        }
    public void Execute()
    {
        for (int x = 0; x < WorldSettings.chunkDimension.x - 1; x++)
        {
            for (int y = 0; y < WorldSettings.chunkDimension.y - 1; y++)
            {
                for (int z = 0; z < WorldSettings.chunkDimension.z - 1; z++)
                {
                    int3 coord = new int3(x, y, z);

                    int cubeIndex = 0;
                    for (int vertex = 0; vertex < 8; vertex++)
                    {
                        if (voxelData[VertexIndex(coord, vertex)].density < WorldSettings.isoLevel)
                        {
                            cubeIndex |= (1 << vertex);
                        }
                    }

                    if (cubeIndex == 0 || cubeIndex == 255)
                    {
                        continue;
                    }

                    for (int i = 0; i < 15 && MarchingCubeTable.triTable[cubeIndex, i] != -1; i += 3)
                    {
                        int vertexIndex = counter.Increment() * 3;

                        float3 v1 = Interpolate(coord, MarchingCubeTable.triTable[cubeIndex, i]);
                        float3 v2 = Interpolate(coord, MarchingCubeTable.triTable[cubeIndex, i + 1]);
                        float3 v3 = Interpolate(coord, MarchingCubeTable.triTable[cubeIndex, i + 2]);

                        float3 normal = math.normalize(math.cross(v2 - v1, v3 - v1));

                        vertexData[vertexIndex] = new VertexData(v1, normal, Color.white);
                        triangles[vertexIndex]  = (ushort)(vertexIndex);

                        vertexData[vertexIndex + 1] = new VertexData(v2, normal, Color.white);
                        triangles[vertexIndex + 1]  = (ushort)(vertexIndex + 1);

                        vertexData[vertexIndex + 2] = new VertexData(v3, normal, Color.white);
                        triangles[vertexIndex + 2]  = (ushort)(vertexIndex + 2);
                    }
                }
            }
        }
    }
Example #8
0
 public void Counter_Increment_Increases_Count_By_1()
 {
     counter.Increment();
     Assert.AreEqual(1, counter.Count);
 }
    static unsafe void GreedyMeshingForJob(Vector2Int chunkSize, Vector2 chunkScale, NativeArray <Vector3> vertices, NativeCounter counter, NativeArray <bool> quadSet)
    {
        for (int x = 0; x < chunkSize.x; x++)
        {
            for (int y = 0; y < chunkSize.y;)
            {
                int        idx          = x + y * chunkSize.x;
                Vector2Int gridPosition = new Vector2Int(x, y);
                if (!quadSet[idx])
                {
                    y++;
                    continue;
                }

                // 높이 계산
                int height = 0;
                for (int dy = gridPosition.y; dy < chunkSize.y; dy++)
                {
                    Vector2Int subGridPosition = new Vector2Int(gridPosition.x, dy);
                    if (!quadSet[subGridPosition.x + subGridPosition.y * chunkSize.x])
                    {
                        break;
                    }

                    height++;
                }

                // 넓이 계산
                int  width = 1;
                bool done  = false;
                for (int dx = gridPosition.x + 1; dx < chunkSize.x; dx++)
                {
                    for (int dy = gridPosition.y; dy < gridPosition.y + height && dy < chunkSize.y; dy++)
                    {
                        Vector2Int subGridPosition = new Vector2Int(dx, dy);
                        if (!quadSet[subGridPosition.x + subGridPosition.y * chunkSize.x])
                        {
                            done = true;
                            break;
                        }
                    }

                    if (done)
                    {
                        break;
                    }

                    width++;
                }

                // 사용한 quadMap 삭제하기
                for (int dx = gridPosition.x; dx < gridPosition.x + width; dx++)
                {
                    for (int dy = gridPosition.y; dy < gridPosition.y + height; dy++)
                    {
                        quadSet[dx + dy * chunkSize.x] = false;
                    }
                }

                // width height 만큼 mesh 만들기
                Vector2Int size = new Vector2Int(width, height);
                Vector2 *  quad = stackalloc[] { (gridPosition + CornerTable[0] * size) * chunkScale, (gridPosition + CornerTable[1] * size) * chunkScale, (gridPosition + CornerTable[2] * size) * chunkScale, (gridPosition + CornerTable[3] * size) * chunkScale };

                for (int i = 0; i < 6; i += 3)
                {
                    int index0 = TriangleTable[15][i];
                    int index1 = TriangleTable[15][i + 1];
                    int index2 = TriangleTable[15][i + 2];

                    Vector3 vertex0 = quad[index0];
                    Vector3 vertex1 = quad[index1];
                    Vector3 vertex2 = quad[index2];

                    int triangleIndex = counter.Increment() * 3;

                    vertices[triangleIndex]     = vertex0;
                    vertices[triangleIndex + 1] = vertex1;
                    vertices[triangleIndex + 2] = vertex2;
                }

                y += height;
            }
        }
    }
Example #10
0
            public void Execute()
            {
                for (int direction = 0; direction < 6; direction++)
                {
                    NativeHashMap <int3, Empty> hashMap = new NativeHashMap <int3, Empty>(chunkSize[VoxelUtil.DirectionAlignedX[direction]] * chunkSize[VoxelUtil.DirectionAlignedY[direction]], Allocator.Temp);
                    for (int depth = 0; depth < chunkSize[VoxelUtil.DirectionAlignedZ[direction]]; depth++)
                    {
                        for (int x = 0; x < chunkSize[VoxelUtil.DirectionAlignedX[direction]]; x++)
                        {
                            for (int y = 0; y < chunkSize[VoxelUtil.DirectionAlignedY[direction]];)
                            {
                                int3 gridPosition = new int3 {
                                    [VoxelUtil.DirectionAlignedX[direction]] = x, [VoxelUtil.DirectionAlignedY[direction]] = y, [VoxelUtil.DirectionAlignedZ[direction]] = depth
                                };

                                Voxel voxel = voxels[VoxelUtil.To1DIndex(gridPosition, chunkSize)];

                                if (voxel.data == Voxel.VoxelType.Air)
                                {
                                    y++;
                                    continue;
                                }

                                if (hashMap.ContainsKey(gridPosition))
                                {
                                    y++;
                                    continue;
                                }

                                int3 neighborPosition = gridPosition + VoxelUtil.VoxelDirectionOffsets[direction];

                                if (TransparencyCheck(voxels, neighborPosition, chunkSize))
                                {
                                    y++;
                                    continue;
                                }

                                VoxelLight light = lightData[VoxelUtil.To1DIndex(gridPosition, chunkSize)];

                                hashMap.TryAdd(gridPosition, new Empty());

                                int height;
                                for (height = 1; height + y < chunkSize[VoxelUtil.DirectionAlignedY[direction]]; height++)
                                {
                                    int3 nextPosition = gridPosition;
                                    nextPosition[VoxelUtil.DirectionAlignedY[direction]] += height;

                                    Voxel      nextVoxel = voxels[VoxelUtil.To1DIndex(nextPosition, chunkSize)];
                                    VoxelLight nextLight = lightData[VoxelUtil.To1DIndex(nextPosition, chunkSize)];

                                    if (nextVoxel.data != voxel.data)
                                    {
                                        break;
                                    }

                                    if (!nextLight.CompareFace(light, direction))
                                    {
                                        break;
                                    }

                                    if (hashMap.ContainsKey(nextPosition))
                                    {
                                        break;
                                    }

                                    hashMap.TryAdd(nextPosition, new Empty());
                                }

                                bool isDone = false;
                                int  width;
                                for (width = 1; width + x < chunkSize[VoxelUtil.DirectionAlignedX[direction]]; width++)
                                {
                                    for (int dy = 0; dy < height; dy++)
                                    {
                                        int3 nextPosition = gridPosition;
                                        nextPosition[VoxelUtil.DirectionAlignedX[direction]] += width;
                                        nextPosition[VoxelUtil.DirectionAlignedY[direction]] += dy;

                                        Voxel      nextVoxel = voxels[VoxelUtil.To1DIndex(nextPosition, chunkSize)];
                                        VoxelLight nextLight = lightData[VoxelUtil.To1DIndex(nextPosition, chunkSize)];

                                        if (nextVoxel.data != voxel.data || hashMap.ContainsKey(nextPosition) || !nextLight.CompareFace(light, direction))
                                        {
                                            isDone = true;
                                            break;
                                        }
                                    }

                                    if (isDone)
                                    {
                                        break;
                                    }

                                    for (int dy = 0; dy < height; dy++)
                                    {
                                        int3 nextPosition = gridPosition;
                                        nextPosition[VoxelUtil.DirectionAlignedX[direction]] += width;
                                        nextPosition[VoxelUtil.DirectionAlignedY[direction]] += dy;
                                        hashMap.TryAdd(nextPosition, new Empty());
                                    }
                                }

                                AddQuadByDirection(direction, voxel.data, light, width, height, gridPosition, counter.Increment(), vertices, normals, uvs, colors, indices);
                                y += height;
                            }
                        }

                        hashMap.Clear();
                    }
                    hashMap.Dispose();
                }
            }
Example #11
0
            public void Execute()
            {
                for (int direction = 0; direction < 6; direction++)
                {
                    for (int depth = 0; depth < chunkSize[VoxelUtil.DirectionAlignedZ[direction]]; depth++)
                    {
                        for (int x = 0; x < chunkSize[VoxelUtil.DirectionAlignedX[direction]]; x++)
                        {
                            for (int y = 0; y < chunkSize[VoxelUtil.DirectionAlignedY[direction]];)
                            {
                                int3 gridPosition = new int3
                                {
                                    [VoxelUtil.DirectionAlignedX[direction]] = x,
                                    [VoxelUtil.DirectionAlignedY[direction]] = y,
                                    [VoxelUtil.DirectionAlignedZ[direction]] = depth
                                };

                                int index = VoxelUtil.To1DIndex(gridPosition, chunkSize);

                                Voxel      voxel = voxels[index];
                                VoxelLight light = lightData[index];

                                if (voxel.data == Voxel.VoxelType.Air)
                                {
                                    y++;
                                    continue;
                                }

                                int3 neighborPosition = gridPosition + VoxelUtil.VoxelDirectionOffsets[direction];

                                if (TransparencyCheck(voxels, neighborPosition, chunkSize))
                                {
                                    y++;
                                    continue;
                                }

                                int height;
                                for (height = 1; height + y < chunkSize[VoxelUtil.DirectionAlignedY[direction]]; height++)
                                {
                                    int3 nextPosition = gridPosition;
                                    nextPosition[VoxelUtil.DirectionAlignedY[direction]] += height;

                                    int nextIndex = VoxelUtil.To1DIndex(nextPosition, chunkSize);

                                    Voxel      nextVoxel = voxels[nextIndex];
                                    VoxelLight nextLight = lightData[nextIndex];

                                    if (nextVoxel.data != voxel.data)
                                    {
                                        break;
                                    }

                                    if (!light.CompareFace(nextLight, direction))
                                    {
                                        break;
                                    }
                                }

                                AddQuadByDirection(direction, voxel.data, light, 1.0f, height, gridPosition, counter.Increment(), vertices, normals, uvs, colors, indices);
                                y += height;
                            }
                        }
                    }
                }
            }
Example #12
0
            public void Execute()
            {
                for (int x = 0; x < chunkSize.x; x++)
                {
                    for (int y = 0; y < chunkSize.y; y++)
                    {
                        for (int z = 0; z < chunkSize.z; z++)
                        {
                            int3 gridPosition = new int3(x, y, z);
                            int  index        = VoxelUtil.To1DIndex(gridPosition, chunkSize);

                            Voxel voxel = voxels[index];

                            if (voxel.data == Voxel.VoxelType.Air)
                            {
                                continue;
                            }

                            for (int direction = 0; direction < 6; direction++)
                            {
                                int3 neighborPosition = gridPosition + VoxelUtil.VoxelDirectionOffsets[direction];

                                if (TransparencyCheck(voxels, neighborPosition, chunkSize))
                                {
                                    continue;
                                }

                                AddQuadByDirection(direction, voxel.data, lightData[index], 1.0f, 1.0f, gridPosition, counter.Increment(), vertices, normals, uvs, colors, indices);
                            }
                        }
                    }
                }
            }