Inheritance: Shape3D
示例#1
0
    public override bool Calculate()
    {
        if (!allInputsReady ()) {
            return false;
        }

        if (Inputs [0].connection != null) {
            Input1Val = Inputs [0].connection.GetValue<GameObject> ();

            CSGObject csg = Input1Val.GetComponent<CSGObject>();
            if(csg == null)
                csg = Input1Val.AddComponent<CSGObject>();
            csg.GenerateSolid();
        }
        if (Inputs [1].connection != null) {

            Input2Val = Inputs [1].connection.GetValue<GameObject> ();
            CSGObject csg = Input2Val.GetComponent<CSGObject> ();
            if (csg == null)
                csg = Input2Val.AddComponent<CSGObject> ();
            csg.GenerateSolid ();
        }

        if ((Input1Val != null) && (Input2Val != null)) {

            solid1 = Input1Val.GetComponent<CSGObject> ().GetSolid ();
            solid2 = Input2Val.GetComponent<CSGObject> ().GetSolid ();

            modeller = new BooleanModeller (solid1, solid2);
            output = null;

            switch (type) {
            case CalcType.Union:
                output = modeller.getUnion ();
                break;
            case CalcType.Intersection:
                output = modeller.getIntersection ();
                break;
            case CalcType.Difference:
                output = modeller.getDifference ();
                break;
            }

            if (output != null) {
                string goname = string.Format ("CSGOut_{0}", (int)this.GetHashCode ());
                GameObject gout = GameObject.Find (goname);
                if (gout == null)
                    gout = new GameObject (goname);
                CSGObject csg = gout.GetComponent<CSGObject>();
                if(csg == null) csg = gout.AddComponent<CSGObject>();
                csg.AssignSolid(output);
                CSGGameObject.GenerateMesh (gout, objectMaterial, output);

                Outputs [0].SetValue<GameObject> (gout);
            }
        } else
            return false;

        return true;
    }
示例#2
0
    public void ApplyCSG()
    {
        switch (operation) {
        case OperatorType.Union:
            temp = modeller.getUnion ();
            break;
        case OperatorType.Intersection:
            temp = modeller.getIntersection ();
            break;
        case OperatorType.Difference:
            temp = modeller.getDifference ();
            break;
        }

        // Apply to output game object
        CSGGameObject.GenerateMesh(output, ObjMaterial, temp);
        // Make sure the output object has its 'solid' updated
        output.GetComponent<CSGObject> ().GenerateSolid ();

        // Hide the original objects
        objectA.SetActive (false);
        objectB.SetActive (false);

        lasten = enableOperator;
        lastop = operation;
    }
示例#3
0
    // Update is called once per frame
    public static void GenerateMesh(GameObject go, Material ObjMaterial, Solid mesh)
    {
        MeshFilter mf = go.GetComponent<MeshFilter> ();
        if(mf == null)
            mf = go.AddComponent<MeshFilter> ();

        Mesh tmesh = new Mesh();
        int mlen = mesh.getVertices().Length;
        Vector3 [] vertices = new Vector3[mlen];
        for(int i=0; i<mlen; i++)
        {
            Net3dBool.Point3d p = mesh.getVertices()[i];
            vertices[i] = new Vector3((float)p.x, (float)p.y, (float)p.z);
        }
        tmesh.vertices = vertices;

        tmesh.triangles = mesh.getIndices ();
        int clen = mesh.getColors ().Length;
        Color [] clrs = new Color[clen];
        for (int j=0; j<clen; j++) {
            Net3dBool.Color3f c = mesh.getColors()[j];
            clrs[j] = new Color((float)c.r, (float)c.g, (float)c.b);
        }
        tmesh.colors = clrs;
        tmesh.RecalculateNormals();
        mf.mesh = tmesh;

        MeshRenderer mr = go.GetComponent<MeshRenderer> ();
        if(mr == null) mr = go.AddComponent<MeshRenderer> ();
        mr.sharedMaterials = new Material[1];
        mr.sharedMaterials[0] = ObjMaterial;
        mr.sharedMaterial = ObjMaterial;
    }
