Ejemplo n.º 1
0
        private void CreateFrontQuad(ref ChunkStruct c, int x, int y, int x2, int y2, int z)
        {
            //The Z-1 is more of a hack because the face previously was shown on the back side rather than the front
            Vector3 voxPosition1 = new Vector3((x) * VOX_SIZE, (y) * VOX_SIZE, (z - 1) * VOX_SIZE);

            voxPosition1.x = voxPosition1.x + (c.Dx * CHUNK_SIZE * VOX_SIZE);
            voxPosition1.y = voxPosition1.y + (c.Dy * CHUNK_SIZE * VOX_SIZE);
            voxPosition1.z = voxPosition1.z + (c.Dz * CHUNK_SIZE * VOX_SIZE);

            Vector3 voxPosition2 = new Vector3((x2) * VOX_SIZE, (y2) * VOX_SIZE, (z) * VOX_SIZE);

            voxPosition2.x = voxPosition2.x + (c.Dx * CHUNK_SIZE * VOX_SIZE);
            voxPosition2.y = voxPosition2.y + (c.Dy * CHUNK_SIZE * VOX_SIZE);
            voxPosition2.z = voxPosition2.z + (c.Dz * CHUNK_SIZE * VOX_SIZE);
            SurfaceTool surfaceTool = c.surfaceTool;

            surfaceTool.AddNormal(new Vector3(0.0f, 0.0f, -1.0f));

            surfaceTool.AddVertex(new Vector3(voxPosition1.x, voxPosition1.y, voxPosition1.z));
            surfaceTool.AddVertex(new Vector3(voxPosition2.x, voxPosition1.y, voxPosition1.z));
            surfaceTool.AddVertex(new Vector3(voxPosition2.x, voxPosition2.y, voxPosition1.z));

            surfaceTool.AddVertex(new Vector3(voxPosition2.x, voxPosition2.y, voxPosition1.z));
            surfaceTool.AddVertex(new Vector3(voxPosition1.x, voxPosition2.y, voxPosition1.z));
            surfaceTool.AddVertex(new Vector3(voxPosition1.x, voxPosition1.y, voxPosition1.z));
            // addQuad(ref c,
            //  new Vector3(x,y,z),
            //  new Vector3(x2,y,z),
            //  new Vector3(x2,y2,z),
            //  new Vector3(x,y2,z));
        }
Ejemplo n.º 2
0
        private void CreateTopQuad(ref ChunkStruct c, int x, int z, int x2, int z2, int y)
        {
            Vector3 voxPosition1 = new Vector3((x) * VOX_SIZE, (y) * VOX_SIZE, (z) * VOX_SIZE);

            voxPosition1.x = voxPosition1.x + (c.Dx * CHUNK_SIZE * VOX_SIZE);
            voxPosition1.y = voxPosition1.y + (c.Dy * CHUNK_SIZE * VOX_SIZE);
            voxPosition1.z = voxPosition1.z + (c.Dz * CHUNK_SIZE * VOX_SIZE);

            Vector3 voxPosition2 = new Vector3((x2) * VOX_SIZE, (y) * VOX_SIZE, (z2) * VOX_SIZE);

            voxPosition2.x = voxPosition2.x + (c.Dx * CHUNK_SIZE * VOX_SIZE);
            voxPosition2.y = voxPosition2.y + (c.Dy * CHUNK_SIZE * VOX_SIZE);
            voxPosition2.z = voxPosition2.z + (c.Dz * CHUNK_SIZE * VOX_SIZE);
            SurfaceTool surfaceTool = c.surfaceTool;

            surfaceTool.AddNormal(new Vector3(0.0f, 1.0f, 0.0f));

            surfaceTool.AddVertex(new Vector3(voxPosition1.x, voxPosition2.y, voxPosition1.z));
            surfaceTool.AddVertex(new Vector3(voxPosition2.x, voxPosition2.y, voxPosition1.z));
            surfaceTool.AddVertex(new Vector3(voxPosition1.x, voxPosition2.y, voxPosition2.z));

            surfaceTool.AddVertex(new Vector3(voxPosition2.x, voxPosition2.y, voxPosition1.z));
            surfaceTool.AddVertex(new Vector3(voxPosition2.x, voxPosition2.y, voxPosition2.z));
            surfaceTool.AddVertex(new Vector3(voxPosition1.x, voxPosition2.y, voxPosition2.z));
            // this.addQuad(ref c,
            //  new Vector3(x, y, z2),
            //  new Vector3(x2, y, z2),
            //  new Vector3(x2, y, z),
            //  new Vector3(x, y, z));
        }
