示例#1
0
        void OnGUIBuildMeshTree()
        {
            GUILayout.Label("One more step is left. Please set the build options and press Build button.");
            EditorGUILayout.Separator();
            EditorGUI.indentLevel++;
            EditorGUILayout.PropertyField(m_serializedObject.FindProperty("m_layerMask"));
            SerializedProperty excludeRenderTypes = m_serializedObject.FindProperty("m_excludeRenderTypes");

            EditorGUILayout.LabelField("Exclude Render Types");
            EditorGUI.indentLevel++;
            for (int i = 0; i < excludeRenderTypes.arraySize + 1; ++i)
            {
                string renderType;
                if (m_editingRenderType == i)
                {
                    renderType = m_editingString;
                }
                else if (i < excludeRenderTypes.arraySize)
                {
                    renderType = excludeRenderTypes.GetArrayElementAtIndex(i).stringValue;
                }
                else
                {
                    renderType = "";
                }
                string controlName = "RenderType" + i.ToString();
                GUI.SetNextControlName(controlName);
                string newRenderType  = EditorGUILayout.TextField(renderType);
                string focusedControl = GUI.GetNameOfFocusedControl();
                if (m_editingRenderType == i)
                {
                    m_editingString = newRenderType;
                    if (focusedControl != controlName || (/*Event.current.isKey &&*/ (Event.current.keyCode == KeyCode.Return || Event.current.keyCode == KeyCode.KeypadEnter)))
                    {
                        ApplyEditingRenderType();
                    }
                    else if (/*Event.current.isKey &&*/ Event.current.keyCode == KeyCode.Escape)
                    {
                        CancelEditingRenderType();
                    }
                }
                else if (renderType != newRenderType)
                {
                    ApplyEditingRenderType();
                    m_editingRenderType = i;
                    m_editingString     = newRenderType;
                }
            }
            EditorGUI.indentLevel--;
            EditorGUILayout.PropertyField(m_serializedObject.FindProperty("m_scaledOffset"));
            EditorGUILayout.PropertyField(m_serializedObject.FindProperty("m_fixedOffset"));
            GUILayout.TextArea("Offset parameters are used to push each vertex along its normal vector to reduce z fighting problem. The offset value is calculated as" + System.Environment.NewLine +
                               "    offset = 0.00000025 x |v| x <Scaled Offset> + <Fixed Offset>," + System.Environment.NewLine +
                               "where |v| is the norm of a vertex position vector." + System.Environment.NewLine +
                               "Scaled Offset makes it easy to adjust the offset value, because z fighting is caused by floating point rounding error, and maximum possible value of the error is roughly proportional to |v|.");
            m_serializedObject.ApplyModifiedProperties();
            EditorGUI.indentLevel--;
            EditorGUILayout.Separator();
            // check layer mask
            bool      hasObject = false;
            LayerMask layers    = ((MeshTree)m_meshTree).layerMask;

            for (int i = 0; i < srcMeshes.Length; ++i)
            {
                if ((layers & (1 << srcMeshes[i].gameObject.layer)) != 0)
                {
                    hasObject = true;
                    break;
                }
            }
            if (!hasObject)
            {
                GUILayout.TextArea("<color=red>No child mesh objects exist in the layers. Please add the layers of the mesh objects to Layer Mask.</color>", m_errorStyle);
                GUI.enabled = false;
            }
            EditorGUILayout.BeginHorizontal();
            GUILayout.FlexibleSpace();
            if (GUILayout.Button("Build", GUILayout.Width(100)))
            {
                MeshTreeEditor.BuildMeshTree(m_meshTree);
                Close();
            }
            EditorGUILayout.EndHorizontal();
            GUI.enabled = true;
        }
