Ejemplo n.º 1
0
        private Vector3?GetHitPoint(Vector2 currentPosition)
        {
            // Conver the mouse position into a ray to intersect with a plane in the world
            Ray currentRay = Camera.current.ScreenPointToRay(EditorHelper.ConvertMousePointPosition(currentPosition));

            // Find the currently active plane
            Plane plane = GetActivePlane();

            // If we hit the plane, return the hit point, otherwise return null
            float distance;

            if (plane.Raycast(currentRay, out distance))
            {
                Vector3 hitPoint = currentRay.GetPoint(distance);

                if (CurrentSettings.PositionSnappingEnabled)
                {
                    Polygon activePolygon = GetActivePolygon();

                    if (activePolygon != null)
                    {
                        // Rotation to go from the polygon's plane to XY plane (for sorting)
                        Quaternion cancellingRotation = Quaternion.Inverse(Quaternion.LookRotation(plane.normal));
                        Quaternion restoringRotation  = Quaternion.LookRotation(plane.normal);
                        hitPoint -= activePolygon.GetCenterPoint();
                        Vector3 localHitPoint = cancellingRotation * hitPoint;
                        // Round in local space
                        localHitPoint = MathHelper.RoundVector3(localHitPoint, CurrentSettings.PositionSnapDistance);
                        // Convert back to correct space
                        hitPoint  = restoringRotation * localHitPoint;
                        hitPoint += activePolygon.GetCenterPoint();
                    }
                    else
                    {
                        hitPoint = MathHelper.RoundVector3(hitPoint, CurrentSettings.PositionSnapDistance);
                    }
                }
                return(hitPoint);
            }
            else
            {
                return(null);
            }
        }
Ejemplo n.º 2
0
        public static Vector2 GetUVOffset(Polygon polygon)
        {
            Vertex vertex1;
            Vertex vertex2;
            Vertex vertex3;

            // Get three vertices which will reliably give us good UV information (i.e. not collinear)
            SurfaceUtility.GetPrimaryPolygonDescribers(polygon, out vertex1, out vertex2, out vertex3);

            Vector2 newUV = GeometryHelper.GetUVForPosition(vertex1.Position,
                                                            vertex2.Position,
                                                            vertex3.Position,
                                                            vertex1.UV,
                                                            vertex2.UV,
                                                            vertex3.UV,
                                                            polygon.GetCenterPoint());

            return(newUV);
        }
Ejemplo n.º 3
0
        internal List <Polygon> ProvideSubtractChunks(List <BrushChunk> subtractChunks)
        {
            List <Polygon> damagedPolygons = new List <Polygon>();

            List <Polygon> addedPolygons = new List <Polygon>();

            // If this chunk knows about any split planes
//			if(splitPlanes.Count > 0)
            {
                // For each of the subtract chunks
                int subtractChunkCount = subtractChunks.Count;
                for (int chunkIndex = 0; chunkIndex < subtractChunkCount; chunkIndex++)
                {
                    int polygonCount = subtractChunks[chunkIndex].Polygons.Count;
                    // For each of the polygons within those subtract chunks
                    for (int j = 0; j < polygonCount; j++)
                    {
                        Polygon polygon = subtractChunks[chunkIndex].Polygons[j];

                        // Disregard any polygons that can't be displayed as final geometry
                        if (polygon.ExcludeFromFinal)
                        {
                            continue;
                        }

                        Plane polygonPlane = polygon.CachedPlaneTest;

                        // Determine if any of the split planes this chunk has been split against match any of those
                        // subtraction polygons
                        bool anyFound = false;


                        int   polygonsCount = polygons.Count;
                        Plane splitPlane;
                        for (int i = 0; i < polygonsCount; i++)
                        {
                            if (!polygons[i].ExcludeFromFinal)
                            {
                                continue;
                            }

                            splitPlane = polygons[i].CachedPlaneTest;

                            if (MathHelper.PlaneEqualsLooserWithFlip(polygonPlane, splitPlane))
                            {
                                anyFound = true;
                                break;
                            }
                        }

                        bool added = false;
                        if (anyFound)
                        {
                            // TODO: Is GetCenterPoint expensive?
                            Vector3 target = polygon.GetCenterPoint();
                            // If this brush chunk contains the subtraction polygon
                            // TODO: This is a heftly call a lot of the time, so see about optimising
                            if (GeometryHelper.PolyhedronContainsPointEpsilon3(this.polygons, target))
                            {
                                added = true;
                                // Duplicate the subtraction polygon
                                polygon = polygon.DeepCopy();


                                // Flip it, so that it can form outer geometry
                                polygon.Flip();
//
//
                                for (int i = 0; i < this.polygons.Count; i++)
                                {
                                    if (!this.polygons[i].ExcludeFromFinal && MathHelper.PlaneEqualsLooser(this.polygons[i].Plane, polygon.Plane))
                                    {
                                        if (!addedPolygons.Contains(this.polygons[i]))
                                        {
                                            //Debug.LogWarning("Removing duplicate from chunk " + this.uniqueID + " subtraction chunk " + subtractChunks[chunkIndex].UniqueID);
                                            polygons[i].ExcludeFromFinal = true;
                                        }
                                    }
                                }

                                // Add it to this brush chunk
                                polygons.Add(polygon);

                                addedPolygons.Add(polygon);
                            }
                        }

                        if (!added)
                        {
                            damagedPolygons.Add(polygon);
                        }
                    }
                }
            }

            return(damagedPolygons);
        }