Exemple #1
0
        public static GameObject[] SliceInstantiate(this GameObject obj, Plane pl, TextureRegion cuttingRegion, Material crossSectionMaterial = null)
        {
            SlicedHull slice = Slicer.Slice(obj, pl, cuttingRegion, crossSectionMaterial);

            if (slice == null)
            {
                return(null);
            }

            GameObject upperHull = slice.CreateUpperHull(obj, crossSectionMaterial);
            GameObject lowerHull = slice.CreateLowerHull(obj, crossSectionMaterial);

            if (upperHull != null && lowerHull != null)
            {
                return(new GameObject[] { upperHull, lowerHull });
            }

            // otherwise return only the upper hull
            if (upperHull != null)
            {
                return(new GameObject[] { upperHull });
            }

            // otherwise return only the lower hull
            if (lowerHull != null)
            {
                return(new GameObject[] { lowerHull });
            }

            // nothing to return, so return nothing!
            return(null);
        }
        /**
         * Helper function which will slice the provided object with the provided plane
         * and instantiate and return the final GameObjects
         *
         * This function will return null if the object failed to slice
         */
        public static GameObject[] SliceInstantiate(GameObject obj, Plane pl, bool genCrossSection = true)
        {
            SlicedHull slice = Slice(obj, pl, genCrossSection);

            if (slice == null)
            {
                return(null);
            }

            GameObject upperHull = slice.CreateUpperHull(obj);
            GameObject lowerHull = slice.CreateLowerHull(obj);

            if (upperHull != null && lowerHull != null)
            {
                return(new GameObject[] { upperHull, lowerHull });
            }

            // otherwise return only the upper hull
            if (upperHull != null)
            {
                return(new GameObject[] { upperHull });
            }

            // otherwise return only the lower hull
            if (lowerHull != null)
            {
                return(new GameObject[] { lowerHull });
            }

            // nothing to return, so return nothing!
            return(null);
        }
