Пример #1
0
    public static ArrayMesh AddNoiseToMesh(PlaneMesh plane, float[,] noiseMap, int chunkSize, int heightMultiplier, Curve heightCurve)
    {
        SurfaceTool st = new SurfaceTool();

        st.CreateFrom(plane, 0);
        ArrayMesh    mesh = new ArrayMesh();
        MeshDataTool dt   = new MeshDataTool();

        mesh = st.Commit();
        dt.CreateFromSurface(mesh, 0);
        for (int y = 0; y < chunkSize; y++)
        {
            for (int x = 0; x < chunkSize; x++)
            {
                int     z           = y;
                int     vertexIndex = z * chunkSize + x;
                Vector3 vertex      = dt.GetVertex(vertexIndex);
                vertex.y = heightCurve.Interpolate(noiseMap[chunkSize - x - 1, chunkSize - z - 1]) * heightMultiplier;
                dt.SetVertex(vertexIndex, vertex);
            }
        }
        for (int surface = 0; surface < mesh.GetSurfaceCount(); surface++)
        {
            mesh.SurfaceRemove(surface);
        }
        dt.CommitToSurface(mesh);
        st.Begin(Mesh.PrimitiveType.Triangles);
        st.CreateFrom(mesh, 0);
        st.Index();
        st.GenerateNormals();
        return(st.Commit());
    }
Пример #2
0
    private void GenerateSurface()
    {
        surfaceTool.Begin(Mesh.PrimitiveType.Triangles);

        for (int x = 0; x < SIZE.x; x++)
        {
            for (int y = 0; y < SIZE.y; y++)
            {
                for (int z = 0; z < SIZE.z; z++)
                {
                    byte blockType = blocks[x, y, z];
                    if (blockType == 0)
                    {
                        continue;
                    }

                    Block block = Game.GetBlock(blockType);

                    IntVector3 localIndex = new IntVector3(x, y, z);

                    //Index in world space
                    IntVector3 index = localIndex + new IntVector3(chunkCoords.x * SIZE.x, 0, chunkCoords.y * SIZE.z);

                    //Position in chunk
                    Vector3 localBlockPosition = localIndex * Block.SIZE;

                    if (terrain.GetBlock(index.x + 1, index.y, index.z) == AIR_ID)
                    {
                        block.AddPosXFace(ref surfaceTool, localBlockPosition, blockType);
                    }
                    if (terrain.GetBlock(index.x - 1, index.y, index.z) == AIR_ID)
                    {
                        block.AddNegXFace(ref surfaceTool, localBlockPosition, blockType);
                    }
                    if (terrain.GetBlock(index.x, index.y + 1, index.z) == AIR_ID)
                    {
                        block.AddPosYFace(ref surfaceTool, localBlockPosition, blockType);
                    }
                    if (index.y != 0 && terrain.GetBlock(index.x, index.y - 1, index.z) == AIR_ID) //Don't draw bottom face
                    {
                        block.AddNegYFace(ref surfaceTool, localBlockPosition, blockType);
                    }
                    if (terrain.GetBlock(index.x, index.y, index.z + 1) == AIR_ID)
                    {
                        block.AddPosZFace(ref surfaceTool, localBlockPosition, blockType);
                    }
                    if (terrain.GetBlock(index.x, index.y, index.z - 1) == AIR_ID)
                    {
                        block.AddNegZFace(ref surfaceTool, localBlockPosition, blockType);
                    }
                }
            }
        }

        surfaceTool.GenerateNormals();

        surfaceTool.SetMaterial(material);

        surfaceTool.Index();
    }
        //Creates a mesh from a surface tool
        public static ArrayMesh CreateMeshFromSurfaceTool(SurfaceTool surfTool)
        {
            var mesh = new ArrayMesh();

            surfTool.GenerateNormals();
            surfTool.Index();
            surfTool.Commit(mesh);
            return(mesh);
        }
