Exemplo n.º 1
0
			public static bool Overlap(TextureAtlas.TextureNode a, TextureAtlas.TextureNode b)
			{
				if (a.tileV || b.tileV)
				{
					return a.packedRect.x <= b.packedRect.x + b.packedRect.width && a.packedRect.x + a.packedRect.width >= b.packedRect.x;
				}
				return a.packedRect.x <= b.packedRect.x + b.packedRect.width && a.packedRect.x + a.packedRect.width >= b.packedRect.x && a.packedRect.y <= b.packedRect.y + b.packedRect.height && a.packedRect.y + a.packedRect.height >= b.packedRect.y;
			}
Exemplo n.º 2
0
			public int CompareTo(TextureAtlas.TextureNode b)
			{
				if (this.tileV && b.tileV)
				{
					return -this.packedRect.width.CompareTo(b.packedRect.width);
				}
				if (this.tileV)
				{
					return -1;
				}
				if (b.tileV)
				{
					return 1;
				}
				return -this.packedRect.height.CompareTo(b.packedRect.height);
			}
Exemplo n.º 3
0
        private bool UpdateTextures(TextureAtlas atlas, List <TreeMaterial> materials)
        {
            if (!this.root.enableMaterialOptimize)
            {
                return(false);
            }
            bool flag = this.optimizedSolidMaterial.GetTexture("_MainTex") != null && this.optimizedSolidMaterial.GetTexture("_BumpSpecMap") != null && this.optimizedSolidMaterial.GetTexture("_TranslucencyMap") != null && this.optimizedCutoutMaterial.GetTexture("_MainTex") != null && this.optimizedCutoutMaterial.GetTexture("_ShadowTex") != null && this.optimizedCutoutMaterial.GetTexture("_BumpSpecMap") != null && this.optimizedCutoutMaterial.GetTexture("_TranslucencyMap");

            UnityEngine.Object[] array = new UnityEngine.Object[materials.Count];
            for (int i = 0; i < materials.Count; i++)
            {
                array[i] = materials[i].material;
            }
            string text = InternalEditorUtility.CalculateHashForObjectsAndDependencies(array);

            text += atlas.GetHashCode();
            if (this.materialHash == text && flag)
            {
                this.UpdateShadowTexture(this.optimizedCutoutMaterial.GetTexture("_ShadowTex") as Texture2D, atlas.atlasWidth, atlas.atlasHeight);
                return(false);
            }
            this.materialHash = text;
            int       atlasWidth   = atlas.atlasWidth;
            int       atlasHeight  = atlas.atlasHeight;
            int       atlasPadding = atlas.atlasPadding;
            Texture2D texture2D    = new Texture2D(atlasWidth, atlasHeight, TextureFormat.ARGB32, true);
            Texture2D texture2D2   = new Texture2D(atlasWidth, atlasHeight, TextureFormat.RGB24, true);
            Texture2D texture2D3   = new Texture2D(atlasWidth, atlasHeight, TextureFormat.ARGB32, true);
            Texture2D texture2D4   = new Texture2D(atlasWidth, atlasHeight, TextureFormat.ARGB32, true);

            texture2D.name  = "diffuse";
            texture2D2.name = "shadow";
            texture2D3.name = "normal_specular";
            texture2D4.name = "translucency_gloss";
            SavedRenderTargetState savedRenderTargetState = new SavedRenderTargetState();

            EditorUtility.SetTemporarilyAllowIndieRenderTexture(true);
            RenderTexture temporary  = RenderTexture.GetTemporary(atlasWidth, atlasHeight, 0, RenderTextureFormat.ARGB32);
            Color         white      = Color.white;
            Color         color      = new Color(0.03f, 0.5f, 0f, 0.5f);
            Color         color2     = new Color(0f, 0f, 0f, 0f);
            Texture2D     texture2D5 = new Texture2D(1, 1);

            texture2D5.SetPixel(0, 0, white);
            texture2D5.Apply();
            Texture2D texture2D6 = new Texture2D(1, 1);

            texture2D6.SetPixel(0, 0, white);
            texture2D6.Apply();
            Texture2D texture2D7 = new Texture2D(1, 1);

            texture2D7.SetPixel(0, 0, color);
            texture2D7.Apply();
            Texture2D texture2D8 = new Texture2D(1, 1);

            texture2D8.SetPixel(0, 0, color2);
            texture2D8.Apply();
            Texture2D texture2D9  = texture2D8;
            Texture2D texture2D10 = new Texture2D(1, 1);

            texture2D10.SetPixel(0, 0, Color.white);
            texture2D10.Apply();
            Material material = EditorGUIUtility.LoadRequired("Inspectors/TreeCreator/TreeTextureCombinerMaterial.mat") as Material;

            for (int j = 0; j < 4; j++)
            {
                RenderTexture.active = temporary;
                GL.LoadPixelMatrix(0f, (float)atlasWidth, 0f, (float)atlasHeight);
                material.SetVector("_TexSize", new Vector4((float)atlasWidth, (float)atlasHeight, 0f, 0f));
                switch (j)
                {
                case 0:
                    GL.Clear(false, true, color);
                    break;

                case 1:
                    GL.Clear(false, true, color2);
                    break;

                case 2:
                    GL.Clear(false, true, color2);
                    break;

                case 3:
                    GL.Clear(false, true, color2);
                    break;
                }
                for (int k = 0; k < atlas.nodes.Count; k++)
                {
                    TextureAtlas.TextureNode textureNode = atlas.nodes[k];
                    Rect    packedRect = textureNode.packedRect;
                    Texture texture    = null;
                    Texture texture2   = null;
                    Color   color3     = default(Color);
                    switch (j)
                    {
                    case 0:
                        texture  = textureNode.normalTexture;
                        texture2 = textureNode.shadowOffsetTexture;
                        color3   = new Color(textureNode.shininess, 0f, 0f, 0f);
                        if (texture == null)
                        {
                            texture = texture2D7;
                        }
                        if (texture2 == null)
                        {
                            texture2 = texture2D9;
                        }
                        break;

                    case 1:
                        texture = textureNode.diffuseTexture;
                        color3  = textureNode.diffuseColor;
                        if (texture == null)
                        {
                            texture = texture2D5;
                        }
                        break;

                    case 2:
                        texture  = textureNode.translucencyTexture;
                        texture2 = textureNode.glossTexture;
                        if (texture == null)
                        {
                            texture = texture2D10;
                        }
                        if (texture2 == null)
                        {
                            texture2 = texture2D8;
                        }
                        break;

                    case 3:
                        texture2 = textureNode.diffuseTexture;
                        if (texture2 == null)
                        {
                            texture2 = texture2D5;
                        }
                        break;
                    }
                    if (textureNode.tileV)
                    {
                        float x   = packedRect.x;
                        float num = (float)atlasPadding / 2f;
                        for (float num2 = num; num2 > 0f; num2 -= 1f)
                        {
                            Rect rect  = new Rect(packedRect);
                            Rect rect2 = new Rect(packedRect);
                            rect.x  = x - num2;
                            rect2.x = x + num2;
                            this.DrawTexture(rect, texture, texture2, material, color3, j);
                            this.DrawTexture(rect2, texture, texture2, material, color3, j);
                        }
                    }
                    this.DrawTexture(packedRect, texture, texture2, material, color3, j);
                }
                switch (j)
                {
                case 0:
                    texture2D3.ReadPixels(new Rect(0f, 0f, (float)atlasWidth, (float)atlasHeight), 0, 0);
                    texture2D3.Apply(true);
                    break;

                case 1:
                    texture2D.ReadPixels(new Rect(0f, 0f, (float)atlasWidth, (float)atlasHeight), 0, 0);
                    texture2D.Apply(true);
                    break;

                case 2:
                    texture2D4.ReadPixels(new Rect(0f, 0f, (float)atlasWidth, (float)atlasHeight), 0, 0);
                    texture2D4.Apply(true);
                    break;

                case 3:
                    texture2D2.ReadPixels(new Rect(0f, 0f, (float)atlasWidth, (float)atlasHeight), 0, 0);
                    texture2D2.Apply(true);
                    break;
                }
            }
            savedRenderTargetState.Restore();
            this.optimizedSolidMaterial.SetPass(0);
            RenderTexture.ReleaseTemporary(temporary);
            UnityEngine.Object.DestroyImmediate(texture2D5);
            UnityEngine.Object.DestroyImmediate(texture2D6);
            UnityEngine.Object.DestroyImmediate(texture2D10);
            UnityEngine.Object.DestroyImmediate(texture2D8);
            UnityEngine.Object.DestroyImmediate(texture2D7);
            EditorUtility.SetTemporarilyAllowIndieRenderTexture(false);
            Texture2D[] array2 = new Texture2D[]
            {
                texture2D,
                texture2D3,
                texture2D4,
                texture2D2
            };
            array2 = TreeData.WriteOptimizedTextures(AssetDatabase.GetAssetPath(this), array2);
            UnityEngine.Object.DestroyImmediate(texture2D);
            UnityEngine.Object.DestroyImmediate(texture2D3);
            UnityEngine.Object.DestroyImmediate(texture2D4);
            UnityEngine.Object.DestroyImmediate(texture2D2);
            this.optimizedSolidMaterial.SetTexture("_MainTex", array2[0]);
            this.optimizedSolidMaterial.SetTexture("_BumpSpecMap", array2[1]);
            this.optimizedSolidMaterial.SetTexture("_TranslucencyMap", array2[2]);
            this.optimizedCutoutMaterial.SetTexture("_MainTex", array2[0]);
            this.optimizedCutoutMaterial.SetTexture("_BumpSpecMap", array2[1]);
            this.optimizedCutoutMaterial.SetTexture("_TranslucencyMap", array2[2]);
            this.optimizedCutoutMaterial.SetTexture("_ShadowTex", array2[3]);
            this.UpdateShadowTexture(array2[3], atlas.atlasWidth, atlas.atlasHeight);
            return(true);
        }
