Exemple #1
0
        public static void InitializeDirections()
        {
            float num = (float)((1.0 + (double)Mathf.Sqrt(5f)) / 2.0);

            TreeAO.directions     = new Vector3[60];
            TreeAO.directions[0]  = new Vector3(0.0f, 1f, 3f * num);
            TreeAO.directions[1]  = new Vector3(0.0f, 1f, -3f * num);
            TreeAO.directions[2]  = new Vector3(0.0f, -1f, 3f * num);
            TreeAO.directions[3]  = new Vector3(0.0f, -1f, -3f * num);
            TreeAO.directions[4]  = new Vector3(1f, 3f * num, 0.0f);
            TreeAO.directions[5]  = new Vector3(1f, -3f * num, 0.0f);
            TreeAO.directions[6]  = new Vector3(-1f, 3f * num, 0.0f);
            TreeAO.directions[7]  = new Vector3(-1f, -3f * num, 0.0f);
            TreeAO.directions[8]  = new Vector3(3f * num, 0.0f, 1f);
            TreeAO.directions[9]  = new Vector3(3f * num, 0.0f, -1f);
            TreeAO.directions[10] = new Vector3(-3f * num, 0.0f, 1f);
            TreeAO.directions[11] = new Vector3(-3f * num, 0.0f, -1f);
            int offset1 = 12;
            int offset2 = TreeAO.PermuteCuboid(TreeAO.directions, offset1, 2f, (float)(1.0 + 2.0 * (double)num), num);
            int offset3 = TreeAO.PermuteCuboid(TreeAO.directions, offset2, (float)(1.0 + 2.0 * (double)num), num, 2f);
            int offset4 = TreeAO.PermuteCuboid(TreeAO.directions, offset3, num, 2f, (float)(1.0 + 2.0 * (double)num));
            int offset5 = TreeAO.PermuteCuboid(TreeAO.directions, offset4, 1f, 2f + num, 2f * num);
            int offset6 = TreeAO.PermuteCuboid(TreeAO.directions, offset5, 2f + num, 2f * num, 1f);

            TreeAO.PermuteCuboid(TreeAO.directions, offset6, 2f * num, 1f, 2f + num);
            for (int index = 0; index < TreeAO.directions.Length; ++index)
            {
                TreeAO.directions[index] = TreeAO.directions[index].normalized;
            }
        }
