Beispiel #1
0
        public void AddVoxel(VoxelChunk chunk, int voxelIndex, Vector3 position, Quaternion rotation, Vector3 scale)
        {
            // Add chunk to cell rendering lists
            InstancedChunk instancedChunk;

            if (!instancedChunks.TryGetValue(chunk, out instancedChunk))
            {
                BatchedCell batchedCell = GetBatchedCell(chunk);
                instancedChunk = new InstancedChunk(chunk, batchedCell);
                instancedChunks.Add(chunk, instancedChunk);
                batchedCell.instancedChunks.Add(instancedChunk);
            }

            // Ensure there're batches for this voxel definition in its cell
            InstancedVoxel  instancedVoxel  = new InstancedVoxel();
            VoxelDefinition voxelDefinition = env.voxelDefinitions [chunk.voxels [voxelIndex].typeIndex];
            BatchedCell     cell            = instancedChunk.batchedCell;
            BatchedMesh     batchedMesh;

            if (!cell.batchedMeshes.TryGetValue(voxelDefinition, out batchedMesh))
            {
                batchedMesh = new BatchedMesh(voxelDefinition);
                Material material = voxelDefinition.material;
                if (material == null)
                {
                    material = defaultInstancingMaterial;
                }
                material.EnableKeyword(SKW_VOXELPLAY_GPU_INSTANCING);
                batchedMesh.material = material;
                cell.batchedMeshes.Add(voxelDefinition, batchedMesh);
            }

            // Add voxel to the rendering lists
            instancedVoxel.batchedMesh     = batchedMesh;
            instancedVoxel.voxelDefinition = voxelDefinition;
            instancedVoxel.meshSize        = voxelDefinition.mesh.bounds.size;
            // only uniform scale is supported in indirect rendering (for optimization purposes)
            instancedVoxel.position.x  = position.x; instancedVoxel.position.y = position.y; instancedVoxel.position.z = position.z; instancedVoxel.position.w = scale.x;
            instancedVoxel.rotation.x  = rotation.x; instancedVoxel.rotation.y = rotation.y; instancedVoxel.rotation.z = rotation.z; instancedVoxel.rotation.w = rotation.w;
            instancedVoxel.color       = chunk.voxels [voxelIndex].color;
            instancedVoxel.packedLight = chunk.voxels [voxelIndex].packedLight;
            instancedChunk.instancedVoxels.Add(instancedVoxel);

            // Mark cell to be rebuilt
            cell.rebuild = true;
        }
Beispiel #2
0
        BatchedCell GetBatchedCell(VoxelChunk chunk)
        {
            Vector3 pos = chunk.position;
            int     cellX, cellY, cellZ;

            FastMath.FloorToInt(pos.x / CELL_SIZE, pos.y / CELL_SIZE, pos.z / CELL_SIZE, out cellX, out cellY, out cellZ);
            pos.x = cellX;
            pos.y = cellY;
            pos.z = cellZ;
            BatchedCell cell;

            if (!cells.TryGetValue(pos, out cell))
            {
                cell = new BatchedCell(pos, CELL_SIZE);
                cells.Add(pos, cell);
            }
            return(cell);
        }
        public void AddVoxel(VoxelChunk chunk, int voxelIndex, Vector3 position, Quaternion rotation, Vector3 scale)
        {
            VoxelDefinition voxelDefinition = env.voxelDefinitions [chunk.voxels [voxelIndex].typeIndex];

            // Ensure there're batches for this voxel definition
            if (voxelDefinition.batchedIndex < 0)
            {
                BatchedMesh batchedMesh = new BatchedMesh(voxelDefinition);
                Material    material    = voxelDefinition.material;
                if (material == null)
                {
                    material = defaultInstancingMaterial;
                }
                material.EnableKeyword(SKW_VOXELPLAY_GPU_INSTANCING);
                batchedMesh.material         = material;
                voxelDefinition.batchedIndex = batchedMeshes.Add(batchedMesh);
            }

            // Add chunk and voxel to the rendering lists
            InstancedChunk instancedChunk;

            if (!instancedChunks.TryGetValue(chunk, out instancedChunk))
            {
                instancedChunk = new InstancedChunk(chunk);
                instancedChunks.Add(chunk, instancedChunk);
            }
            InstancedVoxel instancedVoxel = new InstancedVoxel();

            instancedVoxel.voxelDefinition = voxelDefinition;
            instancedVoxel.meshSize        = voxelDefinition.mesh.bounds.size;
            instancedVoxel.position        = position;
            instancedVoxel.matrix.SetTRS(position, rotation, scale);
            instancedVoxel.color       = chunk.voxels [voxelIndex].color;
            instancedVoxel.packedLight = chunk.voxels [voxelIndex].packedLight;
            instancedChunk.instancedVoxels.Add(instancedVoxel);
            rebuild = true;
        }