Exemplo n.º 4
0
        public bool OptimizeMaterial(List <TreeMaterial> materials, List <TreeVertex> vertices, List <TreeTriangle> triangles)
        {
            if (!this.optimizedSolidMaterial || !this.optimizedCutoutMaterial)
            {
                Debug.LogError("Optimized materials haven't been assigned");
                return(false);
            }
            Shader shader;
            Shader shader2;

            TreeData.ExtractOptimizedShaders(materials, out shader, out shader2);
            this.optimizedSolidMaterial.shader  = shader;
            this.optimizedCutoutMaterial.shader = shader2;
            int num     = 1024;
            int num2    = 1024;
            int padding = 32;

            Profiler.BeginSample("OptimizeMaterial");
            float[] array = new float[materials.Count];
            float   num3  = 0f;
            float   num4  = 0f;

            for (int i = 0; i < materials.Count; i++)
            {
                if (!materials[i].tileV)
                {
                    num4 += 1f;
                }
                else
                {
                    num3 += 1f;
                }
            }
            for (int j = 0; j < materials.Count; j++)
            {
                if (materials[j].tileV)
                {
                    array[j] = 1f;
                }
                else
                {
                    array[j] = 1f / num4;
                }
            }
            TextureAtlas textureAtlas = new TextureAtlas();

            for (int k = 0; k < materials.Count; k++)
            {
                Texture2D texture2D       = null;
                Texture2D normal          = null;
                Texture2D gloss           = null;
                Texture2D transtex        = null;
                Texture2D shadowOffsetTex = null;
                Color     color           = new Color(1f, 1f, 1f, 1f);
                float     num5            = 0.03f;
                Vector2   textureScale    = new Vector2(1f, 1f);
                Material  material        = materials[k].material;
                if (material)
                {
                    if (material.HasProperty("_Color"))
                    {
                        color = material.GetColor("_Color");
                    }
                    if (material.HasProperty("_MainTex"))
                    {
                        texture2D    = (material.mainTexture as Texture2D);
                        textureScale = material.GetTextureScale("_MainTex");
                    }
                    if (material.HasProperty("_BumpMap"))
                    {
                        normal = (material.GetTexture("_BumpMap") as Texture2D);
                    }
                    if (material.HasProperty("_GlossMap"))
                    {
                        gloss = (material.GetTexture("_GlossMap") as Texture2D);
                    }
                    if (material.HasProperty("_TranslucencyMap"))
                    {
                        transtex = (material.GetTexture("_TranslucencyMap") as Texture2D);
                    }
                    if (material.HasProperty("_Shininess"))
                    {
                        num5 = material.GetFloat("_Shininess");
                    }
                    if (material.HasProperty("_ShadowOffset"))
                    {
                        shadowOffsetTex = (material.GetTexture("_ShadowOffset") as Texture2D);
                    }
                }
                num5 = Mathf.Clamp(num5, 0.03f, 1f);
                Vector2 scale = new Vector2(array[k], array[k]);
                if (texture2D)
                {
                    scale.x *= (float)num / (float)texture2D.width;
                    scale.y *= (float)num2 / (float)texture2D.height;
                }
                bool tileV = materials[k].tileV;
                if (!tileV)
                {
                    textureScale = new Vector2(1f, 1f);
                }
                textureAtlas.AddTexture("tex" + k, texture2D, color, normal, gloss, transtex, shadowOffsetTex, num5, scale, tileV, textureScale);
            }
            textureAtlas.Pack(ref num, num2, padding, true);
            this.UpdateTextures(textureAtlas, materials);
            Rect    rect      = default(Rect);
            Vector2 texTiling = new Vector2(1f, 1f);
            int     num6      = -1;

            for (int l = 0; l < triangles.Count; l++)
            {
                TreeTriangle treeTriangle = triangles[l];
                if (treeTriangle.materialIndex != num6)
                {
                    num6      = treeTriangle.materialIndex;
                    rect      = textureAtlas.GetUVRect("tex" + treeTriangle.materialIndex);
                    texTiling = textureAtlas.GetTexTiling("tex" + treeTriangle.materialIndex);
                }
                for (int m = 0; m < 3; m++)
                {
                    TreeVertex treeVertex = vertices[treeTriangle.v[m]];
                    if (!treeVertex.flag)
                    {
                        treeVertex.uv0.x = rect.x + treeVertex.uv0.x * rect.width;
                        treeVertex.uv0.y = (rect.y + treeVertex.uv0.y * rect.height) * texTiling.y;
                        treeVertex.flag  = true;
                    }
                }
                if (treeTriangle.isCutout)
                {
                    treeTriangle.materialIndex = 1;
                }
                else
                {
                    treeTriangle.materialIndex = 0;
                }
            }
            Profiler.EndSample();
            return(true);
        }