Exemple #2
0
        public static void InitializeDirections()
        {
            float num = (1f + Mathf.Sqrt(5f)) / 2f;

            TreeAO.directions     = new Vector3[60];
            TreeAO.directions[0]  = new Vector3(0f, 1f, 3f * num);
            TreeAO.directions[1]  = new Vector3(0f, 1f, -3f * num);
            TreeAO.directions[2]  = new Vector3(0f, -1f, 3f * num);
            TreeAO.directions[3]  = new Vector3(0f, -1f, -3f * num);
            TreeAO.directions[4]  = new Vector3(1f, 3f * num, 0f);
            TreeAO.directions[5]  = new Vector3(1f, -3f * num, 0f);
            TreeAO.directions[6]  = new Vector3(-1f, 3f * num, 0f);
            TreeAO.directions[7]  = new Vector3(-1f, -3f * num, 0f);
            TreeAO.directions[8]  = new Vector3(3f * num, 0f, 1f);
            TreeAO.directions[9]  = new Vector3(3f * num, 0f, -1f);
            TreeAO.directions[10] = new Vector3(-3f * num, 0f, 1f);
            TreeAO.directions[11] = new Vector3(-3f * num, 0f, -1f);
            int offset = 12;

            offset = TreeAO.PermuteCuboid(TreeAO.directions, offset, 2f, 1f + 2f * num, num);
            offset = TreeAO.PermuteCuboid(TreeAO.directions, offset, 1f + 2f * num, num, 2f);
            offset = TreeAO.PermuteCuboid(TreeAO.directions, offset, num, 2f, 1f + 2f * num);
            offset = TreeAO.PermuteCuboid(TreeAO.directions, offset, 1f, 2f + num, 2f * num);
            offset = TreeAO.PermuteCuboid(TreeAO.directions, offset, 2f + num, 2f * num, 1f);
            offset = TreeAO.PermuteCuboid(TreeAO.directions, offset, 2f * num, 1f, 2f + num);
            for (int i = 0; i < TreeAO.directions.Length; i++)
            {
                TreeAO.directions[i] = TreeAO.directions[i].normalized;
            }
        }
        void OnPostprocessModel(GameObject root)
        {
            // Check if path contains "AO Tree"
            string lowerPath = assetPath.ToLower();

            if (lowerPath.IndexOf("ambient-occlusion") != -1)
            {
                var physicsScene = root.scene.GetPhysicsScene();

                Component[] filters = root.GetComponentsInChildren(typeof(MeshFilter));
                foreach (MeshFilter filter in filters)
                {
                    if (filter.sharedMesh != null)
                    {
                        Mesh mesh = filter.sharedMesh;

                        // Calculate AO
                        TreeAO.CalcSoftOcclusion(mesh, physicsScene);

                        // Calculate vertex colors for tree waving
                        Bounds    bounds   = mesh.bounds;
                        Color[]   colors   = mesh.colors;
                        Vector3[] vertices = mesh.vertices;
                        Vector4[] tangents = mesh.tangents;
                        if (colors.Length == 0)
                        {
                            colors = new Color[mesh.vertexCount];
                            for (int i = 0; i < colors.Length; i++)
                            {
                                colors[i] = Color.white;
                            }
                        }

                        float maxAO = 0.0F;
                        for (int i = 0; i < tangents.Length; i++)
                        {
                            maxAO = Mathf.Max(tangents[i].w, maxAO);
                        }

                        float largest = 0.0F;
                        for (int i = 0; i < colors.Length; i++)
                        {
                            Vector2 offset = new Vector2(vertices[i].x, vertices[i].z);
                            float   branch = offset.magnitude;
                            largest = Mathf.Max(branch, largest);
                        }
                        for (int i = 0; i < colors.Length; i++)
                        {
                            Vector2 offset = new Vector2(vertices[i].x, vertices[i].z);
                            float   branch = offset.magnitude / largest;

                            float height = (vertices[i].y - bounds.min.y) / bounds.size.y;
                            //                  colors[i].a = tangents[i].w * maxAO + height;
                            colors[i].a = (height * branch) * 0.6F + height * 0.5F;
                        }
                        mesh.colors = colors;
                    }
                }
            }
        }
        private void OnPostprocessModel(GameObject root)
        {
            string text = base.assetPath.ToLower();

            if (text.IndexOf("ambient-occlusion") != -1)
            {
                Component[] componentsInChildren = root.GetComponentsInChildren(typeof(MeshFilter));
                Component[] array = componentsInChildren;
                for (int i = 0; i < array.Length; i++)
                {
                    MeshFilter meshFilter = (MeshFilter)array[i];
                    if (meshFilter.sharedMesh != null)
                    {
                        Mesh sharedMesh = meshFilter.sharedMesh;
                        TreeAO.CalcSoftOcclusion(sharedMesh);
                        Bounds    bounds   = sharedMesh.bounds;
                        Color[]   array2   = sharedMesh.colors;
                        Vector3[] vertices = sharedMesh.vertices;
                        Vector4[] tangents = sharedMesh.tangents;
                        if (array2.Length == 0)
                        {
                            array2 = new Color[sharedMesh.vertexCount];
                            for (int j = 0; j < array2.Length; j++)
                            {
                                array2[j] = Color.white;
                            }
                        }
                        float b = 0f;
                        for (int k = 0; k < tangents.Length; k++)
                        {
                            b = Mathf.Max(tangents[k].w, b);
                        }
                        float num = 0f;
                        for (int l = 0; l < array2.Length; l++)
                        {
                            Vector2 vector    = new Vector2(vertices[l].x, vertices[l].z);
                            float   magnitude = vector.magnitude;
                            num = Mathf.Max(magnitude, num);
                        }
                        for (int m = 0; m < array2.Length; m++)
                        {
                            Vector2 vector2 = new Vector2(vertices[m].x, vertices[m].z);
                            float   num2    = vector2.magnitude / num;
                            float   num3    = (vertices[m].y - bounds.min.y) / bounds.size.y;
                            array2[m].a = num3 * num2 * 0.6f + num3 * 0.5f;
                        }
                        sharedMesh.colors = array2;
                    }
                }
            }
        }