Пример #4
0
    public Mesh CreateMesh()
    {
        SurfaceTool.Begin(Mesh.PrimitiveType.Triangles);
        SurfaceTool.SetMaterial(DefaultMaterial);

        foreach (var voxel in Voxels.Keys)
        {
            CreateVoxel(Voxels[voxel], voxel);
        }

        SurfaceTool.Index();
        return(SurfaceTool.Commit());
    }
Пример #5
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}");
             * }
             */
        }
Пример #6
0
    private Mesh GenerateMesh(Vector3 pos, Vector2 size, int subdivision = 0)
    {
        SurfaceTool st = new SurfaceTool();

        st.Begin(Mesh.PrimitiveType.Triangles);

        int verts = 2 + subdivision;

        Vector3[,] vertices = new Vector3[verts, verts];

        for (int i = 0; i < verts; i++)
        {
            for (int j = 0; j < verts; j++)
            {
                Vector3 p = new Vector3(
                    ((float)i / (verts - 1)),
                    0f,
                    ((float)j / (verts - 1))
                    );

                p.x *= size.x;
                p.z *= size.y;
                p.y  = GetHeight(p + pos);

                vertices[i, j] = p;
            }
        }

        for (int i = 0; i < verts - 1; i++)
        {
            for (int j = 0; j < verts - 1; j++)
            {
                st.AddVertex(vertices[i + 1, j + 0]);
                st.AddVertex(vertices[i + 1, j + 1]);
                st.AddVertex(vertices[i + 0, j + 0]);

                st.AddVertex(vertices[i + 0, j + 0]);
                st.AddVertex(vertices[i + 1, j + 1]);
                st.AddVertex(vertices[i + 0, j + 1]);
            }
        }

        st.GenerateNormals();
        //st.GenerateTangents();
        st.Index();
        return(st.Commit());
    }
    void _generate_chunk_mesh(int _this_argument_exists_due_to_bug_9924)
    {
        if (0 < data.Count)
        {
            return;
        }

        var surface_tool = new SurfaceTool();

        surface_tool.Begin(Mesh.PrimitiveType.Triangles);

        // For each block, add data to the SurfaceTool and generate a collider.
        foreach (var block_position in data.Keys)
        {
            var block_id = data[block_position];
            _draw_block_mesh(surface_tool, block_position, block_id);
        }
        // Create the chunk's mesh from the SurfaceTool data.
        surface_tool.GenerateNormals();
        // surface_tool.GenerateTangents(); // XXX add back

        surface_tool.Index();
        var array_mesh = surface_tool.Commit();

        var mi = new MeshInstance();

        mi.Mesh = array_mesh;
        var resource = ResourceLoader.Load("res://world/textures/material.tres");

        GD.Print($"====== resource should be material: {resource}");
        var materialOrNull = resource as Material;

        if (materialOrNull == null)
        {
            GD.Print("materialOrNull == null");
        }
        else
        {
            mi.MaterialOverride = materialOrNull;
        }

        AddChild(mi);
    }
Пример #8
0
        private void CreateMesh()
        {
            surfaceTool.Begin(Mesh.PrimitiveType.Triangles);

            for (int i = 0; i < CHUNK_SIZE; i++)
            {
                for (int j = 0; j < CHUNK_SIZE; j++)
                {
                    for (int k = 0; k < CHUNK_SIZE; k++)
                    {
                        if (chunkData[i][j][k] == VoxelTypes.Air)
                        {
                            continue;
                        }
                        CreateFaces(i, j, k);
                    }
                }
            }
            surfaceTool.Index();
            this.SetMesh(surfaceTool.Commit());
        }
Пример #9
0
    private void generate_ui_mesh(Vector2[] positions)
    {
        GD.Print("Generate new UI mesh", positions);

        var st = new SurfaceTool();

        st.Begin(Mesh.PrimitiveType.Triangles);

        foreach (var p in positions)
        {
            GD.Print(p);
            _plane(st, p.x, p.y);
        }

        st.Index();
        st.GenerateNormals();
        st.GenerateTangents();

        var mesh = st.Commit();

        mesh.SurfaceSetMaterial(0, mouse_preview.GetSurfaceMaterial(0));
        GetNode <MeshInstance>("MeshInstance3").Mesh = mesh;
    }