Ejemplo n.º 3
0
    private void Initialize()
    {
        _meshInstance = GetNode <MeshInstance>("Mesh");
        PlaneMesh initialMesh = (PlaneMesh)_meshInstance.Mesh;

        _resX = initialMesh.SubdivideWidth + 1;
        _resZ = initialMesh.SubdivideDepth + 1;

        _noise = new OpenSimplexNoise();
        UpdateNoiseSettings();

        _surfaceTool  = new SurfaceTool();
        _meshDataTool = new MeshDataTool();

        Control HUD = GetNode <Control>("../HUD");

        HUD.Connect("UpdateSeed", this, nameof(_On_Seed_Updated));
        HUD.Connect("UpdateOctaves", this, nameof(_On_Octaves_Updated));
        HUD.Connect("UpdateAmplitude", this, nameof(_On_Amplitude_Updated));
        HUD.Connect("UpdatePeriod", this, nameof(_On_Period_Updated));
        HUD.Connect("UpdateLacunarity", this, nameof(_On_Lacunarity_Updated));
        HUD.Connect("UpdatePersistence", this, nameof(_On_Persistence_Updated));

        _initialized = true;
    }
Ejemplo n.º 4
0
    private void _plane(SurfaceTool st, float posX, float posY)
    {
        int xOffset = (int)posX;
        int yOffset = (int)posY;
        var z       = 0;

        GD.Print(posX, " ", posY);
        st.AddColor(get_color(xOffset, yOffset));
        st.AddUv(new Vector2(0, 1));
        st.AddVertex(new Vector3(-1 + xOffset * 2, 1 + yOffset * 2, z));

        st.AddUv(new Vector2(1, 1));
        st.AddVertex(new Vector3(1 + xOffset * 2, 1 + yOffset * 2, z));

        st.AddUv(new Vector2(1, 0));
        st.AddVertex(new Vector3(1 + xOffset * 2, -1 + yOffset * 2, z));

        st.AddUv(new Vector2(1, 0));
        st.AddVertex(new Vector3(1 + xOffset * 2, -1 + yOffset * 2, z));

        st.AddUv(new Vector2(0, 0));
        st.AddVertex(new Vector3(-1 + xOffset * 2, -1 + yOffset * 2, z));

        st.AddUv(new Vector2(0, 1));
        st.AddVertex(new Vector3(-1 + xOffset * 2, 1 + yOffset * 2, z));
    }
Ejemplo n.º 5
0
        //Adds a triangle to a surface tool.
        public static void AddTriangle(SurfaceTool surfTool, Vector3 point1, Vector3 point2, Vector3 point3, bool reverse = false)
        {
            if (reverse)
            {
                surfTool.AddUv(new Vector2(0, 0));
                surfTool.AddVertex(point1);

                surfTool.AddUv(new Vector2(0, 0));
                surfTool.AddVertex(point2);

                surfTool.AddUv(new Vector2(0, 0));
                surfTool.AddVertex(point3);
            }
            else
            {
                surfTool.AddUv(new Vector2(0, 0));
                surfTool.AddVertex(point3);

                surfTool.AddUv(new Vector2(0, 0));
                surfTool.AddVertex(point2);

                surfTool.AddUv(new Vector2(0, 0));
                surfTool.AddVertex(point1);
            }
        }
Ejemplo n.º 6
0
    // Called when the node enters the scene tree for the first time.
    public override void _Ready()
    {
        global = GetNode <Global>("/root/Global");
        OnStartTimerTimeout();

        CharacterContainer = GetNode <Spatial>("/root/Spatial/CharacterContainer");

        SurfaceTool surfaceTool = new SurfaceTool();

        planeMesh                = new PlaneMesh();
        planeMesh.Material       = (Material)ResourceLoader.Load("mesh_material.tres");
        planeMesh.SubdivideWidth = width - 2;               //Subtract two bc there's already two vertices before we start subdividing
        planeMesh.SubdivideDepth = height - 2;
        planeMesh.Size           = new Vector2(42.4f, 24f); //42.47f (42.4f, 24f)

        surfaceTool.CreateFrom(planeMesh, 0);

        arrayPlane = surfaceTool.Commit();


        meshInstance      = new MeshInstance();
        meshInstance.Mesh = arrayPlane;


        NewDetectedBody();
        AddChild(meshInstance);
        AddChild(detectBody);


        UpdateShapes();
    }
