Example #1
0
        public static void RecenterPivot(FScene scene, DMeshSO so, PivotLocation location, bool bInteractive)
        {
            if (so.Parent != scene)
            {
                DebugUtil.Log("OrientationChanges.RecenterPivot: tried to recenter pivot on non-scene-child");
                return;
            }

            Frame3f          sceneL       = SceneTransforms.SceneToObject(so, Frame3f.Identity);
            AxisAlignedBox3d sceneBoundsL = AxisAlignedBox3d.Empty;

            so.SafeMeshRead((mesh) => {
                sceneBoundsL = MeshMeasurements.BoundsInFrame(mesh, sceneL);
                return(null);
            });

            Vector3d originL = sceneBoundsL.Center;

            if (location == PivotLocation.BaseCenter)
            {
                originL -= sceneBoundsL.Extents.y * Vector3d.AxisY;
            }
            Frame3f newFrameL = new Frame3f(originL);

            RepositionPivotChangeOp change = new RepositionPivotChangeOp(newFrameL, so);

            scene.History.PushChange(change, false);
            if (bInteractive)
            {
                scene.History.PushInteractionCheckpoint();
            }
        }
        /// <summary>
        /// Set the pivot point for a mesh to either the center, or a corner point of the bounding box.
        /// </summary>
        /// <param name="mesh">The <see cref="ProBuilderMesh"/> to adjust vertices for a new pivot point.</param>
        /// <param name="pivotLocation">The new pivot point is either the center of the mesh bounding box, or
        /// the bounds center - extents.</param>
        internal static void SetPivot(this ProBuilderMesh mesh, PivotLocation pivotLocation)
        {
            var bounds = mesh.GetBounds();
            var pivot  = pivotLocation == PivotLocation.Center ? bounds.center : bounds.center - bounds.extents;

            SetPivot(mesh, mesh.transform.TransformPoint(pivot));
        }
Example #3
0
        /// <summary>
        /// Create a shape with default parameters.
        /// </summary>
        /// <param name="shapeType">The ShapeType to create.</param>
        /// <param name="pivotType">Where the shape's pivot will be.</param>
        /// <returns>A new GameObject with the ProBuilderMesh initialized to the primitive shape.</returns>
        public static ProBuilderMesh Instantiate(Type shapeType, PivotLocation pivotType = PivotLocation.Center)
        {
            if (shapeType == null)
            {
                throw new ArgumentNullException("shapeType", "Cannot instantiate a null shape.");
            }

            if (shapeType.IsAssignableFrom(typeof(Shape)))
            {
                throw new ArgumentException("Type needs to derive from Shape");
            }

            try
            {
                var shape = Activator.CreateInstance(shapeType,
                                                     BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.CreateInstance,
                                                     null, null, null, null) as Shape;
                return(Instantiate(shape, pivotType));
            }
            catch (Exception e)
            {
                Debug.LogError($"Failed creating shape \"{shapeType}\". Shapes must contain an empty constructor.\n{e}");
            }

            return(null);
        }
    static ProBuilderMesh GenerateCone(PivotLocation pivotType, float radius, float height, int subdivAxis)
    {
        ProBuilderMesh pb   = ShapeFactory.Instantiate(typeof(Cone), pivotType);
        Cone           cone = pb.GetComponent <ShapeComponent>().shape as Cone;

        cone.m_NumberOfSides = subdivAxis;
        cone.RebuildMesh(pb, new Vector3(radius, height, radius), Quaternion.identity);
        pb.RefreshUV(pb.faces);
        return(pb);
    }
Example #5
0
        internal static void SetPivot(this ProBuilderMesh mesh, PivotLocation pivotType, int firstVertexIndex = 0)
        {
            switch (pivotType)
            {
            case PivotLocation.Center:
                mesh.CenterPivot(null);
                break;

            case PivotLocation.FirstVertex:
                mesh.CenterPivot(new int[1] {
                    firstVertexIndex
                });
                break;
            }
        }
Example #6
0
    public static Vector3 ChangePivot(ref List <Vector3> vertices, PivotLocation pivotLocation)
    {
        var pivotxMin  = vertices.Select(i => i.x).Min();
        var pivotzMin  = vertices.Select(i => i.z).Min();
        var pivotyMin  = vertices.Select(i => i.y).Min();
        var pivotxMax  = vertices.Select(i => i.x).Max();
        var pivotzMax  = vertices.Select(i => i.z).Max();
        var pivotyMax  = vertices.Select(i => i.y).Max();
        var PivotPoint = new Vector3((pivotxMin + pivotxMax) / 2, (pivotyMin + pivotyMax) / 2, (pivotzMin + pivotzMax) / 2);

        for (int i = 0; i < vertices.Count; i++)
        {
            vertices[i] -= PivotPoint;
        }
        return(PivotPoint);
    }
Example #7
0
        internal static void SetPivot(this ProBuilderMesh mesh, PivotLocation pivotType, Vector3?pivotPosition = null)
        {
            if (mesh.vertexCount == 0)
            {
                return;
            }

            switch (pivotType)
            {
            case PivotLocation.Center:
                mesh.CenterPivot(null);
                break;

            case PivotLocation.FirstCorner:
                mesh.SetPivot(pivotPosition == null ? Vector3.zero : (Vector3)pivotPosition);
                break;
            }
        }
Example #8
0
        /// <summary>
        /// Create a shape with default parameters.
        /// </summary>
        /// <param name="shape">The ShapeType to create.</param>
        /// <param name="pivotType">Where the shape's pivot will be.</param>
        /// <returns>A new GameObject with the ProBuilderMesh initialized to the primitive shape.</returns>
        public static ProBuilderMesh Instantiate(Shape shape, PivotLocation pivotType = PivotLocation.Center)
        {
            if (shape == null)
            {
                throw new ArgumentNullException("shape", "Cannot instantiate a null shape.");
            }

            var shapeComponent = new GameObject("Shape").AddComponent <ProBuilderShape>();

            shapeComponent.SetShape(shape, pivotType);
            ProBuilderMesh pb = shapeComponent.mesh;

            pb.renderer.sharedMaterial = BuiltinMaterials.defaultMaterial;

            var attribute = Attribute.GetCustomAttribute(shape.GetType(), typeof(ShapeAttribute));

            if (attribute is ShapeAttribute shapeAttrib)
            {
                pb.gameObject.name = shapeAttrib.name;
            }

            return(pb);
        }
Example #9
0
 /// <summary>
 /// Create a shape with default parameters.
 /// </summary>
 /// <param name="pivotType">Where the shape's pivot will be.</param>
 /// <returns>A new GameObject with the ProBuilderMesh initialized to the primitive shape.</returns>
 public static ProBuilderMesh Instantiate <T>(PivotLocation pivotType = PivotLocation.Center) where T : Shape, new()
 {
     return(Instantiate(typeof(T)));
 }