Пример #1
0
    private void Generate()
    {
        if (!_initialized)
        {
            return;
        }

        _surfaceTool.Clear();
        _meshDataTool.Clear();
        _surfaceTool.CreateFrom(_meshInstance.Mesh, 0);
        _meshDataTool.CreateFromSurface(_surfaceTool.Commit(), 0);

        int numVertices = _meshDataTool.GetVertexCount();

        _data = new float[numVertices];

        for (int i = 0; i < numVertices; i++)
        {
            Vector3 vertex = _meshDataTool.GetVertex(i);
            _data[i] = Amplitude * _noise.GetNoise2d(vertex.x, vertex.z);
            _meshDataTool.SetVertex(i, new Vector3(vertex.x, _data[i], vertex.z));
            _meshDataTool.SetVertexNormal(i, Vector3.Up);
        }

        for (int i = 0; i < numVertices - _resX; i++)
        {
            Vector3 a = _meshDataTool.GetVertex(i);
            Vector3 b = _meshDataTool.GetVertex(i + 1);
            Vector3 c = _meshDataTool.GetVertex(i + _resX);

            Vector3 normal = (c - a).Cross(b - a);

            for (int j = i; j < i + 3; j++)
            {
                _meshDataTool.SetVertexNormal(j, _meshDataTool.GetVertexNormal(j) + normal);
            }
        }

        for (int i = 0; i < numVertices; i++)
        {
            _meshDataTool.SetVertexNormal(i, _meshDataTool.GetVertexNormal(i).Normalized());
        }

        ArrayMesh newMesh = new ArrayMesh();

        _meshDataTool.CommitToSurface(newMesh);
        _meshInstance.Mesh = newMesh;
        _meshInstance.RemoveChild(_meshInstance.GetChild(0));
        _meshInstance.CreateTrimeshCollision();
    }
Пример #2
0
        public void BuildVoxelMesh()
        {
            Builder.Begin(Mesh.PrimitiveType.Triangles);
            // GD.Print("Builder has begun!");
            Builder.SetMaterial(voxelMaterial);

            foreach (VoxelPos pos in Voxels.Keys)
            {
                var voxel = GetVoxel(pos, false);

                if (voxel.HasFront())
                {
                    BuildFace(pos.ToVector3(), Vector3.Right, Vector3.Up, UVFromName(voxel.frontTexture), Vector3.Forward);
                }
                if (voxel.HasBack())
                {
                    BuildFace(pos.Backwards().ToVector3(), Vector3.Up, Vector3.Right, UVFromName(voxel.backTexture), Vector3.Back);
                }
                if (voxel.HasLeft())
                {
                    BuildFace(pos.ToVector3(), Vector3.Up, Vector3.Back, UVFromName(voxel.leftTexture), Vector3.Left);
                }
                if (voxel.HasRight())
                {
                    BuildFace(pos.Right().ToVector3(), Vector3.Back, Vector3.Up, UVFromName(voxel.rightTexture), Vector3.Right);
                }
                if (voxel.HasTop())
                {
                    BuildFace(pos.Up().ToVector3(), Vector3.Right, Vector3.Back, UVFromName(voxel.topTexture), Vector3.Up);
                }
                if (voxel.HasBottom())
                {
                    BuildFace(pos.ToVector3(), Vector3.Back, Vector3.Right, UVFromName(voxel.bottomTexture), Vector3.Down);
                }
            }

            Builder.Index();
            var voxelMesh = GetNode <MeshInstance>("VoxelMesh");

            voxelMesh.Mesh = Builder.Commit();
            Builder.Clear();

            /*
             * foreach (KeyValuePair<VoxelPos, Voxel> pair in Voxels)
             * {
             *  GD.Print($"{pair.Key}: {pair.Value}");
             * }
             */
        }