Ejemplo n.º 7
0
        public SurfaceTool getWFace(SurfaceTool st, Vector3 offset)
        {
            //Upper Tri
            //N W D
            st.add_uv(new Vector2(0, 0));
            st.add_vertex(new Vector3(1 + offset.x, -1 + offset.y, 1 + offset.z));

            //N W U
            st.add_uv(new Vector2(0, 1));
            st.add_vertex(new Vector3(1 + offset.x, 1 + offset.y, 1 + offset.z));

            //S W U
            st.add_uv(new Vector2(1, 1));
            st.add_vertex(new Vector3(-1 + offset.x, 1 + offset.y, 1 + offset.z));

            //LOWER TRI
            //S W U
            st.add_uv(new Vector2(1, 1));
            st.add_vertex(new Vector3(-1 + offset.x, 1 + offset.y, 1 + offset.z));

            //S W D
            st.add_uv(new Vector2(1, 0));
            st.add_vertex(new Vector3(-1 + offset.x, -1 + offset.y, 1 + offset.z));

            //N W D
            st.add_uv(new Vector2(0, 0));
            st.add_vertex(new Vector3(1 + offset.x, -1 + offset.y, 1 + offset.z));

            st.index();

            return(st);
        }
Ejemplo n.º 8
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());
    }
Ejemplo n.º 9
0
        //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);
        }
 void _draw_block_face(SurfaceTool surface_tool,
                       Godot.Collections.Array <Vector3> verts,
                       Godot.Collections.Array <Vector2> uvs)
 {
     surface_tool.AddUv(uvs[1]); surface_tool.AddVertex(verts[1]);
     surface_tool.AddUv(uvs[2]); surface_tool.AddVertex(verts[2]);
     surface_tool.AddUv(uvs[3]); surface_tool.AddVertex(verts[3]);
     surface_tool.AddUv(uvs[2]); surface_tool.AddVertex(verts[2]);
     surface_tool.AddUv(uvs[1]); surface_tool.AddVertex(verts[1]);
     surface_tool.AddUv(uvs[0]); surface_tool.AddVertex(verts[0]);
 }
Ejemplo n.º 11
0
    public void Generate()
    {
        planeMesh                = new PlaneMesh();
        planeMesh.Size           = new Vector2(chunkSize, chunkSize);
        planeMesh.SubdivideDepth = Mathf.RoundToInt(chunkSize * 0.5f);
        planeMesh.SubdivideWidth = Mathf.RoundToInt(chunkSize * 0.5f);

        surfaceTool  = new SurfaceTool();
        meshDataTool = new MeshDataTool();
        surfaceTool.CreateFrom(planeMesh, 0);
        arrayPlane = surfaceTool.Commit();
        meshDataTool.CreateFromSurface(arrayPlane, 0);

        for (int i = 0; i < meshDataTool.GetVertexCount(); i++)
        {
            Vector3 vertex = meshDataTool.GetVertex(i);
            vertex.y = noise.GetNoise3d(
                vertex.x + position.x,
                vertex.y,
                vertex.z + position.z) * References.steepness;

            meshDataTool.SetVertex(i, vertex);
            avgHeight += vertex.y;
        }
        avgHeight /= meshDataTool.GetVertexCount();

        for (int i = 0; i < arrayPlane.GetSurfaceCount(); i++)
        {
            arrayPlane.SurfaceRemove(i);
        }

        for (int i = 0; i < meshDataTool.GetFaceCount(); i++)
        {
            Vector3 A      = meshDataTool.GetVertex(meshDataTool.GetFaceVertex(i, 0));
            Vector3 B      = meshDataTool.GetVertex(meshDataTool.GetFaceVertex(i, 1));
            Vector3 C      = meshDataTool.GetVertex(meshDataTool.GetFaceVertex(i, 2));
            Vector3 face   = (A + B + C) / 3 + position;
            Vector3 normal = meshDataTool.GetFaceNormal(i);
            slope += Maths.Angle(Vector3.Up, normal);
        }
        slope /= meshDataTool.GetFaceCount();

        meshDataTool.CommitToSurface(arrayPlane);
        surfaceTool.Begin(Mesh.PrimitiveType.Triangles);
        surfaceTool.CreateFrom(arrayPlane, 0);
        surfaceTool.GenerateNormals();

        meshInstance      = new MeshInstance();
        meshInstance.Mesh = surfaceTool.Commit();
        meshInstance.SetSurfaceMaterial(0, (Material)ResourceLoader.Load("res://Assets/Shader/Terrain.material"));
        meshInstance.CreateTrimeshCollision();
        meshInstance.CastShadow = GeometryInstance.ShadowCastingSetting.On;
        AddChild(meshInstance);
    }
Ejemplo n.º 12
0
 public ChunkStruct(int chunkSize, float voxSize, int Dx, int Dy, int Dz)
 {
     this.CHUNK_SIZE = chunkSize;
     this.VOX_SIZE   = voxSize;
     this.Dx         = Dx;
     this.Dy         = Dy;
     this.Dz         = Dz;
     chunkData       = new byte[CHUNK_SIZE * CHUNK_SIZE * CHUNK_SIZE];
     surfaceTool     = new SurfaceTool();
     compChunkData   = new byte[1];
     random          = new Random();
     currentlyWorked = 0;
 }
