Пример #1
0
        private static List <Polygon> CreateFromObjects(List <GameObject> objects, TransformVolume volume, float surfaceMaxAngle)
        {
            var pols = new List <Polygon>();

            for (int i = 0, count = objects.Count; i < count; i++)
            {
                Mesh       mesh       = null;
                MeshFilter meshFilter = objects[i].GetComponent <MeshFilter>();
                if (meshFilter == null)
                {
                    MeshCollider meshCollider = objects[i].GetComponent <MeshCollider>();
                    mesh = meshCollider.sharedMesh;
                }
                else
                {
                    mesh = meshFilter.sharedMesh;
                }
                var verts = mesh.vertices;
                for (int v = 0, count2 = verts.Length; v < count2; v++)
                {
                    verts[v] = objects[i].transform.TransformPoint(verts[v]);
                }

                pols.AddRange(CreatePolygons(verts, mesh.triangles));
            }

            return(CutPolygons(pols, volume, surfaceMaxAngle));
        }
Пример #2
0
        public static Volume EditorVolumeControl(TransformVolume transformVolume, float handleSize, Color color)
        {
            Vector3 origin, size;

            Vector3[] controlHandles = new Vector3[6];
            var       transform      = transformVolume.transform;

            Handles.color = color;

            for (int i = 0; i < controlHandles.Length; i++)
            {
                controlHandles[i] = transformVolume.GetSidePosition(i);
            }

            controlHandles[0] = Handles.Slider(controlHandles[0], transform.right, handleSize, Handles.DotCap, 1);
            controlHandles[1] = Handles.Slider(controlHandles[1], transform.right, handleSize, Handles.DotCap, 1);
            controlHandles[2] = Handles.Slider(controlHandles[2], transform.up, handleSize, Handles.DotCap, 1);
            controlHandles[3] = Handles.Slider(controlHandles[3], transform.up, handleSize, Handles.DotCap, 1);
            controlHandles[4] = Handles.Slider(controlHandles[4], transform.forward, handleSize, Handles.DotCap, 1);
            controlHandles[5] = Handles.Slider(controlHandles[5], transform.forward, handleSize, Handles.DotCap, 1);

            origin.x = transform.InverseTransformPoint((controlHandles[0] + controlHandles[1]) * 0.5f).x;
            origin.y = transform.InverseTransformPoint((controlHandles[2] + controlHandles[3]) * 0.5f).y;
            origin.z = transform.InverseTransformPoint((controlHandles[4] + controlHandles[5]) * 0.5f).z;

            size.x = transform.InverseTransformPoint(controlHandles[0]).x - transform.InverseTransformPoint(controlHandles[1]).x;
            size.y = transform.InverseTransformPoint(controlHandles[2]).y - transform.InverseTransformPoint(controlHandles[3]).y;
            size.z = transform.InverseTransformPoint(controlHandles[4]).z - transform.InverseTransformPoint(controlHandles[5]).z;

            return(new Volume(origin, size));
        }
Пример #3
0
        private static List <Polygon> CutPolygons(List <Polygon> polygons, TransformVolume volume, float surfaceMaxAngle)
        {
            Vector3 up   = volume.transform.up;
            var     pols = new List <Polygon>();

            for (int i = 0, count = polygons.Count; i < count; i++)
            {
                var polygon = polygons[i];
                if (!volume.IsInBounds(polygon.Vertices) || !IsAngle(surfaceMaxAngle, up, polygon.Direction))
                {
                    continue;
                }
                if (!volume.IsOnBorder(polygon.Vertices))
                {
                    pols.Add(polygon);
                    continue;
                }

                var tocut = new List <Polygon> {
                    polygon
                };
                for (int s = 0; s < 6; s++)
                {
                    var plane  = new Plane(volume.GetSideDirection(s), volume.GetSidePosition(s));
                    var cutted = new List <Polygon>();
                    for (int c = 0, cc = tocut.Count; c < cc; c++)
                    {
                        cutted.AddRange(tocut[c].Cut(plane));
                    }

                    tocut = cutted;
                }

                pols.AddRange(tocut);
            }

            return(pols);
        }
Пример #4
0
        private static List <Polygon> CreateFromObjects(List <GameObject> objects, TransformVolume volume, float surfaceMaxAngle)
        {
            objectsPols.Clear();
            for (int i = 0, count = objects.Count; i < count; i++)
            {
                bool      isOfflineMeshData = false;
                Vector3[] verts             = null;
                int[]     triangles         = null;
                //OfflineMeshData offlineMeshData = objects[i].GetComponent<OfflineMeshData>();
                //if (null != offlineMeshData) {
                //    verts = offlineMeshData.vertices;
                //    triangles = offlineMeshData.triangles;
                //    isOfflineMeshData = true;
                //} else
                {
                    MeshFilter meshFilter = objects[i].GetComponent <MeshFilter>();
                    if (meshFilter == null || meshFilter.sharedMesh == null)
                    {
                        continue;
                    }
                    Mesh mesh = meshFilter.sharedMesh;
                    verts     = mesh.vertices;
                    triangles = mesh.triangles;
                }
                if (!isOfflineMeshData)
                {
                    for (int v = 0, count2 = verts.Length; v < count2; v++)
                    {
                        verts[v] = objects[i].transform.TransformPoint(verts[v]);
                    }
                }
                objectsPols.AddRange(CreatePolygons(verts, triangles));
            }

            return(CutPolygons(objectsPols, volume, surfaceMaxAngle));
        }
Пример #5
0
        private static List <Polygon> CreateFromTerrain(Terrain terrain, Vector3 terrainPos, Vector3 terrainSize, Bounds bounds, TransformVolume volume, float surfaceMaxAngle)
        {
            Vector3 start = bounds.center - bounds.size * 0.5f;
            Vector3 end   = bounds.center + bounds.size * 0.5f;
            float   size  = terrainSize.x / (terrain.terrainData.heightmapResolution - 1);
            var     pols  = TerrainPlane(terrainPos, size, start, end, terrainSize);

            for (int p = 0, count = pols.Count; p < count; p++)
            {
                for (int v = 0; v < 3; v++)
                {
                    Vector3 vert = pols[p].Vertices[v];
                    vert.y  = terrainPos.y;
                    vert.y += terrain.SampleHeight(pols[p].Vertices[v]);
                    pols[p].Vertices[v] = vert;
                }
            }

            return(CutPolygons(pols, volume, surfaceMaxAngle));
        }