int distanceComparer(Light a, Light b)
        {
            Vector3 posA  = a.transform.position;
            Vector3 posB  = b.transform.position;
            float   distA = FastVector.SqrDistance(ref camPos, ref posA);
            float   distB = FastVector.SqrDistance(ref camPos, ref posB);

            if (distA < distB)
            {
                return(-1);
            }
            if (distA > distB)
            {
                return(1);
            }
            return(0);
        }
        void RebuildZoneRenderingLists(Vector3 observerPos, float visibleDistance)
        {
            // rebuild batch lists to be used in the rendering loop
            for (int k = 0; k < batchedMeshes.count; k++)
            {
                BatchedMesh batchedMesh = batchedMeshes.values [k];
                batchedMesh.batches.Clear();
            }

            float cullDistance = (visibleDistance * VoxelPlayEnvironment.CHUNK_SIZE) * (visibleDistance * VoxelPlayEnvironment.CHUNK_SIZE);

            for (int j = 0; j <= instancedChunks.lastIndex; j++)
            {
                InstancedChunk instancedChunk = instancedChunks.values [j];
                if (instancedChunk == null)
                {
                    continue;
                }
                // check if chunk is in area
                Vector3 chunkCenter = instancedChunk.chunk.position;
                if (FastVector.SqrDistance(ref chunkCenter, ref observerPos) > cullDistance)
                {
                    continue;
                }

                // add instances to batch
                InstancedVoxel[] voxels = instancedChunk.instancedVoxels.values;
                for (int i = 0; i < instancedChunk.instancedVoxels.count; i++)
                {
                    VoxelDefinition vd          = voxels [i].voxelDefinition;
                    BatchedMesh     batchedMesh = batchedMeshes.values [vd.batchedIndex];
                    Batch           batch       = batchedMesh.batches.last;
                    if (batch == null || batch.instancesCount >= Batch.MAX_INSTANCES)
                    {
                        batch = batchedMesh.batches.FetchDirty();
                        if (batch == null)
                        {
                            batch = new Batch();
                            batchedMesh.batches.Add(batch);
                        }
                        batch.Init();
                    }
                    int pos = batch.instancesCount++;
                    // just copying the matrix triggers lot of expensive memcpy() calls so we directly copy the fields
//					batch.matrices[pos] = voxels[i].matrix;
                    batch.matrices [pos].m00 = voxels [i].matrix.m00; batch.matrices [pos].m01 = voxels [i].matrix.m01; batch.matrices [pos].m02 = voxels [i].matrix.m02; batch.matrices [pos].m03 = voxels [i].matrix.m03;
                    batch.matrices [pos].m10 = voxels [i].matrix.m10; batch.matrices [pos].m11 = voxels [i].matrix.m11; batch.matrices [pos].m12 = voxels [i].matrix.m12; batch.matrices [pos].m13 = voxels [i].matrix.m13;
                    batch.matrices [pos].m20 = voxels [i].matrix.m20; batch.matrices [pos].m21 = voxels [i].matrix.m21; batch.matrices [pos].m22 = voxels [i].matrix.m22; batch.matrices [pos].m23 = voxels [i].matrix.m23;
                    batch.matrices [pos].m30 = voxels [i].matrix.m30; batch.matrices [pos].m31 = voxels [i].matrix.m31; batch.matrices [pos].m32 = voxels [i].matrix.m32; batch.matrices [pos].m33 = voxels [i].matrix.m33;

                    batch.colorsAndLight [pos].x = voxels [i].color.r / 255f;
                    batch.colorsAndLight [pos].y = voxels [i].color.g / 255f;
                    batch.colorsAndLight [pos].z = voxels [i].color.b / 255f;
                    batch.colorsAndLight [pos].w = voxels [i].packedLight;
                    batch.UpdateBounds(voxels[i].position, voxels[i].meshSize);
                }
            }

            for (int k = 0; k < batchedMeshes.count; k++)
            {
                BatchedMesh batchedMesh = batchedMeshes.values [k];
                for (int j = 0; j < batchedMesh.batches.count; j++)
                {
                    Batch batch = batchedMesh.batches.values [j];
                    batch.materialPropertyBlock.SetVectorArray("_TintColor", batch.colorsAndLight);
                }
            }
        }