Ejemplo n.º 13
0
    private static MeshInstance DebugMesh()
    {
        SurfaceTool tool = new SurfaceTool();

        tool.Begin(PrimitiveMesh.PrimitiveType.Lines);

        //Front
        tool.AddVertex(new Vector3(0, 0, 0));
        tool.AddVertex(new Vector3(1, 0, 0));
        tool.AddVertex(new Vector3(1, 0, 0));
        tool.AddVertex(new Vector3(1, 1, 0));
        tool.AddVertex(new Vector3(1, 1, 0));
        tool.AddVertex(new Vector3(0, 1, 0));
        tool.AddVertex(new Vector3(0, 1, 0));
        tool.AddVertex(new Vector3(0, 0, 0));

        //Back
        tool.AddVertex(new Vector3(0, 0, 1));
        tool.AddVertex(new Vector3(1, 0, 1));
        tool.AddVertex(new Vector3(1, 0, 1));
        tool.AddVertex(new Vector3(1, 1, 1));
        tool.AddVertex(new Vector3(1, 1, 1));
        tool.AddVertex(new Vector3(0, 1, 1));
        tool.AddVertex(new Vector3(0, 1, 1));
        tool.AddVertex(new Vector3(0, 0, 1));

        //BOTTOM
        tool.AddVertex(new Vector3(0, 0, 0));
        tool.AddVertex(new Vector3(0, 0, 1));
        tool.AddVertex(new Vector3(0, 0, 1));
        tool.AddVertex(new Vector3(1, 0, 1));
        tool.AddVertex(new Vector3(1, 0, 1));
        tool.AddVertex(new Vector3(1, 0, 0));
        tool.AddVertex(new Vector3(1, 0, 0));
        tool.AddVertex(new Vector3(0, 0, 0));

        //TOP
        tool.AddVertex(new Vector3(0, 1, 0));
        tool.AddVertex(new Vector3(0, 1, 1));
        tool.AddVertex(new Vector3(0, 1, 1));
        tool.AddVertex(new Vector3(1, 1, 1));
        tool.AddVertex(new Vector3(1, 1, 1));
        tool.AddVertex(new Vector3(1, 1, 0));
        tool.AddVertex(new Vector3(1, 1, 0));
        tool.AddVertex(new Vector3(0, 1, 0));

        MeshInstance instance = new MeshInstance();

        instance.Mesh = tool.Commit();
        return(instance);
    }
    private void Deform()
    {
        var sn = new OpenSimplexNoise();

        sn.Period      = Period;
        sn.Octaves     = Octaves;
        sn.Seed        = Seed;
        sn.Persistence = Persistence;
        sn.Lacunarity  = Lacunarity;

        var mesh = sphere.Mesh;
        var st   = new SurfaceTool();

        st.CreateFrom(mesh, 0);

        var array = st.Commit();

        var dt = new MeshDataTool();

        dt.CreateFromSurface(array, 0);

        var count  = dt.GetVertexCount();
        var origin = Vector3.Zero;

        for (int i = 0; i < count; i++)
        {
            var vertex = dt.GetVertex(i);
            var n      = -vertex.DirectionTo(origin);
            var noise  = sn.GetNoise3d(vertex.x, vertex.y, vertex.z);
            noise   = Mathf.Clamp(noise, -1f, 1f);
            vertex += n * noise * MoveFactor;
            dt.SetVertex(i, vertex);
        }
        var surfCount = array.GetSurfaceCount();

        for (int i = 0; i < surfCount; i++)
        {
            array.SurfaceRemove(i);
        }

        dt.CommitToSurface(array);
        st.Begin(Mesh.PrimitiveType.Triangles);
        st.CreateFrom(array, 0);
        st.GenerateNormals();

        sphere.Mesh = st.Commit();

        UpdateMaterial();
    }
    public void Generate()
    {
        verts   = new List <Vector3>();
        indices = new List <int>();
        index   = 0;
        faces   = new List <TriangleIndices>();
        middlePointIndexCache = new System.Collections.Generic.Dictionary <long, int>();
        CreateIcosphere();
        SurfaceTool st = new SurfaceTool();
        var         am = CreateTheMesh();

        st.CreateFrom(am, 0);
        st.GenerateNormals();
        Mesh = st.Commit();
    }
Ejemplo n.º 16
0
    public override void _Ready()
    {
        MarchCube(new Vector3(1, 0, 0));
        SurfaceTool st = new SurfaceTool();

        st.Begin(Mesh.PrimitiveType.Triangles);

        foreach (Vector3 vert in vertices)
        {
            st.AddVertex(vert);
        }

        Mesh = st.Commit();

        GD.Print(vertices);
    }
Ejemplo n.º 17
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());
    }
