internal override bool UpdateBaseShape(bool registerUndo = true)
		{
			List<ShapePolygon> newPolygons = null;
			var outlineVertices = settings.GetVertices(buildPlane, worldPosition, base.gridTangent, base.gridBinormal, out shapeIsValid);
			if (shapeIsValid)
			{
				newPolygons = ShapePolygonUtility.CreateCleanPolygonsFromVertices(outlineVertices, brushPosition, buildPlane);
				shapeIsValid = newPolygons != null;
			}
			//CSGBrushEditorManager.ResetMessage();

			if (shapeIsValid && newPolygons != null)
				UpdatePolygons(outlineVertices, newPolygons.ToArray());

			if (editMode != EditMode.EditShape &&
				editMode != EditMode.ExtrudeShape)
				return false;

			if (!shapeIsValid || !HaveExtrusion)
				return true;

			GenerateBrushesFromPolygons(inGridSpace: false);
			UpdateExtrudedShape();
			return true;
		}
Exemple #2
0
        internal override bool CreateControlMeshForBrushIndex(CSGModel parentModel, CSGBrush brush, ShapePolygon polygon, Matrix4x4 localToWorld, float height, out ControlMesh newControlMesh, out Shape newShape)
        {
            var direction = haveForcedDirection ? forcedDirection : buildPlane.normal;

            if (!ShapePolygonUtility.GenerateControlMeshFromVertices(polygon,
                                                                     localToWorld,
                                                                     GeometryUtility.RotatePointIntoPlaneSpace(buildPlane, direction),
                                                                     height,

                                                                     new TexGen(),

                                                                     false,
                                                                     true,
                                                                     out newControlMesh,
                                                                     out newShape))
            {
                return(false);
            }

            brush.Shape       = newShape;
            brush.ControlMesh = newControlMesh;
            InternalCSGModelManager.ValidateBrush(brush, true);
            ControlMeshUtility.RebuildShape(brush);
            return(true);
        }
        public bool IsMouseOverShapePolygons(Matrix4x4 matrix)
        {
            var inverseMatrix = matrix.inverse;
            var mouseRay      = HandleUtility.GUIPointToWorldRay(Event.current.mousePosition);

            mouseRay.origin    = inverseMatrix.MultiplyPoint(mouseRay.origin);
            mouseRay.direction = inverseMatrix.MultiplyVector(mouseRay.direction);

            if (polygons == null)
            {
                return(false);
            }

            for (int i = 0; i < polygons.Length; i++)
            {
                if (ShapePolygonUtility.IntersectsWithShapePolygon2D(polygons[i], mouseRay))
                {
                    return(true);
                }
            }
            return(false);
        }