示例#2
0
        bool OnGUISetEnvironment()
        {
            MeshShadowReceiver receiver = null;

            if (0 < m_projectorManager.receivers.Count)
            {
                receiver = m_projectorManager.receivers[0] as MeshShadowReceiver;
            }

            GameObject   rootObject = null;
            MeshTreeBase meshTree   = null;

            if (receiver != null)
            {
                if (receiver.meshTransform != null)
                {
                    rootObject = receiver.meshTransform.gameObject;
                }
                meshTree = receiver.meshTree;
                GameObject newRootObject = EditorGUILayout.ObjectField("Root Object", rootObject, typeof(GameObject), true) as GameObject;
                if (newRootObject != rootObject)
                {
                    Undo.RecordObject(receiver, "Easy Setup: Set Root Object");
                    receiver.meshTransform = (newRootObject == null) ? null : newRootObject.transform;
                    EditorUtility.SetDirty(receiver);
                    rootObject          = newRootObject;
                    m_environmentMeshes = null;
                }
            }
            EditorGUILayout.PropertyField(m_serializedObject.FindProperty("m_environmentLayers"));
            m_serializedObject.ApplyModifiedProperties();
            bool findObject = false;

            if (receiver != null && receiver.meshTransform != null)
            {
                // check Environment Layers
                for (int i = 0; i < environmentMeshes.Length; ++i)
                {
                    if ((m_projectorManager.environmentLayers & (1 << environmentMeshes[i].gameObject.layer)) != 0)
                    {
                        findObject = true;
                        break;
                    }
                }
                if (!findObject)
                {
                    GUILayout.TextArea("<color=red>No child objects of Root Object found in Environment Layers.</color>", m_errorStyle);
                }
            }
            GUILayout.TextArea("The objects in the Environment Layers will be ignored by Projectors. They will not receive the shadows casted by the Projectors. Instead, shadow receiver will receive the shadows on behalf of them.");
            EditorGUILayout.BeginHorizontal();
            GUILayout.FlexibleSpace();
            if (GUILayout.Button("Edit Layers"))
            {
                Selection.activeObject = AssetDatabase.LoadAllAssetsAtPath("ProjectSettings/TagManager.asset")[0];
            }
            if (receiver != null)
            {
                if (rootObject == null)
                {
                    GUI.enabled = false;
                }
                if (GUILayout.Button("Collect Layers from Root Object"))
                {
                    LayerMask    layers = 0;
                    MeshFilter[] meshes = rootObject.GetComponentsInChildren <MeshFilter>();
                    {
                        var __array1       = meshes;
                        var __arrayLength1 = __array1.Length;
                        for (int __i1 = 0; __i1 < __arrayLength1; ++__i1)
                        {
                            var mesh = (MeshFilter)__array1[__i1];
                            {
                                layers |= (1 << mesh.gameObject.layer);
                            }
                        }
                    }
                    if (layers != m_projectorManager.environmentLayers)
                    {
                        Undo.RecordObject(m_projectorManager, "Easy Setup: Collect Layers from Root Object");
                        m_projectorManager.environmentLayers = layers;
                        EditorUtility.SetDirty(m_projectorManager);
                        m_serializedObject.Update();
                    }
                }
                GUI.enabled = true;
            }
            EditorGUILayout.EndHorizontal();

            EditorGUILayout.Separator();

            if (receiver == null)
            {
                m_isAbleToGoNext = m_projectorManager.environmentLayers != 0;
                return(true);
            }
            MeshTreeBase newMeshTree = EditorGUILayout.ObjectField("Mesh Tree", meshTree, typeof(MeshTreeBase), false) as MeshTreeBase;

            if (newMeshTree != meshTree)
            {
                Undo.RecordObject(receiver, "Easy Setup: Set Mesh Tree");
                receiver.meshTree = newMeshTree;
                meshTree          = newMeshTree;
                EditorUtility.SetDirty(receiver);
            }
            EditorGUILayout.BeginHorizontal();
            GUILayout.FlexibleSpace();
            if (rootObject == null || !findObject)
            {
                GUI.enabled = false;
            }
            if (GUILayout.Button("Search Assets folder"))
            {
                newMeshTree = MeshShadowReceiverEditor.FindMeshTreeFromMeshTransform(rootObject.transform);
                if (newMeshTree == null)
                {
                    EditorUtility.DisplayDialog("No mesh tree found for the Root Object.", "", "OK");
                }
                else
                {
                    Undo.RecordObject(receiver, "Easy Setup: Set Mesh Tree");
                    receiver.meshTree = newMeshTree;
                    meshTree          = newMeshTree;
                    EditorUtility.SetDirty(receiver);
                }
            }
            if (GUILayout.Button("Create a New Mesh Tree"))
            {
                CreateMeshTreeWizard.CreateWizard(rootObject.transform, m_projectorManager.environmentLayers, receiver);
            }
            GUI.enabled = true;
            EditorGUILayout.EndHorizontal();
            m_errorString = null;
            LayerMask missingLayers       = 0;
            LayerMask existingLayers      = 0;
            bool      multiObjectMeshTree = false;

            if (meshTree != null && rootObject != null)
            {
                if (meshTree is TerrainMeshTree)
                {
                    Terrain terrain = rootObject.GetComponent <Terrain>();
                    if (terrain == null)
                    {
                        m_errorString = "<color=red>The mesh tree is a TerrainMeshTree but Root Object is not a terrain object.</color>";
                    }
                    else if (terrain.terrainData != ((TerrainMeshTree)meshTree).terrainData)
                    {
                        m_errorString = "<color=red>The mesh tree was not the one which was created from the Root Object</color>";
                    }
                    else if ((m_projectorManager.environmentLayers & (1 << rootObject.layer)) == 0)
                    {
                        m_errorString  = "<color=red>Environment Layers doesn't contain the layer of the Root Object.</color>";
                        missingLayers |= (1 << rootObject.layer);
                    }
                }
                else
                {
                    Object srcMesh = ((MeshTree)meshTree).srcMesh;
                    if (srcMesh is Mesh)
                    {
                        MeshFilter mesh = rootObject.GetComponent <MeshFilter>();
                        if (mesh == null || mesh.sharedMesh != srcMesh)
                        {
                            m_errorString = "<color=red>The mesh tree was created from a single mesh but the Root Object doesn't have the mesh.</color>";
                        }
                        else if ((m_projectorManager.environmentLayers & (1 << rootObject.layer)) == 0)
                        {
                            m_errorString  = "<color=red>Environment Layers doesn't contain the layer of the Root Object.</color>";
                            missingLayers |= (1 << rootObject.layer);
                        }
                    }
                    else
                    {
                        multiObjectMeshTree = true;
                        if (srcMesh != null)
                        {
                            if (PrefabUtility.GetPrefabParent(rootObject) != srcMesh && rootObject != srcMesh)
                            {
                                PrefabType type = PrefabUtility.GetPrefabType(srcMesh);
                                if (type == PrefabType.Prefab || type == PrefabType.ModelPrefab)
                                {
                                    m_errorString = "<color=red>The mesh tree was created from a prefab object but the Root Object was not instantiated from the prefab.</color>";
                                }
                                else
                                {
                                    m_errorString = "<color=red>The mesh tree was not created from the Root Object.</color>";
                                }
                            }
                        }
                        if (m_errorString == null)
                        {
                            MeshFilter[] meshes         = rootObject.GetComponentsInChildren <MeshFilter>();
                            LayerMask    meshTreeLayers = ((MeshTree)meshTree).layerMask;
                            {
                                var __array2       = meshes;
                                var __arrayLength2 = __array2.Length;
                                for (int __i2 = 0; __i2 < __arrayLength2; ++__i2)
                                {
                                    var mesh = (MeshFilter)__array2[__i2];
                                    {
                                        if ((meshTreeLayers & (1 << mesh.gameObject.layer)) != 0)
                                        {
                                            if ((m_projectorManager.environmentLayers & (1 << mesh.gameObject.layer)) == 0)
                                            {
                                                missingLayers |= (1 << mesh.gameObject.layer);
                                            }
                                            else
                                            {
                                                existingLayers |= (1 << mesh.gameObject.layer);
                                            }
                                        }
                                    }
                                }
                            }
                            if (missingLayers != 0)
                            {
                                m_errorString = "<color=red>Some of the mesh tree layers are missing in Environment Layers.</color>";
                            }
                        }
                    }
                }
            }
            if (m_errorString != null)
            {
                GUILayout.TextArea(m_errorString, m_errorStyle);
                if (missingLayers != 0)
                {
                    EditorGUILayout.BeginHorizontal();
                    GUILayout.FlexibleSpace();
                    if (GUILayout.Button("Add mesh tree layers to Environment Layers"))
                    {
                        Undo.RecordObject(m_projectorManager, "Easy Setup: Add mesh tree layers to Environment Layers");
                        m_projectorManager.environmentLayers |= missingLayers;
                        EditorUtility.SetDirty(m_projectorManager);
                        m_serializedObject.Update();
                    }
                    EditorGUILayout.EndHorizontal();
                    if (multiObjectMeshTree && existingLayers != 0)
                    {
                        EditorGUILayout.BeginHorizontal();
                        GUILayout.FlexibleSpace();
                        if (GUILayout.Button("Rebuild the mesh tree with Environment Layers"))
                        {
                            ((MeshTree)meshTree).layerMask &= ~missingLayers;
                            if (((MeshTree)meshTree).srcMesh == null)
                            {
                                ((MeshTree)meshTree).srcMesh = rootObject;
                            }
                            MeshTreeEditor.BuildMeshTree(meshTree);
                        }
                        EditorGUILayout.EndHorizontal();
                    }
                }
            }
            m_isAbleToGoNext = rootObject != null && meshTree != null && m_errorString == null && m_projectorManager.environmentLayers != 0;
            return(true);
        }