Пример #10
0
    private void generate_mesh()
    {
        GD.Print("Update info mesh");
        var tmpPoints = new List <Vector2>();

        var st = new SurfaceTool();

        st.Begin(Mesh.PrimitiveType.Triangles);

        Vector2 _test = (Vector2)start_pos - (Vector2)end_pos;

        if (Mathf.Abs(_test.x) > Mathf.Abs(_test.y))
        {
            _test.y = 0;
        }
        else
        {
            _test.x = 0;
        }

        int size = (int)_test.Length();

        if (_test.x != 0 && _test.y == 0)
        {
            GD.Print("X");
            for (int i = 0; i < size + 1; i++)
            {
                if (_test.x < 0)
                {
                    _plane(st, ((Vector2)start_pos).x + i, ((Vector2)start_pos).y);
                    tmpPoints.Add(((Vector2)start_pos) + new Vector2(i, 0));
                }
                else
                {
                    _plane(st, ((Vector2)start_pos).x - i, ((Vector2)start_pos).y);
                    tmpPoints.Add(((Vector2)start_pos) + new Vector2(-i, 0));
                }
            }
        }
        else if (_test.y != 0 && _test.x == 0)
        {
            GD.Print("Y");
            for (int i = 0; i < size + 1; i++)
            {
                if (_test.y < 0)
                {
                    _plane(st, ((Vector2)start_pos).x, ((Vector2)start_pos).y + i);
                    tmpPoints.Add(((Vector2)start_pos) + new Vector2(0, i));
                }
                else
                {
                    _plane(st, ((Vector2)start_pos).x, ((Vector2)start_pos).y - i);
                    tmpPoints.Add(((Vector2)start_pos) + new Vector2(0, -i));
                }
            }
        }

        st.Index();
        st.GenerateNormals();
        st.GenerateTangents();

        var mesh = st.Commit();

        mesh.SurfaceSetMaterial(0, mouse_preview.GetSurfaceMaterial(0));
        GetNode <MeshInstance>("mouse_draw_preview").Mesh = mesh;

        curPoints = tmpPoints.ToArray();
        GD.Print(_test, size);
    }
