public void AddTexture(string name, Texture2D diffuse, Color diffuseColor, Texture2D normal, Texture2D gloss, Texture2D transtex, Texture2D shadowOffsetTex, float shininess, Vector2 scale, bool tileV, Vector2 uvTiling) { TextureAtlas.TextureNode textureNode = new TextureAtlas.TextureNode(); textureNode.name = name; textureNode.diffuseTexture = diffuse; textureNode.diffuseColor = diffuseColor; textureNode.normalTexture = normal; textureNode.glossTexture = gloss; textureNode.translucencyTexture = transtex; textureNode.shadowOffsetTexture = shadowOffsetTex; textureNode.shininess = shininess; textureNode.scale = scale; textureNode.tileV = tileV; textureNode.uvTiling = uvTiling; if (diffuse) { textureNode.sourceRect.width = (float)diffuse.width; textureNode.sourceRect.height = (float)diffuse.height; } else { textureNode.sourceRect.width = 64f; textureNode.sourceRect.height = 64f; textureNode.scale = new Vector2(1f, 1f); } this.nodes.Add(textureNode); }
public void AddTexture(string name, Texture2D diffuse, Color diffuseColor, Texture2D normal, Texture2D gloss, Texture2D transtex, Texture2D shadowOffsetTex, float shininess, Vector2 scale, bool tileV, Vector2 uvTiling) { TextureAtlas.TextureNode textureNode = new TextureAtlas.TextureNode(); textureNode.name = name; textureNode.diffuseTexture = diffuse; textureNode.diffuseColor = diffuseColor; textureNode.normalTexture = normal; textureNode.glossTexture = gloss; textureNode.translucencyTexture = transtex; textureNode.shadowOffsetTexture = shadowOffsetTex; textureNode.shininess = shininess; textureNode.scale = scale; textureNode.tileV = tileV; textureNode.uvTiling = uvTiling; if (diffuse) { textureNode.sourceRect.width = (float)diffuse.width; textureNode.sourceRect.height = (float)diffuse.height; } else { textureNode.sourceRect.width = 64f; textureNode.sourceRect.height = 64f; textureNode.scale = new Vector2(1f, 1f); } this.nodes.Add(textureNode); }
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); }
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)); }
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); }
public void Pack(ref int targetWidth, int targetHeight, int padding, bool correctPow2) { if (padding % 2 != 0) { Debug.LogWarning("Padding not an even number"); padding++; } for (int i = 0; i < this.nodes.Count; i++) { TextureAtlas.TextureNode textureNode = this.nodes[i]; textureNode.packedRect.x = 0f; textureNode.packedRect.y = 0f; textureNode.packedRect.width = Mathf.Round(textureNode.sourceRect.width * textureNode.scale.x); textureNode.packedRect.height = Mathf.Min((float)targetHeight, Mathf.Round(textureNode.sourceRect.height * textureNode.scale.y)); if (textureNode.tileV) { textureNode.packedRect.height = (float)targetHeight; } if (correctPow2) { textureNode.packedRect.width = (float)Mathf.ClosestPowerOfTwo((int)textureNode.packedRect.width); textureNode.packedRect.height = (float)Mathf.ClosestPowerOfTwo((int)textureNode.packedRect.height); } } this.nodes.Sort((TextureAtlas.TextureNode a, TextureAtlas.TextureNode b) => a.CompareTo(b)); int num = 0; int num2 = 0; for (int j = 0; j < this.nodes.Count; j++) { TextureAtlas.TextureNode textureNode2 = this.nodes[j]; bool flag = false; for (int k = 0; k < num; k++) { textureNode2.packedRect.x = (float)k; textureNode2.packedRect.y = 0f; flag = true; for (int l = 0; l <= num2; l++) { flag = true; textureNode2.packedRect.y = (float)l; for (int m = 0; m < j; m++) { TextureAtlas.TextureNode textureNode3 = this.nodes[m]; if (TextureAtlas.TextureNode.Overlap(textureNode2, textureNode3)) { flag = false; if (textureNode3.tileV) { l = num2; } else { l = (int)(textureNode3.packedRect.y + textureNode3.packedRect.height); } break; } } if (flag) { break; } } if (flag) { break; } } if (!flag) { textureNode2.packedRect.x = (float)num; textureNode2.packedRect.y = 0f; } num = Mathf.Max(num, (int)(textureNode2.packedRect.x + textureNode2.packedRect.width)); num2 = Mathf.Max(num2, (int)(textureNode2.packedRect.y + textureNode2.packedRect.height)); } int min = Mathf.Max(Mathf.ClosestPowerOfTwo(padding * 2), 64); int num3 = Mathf.Clamp(Mathf.ClosestPowerOfTwo(num), min, targetWidth); targetWidth = num3; this.atlasWidth = targetWidth; this.atlasHeight = targetHeight; this.atlasPadding = padding; float num4 = (float)targetWidth / (float)num; float num5 = (float)targetHeight / (float)num2; for (int n = 0; n < this.nodes.Count; n++) { TextureAtlas.TextureNode textureNode4 = this.nodes[n]; TextureAtlas.TextureNode expr_32A_cp_0 = textureNode4; expr_32A_cp_0.packedRect.x = expr_32A_cp_0.packedRect.x * num4; TextureAtlas.TextureNode expr_33F_cp_0 = textureNode4; expr_33F_cp_0.packedRect.y = expr_33F_cp_0.packedRect.y * num5; TextureAtlas.TextureNode expr_354_cp_0 = textureNode4; expr_354_cp_0.packedRect.width = expr_354_cp_0.packedRect.width * num4; TextureAtlas.TextureNode expr_369_cp_0 = textureNode4; expr_369_cp_0.packedRect.height = expr_369_cp_0.packedRect.height * num5; if (textureNode4.tileV) { textureNode4.packedRect.y = 0f; textureNode4.packedRect.height = (float)targetHeight; TextureAtlas.TextureNode expr_3A9_cp_0 = textureNode4; expr_3A9_cp_0.packedRect.x = expr_3A9_cp_0.packedRect.x + (float)(padding / 2); TextureAtlas.TextureNode expr_3C0_cp_0 = textureNode4; expr_3C0_cp_0.packedRect.width = expr_3C0_cp_0.packedRect.width - (float)padding; } else { TextureAtlas.TextureNode expr_3DA_cp_0 = textureNode4; expr_3DA_cp_0.packedRect.x = expr_3DA_cp_0.packedRect.x + (float)(padding / 2); TextureAtlas.TextureNode expr_3F1_cp_0 = textureNode4; expr_3F1_cp_0.packedRect.y = expr_3F1_cp_0.packedRect.y + (float)(padding / 2); TextureAtlas.TextureNode expr_408_cp_0 = textureNode4; expr_408_cp_0.packedRect.width = expr_408_cp_0.packedRect.width - (float)padding; TextureAtlas.TextureNode expr_41D_cp_0 = textureNode4; expr_41D_cp_0.packedRect.height = expr_41D_cp_0.packedRect.height - (float)padding; } if (textureNode4.packedRect.width < 1f) { textureNode4.packedRect.width = 1f; } if (textureNode4.packedRect.height < 1f) { textureNode4.packedRect.height = 1f; } textureNode4.packedRect.x = Mathf.Round(textureNode4.packedRect.x); textureNode4.packedRect.y = Mathf.Round(textureNode4.packedRect.y); textureNode4.packedRect.width = Mathf.Round(textureNode4.packedRect.width); textureNode4.packedRect.height = Mathf.Round(textureNode4.packedRect.height); textureNode4.uvRect.x = textureNode4.packedRect.x / (float)targetWidth; textureNode4.uvRect.y = textureNode4.packedRect.y / (float)targetHeight; textureNode4.uvRect.width = textureNode4.packedRect.width / (float)targetWidth; textureNode4.uvRect.height = textureNode4.packedRect.height / (float)targetHeight; } }