示例#3
0
        void OnGUISelectMeshTreeTypeAndCreate()
        {
            EditorGUILayout.LabelField("Type of Mesh Tree");
            if (m_srcMesh is TerrainData)
            {
                EditorGUILayout.BeginHorizontal();
                GUILayout.Space(30);
                GUILayout.Label("Terrain Mesh Tree");
                EditorGUILayout.EndHorizontal();
            }
            else
            {
                EditorGUILayout.BeginHorizontal();
                GUILayout.Space(30);
                bool bBinaryTree = GUILayout.Toggle(m_bBinaryTree, "Binary Mesh Tree");
                EditorGUILayout.EndHorizontal();
                EditorGUILayout.BeginHorizontal();
                GUILayout.Space(30);
                bool bOctree = GUILayout.Toggle(!m_bBinaryTree, "Oct Mesh Tree");
                EditorGUILayout.EndHorizontal();
                if (bOctree && m_bBinaryTree)
                {
                    m_bBinaryTree = !bOctree;
                }
                else if (bBinaryTree && !m_bBinaryTree)
                {
                    m_bBinaryTree = bBinaryTree;
                }
                EditorGUILayout.BeginHorizontal();
                GUILayout.Space(30);
                if (GUILayout.Button("<color=blue>What's the difference?</color>", m_errorStyle))
                {
                    Application.OpenURL("http://nyahoon.com/products/fast-shadow-receiver/create-a-mesh-tree");
                }
                EditorGUILayout.EndHorizontal();

                EditorGUILayout.Separator();

                EditorGUILayout.LabelField("Select Source Object");

                EditorGUILayout.BeginHorizontal();
                GUILayout.Space(30);
                bool bSceneObject = GUILayout.Toggle(m_srcMesh == m_rootObject, "Scene Object");
                EditorGUILayout.ObjectField(m_rootObject, typeof(GameObject), true);
                EditorGUILayout.EndHorizontal();
                if (bSceneObject && m_srcMesh != m_rootObject)
                {
                    m_srcMesh = m_rootObject;
                }

                EditorGUILayout.BeginHorizontal();
                GameObject prefabObject = PrefabUtility.GetPrefabParent(m_rootObject) as GameObject;
                GUILayout.Space(30);
                if (prefabObject == null)
                {
                    GUI.enabled = false;
                }
                bool bPrefabObject = GUILayout.Toggle(m_srcMesh == prefabObject, "Prefab Object");
                if (prefabObject != null)
                {
                    EditorGUILayout.ObjectField(prefabObject, typeof(GameObject), true);
                }
                else
                {
                    GUI.enabled = true;
                    if (GUILayout.Button("Create Prefab"))
                    {
                        string path = EditorUtility.SaveFilePanelInProject("Create Prefab", m_rootObject.name, "prefab", "Select a path for the prefab of " + m_rootObject.name);
                        if (!string.IsNullOrEmpty(path))
                        {
                            prefabObject = PrefabUtility.CreatePrefab(path, m_rootObject, ReplacePrefabOptions.ConnectToPrefab);
                            m_srcMesh    = prefabObject;
                            bSceneObject = false;
                        }
                    }
                }
                EditorGUILayout.EndHorizontal();
                if (bPrefabObject && m_srcMesh != prefabObject)
                {
                    m_srcMesh = prefabObject;
                }
                if (m_srcMesh == m_rootObject)
                {
                    EditorGUILayout.BeginHorizontal();
                    GUILayout.Space(30);
                    GUILayout.TextArea("<color=red>If you choose Scene Object, the reference to the object cannot be stored in Mesh Tree settings. So, you will need to set the reference again when you rebuild the mesh tree.</color>", m_errorStyle);
                    EditorGUILayout.EndHorizontal();
                }
                else if (m_srcMesh == prefabObject)
                {
                    // check if m_rootObject is modified from prefabObject.
                    for (int i = 0; i < srcMeshes.Length; ++i)
                    {
                        GameObject go     = srcMeshes[i].gameObject;
                        GameObject prefab = PrefabUtility.GetPrefabParent(go) as GameObject;
                        if (go.layer != prefab.layer)
                        {
                            EditorGUILayout.BeginHorizontal();
                            GUILayout.Space(30);
                            GUILayout.TextArea("<color=red>The layer of the scene object was modified from the prefab.</color>", m_errorStyle);
                            EditorGUILayout.ObjectField(go, typeof(GameObject), true);
                            EditorGUILayout.EndHorizontal();
                            break;
                        }
                        if (go.transform.localPosition != prefab.transform.localPosition || go.transform.localRotation != prefab.transform.localRotation || go.transform.localScale != prefab.transform.localScale)
                        {
                            EditorGUILayout.BeginHorizontal();
                            GUILayout.Space(30);
                            GUILayout.TextArea("<color=red>The transform of the scene object was modified from the prefab.</color>", m_errorStyle);
                            EditorGUILayout.ObjectField(go, typeof(GameObject), true);
                            EditorGUILayout.EndHorizontal();
                            break;
                        }
                    }
                }
            }

            EditorGUILayout.Separator();

            EditorGUILayout.BeginHorizontal();
            GUILayout.FlexibleSpace();
            if (GUILayout.Button("Create Mesh Tree"))
            {
                string path = GetDefaultPathFromSourceMesh(m_srcMesh, m_bBinaryTree);
                path = EditorUtility.SaveFilePanelInProject("Create New Mesh Tree", System.IO.Path.GetFileName(path), "asset", "Select a path for the new Mesh Tree", System.IO.Path.GetDirectoryName(path));
                if (!string.IsNullOrEmpty(path))
                {
                    MeshTreeBase meshTreeBase;
                    if (m_srcMesh is TerrainData)
                    {
                        TerrainMeshTree meshTree = ScriptableObject.CreateInstance <TerrainMeshTree>();
                        meshTree.terrainData = m_srcMesh as TerrainData;
                        meshTreeBase         = meshTree;
                    }
                    else if (m_bBinaryTree)
                    {
                        BinaryMeshTree meshTree = ScriptableObject.CreateInstance <BinaryMeshTree>();
                        meshTree.srcMesh      = m_srcMesh;
                        meshTree.layerMask    = m_layers;
                        meshTree.scaledOffset = 1.0f;
                        meshTreeBase          = meshTree;
                    }
                    else
                    {
                        OctMeshTree meshTree = ScriptableObject.CreateInstance <OctMeshTree>();
                        meshTree.srcMesh      = m_srcMesh;
                        meshTree.layerMask    = m_layers;
                        meshTree.scaledOffset = 1.0f;
                        meshTreeBase          = meshTree;
                    }
                    AssetDatabase.CreateAsset(meshTreeBase, path);
                    AssetDatabase.Refresh();
                    m_meshTree = AssetDatabase.LoadAssetAtPath(path, typeof(MeshTreeBase)) as MeshTreeBase;
                    if (m_receiver != null)
                    {
                        m_receiver.meshTree = m_meshTree;
                    }
                    if (m_srcMesh is TerrainData)
                    {
                        // just build and close. because TerrainMeshTree doesn't have build options.
                        MeshTreeEditor.BuildMeshTree(m_meshTree);
                        Close();
                    }
                    else
                    {
                        m_serializedObject = new SerializedObject(m_meshTree);
                        ++m_currentPage;
                    }
                }
            }
        }