Пример #11
0
        public override void _Ready()
        {
            var surfTool = new SurfaceTool();

            surfTool.Begin(Mesh.PrimitiveType.Triangles);
            Vector3[] vertices = new Vector3[] { new Vector3(0, 0, 0),
                                                 new Vector3(length, 0, 0),
                                                 new Vector3(length, 0, width),
                                                 new Vector3(0, 0, width),

                                                 new Vector3(0, height, 0),
                                                 new Vector3(length, height, 0),
                                                 new Vector3(length, height, width),
                                                 new Vector3(0, height, width) };

            // surfTool.AddNormal(new Vector3(0.0f,0.0f,-1.0f));
            // surfTool.AddVertex(new Vector3(length, -height, -width));
            // surfTool.AddVertex(new Vector3(-length, -height, -width));
            // surfTool.AddVertex(new Vector3(-length, height, -width));
            // surfTool.AddVertex(new Vector3(length, height, -width));

            // // surfTool.AddNormal(new Vector3(0.0f, 0.0f, 1.0f));
            // surfTool.AddVertex(new Vector3(-length, -height, width));
            // surfTool.AddVertex(new Vector3(length, -height, width));
            // surfTool.AddVertex(new Vector3(length, height, width));
            // surfTool.AddVertex(new Vector3(-length, height, width));

            // // surfTool.AddNormal(new Vector3(1.0f, 0.0f, 0.0f));
            // surfTool.AddVertex(new Vector3(length, -height, width));
            // surfTool.AddVertex(new Vector3(length, -height, -width));
            // surfTool.AddVertex(new Vector3(length, height, -width));
            // surfTool.AddVertex(new Vector3(length, height, width));

            // surfTool.AddNormal(new Vector3(-1.0f, 0.0f, 0.0f));
            // surfTool.AddVertex(new Vector3(-length, -height, -width));
            // surfTool.AddVertex(new Vector3(-length, -height, width));
            // surfTool.AddVertex(new Vector3(-length, height, width));
            // surfTool.AddVertex(new Vector3(-length, height, -width));

            //Bottom Face
            surfTool.AddNormal(new Vector3(0.0f, -1.0f, 0.0f));
            surfTool.AddVertex(vertices[1]);
            surfTool.AddVertex(vertices[3]);
            surfTool.AddVertex(vertices[2]);

            surfTool.AddVertex(vertices[1]);
            surfTool.AddVertex(vertices[0]);
            surfTool.AddVertex(vertices[3]);

            surfTool.AddNormal(new Vector3(0.0f, 1.0f, 0.0f));
            surfTool.AddVertex(vertices[4]);
            surfTool.AddVertex(vertices[5]);
            surfTool.AddVertex(vertices[7]);

            surfTool.AddVertex(vertices[5]);
            surfTool.AddVertex(vertices[6]);
            surfTool.AddVertex(vertices[7]);

            surfTool.AddNormal(new Vector3(0.0f, 0.0f, 1.0f));
            surfTool.AddVertex(vertices[0]);
            surfTool.AddVertex(vertices[1]);
            surfTool.AddVertex(vertices[5]);

            surfTool.AddVertex(vertices[5]);
            surfTool.AddVertex(vertices[4]);
            surfTool.AddVertex(vertices[0]);

            surfTool.AddNormal(new Vector3(0.0f, 0.0f, -1.0f));
            surfTool.AddVertex(vertices[3]);
            surfTool.AddVertex(vertices[6]);
            surfTool.AddVertex(vertices[2]);

            surfTool.AddVertex(vertices[3]);
            surfTool.AddVertex(vertices[7]);
            surfTool.AddVertex(vertices[6]);


            surfTool.AddNormal(new Vector3(-1.0f, 0.0f, 0.0f));
            surfTool.AddVertex(vertices[0]);
            surfTool.AddVertex(vertices[7]);
            surfTool.AddVertex(vertices[3]);

            surfTool.AddVertex(vertices[0]);
            surfTool.AddVertex(vertices[4]);
            surfTool.AddVertex(vertices[7]);

            surfTool.AddNormal(new Vector3(1.0f, 0.0f, 0.0f));
            surfTool.AddVertex(vertices[2]);
            surfTool.AddVertex(vertices[5]);
            surfTool.AddVertex(vertices[1]);

            surfTool.AddVertex(vertices[2]);
            surfTool.AddVertex(vertices[6]);
            surfTool.AddVertex(vertices[5]);


            // // surfTool.AddNormal(new Vector3(0.0f, 1.0f, 0.0f));
            // surfTool.AddVertex(new Vector3(length, height, -width));
            // surfTool.AddVertex(new Vector3(-length, height, -width));
            // surfTool.AddVertex(new Vector3(-length, height, width));
            // surfTool.AddVertex(new Vector3(length, height, width));

            surfTool.Index();
            // surfTool.GenerateNormals();

            var mesh = surfTool.Commit();

            this.SetMesh(mesh);
        }
        private void Reset()
        {
            Mesh usedMesh = mesh;

            if (usedMesh == null)
            {
                SurfaceTool st = new SurfaceTool();

                st.Begin(Mesh.PrimitiveType.Triangles);

                switch (drawMode)
                {
                case DrawMode.SingleTriangle:
                {
                    st.AddUv(new Vector2(0, 0));
                    st.AddVertex(new Vector3(-.5f, -.5f, 0));

                    st.AddUv(new Vector2(0, 2));
                    st.AddVertex(new Vector3(-.5f, 1.5f, 0));

                    st.AddUv(new Vector2(2, 0));
                    st.AddVertex(new Vector3(1.5f, -.5f, 0));
                    break;
                }

                case DrawMode.Quad:
                {
                    st.AddUv(new Vector2(0, 0));
                    st.AddVertex(new Vector3(-.5f, -.5f, 0));

                    st.AddUv(new Vector2(0, 1));
                    st.AddVertex(new Vector3(-.5f, .5f, 0));

                    st.AddUv(new Vector2(1, 0));
                    st.AddVertex(new Vector3(.5f, .51f, 0));

                    st.AddUv(new Vector2(1, 0));
                    st.AddVertex(new Vector3(.5f, -.5f, 0));

                    st.AddUv(new Vector2(0, 1));
                    st.AddVertex(new Vector3(-.5f, .5f, 0));

                    st.AddUv(new Vector2(1, 1));
                    st.AddVertex(new Vector3(.5f, .5f, 0));
                    break;
                }
                }

                //st.SetMaterial(particleSystem.testMaterial);
                st.Index();

                usedMesh = st.Commit();
            }

            multimesh = new MultiMesh();
            multimesh.TransformFormat = MultiMesh.TransformFormatEnum.Transform2d;
            multimesh.ColorFormat     = MultiMesh.ColorFormatEnum.Float;
            //multimesh.CustomDataFormat = MultiMesh.CustomDataFormatEnum.Float;
            multimesh.Mesh          = usedMesh;
            multimesh.InstanceCount = particleSystem.maxParticles;
        }