Ejemplo n.º 18
0
        //Creates a surface tool for mesh creation
        public static SurfaceTool CreateSurfaceTool(SpatialMaterial material = null)
        {
            var surfTool = new SurfaceTool();

            surfTool.Begin(Mesh.PrimitiveType.Triangles);
            if (material == null)
            {
                material = new SpatialMaterial();
                //material.SetEmission(new Color(1.0f, 0.0f, 0.0f));
                //material.SetEmissionEnergy(0.5f);
                material.SetAlbedo(new Color(0.5f, 0.0f, 0.0f));
                //material.SetMetallic(0.5f);
                material.SetCullMode(SpatialMaterial.CullMode.Back);
            }
            surfTool.SetMaterial(material);
            return(surfTool);
        }
Ejemplo n.º 19
0
    // TODO: spend some time to actually properly refactor that (the first step is 25% done)
    public override void AddPosXFace(ref SurfaceTool surfaceTool, Vector3 origin, byte blockType)
    {
        Rect2 uvs = Game.GetBlockUV(blockType, BlockFace.Left);

        surfaceTool.AddUv(uvs.Get(1, 1));
        surfaceTool.AddVertex(origin + this[+1, -1, -1]);
        surfaceTool.AddUv(uvs.Get(0, 0));
        surfaceTool.AddVertex(origin + this[+1, +1, +1]);
        surfaceTool.AddUv(uvs.Get(1, 0));
        surfaceTool.AddVertex(origin + this[+1, +1, -1]);

        surfaceTool.AddUv(uvs.Get(1, 1));
        surfaceTool.AddVertex(origin + this[+1, -1, -1]);
        surfaceTool.AddUv(uvs.Get(0, 1));
        surfaceTool.AddVertex(origin + this[+1, -1, +1]);
        surfaceTool.AddUv(uvs.Get(0, 0));
        surfaceTool.AddVertex(origin + this[+1, +1, +1]);
    }
Ejemplo n.º 20
0
    public override void AddNegXFace(ref SurfaceTool surfaceTool, Vector3 origin, byte blockType)
    {
        Rect2 uvs = Game.GetBlockUV(blockType, BlockFace.Right);

        surfaceTool.AddUv(uvs.Get(0, 1));
        surfaceTool.AddVertex(origin + this[-1, -1, -1]);
        surfaceTool.AddUv(uvs.Get(0, 0));
        surfaceTool.AddVertex(origin + this[-1, +1, -1]);
        surfaceTool.AddUv(uvs.Get(1, 0));
        surfaceTool.AddVertex(origin + this[-1, +1, +1]);

        surfaceTool.AddUv(uvs.Position + new Vector2(0, uvs.Size.y));
        surfaceTool.AddVertex(origin + SIZE * new Vector3(-0.5f, -0.5f, -0.5f));
        surfaceTool.AddUv(uvs.Position + new Vector2(uvs.Size.x, 0));
        surfaceTool.AddVertex(origin + SIZE * new Vector3(-0.5f, +0.5f, +0.5f));
        surfaceTool.AddUv(uvs.End);
        surfaceTool.AddVertex(origin + SIZE * new Vector3(-0.5f, -0.5f, +0.5f));
    }
Ejemplo n.º 21
0
    public override void AddNegYFace(ref SurfaceTool surfaceTool, Vector3 origin, byte blockType)
    {
        Rect2 uvs = Game.GetBlockUV(blockType, BlockFace.Bottom);

        surfaceTool.AddUv(uvs.Position);
        surfaceTool.AddVertex(origin + SIZE * new Vector3(-0.5f, -0.5f, -0.5f));
        surfaceTool.AddUv(uvs.End);
        surfaceTool.AddVertex(origin + SIZE * new Vector3(+0.5f, -0.5f, +0.5f));
        surfaceTool.AddUv(uvs.Position + new Vector2(uvs.Size.x, 0));
        surfaceTool.AddVertex(origin + SIZE * new Vector3(+0.5f, -0.5f, -0.5f));

        surfaceTool.AddUv(uvs.Position);
        surfaceTool.AddVertex(origin + SIZE * new Vector3(-0.5f, -0.5f, -0.5f));
        surfaceTool.AddUv(uvs.Position + new Vector2(0, uvs.Size.y));
        surfaceTool.AddVertex(origin + SIZE * new Vector3(-0.5f, -0.5f, +0.5f));
        surfaceTool.AddUv(uvs.End);
        surfaceTool.AddVertex(origin + SIZE * new Vector3(+0.5f, -0.5f, +0.5f));
    }
    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);
    }
