Ejemplo n.º 1
0
 private void AddVertex(Vector3 vertex, Vector2 uv, Vector3 normal)
 {
     Builder.AddUv(uv);
     Builder.AddNormal(normal);
     Builder.AddVertex(vertex);
     collisionShapeFaces.Add(vertex);
 }
Ejemplo n.º 2
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.º 3
0
        private void addTri(Vector3 p1, Vector3 p2, Vector3 p3, Vector3 p4, BlockState data)
        {
            // UVMap uv = UVMap.getMap("aa_test");
            UVMap uv = UVMap.getMap(data.name);

            // if (data == 2) {
            //  uv = UVMap.getMap("bedrock");
            // }
            // else if (data == 3) {
            //  uv = UVMap.getMap("jungle_planks");
            // }

            tool.AddUv(uv.uvMap[0]);
            tool.AddVertex(p4);
            tool.AddUv(uv.uvMap[2]);
            tool.AddVertex(p3);
            tool.AddUv(uv.uvMap[3]);
            tool.AddVertex(p2);

            tool.AddUv(uv.uvMap[0]);
            tool.AddVertex(p4);
            tool.AddUv(uv.uvMap[3]);
            tool.AddVertex(p2);
            tool.AddUv(uv.uvMap[1]);
            tool.AddVertex(p1);
        }
Ejemplo n.º 4
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.º 5
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));
        }
 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.º 7
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));
    }
Ejemplo n.º 8
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.º 9
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.º 10
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.º 11
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]);
                }
            }
        }
        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;
        }
        private Godot.Collections.Array createSurfaceByBones(ArrayMesh mesh, int surface, Skin newSkin, List <UMAReciepeBindPose> origBindPoses)
        {
            var mdt = new MeshDataTool();

            mdt.CreateFromSurface(mesh, surface);

            var st = new SurfaceTool();

            st.Begin(Mesh.PrimitiveType.Triangles);

            var newBindPoses = new List <UMAReciepeBindPose>();

            if (newSkin != null)
            {
                for (int i = 0; i < newSkin.GetBindCount(); i++)
                {
                    newBindPoses.Add(new UMAReciepeBindPose
                    {
                        boneName  = newSkin.GetBindName(i),
                        transform = newSkin.GetBindPose(i),
                        boneIndex = newSkin.GetBindBone(i)
                    });
                }
            }

            var boneAmount = 0;

            for (int i = 0; i < mdt.GetVertexCount(); i++)
            {
                var oldVer  = mdt.GetVertex(i);
                var oldNorm = mdt.GetVertexNormal(i);

                var newVer  = new Vector3();
                var newNorm = new Vector3();

                var indexes = mdt.GetVertexBones(i);

                //  st.AddTangent(mdt.GetVertexTangent(i));
                st.AddBones(mdt.GetVertexBones(i));
                st.AddWeights(mdt.GetVertexWeights(i));

                int boneId = 0;
                foreach (var weight in mdt.GetVertexWeights(i))
                {
                    if (newBindPoses.Count >= indexes[boneId] && origBindPoses.Count >= indexes[boneId])
                    {
                        var restBoneNew      = newBindPoses[indexes[boneId]];
                        var restBoneTemplate = origBindPoses[indexes[boneId]];

                        var dataup    = restBoneNew.transform.Xform(Vector3.Up);
                        var dataright = restBoneNew.transform.Xform(Vector3.Right);

                        var templateup    = restBoneTemplate.transform.Xform(Vector3.Up);
                        var templateright = restBoneTemplate.transform.Xform(Vector3.Right);

                        if (Mathf.Abs(dataup.AngleTo(templateup)) > 1 || Mathf.Abs(dataright.AngleTo(templateright)) > 1)
                        {
                            Transform convertMatrix = restBoneTemplate.transform.Inverse() * restBoneNew.transform;

                            newVer  += convertMatrix.Xform(oldVer) * weight;
                            newNorm += convertMatrix.basis.Xform(oldNorm) * weight;
                        }
                        else
                        {
                            newVer  += oldVer * weight;
                            newNorm += oldNorm * weight;
                        }
                    }
                    else
                    {
                        newVer  += oldVer * weight;
                        newNorm += oldNorm * weight;
                    }

                    boneId++;
                }


                st.AddUv(mdt.GetVertexUv(i));

                if (mdt.GetVertexColor(i) != null)
                {
                    st.AddColor(mdt.GetVertexColor(i));
                }

                if (mdt.GetVertexUv2(i) != null)
                {
                    st.AddUv2(mdt.GetVertexUv2(i));
                }

                st.AddNormal(newNorm);
                st.AddVertex(newVer);

                boneAmount += mdt.GetVertexBones(i).Length;
            }

            //creating indexes
            for (int face = 0; face < mdt.GetFaceCount(); face++)
            {
                for (int faceI = 0; faceI < 3; faceI++)
                {
                    var ind = mdt.GetFaceVertex(face, faceI);
                    st.AddIndex(ind);
                }
            }

            st.GenerateTangents();
            return(st.CommitToArrays());
        }