示例#4
0
    Tuple <GameObject, Solid> DrawHole3D(Vector3 pos, Quaternion rotation)
    {
        GameObject gameObject = new GameObject();

        gameObject.name = "Hole";

        float w = 1;
        float h = 1;
        float g = 1;

        Vector3[] vertices =
        {
            new Vector3(pos.x - w / 2, pos.y - h / 2, pos.z + g / 2),
            new Vector3(pos.x - w / 2, pos.y - h / 2, pos.z - g / 2),
            new Vector3(pos.x - w / 2, pos.y + h / 2, pos.z - g / 2),
            new Vector3(pos.x - w / 2, pos.y + h / 2, pos.z + g / 2),
            new Vector3(pos.x + w / 2, pos.y + h / 2, pos.z + g / 2),
            new Vector3(pos.x + w / 2, pos.y + h / 2, pos.z - g / 2),
            new Vector3(pos.x + w / 2, pos.y - h / 2, pos.z - g / 2),
            new Vector3(pos.x + w / 2, pos.y - h / 2, pos.z + g / 2),
        };

        Point3d[] points3d = vertices.Select(v => new Point3d(v.x, v.y, v.z)).ToArray();

        int[] triangles =
        {
            0, 2, 1, //face front
            0, 3, 2,
            2, 3, 4, //face top
            2, 4, 5,
            1, 2, 5, //face right
            1, 5, 6,
            0, 7, 4, //face left
            0, 4, 3,
            5, 4, 7, //face back
            5, 7, 6,
            0, 6, 7, //face bottom
            0, 1, 6
        };

        var obj = new Net3dBool.Solid(points3d, triangles, getColorArray(8, Color.black));

        MeshFilter mf    = gameObject.AddComponent <MeshFilter>();
        Mesh       tmesh = new Mesh();

        tmesh.vertices  = GetVertices(obj);
        tmesh.triangles = obj.getIndices();
        tmesh.colors    = GetColorsMesh(obj);
        tmesh.RecalculateNormals();
        mf.mesh = tmesh;
        MeshRenderer mr = gameObject.AddComponent <MeshRenderer>();

        //gameObject.transform.Translate(new Vector3(2000, 0, 0), Space.World);

        gameObject.transform.parent = area3D.transform;


        return(new Tuple <GameObject, Solid>(gameObject, obj));
    }
示例#5
0
    public void GenerateSolid()
    {
        // Try to grab any meshes this is attached to - if not, allow setting!
        MeshFilter mf = gameObject.GetComponent<MeshFilter> ();
        if (mf == null) {
            Debug.LogError ("No mesh filter to get vertices from.");
            return;
        }

        solidObject = new Solid (mf.sharedMesh.vertices, mf.sharedMesh.triangles, mf.sharedMesh.colors);
        // Make sure the transform has been pushed into the solid.
        solidObject.ApplyMatrix (gameObject.transform.localToWorldMatrix);
    }
示例#6
0
        //--------------------------------CONSTRUCTORS----------------------------------//
        /**
         * Constructs a BooleanModeller object to apply boolean operation in two solids.
         * Makes preliminary calculations
         *
         * @param solid1 first solid where boolean operations will be applied
         * @param solid2 second solid where boolean operations will be applied
         */
        public BooleanModeller(Solid solid1, Solid solid2)
        {
            //representation to apply boolean operations
            object1 = new Object3D(solid1);
            object2 = new Object3D(solid2);

            //split the faces so that none of them intercepts each other
            object1.splitFaces(object2);
            object2.splitFaces(object1);

            //classify faces as being inside or outside the other solid
            object1.classifyFaces(object2);
            object2.classifyFaces(object1);
        }
示例#7
0
    void Start()
    {
        var one0 = new Net3dBool.Solid(
            VecToNet3dBool(one.vertices),
            one.triangles);
        var another0 = new Net3dBool.Solid(
            VecToNet3dBool(another.vertices),
            another.triangles);

        var modeller = new Net3dBool.BooleanModeller(one0, another0);
        var tmp      = modeller.GetIntersection();

        Mesh mesh = GetComponent <MeshFilter>().mesh;

        mesh.vertices  = VecToUnity(tmp.getVertices());
        mesh.triangles = tmp.getIndices();
    }
示例#8
0
    // Use this for initialization
    void Start()
    {
        // Prepare for an operation on solids - grab them.
        solidA = objectA.GetComponent<CSGObject> ().GetSolid ();
        if (solidA == null) {
            Debug.LogError("ObjectA is not a CSGObject.");
            return;
        }
        solidB = objectB.GetComponent<CSGObject> ().GetSolid ();
        if (solidB == null) {
            Debug.LogError("ObjectB is not a CSGObject.");
            return;
        }

        modeller = new Net3dBool.BooleanModeller(solidA, solidB);
        // Do check...

        lastop = (OperatorType)(1 - (int)operation);
        lasten = !enableOperator;
    }
