Ejemplo n.º 1
0
        static void FacesToMesh()
        {
            for (int i = 0; i < Selection.gameObjects.Length; ++i)
            {
                GameObject go  = Selection.gameObjects[i];
                CSGObject  obj = go.GetComponent <CSGObject>();

                if (obj)
                {
                    obj.TransferFacesToMesh();
                }
            }
        }
Ejemplo n.º 2
0
        static void MergeBspFaces()
        {
            for (int i = 0; i < Selection.gameObjects.Length; ++i)
            {
                GameObject go  = Selection.gameObjects[i];
                CSGObject  obj = go.GetComponent <CSGObject>();

                if (obj)
                {
                    obj.MergeFaces();
                }
            }
        }
Ejemplo n.º 3
0
        static void DrawDebug()
        {
            for (int i = 0; i < Selection.gameObjects.Length; ++i)
            {
                GameObject go  = Selection.gameObjects[i];
                CSGObject  obj = go.GetComponent <CSGObject>();

                if (obj)
                {
                    obj.DrawDebug();
                }
            }
        }
Ejemplo n.º 4
0
        static void MeshOptimize()
        {
            for (int i = 0; i < Selection.gameObjects.Length; ++i)
            {
                GameObject go  = Selection.gameObjects[i];
                CSGObject  obj = go.GetComponent <CSGObject>();

                if (obj)
                {
                    obj.Optimize();
                }
            }
        }
Ejemplo n.º 5
0
        static void BuildTree()
        {
            for (int i = 0; i < Selection.gameObjects.Length; ++i)
            {
                GameObject go  = Selection.gameObjects[i];
                CSGObject  obj = go.GetComponent <CSGObject>();

                if (obj)
                {
                    obj.CreateFromMesh();

                    BspGen gen = new BspGen(BooleanSettings.BspOptimization);

                    obj.rootNode = gen.GenerateBspTree(obj.faces);
                }
            }
        }
Ejemplo n.º 6
0
        //
        public void Perform(ECsgOperation inOper, CSGObject inMaster, CSGObject inSlave)
        {
            // we are processing our slave faces
            processState = EProcessState.Process_Slave;

            // process faces against master tree
            PerformFaces(inMaster.rootNode, inSlave.faces);

            // process face from master tree
            processState = EProcessState.Process_Master;

            // perform master faces on slave bsp tree
            PerformTree(inMaster.rootNode, inSlave.rootNode);

            // check if how do we need to process generated faces
            if (inOper == ECsgOperation.CsgOper_Additive || inOper == ECsgOperation.CsgOper_Subtractive)
            {
                // add deferred faces to master tree...
                for (int i = 0; i < deferredFaces.Count; i++)
                {
                    Face    defFace   = ((DeferredFace)deferredFaces[i]).face;
                    BspNode startNode = ((DeferredFace)deferredFaces[i]).node;

                    // testing
                    startNode = inMaster.rootNode;
                    // add node to master tree
                    BspGen.AddNodeRecursive(startNode, defFace, BspNode.BspFlags_IsNew);
                }
            }
            else
            {
                // clear old faces list
                inMaster.faces.Clear();

                // copy created faces
                for (int i = 0; i < deferredFaces.Count; i++)
                {
                    inMaster.faces.Add(deferredFaces[i].face);
                }
            }

            // clear deferred faces
            deferredFaces.Clear();
        }
Ejemplo n.º 7
0
        private bool intersect(CSGObject inOther)
        {
            MeshFilter thisMeshFilter  = GetComponent <MeshFilter>();
            MeshFilter otherMeshFilter = inOther.GetComponent <MeshFilter>();

            if (thisMeshFilter == null || otherMeshFilter == null)
            {
                return(false);
            }

            Mesh thisMesh  = thisMeshFilter.sharedMesh;
            Mesh otherMesh = otherMeshFilter.sharedMesh;

            if (thisMesh == null || otherMesh == null)
            {
                return(false);
            }

            return(thisMesh.bounds.Intersects(otherMesh.bounds));
        }