Пример #3
0
    public MeshInstance CreateChunkMesh(Chunk chunk)
    {
        MeshInstance instance    = new MeshInstance();
        SurfaceTool  surfacetool = new SurfaceTool();

        surfacetool.Begin(Mesh.PrimitiveType.Points);
        surfacetool.SetMaterial(chunkMaterial);
        int count = 0;

        for (int i = 0; i < Constants.CHUNK_SIZE3D / chunk.Materials; i++)
        {
            Run         run         = chunk.Voxels[i];
            int         objectID    = run.value;
            TerraObject terraObject = registry.SelectByID((int)objectID);
            var         x           = i % CHUNK_SIZE;
            var         y           = (i / CHUNK_SIZE) % CHUNK_SIZE;
            var         z           = i / (CHUNK_SIZE * CHUNK_SIZE);
            if (chunk.Voxels[i].value != 0)
            {
                int face = 0b000000;
                //Left
                if (x == 0 || chunk.Voxels[i - 1].value != objectID)
                {
                    face = 0b000001;
                }

                //Right
                else if (x == 63 || chunk.Voxels[i + 1].value != objectID)
                {
                    face = 0b000010;
                }
                //Top
                else if (y == 63 || chunk.Voxels[i + 64].value != objectID)
                {
                    face = 0b000100;
                }
                //Bottom
                else if (y == 0 || chunk.Voxels[i - 64].value != objectID)
                {
                    face = 0b001000;
                }
                //Back
                else if (z == 63 || chunk.Voxels[i + 4096].value != objectID)
                {
                    face = 0b010000;
                }
                //Front
                else if (z == 0 || chunk.Voxels[i - 4096].value != objectID)
                {
                    face = 0b100000;
                }

                if (face != 0b000000)
                {
                    int     counter   = 0;
                    Vector3 normalAvg = Vector3.Zero;
                    for (int j = 0; j < 6; j++)
                    {
                        int bitFlagN = (face >> j) & 1;
                        if (bitFlagN == 1)
                        {
                            normalAvg = normalAvg + Normals[j];
                            counter  += 1;
                        }
                    }

                    count += 1;
                    surfacetool.AddColor(new Color(1f, 1f, 1f, 1f));
                    Vector3 voxPosition = new Vector3((x) * VOX_SIZE, (y) * VOX_SIZE, (z) * VOX_SIZE);
                    voxPosition.x = voxPosition.x + (chunk.x * CHUNK_SIZE * VOX_SIZE);
                    voxPosition.y = voxPosition.y + (chunk.y * CHUNK_SIZE * VOX_SIZE);
                    voxPosition.z = voxPosition.z + (chunk.z * CHUNK_SIZE * VOX_SIZE);
                    if (counter > 0)
                    {
                        normalAvg = normalAvg / counter;
                        surfacetool.AddNormal(normalAvg);
                    }

                    surfacetool.AddVertex(voxPosition);
                }
            }
        }

        surfacetool.Index();
        instance.Mesh = surfacetool.Commit();
        surfacetool.Clear();
        instance.MaterialOverride = chunkMaterial.Duplicate() as ShaderMaterial;

        // Console.WriteLine("Mesh AABB Pos: {0} , Size: {1}, End: {2}",bb.Position,bb.Size,bb.End);
        return(instance);
    }