Exemple #5
0
        public static void CalcSoftOcclusion(Mesh mesh)
        {
            GameObject gameObject = new GameObject("Test");

            gameObject.layer = 29;
            MeshFilter meshFilter = gameObject.AddComponent <MeshFilter>();

            meshFilter.mesh = mesh;
            gameObject.AddComponent <MeshCollider>();
            if (TreeAO.directions == null)
            {
                TreeAO.InitializeDirections();
            }
            Vector4[] array = new Vector4[TreeAO.directions.Length];
            for (int i = 0; i < TreeAO.directions.Length; i++)
            {
                array[i] = new Vector4(TreeAO.GetWeight(1, TreeAO.directions[i]), TreeAO.GetWeight(2, TreeAO.directions[i]), TreeAO.GetWeight(3, TreeAO.directions[i]), TreeAO.GetWeight(0, TreeAO.directions[i]));
            }
            Vector3[] vertices = mesh.vertices;
            Vector4[] array2   = new Vector4[vertices.Length];
            float     num      = 0f;

            for (int j = 0; j < vertices.Length; j++)
            {
                Vector4 vector = Vector4.zero;
                Vector3 v      = gameObject.transform.TransformPoint(vertices[j]);
                for (int k = 0; k < TreeAO.directions.Length; k++)
                {
                    float num2 = (float)TreeAO.CountIntersections(v, gameObject.transform.TransformDirection(TreeAO.directions[k]), 3f);
                    num2    = Mathf.Pow(0.5f, num2);
                    vector += array[k] * num2;
                }
                vector   /= (float)TreeAO.directions.Length;
                num      += vector.w;
                array2[j] = vector;
            }
            num /= (float)vertices.Length;
            for (int l = 0; l < vertices.Length; l++)
            {
                Vector4[] expr_1D4_cp_0 = array2;
                int       expr_1D4_cp_1 = l;
                expr_1D4_cp_0[expr_1D4_cp_1].w = expr_1D4_cp_0[expr_1D4_cp_1].w - num;
            }
            mesh.tangents = array2;
            UnityEngine.Object.DestroyImmediate(gameObject);
        }
 private void OnPostprocessModel(GameObject root)
 {
     if (base.assetPath.ToLower().IndexOf("ambient-occlusion") != -1)
     {
         Component[] componentsInChildren = root.GetComponentsInChildren(typeof(MeshFilter));
         foreach (MeshFilter filter in componentsInChildren)
         {
             if (filter.sharedMesh != null)
             {
                 Mesh sharedMesh = filter.sharedMesh;
                 TreeAO.CalcSoftOcclusion(sharedMesh);
                 Bounds    bounds   = sharedMesh.bounds;
                 Color[]   colors   = sharedMesh.colors;
                 Vector3[] vertices = sharedMesh.vertices;
                 Vector4[] tangents = sharedMesh.tangents;
                 if (colors.Length == 0)
                 {
                     colors = new Color[sharedMesh.vertexCount];
                     for (int m = 0; m < colors.Length; m++)
                     {
                         colors[m] = Color.white;
                     }
                 }
                 float b = 0f;
                 for (int i = 0; i < tangents.Length; i++)
                 {
                     b = Mathf.Max(tangents[i].w, b);
                 }
                 float num5 = 0f;
                 for (int j = 0; j < colors.Length; j++)
                 {
                     Vector2 vector = new Vector2(vertices[j].x, vertices[j].z);
                     num5 = Mathf.Max(vector.magnitude, num5);
                 }
                 for (int k = 0; k < colors.Length; k++)
                 {
                     Vector2 vector2 = new Vector2(vertices[k].x, vertices[k].z);
                     float   num9    = vector2.magnitude / num5;
                     float   num10   = (vertices[k].y - bounds.min.y) / bounds.size.y;
                     colors[k].a = ((num10 * num9) * 0.6f) + (num10 * 0.5f);
                 }
                 sharedMesh.colors = colors;
             }
         }
     }
 }