Ejemplo n.º 8
0
        public static void FromGameObjectToCSGObject(GameObject from, GameObject[] to, out CSGObject csgForm,
                                                     out CSGObject[] csgTo)
        {
            csgForm = from.GetComponent <CSGObject>();
            csgTo   = new CSGObject[to.Length];

            if (!csgForm)
            {
                csgForm = AddComponent(from);
            }

            for (var i = 0; i < to.Length; i++)
            {
                var temp = to[i].GetComponent <CSGObject>();

                if (!temp)
                {
                    csgTo[i] = AddComponent(to[i]);
                }
            }
        }
Ejemplo n.º 9
0
        public static CSGObject AddComponent(GameObject obj)
        {
            Mesh mesh = obj.GetComponent <MeshFilter>() != null?obj.GetComponent <MeshFilter>().sharedMesh : null;

            if (mesh)
            {
                // TODO: add bsp tree generation on add...
                CSGObject  csg    = obj.AddComponent <CSGObject>();
                MeshFilter filter = obj.GetComponent <MeshFilter>();

                // clone mesh as every csg object needs his own mesh
                filter.sharedMesh = ObjectCloner.CloneMesh(filter.sharedMesh);
                //
                csg.CreateFromMesh();

                return(csg);
            }
            else
            {
                Debug.Log("No Mesh on GameObject");
                return(null);
            }
        }
Ejemplo n.º 10
0
        public static void Subtract(CSGObject from, params CSGObject[] to)
        {
            from.PerformCSG(CsgOperation.ECsgOperation.CsgOper_Subtractive, to);

            BooleanSettings.DestroySlaves(to);
        }
Ejemplo n.º 11
0
        // Use this for initialization
        public override void Build()
        {
            min = new Vector3(-32.0f, -32.0f, -32.0f);
            max = new Vector3(32.0f, 32.0f, 32.0f);

            //
            GameObject go = new GameObject();

            go.name = "CubeGeo";
            MeshFilter   mf  = go.AddComponent <MeshFilter>();
            MeshRenderer mr  = go.AddComponent <MeshRenderer>();
            CSGObject    csg = go.AddComponent <CSGObject>();

            if (mf.sharedMesh == null)
            {
                mf.sharedMesh = new Mesh();
            }

            Mesh mesh = mf.sharedMesh;

            Vector3 p0 = new Vector3(min.x, min.y, min.z);
            Vector3 p1 = new Vector3(min.x, min.y, max.z);
            Vector3 p2 = new Vector3(min.x, max.y, min.z);
            Vector3 p3 = new Vector3(min.x, max.y, max.z);
            Vector3 p4 = new Vector3(max.x, min.y, min.z);
            Vector3 p5 = new Vector3(max.x, min.y, max.z);
            Vector3 p6 = new Vector3(max.x, max.y, min.z);
            Vector3 p7 = new Vector3(max.x, max.y, max.z);

            mesh.Clear();

            mesh.vertices = new[]
            {
                // left
                p0, p1, p2, p3,
                // right
                p4, p5, p6, p7,
                // front
                p1, p3, p5, p7,
                // back
                p0, p2, p4, p6,
                // top
                p2, p3, p6, p7,
                // bottom
                p0, p1, p4, p5
            };

            mesh.uv = new[]
            {
                // left
                Vector2.zero, Vector2.right, Vector2.up, Vector2.one,
                // right
                Vector2.zero, Vector2.right, Vector2.up, Vector2.one,
                // front
                Vector2.zero, Vector2.right, Vector2.up, Vector2.one,
                // back
                Vector2.zero, Vector2.right, Vector2.up, Vector2.one,
                // top
                Vector2.zero, Vector2.right, Vector2.up, Vector2.one,
                // bottom
                Vector2.zero, Vector2.right, Vector2.up, Vector2.one
            };

            mesh.triangles = new[]
            {
                // left
                0, 1, 2, 2, 1, 3,
                // right
                4, 6, 5, 5, 6, 7,
                // front
                8, 10, 9, 9, 10, 11,
                // back
                12, 13, 14, 14, 13, 15,
                // top
                16, 17, 18, 18, 17, 19,
                // bottom
                20, 22, 21, 21, 22, 23
            };

            mesh.RecalculateNormals();
            mesh.RecalculateBounds();

            if (sharedMaterial == null)
            {
                sharedMaterial = new Material(Shader.Find("Unlit/Texture"));
            }

            mr.sharedMaterial = sharedMaterial;
            //	mr.sharedMaterial.shader = Shader.Find("Unlit/Texture");

            Debug.Log(csg);
            Debug.Log(mesh);
            Debug.Log(sharedMaterial);

            mesh.Optimize();
        }
