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; }
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); }
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; }