Exemple #7
0
 private void OnPostprocessModel(GameObject root)
 {
     if (this.assetPath.ToLower().IndexOf("ambient-occlusion") == -1)
     {
         return;
     }
     foreach (MeshFilter componentsInChild in root.GetComponentsInChildren(typeof(MeshFilter)))
     {
         if ((Object)componentsInChild.sharedMesh != (Object)null)
         {
             Mesh sharedMesh = componentsInChild.sharedMesh;
             TreeAO.CalcSoftOcclusion(sharedMesh);
             Bounds    bounds     = sharedMesh.bounds;
             Color[]   colorArray = sharedMesh.colors;
             Vector3[] vertices   = sharedMesh.vertices;
             Vector4[] tangents   = sharedMesh.tangents;
             if (colorArray.Length == 0)
             {
                 colorArray = new Color[sharedMesh.vertexCount];
                 for (int index = 0; index < colorArray.Length; ++index)
                 {
                     colorArray[index] = Color.white;
                 }
             }
             float b1 = 0.0f;
             for (int index = 0; index < tangents.Length; ++index)
             {
                 b1 = Mathf.Max(tangents[index].w, b1);
             }
             float b2 = 0.0f;
             for (int index = 0; index < colorArray.Length; ++index)
             {
                 b2 = Mathf.Max(new Vector2(vertices[index].x, vertices[index].z).magnitude, b2);
             }
             for (int index = 0; index < colorArray.Length; ++index)
             {
                 float num1 = new Vector2(vertices[index].x, vertices[index].z).magnitude / b2;
                 float num2 = (vertices[index].y - bounds.min.y) / bounds.size.y;
                 colorArray[index].a = (float)((double)num2 * (double)num1 * 0.600000023841858 + (double)num2 * 0.5);
             }
             sharedMesh.colors = colorArray;
         }
     }
 }
Exemple #8
0
        public static void CalcSoftOcclusion(Mesh mesh)
        {
            GameObject gameObject = new GameObject("Test");

            gameObject.layer = 29;
            gameObject.AddComponent <MeshFilter>().mesh = mesh;
            gameObject.AddComponent <MeshCollider>();
            if (TreeAO.directions == null)
            {
                TreeAO.InitializeDirections();
            }
            Vector4[] vector4Array1 = new Vector4[TreeAO.directions.Length];
            for (int index = 0; index < TreeAO.directions.Length; ++index)
            {
                vector4Array1[index] = new Vector4(TreeAO.GetWeight(1, TreeAO.directions[index]), TreeAO.GetWeight(2, TreeAO.directions[index]), TreeAO.GetWeight(3, TreeAO.directions[index]), TreeAO.GetWeight(0, TreeAO.directions[index]));
            }
            Vector3[] vertices      = mesh.vertices;
            Vector4[] vector4Array2 = new Vector4[vertices.Length];
            float     num1          = 0.0f;

            for (int index1 = 0; index1 < vertices.Length; ++index1)
            {
                Vector4 zero = Vector4.zero;
                Vector3 v    = gameObject.transform.TransformPoint(vertices[index1]);
                for (int index2 = 0; index2 < TreeAO.directions.Length; ++index2)
                {
                    float num2 = Mathf.Pow(0.5f, (float)TreeAO.CountIntersections(v, gameObject.transform.TransformDirection(TreeAO.directions[index2]), 3f));
                    zero += vector4Array1[index2] * num2;
                }
                zero /= (float)TreeAO.directions.Length;
                num1 += zero.w;
                vector4Array2[index1] = zero;
            }
            float num3 = num1 / (float)vertices.Length;

            for (int index = 0; index < vertices.Length; ++index)
            {
                vector4Array2[index].w -= num3;
            }
            mesh.tangents = vector4Array2;
            Object.DestroyImmediate((Object)gameObject);
        }