Пример #3
0
        void RebuildCellRenderingLists(BatchedCell cell, Vector3 observerPos, float visibleDistance)
        {
            // rebuild batch lists to be used in the rendering loop
            cell.ClearBatches();

            float cullDistance = (visibleDistance * VoxelPlayEnvironment.CHUNK_SIZE) * (visibleDistance * VoxelPlayEnvironment.CHUNK_SIZE);

            for (int j = 0; j < cell.instancedChunks.count; j++)
            {
                InstancedChunk instancedChunk = cell.instancedChunks.values [j];
                if (instancedChunk == null)
                {
                    continue;
                }

                // check if chunk is in area
                Vector3 chunkCenter = instancedChunk.chunk.position;
                if (FastVector.SqrDistance(ref chunkCenter, ref observerPos) > cullDistance)
                {
                    continue;
                }

                // add instances to batch
                InstancedVoxel[] voxels = instancedChunk.instancedVoxels.values;
                for (int i = 0; i < instancedChunk.instancedVoxels.count; i++)
                {
                    BatchedMesh batchedMesh = voxels [i].batchedMesh;

                    Batch batch = batchedMesh.batches.last;
                    if (batch == null || batch.instancesCount >= Batch.MAX_INSTANCES)
                    {
                        batch = batchedMesh.batches.FetchDirty();
                        if (batch == null)
                        {
                            batch = new Batch();
                            batch.instancedMaterial = GameObject.Instantiate <Material> (batchedMesh.material);
                            if (batchedMesh.voxelDefinition.rotationRandomY || batchedMesh.voxelDefinition.rotation != Misc.vector3zero)
                            {
                                batch.instancedMaterial.EnableKeyword(SKW_VOXELPLAY_USE_ROTATION);
                            }
                            batchedMesh.batches.Add(batch);
                        }
                        batch.Init();
                    }
                    int pos = batch.instancesCount++;
                    batch.positions [pos]   = voxels [i].position;
                    batch.rotations [pos].x = voxels [i].rotation.x; batch.rotations [pos].y = voxels [i].rotation.y; batch.rotations [pos].z = voxels [i].rotation.z; batch.rotations [pos].w = voxels [i].rotation.w;
//						batch.scales[pos] = voxels[i].scale;
                    batch.colorsAndLight [pos].x = voxels [i].color.r / 255f;
                    batch.colorsAndLight [pos].y = voxels [i].color.g / 255f;
                    batch.colorsAndLight [pos].z = voxels [i].color.b / 255f;
                    batch.colorsAndLight [pos].w = voxels [i].packedLight;
                    batch.UpdateBounds(voxels [i].position, voxels [i].meshSize);
                }
            }

            for (int i = 0; i <= cell.batchedMeshes.lastIndex; i++)
            {
                BatchedMesh batchedMesh = cell.batchedMeshes.values [i];
                if (batchedMesh == null)
                {
                    continue;
                }
                for (int j = 0; j < batchedMesh.batches.count; j++)
                {
                    Batch batch = batchedMesh.batches.values [j];
                    batch.ComputeBounds();
                    // Set positions
                    batch.positionsBuffer.SetData(batch.positions);
                    batch.instancedMaterial.SetBuffer("_Positions", batch.positionsBuffer);
                    // Set colors and light
                    batch.colorsAndLightBuffer.SetData(batch.colorsAndLight);
                    batch.instancedMaterial.SetBuffer("_ColorsAndLight", batch.colorsAndLightBuffer);
                    // Set rotations
                    batch.rotationsBuffer.SetData(batch.rotations);
                    batch.instancedMaterial.SetBuffer("_Rotations", batch.rotationsBuffer);
                    // Set buffer args
                    Mesh mesh = batchedMesh.voxelDefinition.mesh;
                    batch.args [0] = mesh.GetIndexCount(0);
                    batch.args [1] = (uint)batch.instancesCount;
                    batch.args [2] = mesh.GetIndexStart(0);
                    batch.args [3] = 0;                     // (uint)mesh.GetBaseVertex (0);
                    batch.argsBuffer.SetData(batch.args);
                }
            }
        }