/// void OnEnable() { m_logo = (Texture2D)Resources.Load("RoveLogo"); Color[] bgp = new Color[1]; bgp[0].r = 0.15f; bgp[0].g = 0.15f; bgp[0].b = 0.15f; bgp[0].a = 1.0f; m_bg = new Texture2D(1, 1); m_bg.SetPixels(bgp); m_bg.wrapMode = TextureWrapMode.Repeat; m_bg.Apply(); Color[] gbgp = new Color[1]; gbgp[0].r = 0.8f; gbgp[0].g = 0.6f; gbgp[0].b = 0.18f; gbgp[0].a = 1.0f; m_gbg = new Texture2D(1, 1); m_gbg.SetPixels(gbgp); m_gbg.wrapMode = TextureWrapMode.Repeat; m_gbg.Apply(); m_dark_style = new GUIStyle(); m_dark_style.normal.textColor = Color.white; m_dark_style.normal.background = m_bg; m_dark_style.fontSize = 12; m_dark_style.alignment = TextAnchor.MiddleLeft; m_dark_style.wordWrap = true; RoveMaterials rove_materials = (RoveMaterials)target; m_expand_atlas = new bool[RoveMaterials.ATLAS_COUNT]; m_expand_material = new bool[rove_materials.m_materials.Count]; serializedObject.Update(); serializedObject.ApplyModifiedProperties(); }
/// void Start() { m_mesh_id = RoveSetup.ERROR; m_rove_setup = RoveSetup.GetAPIComponent <RoveSetup>(); if (!m_rove_setup || !m_rove_setup.enabled) { Debug.LogError("Rove: Could not find an active RoveSetup component."); Destroy(this); return; } m_rove_materials = RoveSetup.GetAPIComponent <RoveMaterials>(); if (!m_rove_materials || !m_rove_materials.enabled) { Debug.LogError("Rove: Could not find an active RoveMaterials component."); Destroy(this); return; } m_mesh_renderer = this.GetComponent <MeshRenderer>(); if (!m_mesh_renderer) { Debug.LogError("Rove: Could not find a mesh renderer on object."); Destroy(this); return; } m_mesh_filter = this.GetComponent <MeshFilter>(); if (!m_mesh_filter) { Debug.LogError("Rove: Could not find a mesh filter on object."); Destroy(this); return; } gameObject.transform.hasChanged = true; m_mesh_renderer.enabled = false; }
/// public void SetTexture(uint atlas_type, Texture2D texture) { RoveMaterials rove_materials = RoveSetup.GetAPIComponent <RoveMaterials>(); if (!rove_materials) { Debug.LogError("Failed set material texture."); return; } RoveTexture rove_texture = m_textures[atlas_type]; if (texture == null) { if (rove_texture.m_status != RoveTexture.STATUS_EMPTY) { rove_texture.m_status = RoveTexture.STATUS_CHANGE; } } else { if (rove_texture.m_status != RoveTexture.STATUS_OK) { rove_texture.m_status = RoveTexture.STATUS_NEW; } else { rove_texture.m_status = RoveTexture.STATUS_CHANGE; } } rove_texture.m_texture = texture; rove_materials.m_atlases[atlas_type].m_dirty = true; }
/// void Start() { m_rove_setup = RoveSetup.GetAPIComponent <RoveSetup>(); if (!m_rove_setup || (!m_rove_setup.enabled)) { Debug.LogError("Rove: Could not find an active RoveSetup component."); Destroy(this); return; } m_rove_materials = RoveSetup.GetAPIComponent <RoveMaterials>(); if (!m_rove_materials || (!m_rove_materials.enabled)) { Debug.LogError("Rove: Could not find an active RoveMaterials component."); Destroy(this); return; } }
/// public override void OnInspectorGUI() { if (m_button_style == null) { m_button_style = new GUIStyle(GUI.skin.button); m_button_style.fontSize = 13; m_button_style.alignment = TextAnchor.MiddleLeft; m_button_style.normal.background = m_gbg; m_button_style.normal.textColor = Color.white; m_button_style.active.background = m_gbg; m_button_style.active.textColor = Color.white; } serializedObject.Update(); RoveMaterials rove_materials = (RoveMaterials)target; if (rove_materials.m_materials.Count != m_expand_material.Length) { m_expand_material = new bool[rove_materials.m_materials.Count]; } EditorGUILayout.BeginVertical("box"); { EditorGUILayout.BeginVertical(m_dark_style); { GUILayout.Space(10); EditorGUILayout.BeginHorizontal(); { int logo_width = 256; int logo_height = 64; GUILayout.Space((EditorGUIUtility.currentViewWidth / 2) - (logo_width / 2) - 10); EditorGUILayout.LabelField(new GUIContent(m_logo), GUILayout.Width(logo_width), GUILayout.Height(logo_height)); EditorGUILayout.EndHorizontal(); } GUILayout.Space(10); m_dark_style.normal.background = m_gbg; EditorGUILayout.BeginVertical(m_dark_style); { m_dark_style.fontSize = 17; m_dark_style.alignment = TextAnchor.MiddleCenter; EditorGUILayout.LabelField("Materials", m_dark_style); m_dark_style.fontSize = 12; m_dark_style.alignment = TextAnchor.MiddleLeft; EditorGUILayout.EndVertical(); } m_dark_style.normal.background = m_bg; EditorGUILayout.EndVertical(); } EditorGUILayout.EndVertical(); } EditorGUILayout.BeginVertical(m_dark_style); { m_dark_style.alignment = TextAnchor.MiddleCenter; if (!Application.isPlaying) { GUILayout.Space(10); EditorGUILayout.BeginHorizontal(m_dark_style); { GUILayout.Space(10); EditorGUILayout.LabelField("Scene-wide material information will " + "display during play mode.", m_dark_style); GUILayout.Space(10); EditorGUILayout.EndHorizontal(); } GUILayout.Space(10); } else { GUILayout.Space(20); m_dark_style.fontSize = 17; EditorGUILayout.LabelField("Global List", m_dark_style); m_dark_style.fontSize = 15; GUILayout.Space(20); for (int m = 0; m < rove_materials.m_materials.Count; ++m) { EditorGUILayout.BeginVertical("box"); { EditorGUILayout.BeginVertical(m_dark_style); { m_dark_style.normal.background = m_gbg; EditorGUILayout.BeginHorizontal(m_dark_style); { string material_name = Path.GetFileNameWithoutExtension( AssetDatabase.GetAssetPath(rove_materials.m_materials[m])); if (GUILayout.Button(material_name, m_button_style)) { m_expand_material[m] = !m_expand_material[m]; } EditorGUILayout.EndHorizontal(); } m_dark_style.normal.background = m_bg; if (m_expand_material[m]) { RoveMaterialEditor material_editor = (RoveMaterialEditor)RoveMaterialEditor.CreateEditor( rove_materials.m_materials[m], typeof(RoveMaterialEditor)); material_editor.InspectorProperties(false, 0); } EditorGUILayout.EndVertical(); } EditorGUILayout.EndVertical(); } } GUILayout.Space(20); m_dark_style.fontSize = 17; EditorGUILayout.LabelField("Texture Atlas", m_dark_style); m_dark_style.fontSize = 15; GUILayout.Space(20); for (int a = 0; a < RoveMaterials.ATLAS_COUNT; ++a) { EditorGUILayout.BeginVertical("box"); { EditorGUILayout.BeginVertical(m_dark_style); { string atlas_type; if (a == RoveAtlas.TYPE_ALBEDO) { atlas_type = "Albedo"; } else if (a == RoveAtlas.TYPE_NORMAL) { atlas_type = "Normal"; } else if (a == RoveAtlas.TYPE_METALLIC) { atlas_type = "Metallic"; } else if (a == RoveAtlas.TYPE_EMISSION) { atlas_type = "Emissive"; } else { atlas_type = "Undefined"; } RoveAtlas atlas = rove_materials.m_atlases[a]; m_dark_style.normal.background = m_gbg; EditorGUILayout.BeginHorizontal(m_dark_style); { m_button_style.alignment = TextAnchor.MiddleCenter; if (GUILayout.Button(atlas_type, m_button_style)) { m_expand_atlas[a] = !m_expand_atlas[a]; } m_button_style.alignment = TextAnchor.MiddleLeft; EditorGUILayout.EndHorizontal(); } m_dark_style.normal.background = m_bg; if (m_expand_atlas[a]) { GUILayout.Space(20); EditorGUILayout.BeginHorizontal(m_dark_style); { GUILayout.Space((EditorGUIUtility.currentViewWidth / 2) - (128 / 2) - 25); EditorGUILayout.LabelField(new GUIContent(atlas.m_texture), m_dark_style, GUILayout.Width(128), GUILayout.Height(128)); EditorGUILayout.EndHorizontal(); } GUILayout.Space(20); EditorGUILayout.LabelField(atlas.m_width + " x " + atlas.m_height, m_dark_style); GUILayout.Space(20); } EditorGUILayout.EndVertical(); } EditorGUILayout.EndVertical(); } } m_dark_style.fontSize = 12; m_dark_style.alignment = TextAnchor.MiddleLeft; } EditorGUILayout.EndVertical(); } serializedObject.ApplyModifiedProperties(); }
/// public bool Repack() { RoveMaterials rove_materials = RoveSetup.GetAPIComponent <RoveMaterials>(); if (!rove_materials) { Debug.LogError("Rove: Failed to begin to process atlas."); return(false); } List <RoveMaterial> materials = rove_materials.m_materials; List <int> dirty_textures = new List <int>(); int area = 0; for (int t = 0; t < materials.Count; ++t) { RoveTexture rove_texture = materials[t].m_textures[m_type]; uint status = rove_texture.m_status; if ((rove_texture.m_texture == null) || (status == RoveTexture.STATUS_EMPTY)) { continue; } dirty_textures.Add(t); area += rove_texture.m_texture.width * rove_texture.m_texture.height; } int predicted_size = RoveAtlas.PredictSize(area); if (predicted_size > m_width) { if (predicted_size > 16384) { Debug.LogError("Rove: Predicted atlas exceeds 16384 x 16384, " + "cannot create atlas."); return(false); } Reset(predicted_size, predicted_size); } else { Reset(predicted_size, predicted_size); } if (dirty_textures.Count > 1) { dirty_textures.Sort((a, b) => { RoveTexture rove_texture_a = materials[a].m_textures[m_type]; RoveTexture rove_texture_b = materials[b].m_textures[m_type]; int a_size = rove_texture_a.m_texture.width * rove_texture_a.m_texture.height; int b_size = rove_texture_b.m_texture.width * rove_texture_b.m_texture.height; return(b_size.CompareTo(a_size)); }); } bool packed = false; while (!packed && (m_width <= 16384) && (m_height <= 16384)) { for (int t = 0; t < dirty_textures.Count; ++t) { RoveMaterial material = materials[dirty_textures[t]]; RoveTexture rove_texture = material.m_textures[m_type]; if (!Pack(rove_texture)) { Reset(m_width * 2, m_height * 2); break; } else { material.SetAtlasRect(m_type, rove_texture.m_atlas_rect); rove_texture.m_status = RoveTexture.STATUS_OK; } if (t == (dirty_textures.Count - 1)) { packed = true; } } } if (!packed) { Debug.LogError("Rove: Failed to repack atlas: " + m_type + "."); return(false); } return(true); }
/// public bool ProcessAtlas() { if (!m_dirty) { return(true); } RoveMaterials rove_materials = RoveSetup.GetAPIComponent <RoveMaterials>(); if (!rove_materials) { Debug.LogError("Rove: Failed to begin to process atlas."); return(false); } List <RoveMaterial> materials = rove_materials.m_materials; uint map_flag = 0x0; if (m_type == RoveAtlas.TYPE_ALBEDO) { map_flag = RoveMaterials.MAP_FLAG_ALBEDO; } else if (m_type == RoveAtlas.TYPE_NORMAL) { map_flag = RoveMaterials.MAP_FLAG_NORMAL; } else if (m_type == RoveAtlas.TYPE_METALLIC) { map_flag = RoveMaterials.MAP_FLAG_METALLIC; } else if (m_type == RoveAtlas.TYPE_EMISSION) { map_flag = RoveMaterials.MAP_FLAG_EMISSION; } List <int> dirty_textures = new List <int>(); int area = 0; for (int m = 0; m < materials.Count; ++m) { RoveMaterial material = materials[m]; RoveTexture rove_texture = material.m_textures[m_type]; uint status = rove_texture.m_status; if ((status == RoveTexture.STATUS_EMPTY) || (status == RoveTexture.STATUS_OK)) { continue; } else if (status == RoveTexture.STATUS_CHANGE) { Remove(rove_texture); if (rove_texture.m_texture == null) { rove_texture.m_status = RoveTexture.STATUS_EMPTY; material.m_map_flags &= ~map_flag; material.SetMapFlags(material.m_map_flags); continue; } } material.m_map_flags |= map_flag; material.SetMapFlags(material.m_map_flags); area += rove_texture.m_texture.width * rove_texture.m_texture.height; dirty_textures.Add(m); } int predicted_size = RoveAtlas.PredictSize(area); if (predicted_size > m_width) { if (predicted_size > 16384) { Debug.LogError("Rove: Predicted atlas exceeds 16384 x 16384, " + "cannot create atlas."); return(false); } m_width = m_height = predicted_size; if (!Repack()) { return(false); } } else { if (dirty_textures.Count > 1) { dirty_textures.Sort((a, b) => { RoveTexture rove_texture_a = materials[a].m_textures[m_type]; RoveTexture rove_texture_b = materials[b].m_textures[m_type]; int a_size = rove_texture_a.m_texture.width * rove_texture_a.m_texture.height; int b_size = rove_texture_b.m_texture.width * rove_texture_b.m_texture.height; return(b_size.CompareTo(a_size)); }); } for (int t = 0; t < dirty_textures.Count; ++t) { RoveMaterial material = materials[dirty_textures[t]]; RoveTexture rove_texture = material.m_textures[m_type]; if (!Pack(rove_texture)) { if (!Repack()) { return(false); } else { break; } } else { material.SetAtlasRect(m_type, rove_texture.m_atlas_rect); rove_texture.m_status = RoveTexture.STATUS_OK; } } } RenderTexture temp_rt = RenderTexture.GetTemporary(m_width, m_height, 0, RenderTextureFormat.ARGB32); RenderTexture.active = temp_rt; GL.PushMatrix(); GL.LoadPixelMatrix(0, m_width, m_height, 0); GL.Clear(false, true, new Color(0.0f, 0.0f, 0.0f, 1.0f)); for (int m = 0; m < materials.Count; ++m) { Texture2D texture = materials[m].m_textures[m_type].m_texture; if (texture == null) { continue; } Rect bl_origin = new Rect(materials[m].m_textures[m_type].m_atlas_rect); bl_origin.y = (m_height - bl_origin.y) - bl_origin.height; m_atlas_material.SetTexture("target", materials[m].m_textures[m_type].m_texture); Graphics.DrawTexture(bl_origin, texture, m_atlas_material); } GL.PopMatrix(); m_texture = new Texture2D(m_width, m_height, TextureFormat.ARGB32, false); m_texture.ReadPixels(new Rect(0, 0, m_width, m_height), 0, 0, false); m_texture.Apply(); RenderTexture.active = null; RenderTexture.ReleaseTemporary(temp_rt); UploadAtlas(); m_dirty = false; return(true); }