Пример #13
0
    private void CreatePerceptionPyramidMesh()
    {
        var material = new SpatialMaterial();

        material.FlagsTransparent = true;
        material.AlbedoColor      = new Color(0.0f, 1.0f, 0.0f, 0.02f);
        material.SetCullMode(SpatialMaterial.CullMode.Disabled);

        // Pyramid
        var surfaceTool = new SurfaceTool();

        surfaceTool.Begin(Mesh.PrimitiveType.Triangles);

        // Top face
        surfaceTool.AddNormal(new Vector3(0.0f, 0.5f, 0.5f));
        surfaceTool.AddColor(new Color(0.0f, 1.0f, 0.0f, 0.02f));
        surfaceTool.AddVertex(new Vector3(-perceptionRadius / 2, perceptionRadius / 2, -perceptionRadius)); // top left

        surfaceTool.AddNormal(new Vector3(0.0f, 0.5f, 0.5f));
        surfaceTool.AddColor(new Color(0.0f, 1.0f, 0.0f, 0.02f));
        surfaceTool.AddVertex(new Vector3(perceptionRadius / 2, perceptionRadius / 2, -perceptionRadius)); // top right

        surfaceTool.AddNormal(new Vector3(0.0f, 0.5f, 0.5f));
        surfaceTool.AddColor(new Color(0.0f, 1.0f, 0.0f, 0.02f));
        surfaceTool.AddVertex(new Vector3(0, 0, 2.0f)); // home

        // Right face
        surfaceTool.AddNormal(new Vector3(0.5f, 0.0f, 0.5f));
        surfaceTool.AddColor(new Color(0.0f, 1.0f, 0.0f, 0.02f));
        surfaceTool.AddVertex(new Vector3(perceptionRadius / 2, perceptionRadius / 2, -perceptionRadius)); // top right

        surfaceTool.AddNormal(new Vector3(0.5f, 0.0f, 0.5f));
        surfaceTool.AddColor(new Color(0.0f, 1.0f, 0.0f, 0.02f));
        surfaceTool.AddVertex(new Vector3(perceptionRadius / 2, -perceptionRadius / 2, -perceptionRadius)); // bottom right

        surfaceTool.AddNormal(new Vector3(0.5f, 0.0f, 0.5f));
        surfaceTool.AddColor(new Color(0.0f, 1.0f, 0.0f, 0.02f));
        surfaceTool.AddVertex(new Vector3(0, 0, 2.0f)); // home

        // Bottom face
        surfaceTool.AddNormal(new Vector3(0.0f, -0.5f, 0.5f));
        surfaceTool.AddColor(new Color(0.0f, 1.0f, 0.0f, 0.02f));
        surfaceTool.AddVertex(new Vector3(perceptionRadius / 2, -perceptionRadius / 2, -perceptionRadius)); // bottom right

        surfaceTool.AddNormal(new Vector3(0.0f, -0.5f, 0.5f));
        surfaceTool.AddColor(new Color(0.0f, 1.0f, 0.0f, 0.02f));
        surfaceTool.AddVertex(new Vector3(-perceptionRadius / 2, -perceptionRadius / 2, -perceptionRadius)); // bottom left

        surfaceTool.AddNormal(new Vector3(0.0f, -0.5f, 0.5f));
        surfaceTool.AddColor(new Color(0.0f, 1.0f, 0.0f, 0.02f));
        surfaceTool.AddVertex(new Vector3(0, 0, 2.0f)); // home

        // Left face
        surfaceTool.AddNormal(new Vector3(-0.5f, 0.0f, 0.5f));
        surfaceTool.AddColor(new Color(0.0f, 1.0f, 0.0f, 0.02f));
        surfaceTool.AddVertex(new Vector3(-perceptionRadius / 2, -perceptionRadius / 2, -perceptionRadius)); // bottom left

        surfaceTool.AddNormal(new Vector3(-0.5f, 0.0f, 0.5f));
        surfaceTool.AddColor(new Color(0.0f, 1.0f, 0.0f, 0.02f));
        surfaceTool.AddVertex(new Vector3(-perceptionRadius / 2, perceptionRadius / 2, -perceptionRadius)); // top left

        surfaceTool.AddNormal(new Vector3(-0.5f, 0.0f, 0.5f));
        surfaceTool.AddColor(new Color(0.0f, 1.0f, 0.0f, 0.02f));
        surfaceTool.AddVertex(new Vector3(0, 0, 2.0f)); // home

        // Base face1
        surfaceTool.AddNormal(new Vector3(0.0f, 0.0f, -1.0f));
        surfaceTool.AddColor(new Color(0.0f, 1.0f, 0.0f, 0.02f));
        surfaceTool.AddVertex(new Vector3(perceptionRadius / 2, perceptionRadius / 2, -perceptionRadius)); // top right

        surfaceTool.AddNormal(new Vector3(0.0f, 0.0f, -1.0f));
        surfaceTool.AddColor(new Color(0.0f, 1.0f, 0.0f, 0.02f));
        surfaceTool.AddVertex(new Vector3(-perceptionRadius / 2, perceptionRadius / 2, -perceptionRadius)); // top left

        surfaceTool.AddNormal(new Vector3(0.0f, 0.0f, -1.0f));
        surfaceTool.AddColor(new Color(0.0f, 1.0f, 0.0f, 0.02f));
        surfaceTool.AddVertex(new Vector3(-perceptionRadius / 2, -perceptionRadius / 2, -perceptionRadius)); // bottom left

        // Base face2
        surfaceTool.AddNormal(new Vector3(0.0f, 0.0f, -1.0f));
        surfaceTool.AddColor(new Color(0.0f, 1.0f, 0.0f, 0.02f));
        surfaceTool.AddVertex(new Vector3(-perceptionRadius / 2, -perceptionRadius / 2, -perceptionRadius)); // bottom left

        surfaceTool.AddNormal(new Vector3(0.0f, 0.0f, -1.0f));
        surfaceTool.AddColor(new Color(0.0f, 1.0f, 0.0f, 0.02f));
        surfaceTool.AddVertex(new Vector3(perceptionRadius / 2, -perceptionRadius / 2, -perceptionRadius)); // bottom right

        surfaceTool.AddNormal(new Vector3(0.0f, 0.0f, -1.0f));
        surfaceTool.AddColor(new Color(0.0f, 1.0f, 0.0f, 0.02f));
        surfaceTool.AddVertex(new Vector3(perceptionRadius / 2, perceptionRadius / 2, -perceptionRadius)); // top right

        surfaceTool.Index();

        var mesh = surfaceTool.Commit();

        mesh.SurfaceSetMaterial(0, material);

        var meshInstance = new MeshInstance();

        meshInstance.Mesh = mesh;
        meshInstance.SetName("perceptionMesh");
        meshInstance.SetVisible(false);

        this.AddChild(meshInstance);
    }