Пример #4
0
    private void Render(Chunk pChunk)
    {
        // If chunk is already loaded.
        if (LoadedChunks.ContainsKey(pChunk.Offset))
        {
            return;
        }

        SurfaceTool = new SurfaceTool();
        SurfaceTool.Begin(Mesh.PrimitiveType.Triangles);

        // Adding material
        Material mat = VoxMaterial.Duplicate() as Material;

        SurfaceTool.SetMaterial(mat);

        // Creating the mesh. Voxel by voxel
        for (int y = 0; y < Chunk.ChunkSize.y; y++)
        {
            for (int x = 0; x < Chunk.ChunkSize.x; x++)
            {
                for (int z = 0; z < Chunk.ChunkSize.z; z++)
                {
                    if (!pChunk.Voxels[x, y, z].Active)
                    {
                        continue;
                    }

                    CreateVoxel(SurfaceTool, x, y, z, pChunk);
                }
            }
        }

        // Reduces vertex size
        SurfaceTool.Index();

        // Creating instance
        MeshInstance chunk = new MeshInstance
        {
            Mesh        = SurfaceTool.Commit(),
            Name        = pChunk.Offset.ToString(),
            Translation = new Vector3(pChunk.Offset.x * Chunk.ChunkSize.x, 0, pChunk.Offset.y * Chunk.ChunkSize.z)
        };

        // Creating collisions
        //chunk.CreateTrimeshCollision();


        // Tagging the chunk
        chunk.AddToGroup("Chunk");

        // Chunk is now loaded. Adding to the scene.
        LoadedChunks.TryAdd(pChunk.Offset, pChunk);
        this.CallDeferred("add_child", chunk);

        if (pChunk.VoxelSprite.Count < 64)
        {
            // Adding voxelsprite
            foreach (VoxelSprite voxSprite in pChunk.VoxelSprite)
            {
                var meshInstance = new MeshInstance();
                meshInstance.SetDeferred("mesh", voxSprite.Mesh);

                // Adding next frame to avoid locking problems.
                chunk.CallDeferred("add_child", meshInstance);

                // Moving next frame because not in tree yet
                meshInstance.SetDeferred("translation", voxSprite.Position);

                // Applying wind shader.
                meshInstance.SetDeferred("material_override", GrassMaterial);

                // Scaling down because its a decoration
                meshInstance.Scale = meshInstance.Scale /= 8;
            }
        }



        // Fade in animation.
        //var t2 = new Tween();
        //chunk.AddChild(t2);
        //t2.InterpolateProperty(mat, "albedo_color", new Color(1, 1, 1, 0f), new Color(1, 1, 1, 1f), 1f, Tween.TransitionType.Linear, Tween.EaseType.InOut);
        //t2.Start();

        SurfaceTool.Clear();
    }
