Beispiel #1
0
 private VoxelMesh(VoxelMesh voxelMesh, DefaultMesh defaultMesh)
 {
     DefaultMesh    = defaultMesh;
     TexCoord3D     = voxelMesh.TexCoord3D;
     MaterialAmount = voxelMesh.MaterialAmount;
     MaterialId     = voxelMesh.MaterialId;
     VoxelId        = voxelMesh.VoxelId;
 }
Beispiel #2
0
        private void UpdateChunkDepthGeomatry(Vector3I position, VoxelMesh mesh)
        {
            VAO depthGeometry = VAOLoader.FromMesh(mesh.DefaultMesh, _depthShader);

            if (_chunkDepthGeometrys.ContainsKey(position))
            {
                _chunkDepthGeometrys[position] = depthGeometry;
            }
            else
            {
                _chunkDepthGeometrys.Add(position, depthGeometry);
            }
        }
Beispiel #3
0
        public void Add(VoxelMesh voxelMesh)
        {
            DefaultMesh.Add(voxelMesh.DefaultMesh);
            TexCoord3D.AddRange(voxelMesh.TexCoord3D);
            MaterialAmount.AddRange(voxelMesh.MaterialAmount);
            MaterialId.AddRange(voxelMesh.MaterialId);
            uint max = 0;

            if (VoxelId.Count > 0)
            {
                max = (uint)(VoxelId.Last() + 1);
            }
            foreach (var voxelId in voxelMesh.VoxelId)
            {
                VoxelId.Add(voxelId + max);
            }
        }
Beispiel #4
0
        private void UpdateChunkGeometry(Vector3I position, VoxelMesh mesh)
        {
            VAO geometry = VAOLoader.FromMesh(mesh.DefaultMesh, _voxelShader);
            var loc      = _voxelShader.GetResourceLocation(ShaderResourceType.Attribute, "uv3d");

            geometry.SetAttribute(loc, mesh.TexCoord3D.ToArray(), OpenGl4.VertexAttribPointerType.Float, 3);
            loc = _voxelShader.GetResourceLocation(ShaderResourceType.Attribute, "voxelId");
            geometry.SetAttribute(loc, mesh.VoxelId.ToArray(), OpenGl4.VertexAttribPointerType.Float, 1);

            BufferObject amountBuffer = new BufferObject(OpenGl4.BufferTarget.ShaderStorageBuffer);

            amountBuffer.Set(mesh.MaterialAmount.ToArray(), OpenGl4.BufferUsageHint.StaticCopy);
            BufferObject idBuffer = new BufferObject(OpenGl4.BufferTarget.ShaderStorageBuffer);

            idBuffer.Set(mesh.MaterialId.ToArray(), OpenGl4.BufferUsageHint.StaticCopy);

            if (_chunkGeometrys.ContainsKey(position))
            {
                _chunkGeometrys[position] = geometry;
            }
            else
            {
                _chunkGeometrys.Add(position, geometry);
            }

            if (_amountBuffers.ContainsKey(position))
            {
                _amountBuffers[position] = amountBuffer;
            }
            else
            {
                _amountBuffers.Add(position, amountBuffer);
            }

            if (_idBuffers.ContainsKey(position))
            {
                _idBuffers[position] = idBuffer;
            }
            else
            {
                _idBuffers.Add(position, idBuffer);
            }
        }
Beispiel #5
0
        private void AddVoxelMesh(Chunk chunk, Vector3I pos)
        {
            List <Voxel> neededVoxels  = CalculateNeededVoxels(chunk, pos);
            List <int>   vertexNumbers = GetVertexNumbersFromLookup(neededVoxels);

            if (vertexNumbers.Count > 0)
            {
                VoxelMesh voxelMesh = new VoxelMesh();

                List <float>   internalDistances = CalculateInternalDistances(neededVoxels);
                List <Vector3> internalPositions = CalculateInternalPositions(internalDistances);

                List <float> materialAmounts = CalculateMaterialAmounts(neededVoxels);
                List <int>   materialIds     = CalculateMaterialIds(neededVoxels);

                for (int i = 0; i < 8; i++)
                {
                    voxelMesh.MaterialAmount.Add(materialAmounts[i]);
                    voxelMesh.MaterialId.Add(materialIds[i]);
                }

                uint id = 0;
                void Add(int index, Vector3 n)
                {
                    voxelMesh.DefaultMesh.Position.Add(internalPositions[index]);
                    voxelMesh.DefaultMesh.Normal.Add(n);
                    voxelMesh.DefaultMesh.IDs.Add(id);
                    voxelMesh.TexCoord3D.Add(internalPositions[index]);
                    voxelMesh.VoxelId.Add(0);
                    id++;
                }

                for (int i = 0; i < vertexNumbers.Count; i += 3)
                {
                    Vector3 n = CalculateNormal(internalPositions[vertexNumbers[i]], internalPositions[vertexNumbers[i + 1]], internalPositions[vertexNumbers[i + 2]]);
                    Add(vertexNumbers[i + 2], n);
                    Add(vertexNumbers[i + 1], n);
                    Add(vertexNumbers[i], n);
                }

                this.Add(voxelMesh.Transform(Matrix4x4.CreateTranslation((Vector3)pos)));
            }
        }