/// <summary>
        /// Initialize this object with the various editor-only parameters, and invoke the object creation callback.
        /// </summary>
        /// <param name="pb"></param>
        internal static void InitObject(ProBuilderMesh pb)
        {
            MoveToActiveScene(pb.gameObject);

            ScreenCenter(pb.gameObject);

            SetPivotLocationAndSnap(pb);

            pb.renderer.shadowCastingMode = s_ShadowCastingMode;
            pb.renderer.sharedMaterial    = EditorMaterialUtility.GetUserMaterial();

            GameObjectUtility.SetStaticEditorFlags(pb.gameObject, s_StaticEditorFlags);

            switch (s_ColliderType.value)
            {
            case ColliderType.BoxCollider:
                pb.gameObject.AddComponent <BoxCollider>();
                break;

            case ColliderType.MeshCollider:
                pb.gameObject.AddComponent <MeshCollider>().convex = s_MeshColliderIsConvex;
                break;
            }

            pb.unwrapParameters = new UnwrapParameters(Lightmapping.s_UnwrapParameters);

            pb.Optimize();

            if (meshCreated != null)
            {
                meshCreated(pb);
            }
        }
Пример #2
0
        internal void RebuildShape()
        {
            RecalculateBounds();

            if (m_Bounds.size.sqrMagnitude < .01f ||
                Mathf.Abs(m_Bounds.extents.x) < 0.001f ||
                Mathf.Abs(m_Bounds.extents.z) < 0.001f)
            {
                if (m_ProBuilderShape != null &&
                    m_ProBuilderShape.mesh.vertexCount > 0)
                {
                    m_ProBuilderShape.mesh.Clear();
                    m_ProBuilderShape.mesh.Rebuild();
                    ProBuilderEditor.Refresh(true);
                }
                return;
            }

            if (!m_IsShapeInit)
            {
                var shapeComponent = currentShapeInOverlay;
                EditorShapeUtility.CopyLastParams(shapeComponent.shape, shapeComponent.shape.GetType());
                shapeComponent.gameObject.hideFlags         = HideFlags.HideInHierarchy;
                shapeComponent.mesh.renderer.sharedMaterial = EditorMaterialUtility.GetUserMaterial();
                UndoUtility.RegisterCreatedObjectUndo(shapeComponent.gameObject, "Draw Shape");
                m_IsShapeInit = true;
            }

            m_ProBuilderShape.Rebuild(m_Bounds, m_PlaneRotation);
            ProBuilderEditor.Refresh(false);

            SceneView.RepaintAll();
        }
        /// <summary>
        /// Initialize this object with the various editor-only parameters, and invoke the object creation callback.
        /// </summary>
        /// <param name="pb"></param>
        internal static void InitObject(ProBuilderMesh pb)
        {
            MoveToActiveRoot(pb.gameObject);

            GameObjectUtility.EnsureUniqueNameForSibling(pb.gameObject);
            ScreenCenter(pb.gameObject);
            SnapInstantiatedObject(pb);

#if UNITY_2019_1_OR_NEWER
            ComponentUtility.MoveComponentRelativeToComponent(pb, pb.transform, false);
#endif

            pb.renderer.shadowCastingMode = s_ShadowCastingMode;
            pb.renderer.sharedMaterial    = EditorMaterialUtility.GetUserMaterial();

            GameObjectUtility.SetStaticEditorFlags(pb.gameObject, s_StaticEditorFlags);

            switch (s_ColliderType.value)
            {
            case ColliderType.BoxCollider:
                if (!pb.gameObject.TryGetComponent <BoxCollider>(out _))
                {
                    Undo.AddComponent(pb.gameObject, typeof(BoxCollider));
                }
                break;

            case ColliderType.MeshCollider:
                MeshCollider collider;
                if (!pb.gameObject.TryGetComponent <MeshCollider>(out collider))
                {
                    collider = Undo.AddComponent <MeshCollider>(pb.gameObject);
                }
                // This little dance is required to prevent the Prefab system from detecting an overridden property
                // before ProBuilderMesh.RefreshCollisions has a chance to mark the MeshCollider.sharedMesh property
                // as driven. "AddComponent<MeshCollider>" constructs the MeshCollider and simultaneously assigns
                // the "m_Mesh" property, marking the property dirty. So we undo that change, here then assign the
                // mesh through our own method.
                collider.sharedMesh = null;
                collider.convex     = s_MeshColliderIsConvex;
                pb.Refresh(RefreshMask.Collisions);
                break;
            }

            pb.unwrapParameters = new UnwrapParameters(Lightmapping.s_UnwrapParameters);

            pb.Optimize();

            if (meshCreated != null)
            {
                meshCreated(pb);
            }
        }