Пример #14
0
        public bool createMesh(ChunkSection section)
        {
            tool.Begin(Mesh.PrimitiveType.Triangles);

            bool addedOne = false;

            for (var x = 0; x < 16; x++)
            {
                for (var y = 0; y < 16; y++)
                {
                    for (var z = 0; z < 16; z++)
                    {
                        // get data and check if air
                        BlockState data = getData(section, x, y, z);
                        if (data.transparent)
                        {
                            continue;
                        }

                        addedOne = true;

                        // culling
                        bool renderFront = true;
                        if (z < 16 - 1)
                        {
                            renderFront = getData(section, x, y, z + 1).transparent;
                        }
                        bool renderBack = true;
                        if (z > 0)
                        {
                            renderBack = getData(section, x, y, z - 1).transparent;
                        }
                        bool renderRight = true;
                        if (x < 16 - 1)
                        {
                            renderRight = getData(section, x + 1, y, z).transparent;
                        }
                        bool renderLeft = true;
                        if (x > 0)
                        {
                            renderLeft = getData(section, x - 1, y, z).transparent;
                        }
                        bool renderTop = true;
                        if (y < 16 - 1)
                        {
                            renderTop = getData(section, x, y + 1, z).transparent;
                        }
                        bool renderBot = true;
                        if (y > 0)
                        {
                            renderBot = getData(section, x, y - 1, z).transparent;
                        }

                        // add cube
                        createCube(x, y, z, data, renderFront, renderBack, renderRight, renderLeft, renderTop, renderBot);
                    }
                }
            }

            if (!addedOne)
            {
                return(false);
            }

            tool.Index();

            Mesh = tool.Commit();
            return(true);
        }