Exemple #3
0
        /**
         * Helper function which will slice the provided object with the provided plane
         * and instantiate and return the final GameObjects
         *
         * This function will return null if the object failed to slice
         */
        public static GameObject[] SliceInstantiate(GameObject obj, Plane pl, bool genCrossSection = true)
        {
            SlicedHull slice = Slice(obj, pl, genCrossSection);

            if (slice == null)
            {
                return(null);
            }

            GameObject upperHull = slice.CreateUpperHull();

            if (upperHull != null)
            {
                // set the positional information
                upperHull.transform.position   = obj.transform.position;
                upperHull.transform.rotation   = obj.transform.rotation;
                upperHull.transform.localScale = obj.transform.localScale;

                // the the material information
                upperHull.GetComponent <Renderer>().sharedMaterials = obj.GetComponent <MeshRenderer>().sharedMaterials;
            }

            GameObject lowerHull = slice.CreateLowerHull();

            if (lowerHull != null)
            {
                // set the positional information
                lowerHull.transform.position   = obj.transform.position;
                lowerHull.transform.rotation   = obj.transform.rotation;
                lowerHull.transform.localScale = obj.transform.localScale;

                // the the material information
                lowerHull.GetComponent <Renderer>().sharedMaterials = obj.GetComponent <MeshRenderer>().sharedMaterials;
            }

            // return both if upper and lower hulls were generated
            if (upperHull != null && lowerHull != null)
            {
                return(new GameObject[] { upperHull, lowerHull });
            }

            // otherwise return only the upper hull
            if (upperHull != null)
            {
                return(new GameObject[] { upperHull });
            }

            // otherwise return null
            return(null);
        }
    void BuildFootPath(GameObject footPathPrefab)
    {
        List <Vector2> footPathPoly = CityTest.Shrink(borders, roadSize);
        //GameObject footPathPrefab = Resources.Load<GameObject>("FootPath");
        float footPathSegmentLenght = footPathSize;

        for (int currBorderIndex = 0; currBorderIndex < footPathPoly.Count; currBorderIndex++)
        {
            GameObject footPath = new GameObject("FootPath: " + currBorderIndex.ToString());
            footPath.transform.position = new Vector3(footPathPoly[currBorderIndex].x, 0, footPathPoly[currBorderIndex].y) + thisBlock.transform.position;
            footPath.transform.SetParent(thisBlock.transform);

            // this works the same as the roads for the most part, referance that
            int nextBorderIndex = currBorderIndex + 1;
            if (nextBorderIndex >= footPathPoly.Count)
            {
                nextBorderIndex = 0;
            }
            int prevBorderIndex = currBorderIndex - 1;
            if (prevBorderIndex < 0)
            {
                prevBorderIndex = footPathPoly.Count - 1;
            }
            int nextNextBorderIndex = nextBorderIndex + 1;
            if (nextNextBorderIndex >= footPathPoly.Count)
            {
                nextNextBorderIndex = 0;
            }
            Vector3 FrontWorldPosition  = new Vector3(footPathPoly[currBorderIndex].x, 0, footPathPoly[currBorderIndex].y);
            Vector3 EndWorldPosition    = new Vector3(footPathPoly[nextBorderIndex].x, 0, footPathPoly[nextBorderIndex].y);
            Vector3 nextPosition        = new Vector3(footPathPoly[nextNextBorderIndex].x, 0, footPathPoly[nextNextBorderIndex].y);
            Vector3 prevPosition        = new Vector3(footPathPoly[prevBorderIndex].x, 0, footPathPoly[prevBorderIndex].y);
            Vector3 thisDir             = (EndWorldPosition - FrontWorldPosition).normalized;
            Vector3 nextDir             = (nextPosition - EndWorldPosition).normalized;
            Vector3 prevDir             = (FrontWorldPosition - prevPosition).normalized;
            Vector3 frontSliceDirection = (thisDir + prevDir) * 0.5f;
            Vector3 endSliceDirection   = -(thisDir + nextDir) * 0.5f;
            float   endRoadLenght       = Vector3.Distance(EndWorldPosition, FrontWorldPosition);

            for (float lenght = 0; lenght < endRoadLenght; lenght += footPathSegmentLenght)
            {
                GameObject          footPathObj = GameObject.Instantiate(footPathPrefab, FrontWorldPosition + thisDir * lenght + Vector3.up * 0.001f, Quaternion.LookRotation(thisDir, Vector3.up));
                EzySlice.SlicedHull hull        = footPathObj.Slice(FrontWorldPosition, frontSliceDirection);
                if (hull != null && hull.upperHull)
                {
                    MonoBehaviour.Destroy(footPathObj);
                    footPathObj = hull.CreateUpperHull(footPathObj);
                }

                hull = footPathObj.Slice(EndWorldPosition, endSliceDirection);
                if (hull != null && hull.upperHull)
                {
                    MonoBehaviour.Destroy(footPathObj);
                    footPathObj = hull.CreateUpperHull(footPathObj);
                }



                //THis is required to remove the empty material array elements the slicer leave there for some reason, bug with ezyslice
                Material[] newShared = new Material[1];
                newShared[0] = footPathObj.GetComponent <Renderer>().sharedMaterials[0];
                footPathObj.GetComponent <Renderer>().sharedMaterials = newShared;

                footPathObj.name = "Foot Path";
                footPathObj.transform.SetParent(footPath.transform);
            }
        }
    }
    //build the roads
    void BuildRoads()
    {
        for (int currBorderIndex = 0; currBorderIndex < borders.Count; currBorderIndex++)// for each road
        {
            //create a container object
            GameObject road = new GameObject("Road: " + currBorderIndex.ToString());
            road.transform.position = new Vector3(borders[currBorderIndex].x, 0, borders[currBorderIndex].y) + thisBlock.transform.position;
            road.transform.SetParent(thisBlock.transform);

            //get our prefabs
            GameObject roadEmpty  = Resources.Load <GameObject>("RoadEmpty");
            GameObject roadMiddle = Resources.Load <GameObject>("RoadStraight2");

            // find the index for the next roads in the poly
            int nextBorderIndex = currBorderIndex + 1;
            if (nextBorderIndex >= borders.Count)
            {
                nextBorderIndex = 0;
            }
            int prevBorderIndex = currBorderIndex - 1;
            if (prevBorderIndex < 0)
            {
                prevBorderIndex = borders.Count - 1;
            }
            int nextNextBorderIndex = nextBorderIndex + 1;
            if (nextNextBorderIndex >= borders.Count)
            {
                nextNextBorderIndex = 0;
            }

            // the front and end position of the road we are creating
            Vector3 roadFrontWorldPosition = new Vector3(borders[currBorderIndex].x, 0, borders[currBorderIndex].y);
            Vector3 roadEndWorldPosition   = new Vector3(borders[nextBorderIndex].x, 0, borders[nextBorderIndex].y);

            //the front position of the next road
            Vector3 nextRoadPosition = new Vector3(borders[nextNextBorderIndex].x, 0, borders[nextNextBorderIndex].y);
            //the back position of the previous road
            Vector3 prevRoadPosition = new Vector3(borders[prevBorderIndex].x, 0, borders[prevBorderIndex].y);
            //the direction the roads travel
            Vector3 thisRoadDir = (roadEndWorldPosition - roadFrontWorldPosition).normalized;
            Vector3 nextRoadDir = (nextRoadPosition - roadEndWorldPosition).normalized;
            Vector3 prevRoadDir = (roadFrontWorldPosition - prevRoadPosition).normalized;
            // the slicing directions to slice the end of the roads. this is half the angle between the 2 roads
            Vector3 frontSliceDirection = (thisRoadDir + prevRoadDir) * 0.5f;
            Vector3 endSliceDirection   = -(thisRoadDir + nextRoadDir) * 0.5f;
            float   roadSegmentLenght   = roadSize;

            //roads are split into 3, front, middle end. the front and the end are slices on an angle and dont have lines, the middle are only sliced on the end and perp from the road direction

            //the end point is total distance of the road
            float endRoadLenght = Vector3.Distance(roadEndWorldPosition, roadFrontWorldPosition);
            // the begin point is where the front road ends and the middle begins
            float beginRoadLenght = Mathf.Max(Mathf.Tan((90 - Vector3.Angle(Vector3.Cross(frontSliceDirection, Vector3.up), thisRoadDir)) * Mathf.Deg2Rad) * roadSegmentLenght, Mathf.Tan((Vector3.Angle(Vector3.Cross(frontSliceDirection, Vector3.up), thisRoadDir)) * Mathf.Deg2Rad) * roadSegmentLenght);
            beginRoadLenght = Mathf.Clamp(beginRoadLenght, 0, endRoadLenght / 2);
            // the middle is where the middle road ends the and end road begins
            float middleRoadLenght = Mathf.Max((Mathf.Tan((90 - Vector3.Angle(Vector3.Cross(endSliceDirection, Vector3.up), thisRoadDir)) * Mathf.Deg2Rad) * roadSegmentLenght), (Mathf.Tan((Vector3.Angle(Vector3.Cross(endSliceDirection, Vector3.up), thisRoadDir)) * Mathf.Deg2Rad) * roadSegmentLenght));
            middleRoadLenght = endRoadLenght - Mathf.Clamp(middleRoadLenght, 0, endRoadLenght / 2);

            GameObject roadObj;
            //beginRoad ---- CHANGE TO GO OTHER WAY AND AVOID CUTTING OF THE BACK
            for (float lenght = 0; lenght < beginRoadLenght; lenght += roadSegmentLenght)
            {
                //create a road tile along our road
                roadObj = GameObject.Instantiate(roadEmpty, roadFrontWorldPosition + thisRoadDir * lenght + Vector3.up * 0.001f, Quaternion.LookRotation(thisRoadDir, Vector3.up));

                //slice the front road angle
                EzySlice.SlicedHull hull = roadObj.Slice(roadFrontWorldPosition, frontSliceDirection);
                if (hull != null && hull.upperHull)
                {
                    MonoBehaviour.Destroy(roadObj);
                    roadObj = hull.CreateUpperHull(roadObj);
                }

                hull = roadObj.Slice(roadEndWorldPosition, endSliceDirection);
                if (hull != null && hull.upperHull)
                {
                    MonoBehaviour.Destroy(roadObj);
                    roadObj = hull.CreateUpperHull(roadObj);
                }
                //slice the back off the road
                if (lenght + roadSegmentLenght > beginRoadLenght)
                {
                    hull = roadObj.Slice(roadFrontWorldPosition + thisRoadDir * beginRoadLenght, -thisRoadDir);

                    if (hull != null && hull.upperHull)
                    {
                        MonoBehaviour.Destroy(roadObj);
                        roadObj = hull.CreateUpperHull(roadObj);
                    }
                }
                roadObj.name = "Begin Part";

                roadObj.transform.SetParent(road.transform);
            }


            //middleRoad
            for (float lenght = beginRoadLenght; lenght < middleRoadLenght; lenght += roadSegmentLenght)
            {
                //create a road tile along our road
                roadObj = GameObject.Instantiate(roadMiddle, roadFrontWorldPosition + thisRoadDir * lenght + Vector3.up * 0.001f, Quaternion.LookRotation(thisRoadDir, Vector3.up));

                //slice back off
                if (lenght + roadSegmentLenght > middleRoadLenght)
                {
                    EzySlice.SlicedHull hull = roadObj.Slice(roadFrontWorldPosition + thisRoadDir * middleRoadLenght, -thisRoadDir);

                    if (hull != null && hull.upperHull)
                    {
                        MonoBehaviour.Destroy(roadObj);
                        roadObj = hull.CreateUpperHull(roadObj);
                    }
                }

                roadObj.name = "Middle Part";
                roadObj.transform.SetParent(road.transform);
            }

            //endRoad
            for (float lenght = middleRoadLenght; lenght < endRoadLenght; lenght += roadSegmentLenght)
            {
                GameObject roadObj1 = GameObject.Instantiate(roadEmpty, roadFrontWorldPosition + thisRoadDir * lenght + Vector3.up * 0.001f, Quaternion.LookRotation(thisRoadDir, Vector3.up));
                //slice the road angle
                EzySlice.SlicedHull hull = roadObj1.Slice(roadEndWorldPosition, endSliceDirection);
                if (hull != null && hull.upperHull)
                {
                    MonoBehaviour.Destroy(roadObj1);
                    roadObj1 = hull.CreateUpperHull(roadObj1);
                }

                roadObj1.name = "End Part";

                roadObj1.transform.SetParent(road.transform);
            }
        }
    }