Ejemplo n.º 23
0
    public void generateMeshAndColors(float[,] dataTerrain)
    {
        //init tool
        SurfaceTool st = new SurfaceTool();

        st.Begin(Mesh.PrimitiveType.Triangles);
        st.SetMaterial(mat);

        //get HeightsQuats and create triangles
        int maxI      = dataTerrain.GetLength(0);
        int maxJ      = dataTerrain.GetLength(1);
        int quadCount = 0;

        //1º rows, 2º columns
        for (int i = 0; i < maxI - 1; i++)
        {
            for (int j = 0; j < maxJ - 1; j++)
            {
                Quat q = new Quat();//heights to Quat

                q.x = dataTerrain[i, j];       q.y = dataTerrain[i, j + 1];

                q.z = dataTerrain[i + 1, j];     q.w = dataTerrain[i + 1, j + 1];

                //to mesh
                Vector3 offset = new Vector3(j, 0, i);
                createQuad(st, offset, q);

                /*//debug algoritm
                 * GD.Print(string.Format("•quad{0}:({1},{2}): points: \n{3} {4} {5}   {6} {7} {8}\n{9} {10} {11}   {12} {13} {14}",
                 * quadCount,i,j,
                 * offset.x, q.x, offset.z - 1,       offset.x + 1, q.y, offset.z-1,
                 * offset.x, q.z, offset.z,         offset.x + 1, q.w, offset.z));
                 */
                quadCount++;
            }
        }

        //finally
        st.GenerateNormals();
        st.Commit(tmpMesh);
        this.SetMesh(tmpMesh);
    }
Ejemplo n.º 24
0
        public Chunk(int x, int y, int z, int CHUNK_SIZE = 16, float VOX_SIZE = 1.0f)
        {
            Dx = x;
            Dy = y;
            Dz = z;
            this.CHUNK_SIZE = CHUNK_SIZE;
            surfaceTool     = new SurfaceTool();
            this.VOX_SIZE   = VOX_SIZE;
            vertices        = new Vector3[] { new Vector3(0, 0, 0),
                                              new Vector3(VOX_SIZE, 0, 0),
                                              new Vector3(VOX_SIZE, 0, VOX_SIZE),
                                              new Vector3(0, 0, VOX_SIZE),

                                              new Vector3(0, VOX_SIZE, 0),
                                              new Vector3(VOX_SIZE, VOX_SIZE, 0),
                                              new Vector3(VOX_SIZE, VOX_SIZE, VOX_SIZE),
                                              new Vector3(0, VOX_SIZE, VOX_SIZE) };
            InitializedVoxelData();
            CreateMesh();
        }
Ejemplo n.º 25
0
        public void Render()
        {
            SurfaceTool tool = new SurfaceTool();

            tool.Begin(Mesh.PrimitiveType.Triangles);

            for (int x = 1; x < Size.x - 1; x++)
            {
                for (int z = 1; z < Size.z - 1; z++)
                {
                    for (int y = 1; y < Size.y - 1; y++)
                    {
                        BuildFaces(GetSideFree(x, y, z), tool, x, y, z);
                    }
                }
            }

            tool.GenerateNormals();

            Model.Mesh = tool.Commit();
        }
Ejemplo n.º 26
0
    private SurfaceTool createQuad(SurfaceTool st, Vector3 pos, Quat q)
    {
        //1Quad = 4 points = 2 triangles
        Vector3 v1 = new Vector3(0, q.x, -1) + pos;
        Vector3 v2 = new Vector3(1, q.y, -1) + pos;
        Vector3 v3 = new Vector3(1, q.w, 0) + pos;
        Vector3 v4 = new Vector3(0, q.z, 0) + pos;

        //tri1
        st.AddUv(new Vector2(0, 0));
        st.AddColor(heightToColor(v1.y));// active albedoVertexColors on material
        st.AddVertex(v1);


        st.AddUv(new Vector2(0, 1));
        st.AddColor(heightToColor(v2.y));
        st.AddVertex(v2);


        st.AddUv(new Vector2(1, 1));
        st.AddColor(heightToColor(v4.y));
        st.AddVertex(v4);

        //tri2
        st.AddUv(new Vector2(0, 0));
        st.AddColor(heightToColor(v2.y));
        st.AddVertex(v2);

        st.AddUv(new Vector2(0, 1));
        st.AddColor(heightToColor(v3.y));
        st.AddVertex(v3);

        st.AddUv(new Vector2(1, 1));
        st.AddColor(heightToColor(v4.y));
        st.AddVertex(v4);

        return(st);
    }
Ejemplo n.º 27
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;
    }
