コード例 #1
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);
        }
コード例 #2
0
    private void CreatePerceptionPyramidMesh()
    {
        var material = new SpatialMaterial();

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

        // Pyramid
        var surfaceTool = new SurfaceTool();

        surfaceTool.Begin(Mesh.PrimitiveType.Triangles);

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

        surfaceTool.Index();

        var mesh = surfaceTool.Commit();

        mesh.SurfaceSetMaterial(0, material);

        var meshInstance = new MeshInstance();

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

        this.AddChild(meshInstance);
    }
コード例 #3
0
    // 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);
         */
    }