Ejemplo n.º 12
0
        // Use this for initialization
        public override void OnInspectorGUI()
        {
            CSGObject obj = (CSGObject)target;

            GUILayout.BeginHorizontal();
            if (GUILayout.Button("Intersect"))
            {
                // find game objects (TODO: check if they are touching us)
                Object[]     others = FindObjectsOfType(typeof(GameObject));
                GameObject[] gos    = new GameObject[others.Length];
                int          i      = 0;
                foreach (GameObject gameObj in others)
                {
                    gos[i] = gameObj;
                    ++i;
                }

                var gosCSG = gos.Select(Boolean.FromGameObjectToCSGObject).ToArray();

                obj.PerformCSG(CsgOperation.ECsgOperation.CsgOper_Intersect, gosCSG);
            }

            if (GUILayout.Button("DeIntersect"))
            {
                // find game objects (TODO: check if they are touching us)
                Object[]     others = FindObjectsOfType(typeof(GameObject));
                GameObject[] gos    = new GameObject[others.Length];
                int          i      = 0;
                foreach (GameObject gameObj in others)
                {
                    gos[i] = gameObj;
                    ++i;
                }

                var gosCSG = gos.Select(Boolean.FromGameObjectToCSGObject).ToArray();

                obj.PerformCSG(CsgOperation.ECsgOperation.CsgOper_DeIntersect, gosCSG);
            }

            GUILayout.Button("...");
            GUILayout.EndHorizontal();

            EditorGUILayout.BeginVertical();

            EditorGUILayout.BeginHorizontal();

            EditorGUILayout.LabelField("Texturing");
            obj.texMode = EditorGUILayout.Popup(obj.texMode, new string[] { "Original", "Planar Mapping" });

            EditorGUILayout.EndHorizontal();

            // add
            if (obj.texMode == CSGObject.TexMode_Planar)
            {
                // offset u
                EditorGUILayout.BeginHorizontal();
                float newTexOffsetU = EditorGUILayout.FloatField("OffsetU", obj.globalTexOffsetU);

                if (obj.globalTexOffsetU != newTexOffsetU)
                {
                    obj.globalTexOffsetU = newTexOffsetU;
                    obj.TransferFacesToMesh();
                }

                EditorGUILayout.EndHorizontal();

                // offset v
                EditorGUILayout.BeginHorizontal();

                float newTexOffsetV = EditorGUILayout.FloatField("OffsetV", obj.globalTexOffsetV);

                if (obj.globalTexOffsetV != newTexOffsetV)
                {
                    obj.globalTexOffsetV = newTexOffsetV;
                    obj.TransferFacesToMesh();
                }

                EditorGUILayout.EndHorizontal();


                // scale u
                EditorGUILayout.BeginHorizontal();
                float newTexScaleU = EditorGUILayout.FloatField("ScaleU", obj.globalTexScaleU);
                newTexScaleU = Mathf.Clamp(newTexScaleU, 0.001f, 16.0f);

                if (obj.globalTexScaleU != newTexScaleU)
                {
                    obj.globalTexScaleU = newTexScaleU;
                    obj.TransferFacesToMesh();
                }

                EditorGUILayout.EndHorizontal();

                // scale v
                EditorGUILayout.BeginHorizontal();
                float newTexScaleV = EditorGUILayout.FloatField("ScaleV", obj.globalTexScaleV);
                newTexScaleV = Mathf.Clamp(newTexScaleV, 0.001f, 16.0f);

                if (obj.globalTexScaleV != newTexScaleV)
                {
                    obj.globalTexScaleV = newTexScaleV;
                    obj.TransferFacesToMesh();
                }

                EditorGUILayout.EndHorizontal();
            }

            EditorGUILayout.EndVertical();
        }