Exemplo n.º 5
0
		private bool UpdateTextures(TextureAtlas atlas, List<TreeMaterial> materials)
		{
			if (!this.root.enableMaterialOptimize)
			{
				return false;
			}
			bool flag = this.optimizedSolidMaterial.GetTexture("_MainTex") != null && this.optimizedSolidMaterial.GetTexture("_BumpSpecMap") != null && this.optimizedSolidMaterial.GetTexture("_TranslucencyMap") != null && this.optimizedCutoutMaterial.GetTexture("_MainTex") != null && this.optimizedCutoutMaterial.GetTexture("_ShadowTex") != null && this.optimizedCutoutMaterial.GetTexture("_BumpSpecMap") != null && this.optimizedCutoutMaterial.GetTexture("_TranslucencyMap");
			UnityEngine.Object[] array = new UnityEngine.Object[materials.Count];
			for (int i = 0; i < materials.Count; i++)
			{
				array[i] = materials[i].material;
			}
			string text = InternalEditorUtility.CalculateHashForObjectsAndDependencies(array);
			text += atlas.GetHashCode();
			if (this.materialHash == text && flag)
			{
				this.UpdateShadowTexture(this.optimizedCutoutMaterial.GetTexture("_ShadowTex") as Texture2D, atlas.atlasWidth, atlas.atlasHeight);
				return false;
			}
			this.materialHash = text;
			int atlasWidth = atlas.atlasWidth;
			int atlasHeight = atlas.atlasHeight;
			int atlasPadding = atlas.atlasPadding;
			Texture2D texture2D = new Texture2D(atlasWidth, atlasHeight, TextureFormat.ARGB32, true);
			Texture2D texture2D2 = new Texture2D(atlasWidth, atlasHeight, TextureFormat.RGB24, true);
			Texture2D texture2D3 = new Texture2D(atlasWidth, atlasHeight, TextureFormat.ARGB32, true);
			Texture2D texture2D4 = new Texture2D(atlasWidth, atlasHeight, TextureFormat.ARGB32, true);
			texture2D.name = "diffuse";
			texture2D2.name = "shadow";
			texture2D3.name = "normal_specular";
			texture2D4.name = "translucency_gloss";
			SavedRenderTargetState savedRenderTargetState = new SavedRenderTargetState();
			EditorUtility.SetTemporarilyAllowIndieRenderTexture(true);
			RenderTexture temporary = RenderTexture.GetTemporary(atlasWidth, atlasHeight, 0, RenderTextureFormat.ARGB32);
			Color white = Color.white;
			Color color = new Color(0.03f, 0.5f, 0f, 0.5f);
			Color color2 = new Color(0f, 0f, 0f, 0f);
			Texture2D texture2D5 = new Texture2D(1, 1);
			texture2D5.SetPixel(0, 0, white);
			texture2D5.Apply();
			Texture2D texture2D6 = new Texture2D(1, 1);
			texture2D6.SetPixel(0, 0, white);
			texture2D6.Apply();
			Texture2D texture2D7 = new Texture2D(1, 1);
			texture2D7.SetPixel(0, 0, color);
			texture2D7.Apply();
			Texture2D texture2D8 = new Texture2D(1, 1);
			texture2D8.SetPixel(0, 0, color2);
			texture2D8.Apply();
			Texture2D texture2D9 = texture2D8;
			Texture2D texture2D10 = new Texture2D(1, 1);
			texture2D10.SetPixel(0, 0, Color.white);
			texture2D10.Apply();
			Material material = EditorGUIUtility.LoadRequired("Inspectors/TreeCreator/TreeTextureCombinerMaterial.mat") as Material;
			for (int j = 0; j < 4; j++)
			{
				RenderTexture.active = temporary;
				GL.LoadPixelMatrix(0f, (float)atlasWidth, 0f, (float)atlasHeight);
				material.SetVector("_TexSize", new Vector4((float)atlasWidth, (float)atlasHeight, 0f, 0f));
				switch (j)
				{
				case 0:
					GL.Clear(false, true, color);
					break;
				case 1:
					GL.Clear(false, true, color2);
					break;
				case 2:
					GL.Clear(false, true, color2);
					break;
				case 3:
					GL.Clear(false, true, color2);
					break;
				}
				for (int k = 0; k < atlas.nodes.Count; k++)
				{
					TextureAtlas.TextureNode textureNode = atlas.nodes[k];
					Rect packedRect = textureNode.packedRect;
					Texture texture = null;
					Texture texture2 = null;
					Color color3 = default(Color);
					switch (j)
					{
					case 0:
						texture = textureNode.normalTexture;
						texture2 = textureNode.shadowOffsetTexture;
						color3 = new Color(textureNode.shininess, 0f, 0f, 0f);
						if (texture == null)
						{
							texture = texture2D7;
						}
						if (texture2 == null)
						{
							texture2 = texture2D9;
						}
						break;
					case 1:
						texture = textureNode.diffuseTexture;
						color3 = textureNode.diffuseColor;
						if (texture == null)
						{
							texture = texture2D5;
						}
						break;
					case 2:
						texture = textureNode.translucencyTexture;
						texture2 = textureNode.glossTexture;
						if (texture == null)
						{
							texture = texture2D10;
						}
						if (texture2 == null)
						{
							texture2 = texture2D8;
						}
						break;
					case 3:
						texture2 = textureNode.diffuseTexture;
						if (texture2 == null)
						{
							texture2 = texture2D5;
						}
						break;
					}
					if (textureNode.tileV)
					{
						float x = packedRect.x;
						float num = (float)atlasPadding / 2f;
						for (float num2 = num; num2 > 0f; num2 -= 1f)
						{
							Rect rect = new Rect(packedRect);
							Rect rect2 = new Rect(packedRect);
							rect.x = x - num2;
							rect2.x = x + num2;
							this.DrawTexture(rect, texture, texture2, material, color3, j);
							this.DrawTexture(rect2, texture, texture2, material, color3, j);
						}
					}
					this.DrawTexture(packedRect, texture, texture2, material, color3, j);
				}
				switch (j)
				{
				case 0:
					texture2D3.ReadPixels(new Rect(0f, 0f, (float)atlasWidth, (float)atlasHeight), 0, 0);
					texture2D3.Apply(true);
					break;
				case 1:
					texture2D.ReadPixels(new Rect(0f, 0f, (float)atlasWidth, (float)atlasHeight), 0, 0);
					texture2D.Apply(true);
					break;
				case 2:
					texture2D4.ReadPixels(new Rect(0f, 0f, (float)atlasWidth, (float)atlasHeight), 0, 0);
					texture2D4.Apply(true);
					break;
				case 3:
					texture2D2.ReadPixels(new Rect(0f, 0f, (float)atlasWidth, (float)atlasHeight), 0, 0);
					texture2D2.Apply(true);
					break;
				}
			}
			savedRenderTargetState.Restore();
			this.optimizedSolidMaterial.SetPass(0);
			RenderTexture.ReleaseTemporary(temporary);
			UnityEngine.Object.DestroyImmediate(texture2D5);
			UnityEngine.Object.DestroyImmediate(texture2D6);
			UnityEngine.Object.DestroyImmediate(texture2D10);
			UnityEngine.Object.DestroyImmediate(texture2D8);
			UnityEngine.Object.DestroyImmediate(texture2D7);
			EditorUtility.SetTemporarilyAllowIndieRenderTexture(false);
			Texture2D[] array2 = new Texture2D[]
			{
				texture2D,
				texture2D3,
				texture2D4,
				texture2D2
			};
			array2 = TreeData.WriteOptimizedTextures(AssetDatabase.GetAssetPath(this), array2);
			UnityEngine.Object.DestroyImmediate(texture2D);
			UnityEngine.Object.DestroyImmediate(texture2D3);
			UnityEngine.Object.DestroyImmediate(texture2D4);
			UnityEngine.Object.DestroyImmediate(texture2D2);
			this.optimizedSolidMaterial.SetTexture("_MainTex", array2[0]);
			this.optimizedSolidMaterial.SetTexture("_BumpSpecMap", array2[1]);
			this.optimizedSolidMaterial.SetTexture("_TranslucencyMap", array2[2]);
			this.optimizedCutoutMaterial.SetTexture("_MainTex", array2[0]);
			this.optimizedCutoutMaterial.SetTexture("_BumpSpecMap", array2[1]);
			this.optimizedCutoutMaterial.SetTexture("_TranslucencyMap", array2[2]);
			this.optimizedCutoutMaterial.SetTexture("_ShadowTex", array2[3]);
			this.UpdateShadowTexture(array2[3], atlas.atlasWidth, atlas.atlasHeight);
			return true;
		}