Пример #4
0
 public void SetDefaultValues()
 {
     array = new Material[10]
     {
         EditorMaterialUtility.GetUserMaterial(),
             null,
             null,
             null,
             null,
             null,
             null,
             null,
             null,
             null
     };
 }
Пример #5
0
        static ActionResult MenuBooleanOperation(BooleanOperation operation, ProBuilderMesh lhs, ProBuilderMesh rhs)
        {
            if (lhs == null || rhs == null)
            {
                return(new ActionResult(ActionResult.Status.Failure, "Must Select 2 Objects"));
            }

            string op_string = operation == BooleanOperation.Union ? "Union" : (operation == BooleanOperation.Subtract ? "Subtract" : "Intersect");

            ProBuilderMesh[] sel = new ProBuilderMesh[] { lhs, rhs };

            UndoUtility.RecordSelection(sel, op_string);

            Mesh c;

            switch (operation)
            {
            case BooleanOperation.Union:
                c = CSG.Union(lhs.gameObject, rhs.gameObject);
                break;

            case BooleanOperation.Subtract:
                c = CSG.Subtract(lhs.gameObject, rhs.gameObject);
                break;

            default:
                c = CSG.Intersect(lhs.gameObject, rhs.gameObject);
                break;
            }

            GameObject go = new GameObject();

            go.AddComponent <MeshRenderer>().sharedMaterial = EditorMaterialUtility.GetUserMaterial();
            go.AddComponent <MeshFilter>().sharedMesh       = c;

            ProBuilderMesh pb = InternalMeshUtility.CreateMeshWithTransform(go.transform, false);

            DestroyImmediate(go);

            Selection.objects = new Object[] { pb.gameObject };

            return(new ActionResult(ActionResult.Status.Success, op_string));
        }
Пример #6
0
        void SetPreviewMesh(ProBuilderMesh mesh)
        {
            ApplyPreviewTransform(mesh);
            Mesh umesh = mesh.mesh;

            if (umesh != null)
            {
                umesh.hideFlags = HideFlags.DontSave;
            }

            if (m_PreviewObject)
            {
                if (!m_PreviewObject.TryGetComponent(out MeshFilter mf))
                {
                    mf = m_PreviewObject.AddComponent <MeshFilter>();
                }
                if (mf.sharedMesh != null)
                {
                    DestroyImmediate(mf.sharedMesh);
                }
                m_PreviewObject.GetComponent <MeshFilter>().sharedMesh = umesh;
                mesh.preserveMeshAssetOnDestroy = true;
                m_PreviewObject.name            = mesh.gameObject.name;
                if (!m_PreviewObject.name.EndsWith("-preview"))
                {
                    m_PreviewObject.name += "-preview";
                }
                DestroyImmediate(mesh.gameObject);
            }
            else
            {
                m_PreviewObject                 = mesh.gameObject;
                m_PreviewObject.hideFlags       = HideFlags.DontSave;
                mesh.preserveMeshAssetOnDestroy = true;
                DestroyImmediate(mesh);
                Selection.activeTransform = m_PreviewObject.transform;
                m_PreviewObject.name     += "-preview";
            }

            m_PreviewObject.GetComponent <MeshRenderer>().sharedMaterial = EditorMaterialUtility.GetShapePreviewMaterial();
            EditorUtility.MoveToActiveScene(m_PreviewObject.gameObject);
        }