示例#9
0
        //----------------------------------CONSTRUCTOR---------------------------------//
        /**
         * Constructs a Object3d object based on a solid file.
         *
         * @param solid solid used to construct the Object3d object
         */
        public Object3D(Solid solid)
        {
            Vertex v1, v2, v3, vertex;
            Point3d[] verticesPoints = solid.getVertices();
            int[] indices = solid.getIndices();
            Color3f[] colors = solid.getColors();
            var verticesTemp = new List<Vertex>();

            Dictionary<int,int> revlookup = new Dictionary<int, int> ();
            for (int d=0; d<indices.Length; d++)
                revlookup [indices [d]] = d;

            //create vertices
            vertices = new List<Vertex>();
            for (int i = 0; i < verticesPoints.Length; i++)
            {
                Color3f col = new Color3f(1, 1, 1);
                if(colors.Length > 0)
                    col = colors[i];

                vertex = addVertex(verticesPoints[i], col, Vertex.UNKNOWN);
                verticesTemp.Add(vertex);
            }

            //create faces
            faces = new List<Face>();
            for (int i = 0; i < indices.Length; i = i + 3)
            {
                v1 = verticesTemp[indices[i]];
                v2 = verticesTemp[indices[i + 1]];
                v3 = verticesTemp[indices[i + 2]];
                addFace(v1, v2, v3);
            }

            //create bound
            bound = new Bound(verticesPoints);
        }
示例#10
0
 public void AssignSolid(Solid _solid)
 {
     solidObject = _solid;
 }
示例#11
0
 /// <summary>
 /// Constructs a BooleanModeller object to apply boolean operation in two solids. Makes preliminary calculations
 /// </summary>
 /// <param name="solid1"></param>
 /// <param name="solid2"></param>
 public BooleanModeller(Solid solid1, Solid solid2)
     : this(solid1, solid2, null, CancellationToken.None)
 {
 }
示例#12
0
    public Tuple <GameObject, Solid> DrawMesh3D(Vector3 startPoint, Vector3 endPoint, float width, float height, string name)
    {
        activeFloor = currentStatusDoma.activeFloor;
        startPoint  = new Vector3(startPoint.x, activeFloor.LevelBottom, startPoint.z);
        endPoint    = new Vector3(endPoint.x, activeFloor.LevelBottom, endPoint.z);
        float h = height;

        GameObject gameObject = new GameObject();

        gameObject.name = name;

        var between  = endPoint - startPoint;
        var distance = Vector3.Distance(startPoint, endPoint);

        Vector3 vNormalized    = endPoint - startPoint;
        Vector3 vPerpendicular = Vector3.Normalize(Vector3.Cross(vNormalized, Vector3.up));

        var P1 = startPoint + vPerpendicular * width / 2;
        var P2 = startPoint - vPerpendicular * width / 2;
        var P3 = endPoint - vPerpendicular * width / 2;
        var P4 = endPoint + vPerpendicular * width / 2;

        Vector3[] vertices =
        {
            P1,
            P2,
            new Vector3(P2.x,P2.y + h,  P2.z),
            new Vector3(P1.x,P1.y + h,  P1.z),
            new Vector3(P4.x,P4.y + h,  P4.z),
            new Vector3(P3.x,P3.y + h,  P3.z),
            P3,
            P4,
        };

        Point3d[] points3d = vertices.Select(v => new Point3d(v.x, v.y, v.z)).ToArray();

        int[] triangles =
        {
            0, 2, 1, //face front
            0, 3, 2,
            2, 3, 4, //face top
            2, 4, 5,
            1, 2, 5, //face right
            1, 5, 6,
            0, 7, 4, //face left
            0, 4, 3,
            5, 4, 7, //face back
            5, 7, 6,
            0, 6, 7, //face bottom
            0, 1, 6
        };

        // UVs dla tekstury
        Vector2[] uvs = new Vector2[vertices.Length];
        for (int i = 0; i < uvs.Length; i++)
        {
            uvs[i] = new Vector2(vertices[i].x, vertices[i].y);
        }


        var obj = new Net3dBool.Solid(points3d, triangles, getColorArray(8, Color.black));

        MeshFilter mf    = gameObject.AddComponent <MeshFilter>();
        Mesh       tmesh = new Mesh();

        tmesh.vertices  = GetVertices(obj);
        tmesh.triangles = obj.getIndices();
        tmesh.uv        = uvs;
        //tmesh.colors = GetColorsMesh(obj);
        tmesh.RecalculateNormals();
        mf.mesh = tmesh;
        MeshRenderer mr = gameObject.AddComponent <MeshRenderer>();

        gameObject.transform.Translate(new Vector3(2000, 0, 0), Space.World);

        gameObject.GetComponent <MeshRenderer>().material = material;

        gameObject.transform.parent = area3D.transform;

        // Doda Collider gdy ściana ma długość min. 20 cm
        if (Vector3.Distance(startPoint, endPoint) > 0.2f)
        {
            gameObject.AddComponent <MeshCollider>();
        }

        gameObject.tag   = "Wall";
        gameObject.layer = LayerMask.NameToLayer("3DArea");

        return(new Tuple <GameObject, Solid>(gameObject, obj));
    }