Esempio n. 1
0
        public static int GetUniqueId(this DaydreamVertexLighting comp)
        {
            MeshFilter   filter   = comp.GetComponent <MeshFilter>();
            MeshRenderer renderer = comp.GetComponent <MeshRenderer>();
            Vector3      center   = renderer != null ? renderer.bounds.center : Vector3.zero;
            string       id       = string.Format("{0}|{1}{2}{3}"
                                                  , filter != null ? filter.sharedMesh.name : "missing_mesh"
                                                  , center.x
                                                  , center.y
                                                  , center.z);

            return(id.GetHashCode());
        }
        public void OnEnable()
        {
            DaydreamVertexLighting source     = target as DaydreamVertexLighting;
            MeshFilter             meshFilter = source.GetComponent <MeshFilter>();

            Init(meshFilter);
        }
        public void OnEnable()
        {
            DaydreamVertexLighting source     = target as DaydreamVertexLighting;
            MeshFilter             meshFilter = source.GetComponent <MeshFilter>();
            Mesh untessellatedMesh            = source.GetUntessellatedMesh();

            Init(meshFilter, untessellatedMesh);
        }
        //private static void BuildWorldVertices(DaydreamVertexLighting source)
        //{
        //    s_debugState.m_worldVerPos = source.m_sourceMesh.vertices;
        //    s_debugState.m_worldNormals = source.m_sourceMesh.normals;
        //    for (int i = 0; i < s_debugState.m_worldNormals.Length; ++i)
        //    {
        //        s_debugState.m_worldVerPos[i] = source.transform.TransformPoint(s_debugState.m_worldVerPos[i]);
        //        s_debugState.m_worldNormals[i] = source.transform.TransformVector(s_debugState.m_worldNormals[i]).normalized;
        //    }
        //}

        #region Test Methods
        public void TestBuildBVHNative(DaydreamVertexLighting source)
        {
            DateTime   start      = DateTime.Now;
            MeshFilter meshFilter = source.GetComponent <MeshFilter>();

            if (meshFilter != null)
            {
                VertexBakerLib.Instance.BuildBVH(new MeshFilter[] { meshFilter });;
            }

            VertexBakerLib.Log("Seconds to complete: " + (DateTime.Now - start).TotalSeconds);
        }
        public void RefreshVisuals(DaydreamVertexLighting source)
        {
            s_bvhWrapper.UnLoad();
            s_cacheWrapper.UnLoad();

            MeshFilter meshFilter = source.GetComponent <MeshFilter>();
            string     filepath   = BVH.ConvertMeshIdToBVHPath(meshFilter.GetUniqueId());

            s_bvhWrapper.SetPath(filepath);
            s_bvhWrapper.Validate();

            s_cacheWrapper.SetPath(filepath);
            s_cacheWrapper.Validate();
        }
        public void TestLoadBVH(DaydreamVertexLighting source)
        {
            DateTime   start      = DateTime.Now;
            MeshFilter meshFilter = source.GetComponent <MeshFilter>();

            if (meshFilter != null)
            {
                try
                {
                    VertexBakerLib.BVHHandle bvhHandle = null;
                    if (VertexBakerLib.Instance.LoadBVH(meshFilter, ref bvhHandle))
                    {
                        VertexBakerLib.Instance.FreeHandle(bvhHandle.Ptr());
                    }
                }
                catch (Exception e)
                {
                    Debug.LogError(e.Message + "\n" + e.StackTrace);
                }
            }

            VertexBakerLib.Log("Seconds to complete: " + (DateTime.Now - start).TotalSeconds);
        }
        static void DrawBakerGizmos(DaydreamVertexLighting source, GizmoType gizmoType)
        {
            if (s_bvhHandle != null && !s_bvhHandle.IsValid())
            {
                TryLoadBVH(source.GetComponent <MeshFilter>());
            }

            if (s_bvhWrapper.IsValid() && s_cacheWrapper.IsValid() && s_bvhHandle.IsValid())
            {
                if (s_debugState.m_worldVerPos == null || s_debugState.m_worldVerPos.Length == 0)
                {
                    BuildWorldVertices(source.GetComponent <MeshFilter>());
                }

                // update selection
                if (Event.current != null && s_inputUpdate)
                {
                    s_inputUpdate = false;
                    Ray   ray      = HandleUtility.GUIPointToWorldRay(s_position);
                    float farPlane = 10000.0f;
                    if (Camera.main != null)
                    {
                        farPlane = Camera.main.farClipPlane;
                    }
                    int index = VertexBakerLib.Instance.RayToIndex(s_bvhHandle, ray.origin, ray.direction * farPlane);
                    if (index < 0)
                    {
                        return;
                    }
                    s_currentIdx = index;
                }

                if (s_currentIdx + s_debugState.m_indexOffset < 0)
                {
                    return;
                }

                int idx = (s_currentIdx + s_debugState.m_indexOffset) % s_debugState.m_worldVerPos.Length;

                s_debugState.m_vertexSampleIndex = idx;

                float patchScale   = Mathf.Max(source.transform.localToWorldMatrix.m00, source.transform.localToWorldMatrix.m11, source.transform.localToWorldMatrix.m22);
                float radius       = s_cacheWrapper.GetPatchRadius(idx);
                float sampleRadius = radius * patchScale;

                if (s_debugState.m_showBVH)
                {
                    List <Bounds> bounds = s_bvhWrapper.GetBounds();

                    foreach (Bounds b in bounds)
                    {
                        Gizmos.DrawWireCube(b.center, b.size);
                    }
                }

                if (s_debugState.m_showSamplePatch)
                {
                    Color c = Color.green;
                    PushGizmoColor(c);
                    Gizmos.DrawWireSphere(s_debugState.m_worldVerPos[idx], sampleRadius);

                    Vector3[] corners = s_bvhWrapper.GetPatchCorners(idx);

                    PushGizmoColor(Color.blue);
                    Vector3 n = s_debugState.m_worldNormals[idx] * 0.07f;
                    Gizmos.DrawLine(n + corners[0], n + corners[1]); // TL->TR
                    Gizmos.DrawLine(n + corners[1], n + corners[2]); // TR->BR
                    Gizmos.DrawLine(n + corners[2], n + corners[3]); // BR->BL
                    Gizmos.DrawLine(n + corners[3], n + corners[0]); // BL->TL
                    PopGizmoColor();

                    PopGizmoColor();
                }

                if (s_debugState.m_showAOSamples)
                {
                    UpdateAOSample(source, idx);
                    if (s_occluderTestData.m_testPoints.Count > 0)
                    {
                        PushGizmoColor(Color.cyan);
                        Gizmos.DrawWireSphere(s_debugState.m_worldVerPos[idx], s_cacheWrapper.GetPatchRadius(idx) * BakeData.Instance().GetBakeSettings().SelectedBakeSet.m_occlusionRayLength);
                        PushGizmoColor(Color.green * 1.5f);
                        Gizmos.DrawSphere(s_debugState.m_worldVerPos[idx], 0.01f);
                        PopGizmoColor();
                        PopGizmoColor();

                        PushGizmoColor(Color.green + Color.cyan * 0.5f);
                        for (int i = 0; i < s_occluderTestData.m_testPoints.Count; ++i)
                        {
                            Gizmos.DrawSphere(s_occluderTestData.m_testPoints[i], sampleRadius * 0.1F);
                        }
                        PopGizmoColor();
                        PushGizmoColor(Color.red);
                        for (int i = 0; i < s_occluderTestData.m_colPoints.Count; ++i)
                        {
                            Gizmos.DrawSphere(s_occluderTestData.m_colPoints[i], sampleRadius * 0.1F);
                        }
                        PopGizmoColor();

                        PushGizmoColor(new Color(1f, 0f, 1f));
                        Gizmos.DrawLine(s_debugState.m_worldVerPos[idx], s_debugState.m_worldVerPos[idx] + s_occluderTestData.m_accumulatedRayDir);
                        PopGizmoColor();

                        s_debugState.m_accessability = s_occluderTestData.m_accessability;
                    }
                }

                if (s_debugState.m_showAdjacencies)
                {
                    List <int> adj = s_cacheWrapper.GetAdjacencies(idx);
                    PushGizmoColor(Color.cyan);
                    Gizmos.DrawSphere(s_debugState.m_worldVerPos[idx], SphereSize(sampleRadius));
                    PopGizmoColor();

                    PushGizmoColor(Color.red);
                    for (int i = 0; i < adj.Count; ++i)
                    {
                        if (s_debugState.m_worldVerPos.Length > adj[i])
                        {
                            Gizmos.DrawSphere(s_debugState.m_worldVerPos[adj[i]], SphereSize(sampleRadius));
                        }
                    }
                    PopGizmoColor();
                }

                if (s_debugState.m_showLightBlockerSamples)
                {
                    UpdateLightBlockerSamplers(source, idx);
                }

                if (s_debugState.m_showNormals)
                {
                    PushGizmoColor(Color.green);
                    Gizmos.DrawLine(s_debugState.m_worldVerPos[idx], s_debugState.m_worldVerPos[idx] + s_debugState.m_worldNormals[idx]);
                    if (s_debugState.m_showAdjacencies)
                    {
                        List <int> adj = s_cacheWrapper.GetAdjacencies(idx);
                        for (int i = 0; i < adj.Count; ++i)
                        {
                            Gizmos.DrawLine(s_debugState.m_worldVerPos[adj[i]], s_debugState.m_worldVerPos[adj[i]] + s_debugState.m_worldNormals[idx]);
                        }
                    }
                    PopGizmoColor();
                }

                if (s_debugState.m_showBentNormalSamples)
                {
                    List <int> adj = s_cacheWrapper.GetAdjacencies(idx);

                    PushGizmoColor(Color.red);
                    Vector3 pos  = s_debugState.m_worldVerPos[idx];
                    Vector3 norm = s_cacheWrapper.GetBentNormal(idx);
                    // normal move to world space
                    norm = source.transform.TransformVector(norm).normalized;

                    Gizmos.DrawLine(pos, pos + norm);

                    for (int i = 0; i < adj.Count; ++i)
                    {
                        pos  = s_debugState.m_worldVerPos[adj[i]];
                        norm = s_cacheWrapper.GetBentNormal(adj[i]);
                        // normal move to world space
                        norm = source.transform.TransformVector(norm).normalized;

                        Gizmos.DrawLine(pos, pos + norm);
                    }
                    PopGizmoColor();
                }
            }
        }
        static void DrawStaticRevertMaterials()
        {
            int convertedCount = s_staticMaterialHistory.m_convertedMaterials != null ? s_staticMaterialHistory.m_convertedMaterials.Count : 0;

            if (convertedCount == 0)
            {
                return;
            }

            // remove anything flagged for revert
            foreach (DaydreamRendererMaterialHistory.Entry sm in m_removeList)
            {
                s_staticMaterialHistory.m_convertedMaterials.Remove(sm);
            }
            m_removeList.Clear();

            foreach (DaydreamRendererMaterialHistory.Entry sm in m_removeSplitList)
            {
                s_staticMaterialHistory.m_splitMaterials.Remove(sm);
                s_listContainerStaticMats.Update();
            }
            m_removeSplitList.Clear();

            // Draw list of converted materials
            //s_listContainerStaticMats = new SerializedObject(s_staticMaterialHistory);
            s_listContainerStaticMats.Update();
            SerializedProperty convertList = s_listContainerStaticMats.FindProperty("m_convertedMaterials");

            EditorGUILayout.Separator();
            EditorGUILayout.LabelField(String.Format(Styles.kStaticConvertedMaterialListFrmt, convertList.arraySize));
            EditorGUILayout.BeginVertical();
            EditorGUIUtility.labelWidth = 75f;

            m_scrollPosConverted = DrawRevertList(m_scrollPosConverted, convertList, 400, delegate(SerializedProperty matHistoryEntry, int elIndex)
            {
                // OnRevert Callback

                // trigger refresh of metrics
                s_gatherMetrics = true;

                Material main   = matHistoryEntry.FindPropertyRelative("m_material").objectReferenceValue as Material;
                Material backup = matHistoryEntry.FindPropertyRelative("m_backupMaterial").objectReferenceValue as Material;

                main.shader = backup.shader;
                main.CopyPropertiesFromMaterial(backup);
                EditorUtility.SetDirty(main);
                AssetDatabase.SaveAssets();

                List <GameObject> revertSites = FindAllMaterialSites(main.name);
                foreach (GameObject go in revertSites)
                {
                    DaydreamVertexLighting dvl = go.GetComponent <DaydreamVertexLighting>();
                    if (dvl != null)
                    {
                        MeshRenderer mr = dvl.GetComponent <MeshRenderer>();
                        if (mr != null)
                        {
                            mr.additionalVertexStreams = null;
                        }
                        DestroyImmediate(dvl);
                    }
                }
                m_removeList.Add(s_staticMaterialHistory.m_convertedMaterials[elIndex]);
            });

            SerializedProperty splitList = s_listContainerStaticMats.FindProperty("m_splitMaterials");

            if (splitList.arraySize > 0)
            {
                EditorGUILayout.HelpBox(String.Format(Styles.kStaticSplitMaterialListFrmt, splitList.arraySize), MessageType.Info);
            }
            // Revert list for split materials
            m_scrollPosSplit = DrawRevertList(m_scrollPosSplit, splitList, 400, delegate(SerializedProperty matHistoryEntry, int elIndex)
            {
                // OnRevert Callback

                // trigger refresh of metrics
                s_gatherMetrics = true;

                Material main = matHistoryEntry.FindPropertyRelative("m_material").objectReferenceValue as Material;

                Material split = matHistoryEntry.FindPropertyRelative("m_splitMaterial").objectReferenceValue as Material;
                string path    = matHistoryEntry.FindPropertyRelative("m_sourceScenePath").stringValue;

                List <GameObject> gos = Utilities.FindAll(path);
                foreach (GameObject go in gos)
                {
                    if (go != null)
                    {
                        MeshRenderer mr = go.GetComponent <MeshRenderer>();
                        if (mr != null)
                        {
                            Material[] mats = mr.sharedMaterials;
                            //bool restored = false;
                            for (int midx = 0, k = mats.Length; midx < k; ++midx)
                            {
                                if (mr.sharedMaterials[midx].name == split.name)
                                {
                                    // restore the material
                                    //restored = true;
                                    mats[midx] = main;
                                    EditorUtility.SetDirty(mr);
                                    AssetDatabase.SaveAssets();
                                }
                            }

                            mr.sharedMaterials = mats;
                            //if (restored)
                            {
                                m_removeSplitList.Add(s_staticMaterialHistory.m_splitMaterials[elIndex]);

                                //// remove daydream component
                                //DaydreamVertexLighting dvl = mr.GetComponent<DaydreamVertexLighting>();
                                //if (dvl != null)
                                //{
                                //    // remove vertex stream
                                //    mr.additionalVertexStreams = null;
                                //    DestroyImmediate(dvl);
                                //}
                            }
                        }
                    }
                }
            },
                                              // draw extra properties of the material-history serialized object
                                              new string[]
            {
                "m_sourceScenePath",
            }
                                              );// end function call

            EditorGUILayout.EndVertical();
        }