Ejemplo n.º 14
0
        private static void BuildDoorMesh()
        {
            if (_doorMesh == null)
            {
                Color white = new Color(1f, 1f, 1f, 1f);

                using (SurfaceTool st = new SurfaceTool())
                {
                    Vector3[] cellVerts = Level.GetVerticesForCell();

                    Vector3[] verts = new Vector3[] {
                        Lerp(cellVerts[(int)Level.CellVertexIndex.Top_SW], cellVerts[(int)Level.CellVertexIndex.Top_NW], 0.5f),
                        Lerp(cellVerts[(int)Level.CellVertexIndex.Top_SE], cellVerts[(int)Level.CellVertexIndex.Top_NE], 0.5f),
                        Lerp(cellVerts[(int)Level.CellVertexIndex.Bot_SW], cellVerts[(int)Level.CellVertexIndex.Bot_NW], 0.5f),
                        Lerp(cellVerts[(int)Level.CellVertexIndex.Bot_SE], cellVerts[(int)Level.CellVertexIndex.Bot_NE], 0.5f)
                    };

                    st.Begin(Godot.Mesh.PrimitiveType.Triangles);

                    st.AddUv(new Vector2(0f, 1f));
                    st.AddColor(white);
                    st.AddNormal(Level.South);
                    st.AddVertex(verts[3]);
                    st.AddUv(new Vector2(0f, 0f));
                    st.AddColor(white);
                    st.AddNormal(Level.South);
                    st.AddVertex(verts[1]);
                    st.AddUv(new Vector2(1f, 0f));
                    st.AddColor(white);
                    st.AddNormal(Level.South);
                    st.AddVertex(verts[0]);

                    st.AddUv(new Vector2(1f, 1f));
                    st.AddColor(white);
                    st.AddNormal(Level.South);
                    st.AddVertex(verts[2]);
                    st.AddUv(new Vector2(0f, 1f));
                    st.AddColor(white);
                    st.AddNormal(Level.South);
                    st.AddVertex(verts[3]);
                    st.AddUv(new Vector2(1f, 0f));
                    st.AddColor(white);
                    st.AddNormal(Level.South);
                    st.AddVertex(verts[0]);


                    st.AddUv(new Vector2(1f, 0f));
                    st.AddColor(white);
                    st.AddNormal(Level.North);
                    st.AddVertex(verts[0]);
                    st.AddUv(new Vector2(0f, 0f));
                    st.AddColor(white);
                    st.AddNormal(Level.North);
                    st.AddVertex(verts[1]);
                    st.AddUv(new Vector2(0f, 1f));
                    st.AddColor(white);
                    st.AddNormal(Level.North);
                    st.AddVertex(verts[3]);

                    st.AddUv(new Vector2(1f, 0f));
                    st.AddColor(white);
                    st.AddNormal(Level.North);
                    st.AddVertex(verts[0]);
                    st.AddUv(new Vector2(0f, 1f));
                    st.AddColor(white);
                    st.AddNormal(Level.North);
                    st.AddVertex(verts[3]);
                    st.AddUv(new Vector2(1f, 1f));
                    st.AddColor(white);
                    st.AddNormal(Level.North);
                    st.AddVertex(verts[2]);

                    _doorMesh = st.Commit();
                }
            }
        }
Ejemplo n.º 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);
    }
Ejemplo n.º 16
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;
        }