Exemplo n.º 6
0
		public bool OptimizeMaterial(List<TreeMaterial> materials, List<TreeVertex> vertices, List<TreeTriangle> triangles)
		{
			if (!this.optimizedSolidMaterial || !this.optimizedCutoutMaterial)
			{
				Debug.LogError("Optimized materials haven't been assigned");
				return false;
			}
			Shader shader;
			Shader shader2;
			TreeData.ExtractOptimizedShaders(materials, out shader, out shader2);
			this.optimizedSolidMaterial.shader = shader;
			this.optimizedCutoutMaterial.shader = shader2;
			int num = 1024;
			int num2 = 1024;
			int padding = 32;
			Profiler.BeginSample("OptimizeMaterial");
			float[] array = new float[materials.Count];
			float num3 = 0f;
			float num4 = 0f;
			for (int i = 0; i < materials.Count; i++)
			{
				if (!materials[i].tileV)
				{
					num4 += 1f;
				}
				else
				{
					num3 += 1f;
				}
			}
			for (int j = 0; j < materials.Count; j++)
			{
				if (materials[j].tileV)
				{
					array[j] = 1f;
				}
				else
				{
					array[j] = 1f / num4;
				}
			}
			TextureAtlas textureAtlas = new TextureAtlas();
			for (int k = 0; k < materials.Count; k++)
			{
				Texture2D texture2D = null;
				Texture2D normal = null;
				Texture2D gloss = null;
				Texture2D transtex = null;
				Texture2D shadowOffsetTex = null;
				Color color = new Color(1f, 1f, 1f, 1f);
				float num5 = 0.03f;
				Vector2 textureScale = new Vector2(1f, 1f);
				Material material = materials[k].material;
				if (material)
				{
					if (material.HasProperty("_Color"))
					{
						color = material.GetColor("_Color");
					}
					if (material.HasProperty("_MainTex"))
					{
						texture2D = (material.mainTexture as Texture2D);
						textureScale = material.GetTextureScale("_MainTex");
					}
					if (material.HasProperty("_BumpMap"))
					{
						normal = (material.GetTexture("_BumpMap") as Texture2D);
					}
					if (material.HasProperty("_GlossMap"))
					{
						gloss = (material.GetTexture("_GlossMap") as Texture2D);
					}
					if (material.HasProperty("_TranslucencyMap"))
					{
						transtex = (material.GetTexture("_TranslucencyMap") as Texture2D);
					}
					if (material.HasProperty("_Shininess"))
					{
						num5 = material.GetFloat("_Shininess");
					}
					if (material.HasProperty("_ShadowOffset"))
					{
						shadowOffsetTex = (material.GetTexture("_ShadowOffset") as Texture2D);
					}
				}
				num5 = Mathf.Clamp(num5, 0.03f, 1f);
				Vector2 scale = new Vector2(array[k], array[k]);
				if (texture2D)
				{
					scale.x *= (float)num / (float)texture2D.width;
					scale.y *= (float)num2 / (float)texture2D.height;
				}
				bool tileV = materials[k].tileV;
				if (!tileV)
				{
					textureScale = new Vector2(1f, 1f);
				}
				textureAtlas.AddTexture("tex" + k, texture2D, color, normal, gloss, transtex, shadowOffsetTex, num5, scale, tileV, textureScale);
			}
			textureAtlas.Pack(ref num, num2, padding, true);
			this.UpdateTextures(textureAtlas, materials);
			Rect rect = default(Rect);
			Vector2 texTiling = new Vector2(1f, 1f);
			int num6 = -1;
			for (int l = 0; l < triangles.Count; l++)
			{
				TreeTriangle treeTriangle = triangles[l];
				if (treeTriangle.materialIndex != num6)
				{
					num6 = treeTriangle.materialIndex;
					rect = textureAtlas.GetUVRect("tex" + treeTriangle.materialIndex);
					texTiling = textureAtlas.GetTexTiling("tex" + treeTriangle.materialIndex);
				}
				for (int m = 0; m < 3; m++)
				{
					TreeVertex treeVertex = vertices[treeTriangle.v[m]];
					if (!treeVertex.flag)
					{
						treeVertex.uv0.x = rect.x + treeVertex.uv0.x * rect.width;
						treeVertex.uv0.y = (rect.y + treeVertex.uv0.y * rect.height) * texTiling.y;
						treeVertex.flag = true;
					}
				}
				if (treeTriangle.isCutout)
				{
					treeTriangle.materialIndex = 1;
				}
				else
				{
					treeTriangle.materialIndex = 0;
				}
			}
			Profiler.EndSample();
			return true;
		}
