Beispiel #1
0
        void UpdateMesh(int newVersion, int blockBufferSize, int vertexBufferSize, int faceBufferSize)
        {
            MeshVersion = newVersion;

            int[]   blockArray  = new int[blockBufferSize];
            float[] vertexArray = new float[vertexBufferSize];
            int[]   faceArray   = new int[faceBufferSize];

            int fullBlocks = 0;

            unsafe
            {
                fixed(int *blockBufferPtr = &blockArray[0], faceBufferPtr = &faceArray[0])
                {
                    fixed(float *vertexBufferPtr = &vertexArray[0])
                    {
                        fullBlocks = SDPlugin.SixDegreesSDK_GetBlockMesh(blockBufferPtr, vertexBufferPtr, faceBufferPtr, blockBufferSize, vertexBufferSize, faceBufferSize);
                    }
                }
            }
            bool gotAllBlocks = (fullBlocks == blockBufferSize / 6);

            if (fullBlocks > 0)
            {
            }
            else
            {
                Console.WriteLine("SixDegreesSDK_GetMeshBlocks() gave us an empty mesh, will not update.");
                return;
            }

            if (!gotAllBlocks)
            {
                Console.WriteLine("SixDegreesSDK_GetMeshBlocks() returned %d full blocks, expected %d, will not update", fullBlocks, (blockBufferSize / 6));
                return;
            }

            var vertexCount    = vertexBufferSize / 6;
            var blocksToUpdate = new List <Vector3>();

            var firstBlockVertex = 0;
            var firstBlockFace   = 0;

            // Update all the full blocks returned by the API
            for (int b = 0; b < blockBufferSize; b += 6)
            {
                // Transform block coordinates from 6D right-handed coordinates to Unity left-handed coordinates
                // By flipping the sign of Z
                Vector3 blockCoords      = new Vector3(blockArray[b], blockArray[b + 1], blockArray[b + 2]);
                int     blockVertexCount = blockArray[b + 3];
                int     blockFaceCount   = blockArray[b + 4];
                int     blockVersion     = blockArray[b + 5];

                var block = GetOrCreateBlock(blockCoords);
                block.MeshVersion = MeshVersion;

                // Update block if it is outdated
                if (block.Version < blockVersion)
                {
                    blocksToUpdate.Add(blockCoords);
                    block.Version  = blockVersion;
                    block.Vertices = new List <SCNVector3>();
                    block.Normals  = new List <SCNVector3>();
                    block.Faces    = new List <int>();

                    // copy vertices
                    for (int j = firstBlockVertex; j < firstBlockVertex + blockVertexCount; j++)
                    {
                        var vertex = j;
                        var pos    = vertex * 3;
                        block.Vertices.Add(new SCNVector3(vertexArray[pos],
                                                          vertexArray[pos + 1],
                                                          vertexArray[pos + 2]));

                        var norm = (vertex + vertexCount) * 3;
                        block.Normals.Add(new SCNVector3(vertexArray[norm],
                                                         vertexArray[norm + 1],
                                                         vertexArray[norm + 2]));
                    }

                    // copy faces
                    var offset = firstBlockVertex;
                    for (int face = firstBlockFace; face < firstBlockFace + blockFaceCount; face++)
                    {
                        var f = face * 3;
                        block.Faces.Add(faceArray[f] - offset);
                        block.Faces.Add(faceArray[f + 1] - offset);
                        block.Faces.Add(faceArray[f + 2] - offset);
                    }
                }
                firstBlockVertex += blockVertexCount;
                firstBlockFace   += blockFaceCount;
            }

            var blocksToDelete = new List <Vector3>();

            // Clean up outdated blocks
            foreach (var item in Blocks.Values)
            {
                if (item.MeshVersion != MeshVersion)
                {
                    blocksToDelete.Add(item.Coordinates);
                }
            }

            DeleteBlocks(blocksToDelete);
            UpdateBlocks(blocksToUpdate);
        }