Ejemplo n.º 28
0
        // public void SetPolygons() {
        //   this.a.AddPolygon(this)
        //   this.b.AddPolygon(this)
        //   this.c.AddPolygon(this)
        // }

        public void AddToSurfaceTool(SurfaceTool surfaceTool)
        {
            // System.Diagnostics.Debug.WriteLine("This vertex A-B-C: [" + a.x + ", " + a.y + ", " + a.z + "] - [" + b.x + ", " + b.y + ", " + b.z + "] - [" + c.x + ", " + c.y + ", " + c.z + "]");

            const float minLimit = -0.05f;
            const float maxLimit = 0.069f;

            surfaceTool.AddColor(a.determineVertexColor(minLimit, maxLimit));
            surfaceTool.AddColor(b.determineVertexColor(minLimit, maxLimit));
            surfaceTool.AddColor(c.determineVertexColor(minLimit, maxLimit));

            // Add UV coords
            surfaceTool.AddUv(
                new Vector2(
                    Mathf.Atan2(a.z, a.x) / (2f * Mathf.Pi),
                    Mathf.Asin(a.y) / Mathf.Pi + 0.5f
                    )
                );
            surfaceTool.AddUv(
                new Vector2(
                    Mathf.Atan2(b.z, b.x) / (2f * Mathf.Pi),
                    Mathf.Asin(b.y) / Mathf.Pi + 0.5f
                    )
                );
            surfaceTool.AddUv(
                new Vector2(
                    Mathf.Atan2(c.z, c.x) / (2f * Mathf.Pi),
                    Mathf.Asin(c.y) / Mathf.Pi + 0.5f
                    )
                );

            // add vertices
            surfaceTool.AddVertex(new Vector3(a.x, a.y, a.z));
            surfaceTool.AddVertex(new Vector3(b.x, b.y, b.z));
            surfaceTool.AddVertex(new Vector3(c.x, c.y, c.z));
        }
Ejemplo n.º 29
0
        public static void CreateCube(SurfaceTool st, float size, Vector3 position, Sides sides)
        {
            if (sides.HasFlag(Sides.North) ||
                sides.HasFlag(Sides.East) ||
                sides.HasFlag(Sides.South) ||
                sides.HasFlag(Sides.West))
            {
                Color white = new Color(1f, 1f, 1f, 1f);

                Vector3[] vertices = GetVerticesForCell(size, position);

                if (sides.HasFlag(Sides.North))
                {
                    st.AddUv(new Vector2(1, 1));
                    st.AddNormal(North);
                    st.AddColor(white);
                    st.AddVertex(vertices[(int)CellVertexIndex.Bot_NW]);
                    st.AddUv(new Vector2(0, 0));
                    st.AddNormal(North);
                    st.AddColor(white);
                    st.AddVertex(vertices[(int)CellVertexIndex.Top_NE]);
                    st.AddUv(new Vector2(1, 0));
                    st.AddNormal(North);
                    st.AddColor(white);
                    st.AddVertex(vertices[(int)CellVertexIndex.Top_NW]);

                    st.AddUv(new Vector2(1, 1));
                    st.AddNormal(North);
                    st.AddColor(white);
                    st.AddVertex(vertices[(int)CellVertexIndex.Bot_NW]);
                    st.AddUv(new Vector2(0, 1));
                    st.AddNormal(North);
                    st.AddColor(white);
                    st.AddVertex(vertices[(int)CellVertexIndex.Bot_NE]);
                    st.AddUv(new Vector2(0, 0));
                    st.AddNormal(North);
                    st.AddColor(white);
                    st.AddVertex(vertices[(int)CellVertexIndex.Top_NE]);
                }

                if (sides.HasFlag(Sides.East))
                {
                    st.AddUv(new Vector2(0, 0));
                    st.AddNormal(East);
                    st.AddColor(white);
                    st.AddVertex(vertices[(int)CellVertexIndex.Top_SE]);
                    st.AddUv(new Vector2(1, 0));
                    st.AddNormal(East);
                    st.AddColor(white);
                    st.AddVertex(vertices[(int)CellVertexIndex.Top_NE]);
                    st.AddUv(new Vector2(0, 1));
                    st.AddNormal(East);
                    st.AddColor(white);
                    st.AddVertex(vertices[(int)CellVertexIndex.Bot_SE]);

                    st.AddUv(new Vector2(1, 0));
                    st.AddNormal(East);
                    st.AddColor(white);
                    st.AddVertex(vertices[(int)CellVertexIndex.Top_NE]);
                    st.AddUv(new Vector2(1, 1));
                    st.AddNormal(East);
                    st.AddColor(white);
                    st.AddVertex(vertices[(int)CellVertexIndex.Bot_NE]);
                    st.AddUv(new Vector2(0, 1));
                    st.AddNormal(East);
                    st.AddColor(white);
                    st.AddVertex(vertices[(int)CellVertexIndex.Bot_SE]);
                }

                if (sides.HasFlag(Sides.South))
                {
                    st.AddUv(new Vector2(0, 0));
                    st.AddNormal(South);
                    st.AddColor(white);
                    st.AddVertex(vertices[(int)CellVertexIndex.Top_SW]);
                    st.AddUv(new Vector2(1, 0));
                    st.AddNormal(South);
                    st.AddColor(white);
                    st.AddVertex(vertices[(int)CellVertexIndex.Top_SE]);
                    st.AddUv(new Vector2(0, 1));
                    st.AddNormal(South);
                    st.AddColor(white);
                    st.AddVertex(vertices[(int)CellVertexIndex.Bot_SW]);

                    st.AddUv(new Vector2(1, 0));
                    st.AddNormal(South);
                    st.AddColor(white);
                    st.AddVertex(vertices[(int)CellVertexIndex.Top_SE]);
                    st.AddUv(new Vector2(1, 1));
                    st.AddNormal(South);
                    st.AddColor(white);
                    st.AddVertex(vertices[(int)CellVertexIndex.Bot_SE]);
                    st.AddUv(new Vector2(0, 1));
                    st.AddNormal(South);
                    st.AddColor(white);
                    st.AddVertex(vertices[(int)CellVertexIndex.Bot_SW]);
                }

                if (sides.HasFlag(Sides.West))
                {
                    st.AddUv(new Vector2(1, 1));
                    st.AddNormal(West);
                    st.AddColor(white);
                    st.AddVertex(vertices[(int)CellVertexIndex.Bot_SW]);
                    st.AddUv(new Vector2(0, 0));
                    st.AddNormal(West);
                    st.AddColor(white);
                    st.AddVertex(vertices[(int)CellVertexIndex.Top_NW]);
                    st.AddUv(new Vector2(1, 0));
                    st.AddNormal(West);
                    st.AddColor(white);
                    st.AddVertex(vertices[(int)CellVertexIndex.Top_SW]);

                    st.AddUv(new Vector2(1, 1));
                    st.AddNormal(West);
                    st.AddColor(white);
                    st.AddVertex(vertices[(int)CellVertexIndex.Bot_SW]);
                    st.AddUv(new Vector2(0, 1));
                    st.AddNormal(West);
                    st.AddColor(white);
                    st.AddVertex(vertices[(int)CellVertexIndex.Bot_NW]);
                    st.AddUv(new Vector2(0, 0));
                    st.AddNormal(West);
                    st.AddColor(white);
                    st.AddVertex(vertices[(int)CellVertexIndex.Top_NW]);
                }
            }
        }