Exemplo n.º 7
0
        private bool UpdateTextures(TextureAtlas atlas, List<TreeMaterial> materials)
        {
            if (!this.root.enableMaterialOptimize)
            {
                return false;
            }
            bool flag = ((((this.optimizedSolidMaterial.GetTexture("_MainTex") != null) && (this.optimizedSolidMaterial.GetTexture("_BumpSpecMap") != null)) && ((this.optimizedSolidMaterial.GetTexture("_TranslucencyMap") != null) && (this.optimizedCutoutMaterial.GetTexture("_MainTex") != null))) && ((this.optimizedCutoutMaterial.GetTexture("_ShadowTex") != null) && (this.optimizedCutoutMaterial.GetTexture("_BumpSpecMap") != null))) && ((bool) this.optimizedCutoutMaterial.GetTexture("_TranslucencyMap"));
            UnityEngine.Object[] objects = new UnityEngine.Object[materials.Count];
            for (int i = 0; i < materials.Count; i++)
            {
                objects[i] = materials[i].material;
            }
            string str = InternalEditorUtility.CalculateHashForObjectsAndDependencies(objects) + atlas.GetHashCode();
            if ((this.materialHash == str) && flag)
            {
                this.UpdateShadowTexture(this.optimizedCutoutMaterial.GetTexture("_ShadowTex") as Texture2D, atlas.atlasWidth, atlas.atlasHeight);
                return false;
            }
            this.materialHash = str;
            int atlasWidth = atlas.atlasWidth;
            int atlasHeight = atlas.atlasHeight;
            int atlasPadding = atlas.atlasPadding;
            Texture2D textured = new Texture2D(atlasWidth, atlasHeight, TextureFormat.ARGB32, true);
            Texture2D textured2 = new Texture2D(atlasWidth, atlasHeight, TextureFormat.RGB24, true);
            Texture2D textured3 = new Texture2D(atlasWidth, atlasHeight, TextureFormat.ARGB32, true);
            Texture2D textured4 = new Texture2D(atlasWidth, atlasHeight, TextureFormat.ARGB32, true);
            textured.name = "diffuse";
            textured2.name = "shadow";
            textured3.name = "normal_specular";
            textured4.name = "translucency_gloss";
            SavedRenderTargetState state = new SavedRenderTargetState();
            EditorUtility.SetTemporarilyAllowIndieRenderTexture(true);
            RenderTexture temp = RenderTexture.GetTemporary(atlasWidth, atlasHeight, 0, RenderTextureFormat.ARGB32);
            Color white = Color.white;
            Color color = new Color(0.03f, 0.5f, 0f, 0.5f);
            Color color3 = new Color(0f, 0f, 0f, 0f);
            Texture2D textured5 = new Texture2D(1, 1);
            textured5.SetPixel(0, 0, white);
            textured5.Apply();
            Texture2D textured6 = new Texture2D(1, 1);
            textured6.SetPixel(0, 0, white);
            textured6.Apply();
            Texture2D textured7 = new Texture2D(1, 1);
            textured7.SetPixel(0, 0, color);
            textured7.Apply();
            Texture2D textured8 = new Texture2D(1, 1);
            textured8.SetPixel(0, 0, color3);
            textured8.Apply();
            Texture2D textured9 = textured8;
            Texture2D textured10 = new Texture2D(1, 1);
            textured10.SetPixel(0, 0, Color.white);
            textured10.Apply();
            Material material = EditorGUIUtility.LoadRequired("Inspectors/TreeCreator/TreeTextureCombinerMaterial.mat") as Material;
            for (int j = 0; j < 4; j++)
            {
                RenderTexture.active = temp;
                GL.LoadPixelMatrix(0f, (float) atlasWidth, 0f, (float) atlasHeight);
                material.SetVector("_TexSize", new Vector4((float) atlasWidth, (float) atlasHeight, 0f, 0f));
                switch (j)
                {
                    case 0:
                        GL.Clear(false, true, color);
                        break;

                    case 1:
                        GL.Clear(false, true, color3);
                        break;

                    case 2:
                        GL.Clear(false, true, color3);
                        break;

                    case 3:
                        GL.Clear(false, true, color3);
                        break;
                }
                for (int k = 0; k < atlas.nodes.Count; k++)
                {
                    TextureAtlas.TextureNode node = atlas.nodes[k];
                    Rect packedRect = node.packedRect;
                    Texture rgbTexture = null;
                    Texture alphaTexture = null;
                    Color diffuseColor = new Color();
                    switch (j)
                    {
                        case 0:
                            rgbTexture = node.normalTexture;
                            alphaTexture = node.shadowOffsetTexture;
                            diffuseColor = new Color(node.shininess, 0f, 0f, 0f);
                            if (rgbTexture == null)
                            {
                                rgbTexture = textured7;
                            }
                            if (alphaTexture == null)
                            {
                                alphaTexture = textured9;
                            }
                            break;

                        case 1:
                            rgbTexture = node.diffuseTexture;
                            diffuseColor = node.diffuseColor;
                            if (rgbTexture == null)
                            {
                                rgbTexture = textured5;
                            }
                            break;

                        case 2:
                            rgbTexture = node.translucencyTexture;
                            alphaTexture = node.glossTexture;
                            if (rgbTexture == null)
                            {
                                rgbTexture = textured10;
                            }
                            if (alphaTexture == null)
                            {
                                alphaTexture = textured8;
                            }
                            break;

                        case 3:
                            alphaTexture = node.diffuseTexture;
                            if (alphaTexture == null)
                            {
                                alphaTexture = textured5;
                            }
                            break;
                    }
                    if (node.tileV)
                    {
                        float x = packedRect.x;
                        float num8 = ((float) atlasPadding) / 2f;
                        for (float m = num8; m > 0f; m--)
                        {
                            Rect rect = new Rect(packedRect);
                            Rect rect3 = new Rect(packedRect);
                            rect.x = x - m;
                            rect3.x = x + m;
                            this.DrawTexture(rect, rgbTexture, alphaTexture, material, diffuseColor, j);
                            this.DrawTexture(rect3, rgbTexture, alphaTexture, material, diffuseColor, j);
                        }
                    }
                    this.DrawTexture(packedRect, rgbTexture, alphaTexture, material, diffuseColor, j);
                }
                switch (j)
                {
                    case 0:
                        textured3.ReadPixels(new Rect(0f, 0f, (float) atlasWidth, (float) atlasHeight), 0, 0);
                        textured3.Apply(true);
                        break;

                    case 1:
                        textured.ReadPixels(new Rect(0f, 0f, (float) atlasWidth, (float) atlasHeight), 0, 0);
                        textured.Apply(true);
                        break;

                    case 2:
                        textured4.ReadPixels(new Rect(0f, 0f, (float) atlasWidth, (float) atlasHeight), 0, 0);
                        textured4.Apply(true);
                        break;

                    case 3:
                        textured2.ReadPixels(new Rect(0f, 0f, (float) atlasWidth, (float) atlasHeight), 0, 0);
                        textured2.Apply(true);
                        break;
                }
            }
            state.Restore();
            this.optimizedSolidMaterial.SetPass(0);
            RenderTexture.ReleaseTemporary(temp);
            UnityEngine.Object.DestroyImmediate(textured5);
            UnityEngine.Object.DestroyImmediate(textured6);
            UnityEngine.Object.DestroyImmediate(textured10);
            UnityEngine.Object.DestroyImmediate(textured8);
            UnityEngine.Object.DestroyImmediate(textured7);
            EditorUtility.SetTemporarilyAllowIndieRenderTexture(false);
            Texture2D[] textures = new Texture2D[] { textured, textured3, textured4, textured2 };
            textures = WriteOptimizedTextures(AssetDatabase.GetAssetPath(this), textures);
            UnityEngine.Object.DestroyImmediate(textured);
            UnityEngine.Object.DestroyImmediate(textured3);
            UnityEngine.Object.DestroyImmediate(textured4);
            UnityEngine.Object.DestroyImmediate(textured2);
            this.optimizedSolidMaterial.SetTexture("_MainTex", textures[0]);
            this.optimizedSolidMaterial.SetTexture("_BumpSpecMap", textures[1]);
            this.optimizedSolidMaterial.SetTexture("_TranslucencyMap", textures[2]);
            this.optimizedCutoutMaterial.SetTexture("_MainTex", textures[0]);
            this.optimizedCutoutMaterial.SetTexture("_BumpSpecMap", textures[1]);
            this.optimizedCutoutMaterial.SetTexture("_TranslucencyMap", textures[2]);
            this.optimizedCutoutMaterial.SetTexture("_ShadowTex", textures[3]);
            this.UpdateShadowTexture(textures[3], atlas.atlasWidth, atlas.atlasHeight);
            return true;
        }
