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