Пример #7
0
        void OnGUI()
        {
            DoContextMenu();

            GUILayout.Label("Quick Material", EditorStyles.boldLabel);
            Rect r    = GUILayoutUtility.GetLastRect();
            int  left = Screen.width - 68;

            GUILayout.BeginHorizontal(GUILayout.MaxWidth(Screen.width - 74));
            GUILayout.BeginVertical();

            m_QueuedMaterial = (Material)EditorGUILayout.ObjectField(m_QueuedMaterial, typeof(Material), true);

            GUILayout.Space(2);

            if (GUILayout.Button("Apply (Ctrl+Shift+Click)"))
            {
                ApplyMaterial(MeshSelection.topInternal, m_QueuedMaterial);
            }

            GUI.enabled = editor != null && MeshSelection.selectedFaceCount > 0;
            if (GUILayout.Button("Match Selection"))
            {
                m_QueuedMaterial = EditorMaterialUtility.GetActiveSelection();
            }
            GUI.enabled = true;

            GUILayout.EndVertical();

            GUI.Box(new Rect(left, r.y + r.height + 2, 64, 64), "");

            var previewTexture = EditorMaterialUtility.GetPreviewTexture(m_QueuedMaterial);

            if (previewTexture != null)
            {
                GUI.Label(new Rect(left + 2, r.y + r.height + 4, 60, 60), previewTexture);
            }
            else
            {
                GUI.Box(new Rect(left + 2, r.y + r.height + 4, 60, 60), "");
                GUI.Label(new Rect(left + 2, r.height + 28, 120, 32), "None\n(Texture)");
            }

            GUILayout.EndHorizontal();

            GUILayout.Space(4);

            GUI.backgroundColor = PreferenceKeys.proBuilderDarkGray;
            UI.EditorGUIUtility.DrawSeparator(2);
            GUI.backgroundColor = Color.white;

            GUILayout.Label("Material Palette", EditorStyles.boldLabel);

            EditorGUI.BeginChangeCheck();

            m_CurrentPaletteIndex = EditorGUILayout.Popup("", m_CurrentPaletteIndex, m_AvailablePalettes_Str);

            if (EditorGUI.EndChangeCheck())
            {
                MaterialPalette newPalette = null;

                // Add a new material palette
                if (m_CurrentPaletteIndex >= m_AvailablePalettes.Length)
                {
                    string path = AssetDatabase.GenerateUniqueAssetPath("Assets/Material Palette.asset");
                    newPalette = FileUtility.LoadRequired <MaterialPalette>(path);
                    EditorGUIUtility.PingObject(newPalette);
                }
                else
                {
                    newPalette = m_AvailablePalettes[m_CurrentPaletteIndex];
                }

                SetMaterialPalette(newPalette);
            }

            EditorGUI.BeginChangeCheck();
            s_CurrentPalette = (MaterialPalette)EditorGUILayout.ObjectField(s_CurrentPalette, typeof(MaterialPalette), false);
            if (EditorGUI.EndChangeCheck())
            {
                SetMaterialPalette(s_CurrentPalette);
            }

            GUILayout.Space(4);

            Material[] materials = CurrentPalette;

            m_ViewScroll = GUILayout.BeginScrollView(m_ViewScroll);

            for (int i = 0; i < materials.Length; i++)
            {
                if (i == 10)
                {
                    GUILayout.Space(2);
                    GUI.backgroundColor = PreferenceKeys.proBuilderLightGray;
                    UI.EditorGUIUtility.DrawSeparator(1);
                    GUI.backgroundColor = Color.white;
                    GUILayout.Space(2);
                }

                GUILayout.BeginHorizontal();
                if (i < 10)
                {
                    if (GUILayout.Button("Alt + " + (i == 9 ? 0 : (i + 1)).ToString(), EditorStyles.miniButton, GUILayout.MaxWidth(58)))
                    {
                        ApplyMaterial(MeshSelection.topInternal, materials[i]);
                    }
                }
                else
                {
                    if (GUILayout.Button("Apply", EditorStyles.miniButtonLeft, GUILayout.MaxWidth(44)))
                    {
                        ApplyMaterial(MeshSelection.topInternal, materials[i]);
                    }

                    GUI.backgroundColor = Color.red;
                    if (GUILayout.Button("", EditorStyles.miniButtonRight, GUILayout.MaxWidth(14)))
                    {
                        Material[] temp = new Material[materials.Length - 1];
                        System.Array.Copy(materials, 0, temp, 0, materials.Length - 1);
                        materials = temp;
                        SaveUserMaterials(materials);
                        return;
                    }
                    GUI.backgroundColor = Color.white;
                }

                EditorGUI.BeginChangeCheck();
                materials[i] = (Material)EditorGUILayout.ObjectField(materials[i], typeof(Material), false);
                if (EditorGUI.EndChangeCheck())
                {
                    SaveUserMaterials(materials);
                }

                GUILayout.EndHorizontal();
            }


            if (GUILayout.Button("Add"))
            {
                Material[] temp = new Material[materials.Length + 1];
                System.Array.Copy(materials, 0, temp, 0, materials.Length);
                materials = temp;
                SaveUserMaterials(materials);
            }

            GUILayout.EndScrollView();
        }