Пример #5
0
        public void CalcMesh()
        {
            SurfaceTool st = new SurfaceTool();
            float       s  = 0.5f;

            st.Clear();
            st.Begin(Mesh.PrimitiveType.Triangles);
            List <Vector3> faces = new List <Vector3>();


            for (int y = 0; y < WorldGenerator.Height; y++)
            {
                for (int z = 0; z < 16; z++)
                {
                    for (int x = 0; x < 16; x++)
                    {
                        int bv = WorldGenerator.GetBlock(x + f * _x, y, z + f * _y);
                        bv = GetBlock(x + f * _x, y, z + f * _y);
                        int   bTop = WorldGenerator.GetBlock(x + f * _x, y + 1, z + f * _y);
                        int   bBottom = WorldGenerator.GetBlock(x + f * _x, y - 1, z + f * _y);
                        int   bLeft = WorldGenerator.GetBlock(x + f * _x - 1, y, z + f * _y);
                        int   bRight = WorldGenerator.GetBlock(x + f * _x + 1, y, z + f * _y);
                        int   bFront = WorldGenerator.GetBlock(x + f * _x, y, z + f * _y - 1);
                        int   bBack = WorldGenerator.GetBlock(x + f * _x, y, z + f * _y + 1);
                        float tex = 0; float t = 1.0f / 8.0f;
                        float x1, y2 = t * (bv - 1.0f), x2, y1 = t * bv;

                        // TOP
                        if (bv > 0 && bTop < 1)
                        {
                            x1 = 0 * t; x2 = 1 * t;

                            st.AddUv(GetTexCoords(bv, 0, 0, 0));
                            st.AddVertex(new Vector3(x - s, y + s, z - s));
                            faces.Add(new Vector3(x - s, y + s, z - s));

                            st.AddUv(GetTexCoords(bv, 0, 1, 0));
                            st.AddVertex(new Vector3(x + s, y + s, z - s));
                            faces.Add(new Vector3(x + s, y + s, z - s));

                            st.AddUv(GetTexCoords(bv, 0, 0, 1));
                            st.AddVertex(new Vector3(x - s, y + s, z + s));
                            faces.Add(new Vector3(x - s, y + s, z + s));

                            st.AddUv(GetTexCoords(bv, 0, 1, 1));
                            st.AddVertex(new Vector3(x + s, y + s, z + s));
                            faces.Add(new Vector3(x + s, y + s, z + s));

                            st.AddUv(GetTexCoords(bv, 0, 0, 1));
                            st.AddVertex(new Vector3(x - s, y + s, z + s));
                            faces.Add(new Vector3(x - s, y + s, z + s));

                            st.AddUv(GetTexCoords(bv, 0, 1, 0));
                            st.AddVertex(new Vector3(x + s, y + s, z - s));
                            faces.Add(new Vector3(x + s, y + s, z - s));
                        }

                        // Bottom
                        if (bv > 0 && bBottom < 1)
                        {
                            st.AddUv(GetTexCoords(bv, 1, 0, 0));
                            st.AddVertex(new Vector3(x - s, y - s, z - s));
                            faces.Add(new Vector3(x - s, y - s, z - s));

                            st.AddUv(GetTexCoords(bv, 1, 1, 0));
                            st.AddVertex(new Vector3(x - s, y - s, z + s));
                            faces.Add(new Vector3(x - s, y - s, z + s));

                            st.AddUv(GetTexCoords(bv, 1, 0, 1));
                            st.AddVertex(new Vector3(x + s, y - s, z - s));
                            faces.Add(new Vector3(x + s, y - s, z - s));

                            st.AddUv(GetTexCoords(bv, 1, 1, 1));
                            st.AddVertex(new Vector3(x + s, y - s, z + s));
                            faces.Add(new Vector3(x + s, y - s, z + s));

                            st.AddUv(GetTexCoords(bv, 1, 0, 1));
                            st.AddVertex(new Vector3(x + s, y - s, z - s));
                            faces.Add(new Vector3(x + s, y - s, z - s));

                            st.AddUv(GetTexCoords(bv, 1, 1, 0));
                            st.AddVertex(new Vector3(x - s, y - s, z + s));
                            faces.Add(new Vector3(x - s, y - s, z + s));
                        }

                        // Front
                        if (bv > 0 && bFront < 1)
                        {
                            x1 = 2 * t; x2 = 3 * t;

                            st.AddUv(GetTexCoords(bv, 2, 0, 0));
                            st.AddVertex(new Vector3(x - s, y - s, z - s));
                            faces.Add(new Vector3(x - s, y - s, z - s));

                            st.AddUv(GetTexCoords(bv, 2, 1, 0));
                            st.AddVertex(new Vector3(x + s, y - s, z - s));
                            faces.Add(new Vector3(x + s, y - s, z - s));

                            st.AddUv(GetTexCoords(bv, 2, 0, 1));
                            st.AddVertex(new Vector3(x - s, y + s, z - s));
                            faces.Add(new Vector3(x - s, y + s, z - s));

                            st.AddUv(GetTexCoords(bv, 2, 1, 1));
                            st.AddVertex(new Vector3(x + s, y + s, z - s));
                            faces.Add(new Vector3(x + s, y + s, z - s));

                            st.AddUv(GetTexCoords(bv, 2, 0, 1));
                            st.AddVertex(new Vector3(x - s, y + s, z - s));
                            faces.Add(new Vector3(x - s, y + s, z - s));

                            st.AddUv(GetTexCoords(bv, 2, 1, 0));
                            st.AddVertex(new Vector3(x + s, y - s, z - s));
                            faces.Add(new Vector3(x + s, y - s, z - s));
                        }

                        // Back
                        if (bv > 0 && bBack < 1)
                        {
                            x1 = 3 * t; x2 = 4 * t;

                            st.AddUv(GetTexCoords(bv, 3, 1, 0));
                            st.AddVertex(new Vector3(x - s, y - s, z + s));
                            faces.Add(new Vector3(x - s, y - s, z + s));

                            st.AddUv(GetTexCoords(bv, 3, 1, 1));
                            st.AddVertex(new Vector3(x - s, y + s, z + s));
                            faces.Add(new Vector3(x - s, y + s, z + s));

                            st.AddUv(GetTexCoords(bv, 3, 0, 0));
                            st.AddVertex(new Vector3(x + s, y - s, z + s));
                            faces.Add(new Vector3(x + s, y - s, z + s));

                            st.AddUv(GetTexCoords(bv, 3, 0, 1));
                            st.AddVertex(new Vector3(x + s, y + s, z + s));
                            faces.Add(new Vector3(x + s, y + s, z + s));

                            st.AddUv(GetTexCoords(bv, 3, 0, 0));
                            st.AddVertex(new Vector3(x + s, y - s, z + s));
                            faces.Add(new Vector3(x + s, y - s, z + s));

                            st.AddUv(GetTexCoords(bv, 3, 1, 1));
                            st.AddVertex(new Vector3(x - s, y + s, z + s));
                            faces.Add(new Vector3(x - s, y + s, z + s));
                        }


                        // Left
                        if (bv > 0 && bLeft < 1)
                        {
                            x1 = 4 * t; x2 = 5 * t;

                            st.AddUv(GetTexCoords(bv, 4, 1, 0));
                            st.AddVertex(new Vector3(x - s, y - s, z - s));
                            faces.Add(new Vector3(x - s, y - s, z - s));

                            st.AddUv(GetTexCoords(bv, 4, 1, 1));
                            st.AddVertex(new Vector3(x - s, y + s, z - s));
                            faces.Add(new Vector3(x - s, y + s, z - s));

                            st.AddUv(GetTexCoords(bv, 4, 0, 0));
                            st.AddVertex(new Vector3(x - s, y - s, z + s));
                            faces.Add(new Vector3(x - s, y - s, z + s));

                            st.AddUv(GetTexCoords(bv, 4, 0, 1));
                            st.AddVertex(new Vector3(x - s, y + s, z + s));
                            faces.Add(new Vector3(x - s, y + s, z + s));

                            st.AddUv(GetTexCoords(bv, 4, 0, 0));
                            st.AddVertex(new Vector3(x - s, y - s, z + s));
                            faces.Add(new Vector3(x - s, y - s, z + s));

                            st.AddUv(GetTexCoords(bv, 4, 1, 1));
                            st.AddVertex(new Vector3(x - s, y + s, z - s));
                            faces.Add(new Vector3(x - s, y + s, z - s));
                        }


                        // Right
                        if (bv > 0 && bRight < 1)
                        {
                            st.AddUv(GetTexCoords(bv, 5, 0, 0));
                            st.AddVertex(new Vector3(x + s, y - s, z - s));
                            faces.Add(new Vector3(x + s, y - s, z - s));

                            st.AddUv(GetTexCoords(bv, 5, 1, 0));
                            st.AddVertex(new Vector3(x + s, y - s, z + s));
                            faces.Add(new Vector3(x + s, y - s, z + s));

                            st.AddUv(GetTexCoords(bv, 5, 0, 1));
                            st.AddVertex(new Vector3(x + s, y + s, z - s));
                            faces.Add(new Vector3(x + s, y + s, z - s));

                            st.AddUv(GetTexCoords(bv, 5, 1, 1));
                            st.AddVertex(new Vector3(x + s, y + s, z + s));
                            faces.Add(new Vector3(x + s, y + s, z + s));

                            st.AddUv(GetTexCoords(bv, 5, 0, 1));
                            st.AddVertex(new Vector3(x + s, y + s, z - s));
                            faces.Add(new Vector3(x + s, y + s, z - s));

                            st.AddUv(GetTexCoords(bv, 5, 1, 0));
                            st.AddVertex(new Vector3(x + s, y - s, z + s));
                            faces.Add(new Vector3(x + s, y - s, z + s));
                        }
                    }
                }
            }

            st.GenerateNormals();
            mi.Mesh = st.Commit();
            try
            {
                //ccs.SetFaces(mi.Mesh.GetFaces());
                ccs.SetFaces(faces.ToArray());
            } catch (Exception ex)
            {
                World.log(ex.Message);
            }
            mesh_ready = true;
        }