Exemplo n.º 8
0
 public bool OptimizeMaterial(List<TreeMaterial> materials, List<TreeVertex> vertices, List<TreeTriangle> triangles)
 {
     Shader shader;
     Shader shader2;
     if ((this.optimizedSolidMaterial == null) || (this.optimizedCutoutMaterial == null))
     {
         Debug.LogError("Optimized materials haven't been assigned");
         return false;
     }
     ExtractOptimizedShaders(materials, out shader, out shader2);
     this.optimizedSolidMaterial.shader = shader;
     this.optimizedCutoutMaterial.shader = shader2;
     int targetWidth = 0x400;
     int targetHeight = 0x400;
     int padding = 0x20;
     Profiler.BeginSample("OptimizeMaterial");
     float[] numArray = new float[materials.Count];
     float num4 = 0f;
     float num5 = 0f;
     for (int i = 0; i < materials.Count; i++)
     {
         if (!materials[i].tileV)
         {
             num5++;
         }
         else
         {
             num4++;
         }
     }
     for (int j = 0; j < materials.Count; j++)
     {
         if (materials[j].tileV)
         {
             numArray[j] = 1f;
         }
         else
         {
             numArray[j] = 1f / num5;
         }
     }
     TextureAtlas atlas = new TextureAtlas();
     for (int k = 0; k < materials.Count; k++)
     {
         Texture2D diffuse = null;
         Texture2D normal = null;
         Texture2D gloss = null;
         Texture2D transtex = null;
         Texture2D shadowOffsetTex = null;
         Color diffuseColor = new Color(1f, 1f, 1f, 1f);
         float @float = 0.03f;
         Vector2 uvTiling = new Vector2(1f, 1f);
         Material material = materials[k].material;
         if (material != null)
         {
             if (material.HasProperty("_Color"))
             {
                 diffuseColor = material.GetColor("_Color");
             }
             if (material.HasProperty("_MainTex"))
             {
                 diffuse = material.mainTexture as Texture2D;
                 uvTiling = material.GetTextureScale("_MainTex");
             }
             if (material.HasProperty("_BumpMap"))
             {
                 normal = material.GetTexture("_BumpMap") as Texture2D;
             }
             if (material.HasProperty("_GlossMap"))
             {
                 gloss = material.GetTexture("_GlossMap") as Texture2D;
             }
             if (material.HasProperty("_TranslucencyMap"))
             {
                 transtex = material.GetTexture("_TranslucencyMap") as Texture2D;
             }
             if (material.HasProperty("_Shininess"))
             {
                 @float = material.GetFloat("_Shininess");
             }
             if (material.HasProperty("_ShadowOffset"))
             {
                 shadowOffsetTex = material.GetTexture("_ShadowOffset") as Texture2D;
             }
         }
         @float = Mathf.Clamp(@float, 0.03f, 1f);
         Vector2 scale = new Vector2(numArray[k], numArray[k]);
         if (diffuse != null)
         {
             scale.x *= ((float) targetWidth) / ((float) diffuse.width);
             scale.y *= ((float) targetHeight) / ((float) diffuse.height);
         }
         bool tileV = materials[k].tileV;
         if (!tileV)
         {
             uvTiling = new Vector2(1f, 1f);
         }
         atlas.AddTexture("tex" + k, diffuse, diffuseColor, normal, gloss, transtex, shadowOffsetTex, @float, scale, tileV, uvTiling);
     }
     atlas.Pack(ref targetWidth, targetHeight, padding, true);
     this.UpdateTextures(atlas, materials);
     Rect uVRect = new Rect();
     Vector2 texTiling = new Vector2(1f, 1f);
     int materialIndex = -1;
     for (int m = 0; m < triangles.Count; m++)
     {
         TreeTriangle triangle = triangles[m];
         if (triangle.materialIndex != materialIndex)
         {
             materialIndex = triangle.materialIndex;
             uVRect = atlas.GetUVRect("tex" + triangle.materialIndex);
             texTiling = atlas.GetTexTiling("tex" + triangle.materialIndex);
         }
         for (int n = 0; n < 3; n++)
         {
             TreeVertex vertex = vertices[triangle.v[n]];
             if (!vertex.flag)
             {
                 vertex.uv0.x = uVRect.x + (vertex.uv0.x * uVRect.width);
                 vertex.uv0.y = (uVRect.y + (vertex.uv0.y * uVRect.height)) * texTiling.y;
                 vertex.flag = true;
             }
         }
         if (triangle.isCutout)
         {
             triangle.materialIndex = 1;
         }
         else
         {
             triangle.materialIndex = 0;
         }
     }
     Profiler.EndSample();
     return true;
 }