Exemple #4
0
        internal override bool StartExtrudeMode(bool showErrorMessage = true)
        {
            // reverse buildPlane if it's different
            if (BuildPlaneIsReversed)
            {
                buildPlane = buildPlane.Negated();
                CalculateWorldSpaceTangents();
            }

            var outlineVertices = settings.GetVertices(buildPlane, worldPosition, base.gridTangent, base.gridBinormal, out shapeIsValid);

            if (!shapeIsValid)
            {
                ClearPolygons();
                //if (showErrorMessage)
                //	CSGBrushEditorManager.ShowMessage("Could not create brush from given 2D shape");
                HideGenerateBrushes();
                return(false);
            }

            var newPolygons = ShapePolygonUtility.CreateCleanPolygonsFromVertices(outlineVertices, brushPosition, buildPlane);

            if (newPolygons == null)
            {
                shapeIsValid = false;
                ClearPolygons();
                //if (showErrorMessage)
                //	CSGBrushEditorManager.ShowMessage("Could not create brush from given 2D shape");
                HideGenerateBrushes();
                return(false);
            }
            EditModeManager.ResetMessage();
            shapeIsValid = true;

            UpdatePolygons(outlineVertices, newPolygons.ToArray());
            GenerateBrushesFromPolygons(inGridSpace: false);
            return(true);
        }
        internal override bool CreateControlMeshForBrushIndex(CSGModel parentModel, CSGBrush brush, ShapePolygon polygon, Matrix4x4 localToWorld, float height, out ControlMesh newControlMesh, out Shape newShape)
        {
            bool smooth            = settings.circleSmoothShading;
            bool singleSurfaceEnds = settings.circleSingleSurfaceEnds;
            var  direction         = haveForcedDirection ? forcedDirection : buildPlane.normal;

            if (!ShapePolygonUtility.GenerateControlMeshFromVertices(polygon,
                                                                     localToWorld,
                                                                     GeometryUtility.RotatePointIntoPlaneSpace(buildPlane, direction),
                                                                     height,

                                                                     new TexGen(),

                                                                     smooth,
                                                                     singleSurfaceEnds,
                                                                     out newControlMesh,
                                                                     out newShape))
            {
                return(false);
            }

            brush.Shape       = newShape;
            brush.ControlMesh = newControlMesh;
            InternalCSGModelManager.ValidateBrush(brush, true);
            ControlMeshUtility.RebuildShape(brush);

            var   vertices      = polygon.Vertices;
            float circumference = 0.0f;

            for (int j = vertices.Length - 1, i = 0; i < vertices.Length; j = i, i++)
            {
                circumference += (vertices[j] - vertices[i]).magnitude;
            }

            var shape = brush.Shape;

            float desiredTextureLength = Mathf.Max(1.0f, Mathf.Round(circumference));
            float scalar = desiredTextureLength / circumference;

            shape.TexGens[0].Scale.x       = scalar;
            shape.TexGens[0].Scale.y       = shape.TexGens[0].Scale.y;
            shape.TexGens[0].Translation.x = 0;

            var count = vertices.Length;

            if (!singleSurfaceEnds)
            {
                GeometryUtility.ContinueTexGenFromSurfaceToSurface(brush, 0, count);
                GeometryUtility.ContinueTexGenFromSurfaceToSurface(brush, 0, count + count);
                for (int j = 0, i = 1; i < count; j = i, i++)
                {
                    GeometryUtility.ContinueTexGenFromSurfaceToSurface(brush, j, i);
                    GeometryUtility.ContinueTexGenFromSurfaceToSurface(brush, i, i + count);
                    GeometryUtility.ContinueTexGenFromSurfaceToSurface(brush, i, i + count + count);
                }
            }
            else
            {
                for (int j = 0, i = 1; i < count; j = i, i++)
                {
                    GeometryUtility.ContinueTexGenFromSurfaceToSurface(brush, j, i);
                }
            }

            return(true);
        }
        public static void FixMaterials(Vector3[]                       originalVertices,
                                        List <ShapePolygon> polygons,
                                        Quaternion rotation,
                                        Vector3 origin,
                                        CSGPlane buildPlane,
                                        Material[]                      edgeMaterials = null,
                                        TexGen[]                        edgeTexgens   = null,
                                        ShapeEdge[]                     shapeEdges    = null)
        {
            if (shapeEdges != null)
            {
                for (int e = 0; e < shapeEdges.Length; e++)
                {
                    shapeEdges[e].EdgeIndex    = -1;
                    shapeEdges[e].PolygonIndex = -1;
                }
            }

            for (int p = 0; p < polygons.Count; p++)
            {
                var shapePolygon = polygons[p];
                var vertices3d   = shapePolygon.Vertices;

                var shapeMaterials = new Material[vertices3d.Length];
                var shapeTexgens   = new TexGen[vertices3d.Length];
                //var shapeTexgenFlags	= new TexGenFlags[vertices3d.Length];

                if (edgeMaterials != null &&
                    edgeTexgens != null)
                {
                    var indices = new int[vertices3d.Length];
                    for (int n0 = 0; n0 < vertices3d.Length; n0++)
                    {
                        indices[n0] = -1;
                        for (int n1 = 0; n1 < originalVertices.Length; n1++)
                        {
                            float diff = (vertices3d[n0] - originalVertices[n1]).sqrMagnitude;
                            if (diff > MathConstants.EqualityEpsilonSqr)
                            {
                                continue;
                            }

                            indices[n0] = n1;
                            break;
                        }
                    }

                    for (int n0 = indices.Length - 1, n1 = 0; n1 < indices.Length; n0 = n1, n1++)
                    {
                        var vertex0 = indices[n0] % edgeMaterials.Length;
                        var vertex1 = indices[n1] % edgeMaterials.Length;

                        if (vertex0 == -1 || vertex1 == -1)
                        {
                            shapeMaterials[n1] = CSGSettings.DefaultMaterial;
                            shapeTexgens[n1]   = new TexGen(n0);
                            //shapeTexgenFlags[n1]	= TexGenFlags.Discarded;
                        }
                        else
                        if ((Mathf.Abs(vertex1 - vertex0) == 1 ||
                             vertex0 == vertices3d.Length - 1 && vertex1 == 0))
                        {
                            if (shapeEdges != null)
                            {
                                shapeEdges[vertex0].PolygonIndex = p;
                                shapeEdges[vertex0].EdgeIndex    = n1;
                            }
                            shapeMaterials[n1] = edgeMaterials[vertex0];
                            shapeTexgens  [n1] = edgeTexgens[vertex0];
                            //shapeTexgenFlags[n1]	= TexGenFlags.None;
                        }
                        else
                        {
                            shapeMaterials[n1] = edgeMaterials[vertex0];                            // CSGSettings.DefaultMaterial;
                            shapeTexgens  [n1] = new TexGen(n0);
                            //shapeTexgenFlags[n1]	= TexGenFlags.None;
                        }
                    }
                }
                else
                {
                    for (int n0 = 0; n0 < vertices3d.Length; n0++)
                    {
                        shapeMaterials[n0] = CSGSettings.DefaultMaterial;
                        shapeTexgens[n0]   = new TexGen(n0);
                        //shapeTexgenFlags[n0]	= TexGenFlags.None;
                    }
                }

                shapePolygon.EdgeMaterials = shapeMaterials;
                shapePolygon.EdgeTexgens   = shapeTexgens;

                ShapePolygonUtility.RemoveDuplicatePoints(shapePolygon);
            }
        }