Пример #15
0
    public void CreateMesh(Tile tile, PackedScene sphereMeshScene, PackedScene greenSphereMeshScene, int numberOfTiles, float radius)
    {
        var surfTool = new SurfaceTool();
        var mesh     = new ArrayMesh();
        var material = new SpatialMaterial();

        material.SetEmission(new Color(1.0f, 0.0f, 0.0f));
        material.SetAlbedo(new Color(1.0f, 0.0f, 0.0f));
        surfTool.SetMaterial(material);
        surfTool.Begin(Mesh.PrimitiveType.LineLoop);
        decimal tileCenterX   = 0;
        decimal tileCenterY   = 0;
        decimal tileCenterZ   = 0;
        decimal polygonRadius = 0;

        List <Point> points            = tile.boundary;
        var          lastPoint         = points[points.Count - 1];
        Vector3      lastPointVector   = new Vector3((float)lastPoint.x, (float)lastPoint.y, (float)lastPoint.z);
        decimal      polygonSideLength = 0;

        foreach (Point point in points)
        {
            surfTool.AddUv(new Vector2(0, 0));
            surfTool.AddVertex(new Vector3((float)point.x, (float)point.y, (float)point.z));

            tileCenterX += point.x / points.Count;
            tileCenterY += point.y / points.Count;
            tileCenterZ += point.z / points.Count;
            Vector3 currentVector = new Vector3((float)point.x, (float)point.y, (float)point.z);
            polygonSideLength += (decimal)currentVector.DistanceTo(lastPointVector);
            lastPointVector    = currentVector;
        }
        polygonSideLength = polygonSideLength / points.Count;

        var tileCenterPoint = new Vector3((float)tileCenterX, (float)tileCenterY, (float)tileCenterZ);
        var firstPoint      = new Vector3((float)points[0].x, (float)points[0].y, (float)points[0].z);

        foreach (Point point in points)
        {
            var vector = new Vector3((float)point.x, (float)point.y, (float)point.z);
            polygonRadius += (decimal)vector.DistanceTo(tileCenterPoint);
        }
        polygonRadius = polygonRadius / points.Count;

        var polygonRotation = firstPoint.AngleTo(tileCenterPoint);


        var sphereCenterPoint = new Vector3(0f, 0f, 0f);

        var          sphereScale = radius / numberOfTiles;
        MeshInstance sphere      = (MeshInstance)sphereMeshScene.Instance();

        sphere.SetTranslation(tileCenterPoint);
        sphere.SetScale(new Vector3(sphereScale, sphereScale, sphereScale));
        this.AddChild(sphere);

        MeshInstance sphere2 = (MeshInstance)greenSphereMeshScene.Instance();

        sphere2.SetTranslation(firstPoint);
        sphere2.SetScale(new Vector3(sphereScale, sphereScale, sphereScale));
        this.AddChild(sphere2);

        MeshInstance sphere3 = (MeshInstance)greenSphereMeshScene.Instance();

        sphere3.SetTranslation((firstPoint - tileCenterPoint) / 2 + tileCenterPoint);
        sphere3.SetScale(new Vector3(sphereScale, sphereScale, sphereScale));
        this.AddChild(sphere3);

        surfTool.GenerateNormals();
        surfTool.Index();
        surfTool.Commit(mesh);
        var meshInstance = new MeshInstance();

        meshInstance.SetMesh(mesh);
        this.AddChild(meshInstance);
    }
