Exemplo n.º 1
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();
    }
Exemplo n.º 2
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());
    }
        //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);
        }
Exemplo n.º 4
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);
    }
    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();
    }
Exemplo n.º 7
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());
    }
Exemplo n.º 8
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);
    }
    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);
    }
Exemplo n.º 10
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();
        }
Exemplo n.º 11
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;
    }
Exemplo n.º 12
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);
    }
Exemplo n.º 13
0
    // Generates sphere with radius 1
    public void GenerateSphere(int iterations)
    {
        float radius = 10f;

        System.Diagnostics.Debug.WriteLine("Generating sphere!");
        Stopwatch totalTimer = new Stopwatch();

        totalTimer.Start();

        SurfaceTool surfaceTool = new SurfaceTool();

        this.planetMesh = new ArrayMesh();

        material.VertexColorUseAsAlbedo = true;
        surfaceTool.SetMaterial(material);
        surfaceTool.Begin(PrimitiveType.Triangles);

        // GENERATE SPHERE:
        Stopwatch planetGeneratorTimer = new Stopwatch();

        planetGeneratorTimer.Start();

        // 9 gives ~30s generate time, which is acceptable
        this.currentStatus = "Generating base topology (this step can take long!)";
        PlanetTopologyData[] planetData = pg.GenerateBaseTopology(iterations, radius);

        planetGeneratorTimer.Stop();
        TimeSpan ts = planetGeneratorTimer.Elapsed;

        System.Diagnostics.Debug.WriteLine("Sphere generated. Number of faces: " + (planetData[0].faces.Count * 20) + "; time needed: " + ts.Minutes + "m " + ts.Seconds + "." + ts.Milliseconds);

        // todo: create biomes

        // Add perlino
        Stopwatch perlinTimer = new Stopwatch();

        perlinTimer.Start();

        this.currentStatus = "Adding perlin noise ...";

        Parallel.For(0, planetData.Length, new ParallelOptions {
            MaxDegreeOfParallelism = System.Environment.ProcessorCount
        }, i => {
            PlanetTopologyData data = planetData[i];
            data.vertices           = pg.AddPerlinDisplacement(data.vertices, radius, 16, 0.42f);

            // float maxDispalcement = 0;
            // float maxNegDisplacement = 0;
            // foreach (PlanetVertex pv in data.vertices) {
            //   if (pv.h > maxDispalcement) {
            //     maxDispalcement = pv.h;
            //   }
            //   if (pv.h < maxNegDisplacement) {
            //     maxNegDisplacement = pv.h;
            //   }
            // }
            // Debug.WriteLine("Max displacement for this face (from-to) " + maxNegDisplacement +" -> " + maxDispalcement);
        });

        perlinTimer.Stop();
        ts = perlinTimer.Elapsed;
        System.Diagnostics.Debug.WriteLine("Perlin applied. Time needed: " + ts.Minutes + "m " + ts.Seconds + "." + ts.Milliseconds);

        Stopwatch surfaceToolTimer = new Stopwatch();

        surfaceToolTimer.Start();

        this.currentStatus = "Building planet mesh ...";

        // // Possible future for multithreading ahead. Might happen some day.
        //
        // SurfaceTool[] surfaceTool = new SurfaceTool[planetData.Length];
        // this.planetMeshes = new ArrayMesh[planetData.Length];

        // material.VertexColorUseAsAlbedo = true;

        // Parallel.For(0, planetData.Length, new ParallelOptions {MaxDegreeOfParallelism = System.Environment.ProcessorCount}, i => {
        //   surfaceTool[i] = new SurfaceTool();
        //   this.planetMeshes[i] = new ArrayMesh();

        //   surfaceTool[i].SetMaterial(material);
        //   surfaceTool[i].Begin(PrimitiveType.Triangles);

        //   for (int j = 0; j < planetData[i].faces.Count; j++) {
        //     planetData[i].faces[j].AddToSurfaceTool(surfaceTool[i]);
        //   }

        //   surfaceTool[i].GenerateNormals();
        //   surfaceTool[i].Commit(this.planetMeshes[i]);
        // });

        int faceCount;

        for (int i = 0; i < planetData.Length; i++)
        {
            faceCount = planetData[i].faces.Count;
            for (int j = 0; j < faceCount; j++)
            {
                planetData[i].faces[j].AddToSurfaceTool(surfaceTool);
            }
        }

        // foreach (PlanetTopologyData data in planetData) {
        //   foreach (PlanetCell c in data.faces) {
        //     c.AddToSurfaceTool(surfaceTool);
        //   }
        // }

        surfaceTool.GenerateNormals();
        surfaceTool.Commit(this.planetMesh);

        surfaceToolTimer.Stop();
        ts = surfaceToolTimer.Elapsed;
        System.Diagnostics.Debug.WriteLine("SurfaceTool generated our mesh. Time needed: " + ts.Minutes + "m " + ts.Seconds + "." + ts.Milliseconds);
        totalTimer.Stop();
        ts = totalTimer.Elapsed;
        System.Diagnostics.Debug.WriteLine("————————————————— total time needed: " + ts.Minutes + "m " + ts.Seconds + "." + ts.Milliseconds + "—————————————————");

        this.Mesh          = planetMesh;
        this.currentStatus = "";
    }
Exemplo n.º 14
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());
    }
Exemplo 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);
    }
Exemplo 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;
        }