예제 #1
0
    public static Material FromColor(Color color)
    {
        var key = color.ToHtml();

        if (Materials.ContainsKey(key))
        {
            return(Materials[key]);
        }
        else
        {
            var mat = new SpatialMaterial();
            mat.SetAlbedo(color);
            Materials[key] = mat;
            return(mat);
        }
    }
        //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);
        }
    // Creates a wire mesh and places the spheres to make sure the tile is aligned.
    private void CreateMeshForTile(Tile tile, float sphereScale)
    {
        //Get tile center point
        decimal      tileCenterX = 0;
        decimal      tileCenterY = 0;
        decimal      tileCenterZ = 0;
        List <Point> points      = tile.boundary;

        //Calculate the average center point and polygon side length.
        foreach (Point point in points)
        {
            tileCenterX += point.x / points.Count;
            tileCenterY += point.y / points.Count;
            tileCenterZ += point.z / points.Count;
        }
        //Create the center point
        var tileCenterPoint = new Vector3((float)tileCenterX, (float)tileCenterY, (float)tileCenterZ);

        if (points.Count == 5)
        {
            tileCenterPoint = tileCenterPoint + (tileCenterPoint.Normalized() * 6);
        }
        GD.Print(tileCenterPoint);

        //Create surface tool
        var material = new SpatialMaterial();

        //material.SetEmission(new Color(1.0f, 0.0f, 0.0f));
        //material.SetEmissionEnergy(0.5f);
        material.SetAlbedo(new Color(0.0f, 0.0f, 0.0f));
        //material.SetMetallic(0.5f);
        material.SetCullMode(SpatialMaterial.CullMode.Back);
        var surfaceTool = MeshCreation.CreateSurfaceTool(material);

        //Make triangles for polygon
        for (var index = 0; index < points.Count; index++)
        {
            var     point = new Vector3((float)points[index].x, (float)points[index].y, (float)points[index].z);
            Vector3 nextPoint;
            if (index < points.Count - 1)
            {
                nextPoint = new Vector3((float)points[index + 1].x, (float)points[index + 1].y, (float)points[index + 1].z);
            }
            else
            {
                nextPoint = new Vector3((float)points[0].x, (float)points[0].y, (float)points[0].z);
            }
            if (points.Count == 5)
            {
                point     = point + (point.Normalized() * 6);
                nextPoint = nextPoint + (nextPoint.Normalized() * 6);
            }
            MeshCreation.AddTriangle(surfaceTool, point, nextPoint, tileCenterPoint, true);
        }
        //Add to scene
        var meshInstance = MeshCreation.CreateMeshInstanceFromMesh(MeshCreation.CreateMeshFromSurfaceTool(surfaceTool));

        this.AddChild(meshInstance);


        /*
         * 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.Begin(Mesh.PrimitiveType.TriangleStrip);
         * //LineLoop - Nothing
         * //Lines - Nothing
         * //LineStrip - Nothing
         * //Points - Nothing
         * //TriangleFan - Nothing
         * //Triangles - Nothing
         * //TriangleStrip - Nothing
         * surfTool.SetMaterial(material);
         * 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);
         *
         * MeshInstance sphere = (MeshInstance)blueSphereMeshScene.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);
         * meshInstance.CreateTrimeshCollision();
         * this.AddChild(meshInstance);
         */
    }
예제 #4
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);
    }