Пример #16
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);
    }
Пример #17
0
    private Mesh GenerateMesh()
    {
        st = new SurfaceTool();
        st.Begin(Mesh.PrimitiveType.Triangles);

        for (int i = 0; i < numPoints - 1; i++)
        {
            Vector3 thisP = points[i + 0];
            Vector3 nxtP  = points[i + 1];

            Vector3 thisR = rightVecs[i + 0];
            Vector3 nxtR  = rightVecs[i + 1];

            float thisL = lengths[i + 0];
            float nxtL  = lengths[i + 1];

            Vector3 vec1 = thisP + thisR * roadWidth * .5f;
            Vector3 vec2 = thisP - thisR * roadWidth * .5f;
            Vector3 vec3 = nxtP - nxtR * roadWidth * .5f;
            Vector3 vec4 = nxtP + nxtR * roadWidth * .5f;

            Vector2 uv1 = Vector2.Zero;
            Vector2 uv2 = Vector2.Zero;
            Vector2 uv3 = Vector2.Zero;
            Vector2 uv4 = Vector2.Zero;

            if (tileUV)
            {
                uv1 = new Vector2(thisL / roadWidth, 0f);
                uv2 = new Vector2(thisL / roadWidth, 1f);
                uv3 = new Vector2(nxtL / roadWidth, 1f);
                uv4 = new Vector2(nxtL / roadWidth, 0f);
            }
            else
            {
                uv1 = new Vector2(thisL / totalLength, 0f);
                uv2 = new Vector2(thisL / totalLength, 1f);
                uv3 = new Vector2(nxtL / totalLength, 1f);
                uv4 = new Vector2(nxtL / totalLength, 0f);
            }

            uv1 *= UVScale;
            uv2 *= UVScale;
            uv3 *= UVScale;
            uv4 *= UVScale;

            CreateTRI(
                vec1, vec2, vec4,
                uv1, uv2, uv4
                );
            CreateTRI(
                vec4, vec2, vec3,
                uv4, uv2, uv3
                );
        }

        AddCaps();

        st.Index();
        st.GenerateNormals();
        return(st.Commit());
    }
Пример #18
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();
    }