Ejemplo n.º 30
0
        public void BuildMeshFromCells()
        {
            if (Mesh != null)
            {
                var msh = Mesh;
                Mesh = null;
                msh.Dispose();
            }

            Dictionary <int, SurfaceTool> surfaces = new Dictionary <int, SurfaceTool>();

            for (int y = 0; y < Map.Height; y++)
            {
                for (int x = 0; x < Map.Width; x++)
                {
                    Cell cell = Cells[y, x];

                    var indices = (
                        new int[] {
                        cell.North,
                        cell.East,
                        cell.South,
                        cell.West
                    })
                                  .Select(i => i)
                                  .Distinct();

                    foreach (var index in indices)
                    {
                        if (index == Cell.NoWall)
                        {
                            continue;
                        }

                        if (!surfaces.ContainsKey(index))
                        {
                            surfaces.Add(index, new SurfaceTool());
                        }
                    }
                }
            }

            ArrayMesh   mesh = new ArrayMesh();
            SurfaceTool st   = new SurfaceTool();

            foreach (var kvp in surfaces)
            {
                kvp.Value.Begin(Mesh.PrimitiveType.Triangles);
                kvp.Value.SetMaterial(Assets.GetTexture(kvp.Key));
            }

            for (int y = 0; y < Map.Height; y++)
            {
                for (int x = 0; x < Map.Width; x++)
                {
                    Cell cell = Cells[y, x];

                    Vector3 pos = MapToWorld(x, y);

                    if (cell.North != Cell.NoWall)
                    {
                        CreateCube(surfaces[cell.North], CellSize, pos, Sides.North);
                    }

                    if (cell.East != Cell.NoWall)
                    {
                        CreateCube(surfaces[cell.East], CellSize, pos, Sides.East);
                    }

                    if (cell.South != Cell.NoWall)
                    {
                        CreateCube(surfaces[cell.South], CellSize, pos, Sides.South);
                    }

                    if (cell.West != Cell.NoWall)
                    {
                        CreateCube(surfaces[cell.West], CellSize, pos, Sides.West);
                    }
                }
            }

            foreach (var kvp in surfaces)
            {
                kvp.Value.Commit(mesh);
                kvp.Value.Dispose();
            }

            Mesh = mesh;

            CreateTrimeshCollision();

            WorldBody = GetNode <StaticBody>($"{nameof(Level)}_col");
            WorldBody.CollisionLayer = (int)CollisionLayers.Walls;
        }