/// 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[] wbgp = new Color[1]; wbgp[0].r = 0.44f; wbgp[0].g = 0.44f; wbgp[0].b = 0.44f; wbgp[0].a = 1.0f; m_wbg = new Texture2D(1, 1); m_wbg.SetPixels(wbgp); m_wbg.wrapMode = TextureWrapMode.Repeat; m_wbg.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; RoveMaterial rove_material = (RoveMaterial)target; m_material_name = Path.GetFileNameWithoutExtension( AssetDatabase.GetAssetPath(rove_material)); mp_folded_out = serializedObject.FindProperty("m_folded_out"); mp_double_sided = serializedObject.FindProperty("m_double_sided"); mp_albedo = serializedObject.FindProperty("m_albedo"); mp_metallic = serializedObject.FindProperty("m_metallic"); mp_smoothness = serializedObject.FindProperty("m_smoothness"); mp_glass = serializedObject.FindProperty("m_glass"); mp_emission = serializedObject.FindProperty("m_emission"); SerializedProperty textures = serializedObject.FindProperty("m_textures"); mp_albedo_texture = textures.GetArrayElementAtIndex((int)RoveAtlas.TYPE_ALBEDO); mp_normal_texture = textures.GetArrayElementAtIndex((int)RoveAtlas.TYPE_NORMAL); mp_metallic_texture = textures.GetArrayElementAtIndex((int)RoveAtlas.TYPE_METALLIC); mp_emission_texture = textures.GetArrayElementAtIndex((int)RoveAtlas.TYPE_EMISSION); mp_albedo_texture2d = mp_albedo_texture.FindPropertyRelative("m_texture"); mp_normal_texture2d = mp_normal_texture.FindPropertyRelative("m_texture"); mp_metallic_texture2d = mp_metallic_texture.FindPropertyRelative("m_texture"); mp_emission_texture2d = mp_emission_texture.FindPropertyRelative("m_texture"); serializedObject.Update(); serializedObject.ApplyModifiedProperties(); }
/// public void ChangeSubMeshMaterial(int sub_mesh_index, RoveMaterial material) { uint material_index; if (material != null) { material_index = m_rove_materials.AddMaterial(m_materials[sub_mesh_index]); if (material_index == RoveSetup.ERROR) { m_rove_setup.m_setup_success = false; Destroy(m_rove_setup); return; } } else { material_index = 0; } RoveInternal.ChangeSubMeshMaterial(m_mesh_id, (uint)sub_mesh_index, material_index); }
/// 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(); RoveMaterial rove_material = (RoveMaterial)target; 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("Material", 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("box"); { EditorGUILayout.BeginVertical(m_dark_style); { GUILayout.Space(15); m_dark_style.fontSize = 17; m_dark_style.alignment = TextAnchor.MiddleCenter; EditorGUILayout.LabelField(m_material_name, m_dark_style); m_dark_style.fontSize = 12; m_dark_style.alignment = TextAnchor.MiddleLeft; GUILayout.Space(15); EditorGUILayout.EndVertical(); } EditorGUILayout.EndVertical(); } InspectorProperties(false, 0); }
/// public RoveMaterial InspectorProperties(bool folded, int sub_material_index) { 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; } RoveMaterial rove_material = (RoveMaterial)target; EditorGUILayout.BeginVertical("box"); { EditorGUILayout.BeginVertical(m_dark_style); { bool fold_out = true; if (folded) { fold_out = mp_folded_out.boolValue; Rect drop_rect = EditorGUILayout.BeginHorizontal(m_dark_style); { m_dark_style.normal.background = m_gbg; EditorGUILayout.BeginHorizontal(m_dark_style); { if (GUILayout.Button(m_material_name, m_button_style)) { mp_folded_out.boolValue = !mp_folded_out.boolValue; } if (GUILayout.Button("Select", GUILayout.MaxWidth(75.0f))) { EditorGUIUtility.ShowObjectPicker <RoveMaterial>(target, false, " ", sub_material_index); } EditorGUILayout.EndHorizontal(); } m_dark_style.normal.background = m_bg; EditorGUILayout.EndHorizontal(); } if (Event.current.type == EventType.DragUpdated) { if (drop_rect.Contains(Event.current.mousePosition)) { if (DragAndDrop.objectReferences.Length == 1 && (DragAndDrop.objectReferences[0].GetType()) == typeof(RoveMaterial)) { DragAndDrop.visualMode = DragAndDropVisualMode.Link; } else { DragAndDrop.visualMode = DragAndDropVisualMode.Rejected; } } } else if (Event.current.type == EventType.DragPerform) { if (drop_rect.Contains(Event.current.mousePosition)) { if (DragAndDrop.objectReferences.Length == 1 && (DragAndDrop.objectReferences[0].GetType()) == typeof(RoveMaterial)) { DragAndDrop.AcceptDrag(); RoveMaterial dropped_material = (RoveMaterial)DragAndDrop.objectReferences[0]; if (dropped_material != null) { dropped_material.m_folded_out = mp_folded_out.boolValue; } EditorGUILayout.EndVertical(); EditorGUILayout.EndVertical(); return(dropped_material); } } } else if (Event.current.commandName == "ObjectSelectorUpdated") { if (EditorGUIUtility.GetObjectPickerControlID() == sub_material_index) { RoveMaterial chosen_material = (RoveMaterial)EditorGUIUtility.GetObjectPickerObject(); if (chosen_material != null) { chosen_material.m_folded_out = mp_folded_out.boolValue; } EditorGUILayout.EndVertical(); EditorGUILayout.EndVertical(); return(chosen_material); } } } if (fold_out) { GUILayout.Space(20); EditorGUILayout.BeginHorizontal(); { EditorGUILayout.LabelField("Type: ", m_dark_style); int choice = (mp_glass.floatValue > 0.0f) ? 1 : 0; EditorGUI.BeginChangeCheck(); { choice = EditorGUILayout.Popup(choice, m_available_types); mp_glass.floatValue = (choice == 0) ? 0.0f : 1.0f; if (Application.isPlaying && EditorGUI.EndChangeCheck()) { rove_material.SetGlass(mp_glass.floatValue); } } GUILayout.FlexibleSpace(); EditorGUILayout.EndHorizontal(); } GUILayout.Space(20); EditorGUILayout.BeginHorizontal(m_dark_style); { EditorGUILayout.BeginVertical(m_dark_style); { EditorGUILayout.BeginHorizontal(m_dark_style); { m_dark_style.normal.background = m_wbg; EditorGUILayout.BeginVertical(m_dark_style); { EditorGUI.BeginChangeCheck(); { mp_albedo_texture2d.objectReferenceValue = EditorGUILayout.ObjectField( mp_albedo_texture2d.objectReferenceValue, typeof(Texture2D), false, GUILayout.MaxWidth(30.0f)); if (Application.isPlaying && EditorGUI.EndChangeCheck()) { rove_material.SetTexture(RoveAtlas.TYPE_ALBEDO, (Texture2D)mp_albedo_texture2d.objectReferenceValue); serializedObject.Update(); } } EditorGUILayout.EndVertical(); } m_dark_style.normal.background = m_bg; EditorGUILayout.LabelField("Albedo: ", m_dark_style, GUILayout.MaxWidth(90.0f)); GUILayout.FlexibleSpace(); EditorGUILayout.EndHorizontal(); } EditorGUILayout.Space(); if (mp_glass.floatValue == 0.0f) { EditorGUILayout.BeginHorizontal(m_dark_style); { m_dark_style.normal.background = m_wbg; EditorGUILayout.BeginVertical(m_dark_style); { EditorGUI.BeginChangeCheck(); { mp_metallic_texture2d.objectReferenceValue = EditorGUILayout.ObjectField( mp_metallic_texture2d.objectReferenceValue, typeof(Texture2D), false, GUILayout.MaxWidth(30.0f)); if (Application.isPlaying && EditorGUI.EndChangeCheck()) { rove_material.SetTexture(RoveAtlas.TYPE_METALLIC, (Texture2D)mp_metallic_texture2d.objectReferenceValue); serializedObject.Update(); } } EditorGUILayout.EndVertical(); } m_dark_style.normal.background = m_bg; EditorGUILayout.LabelField("Metallic: ", m_dark_style, GUILayout.MaxWidth(90.0f)); GUILayout.FlexibleSpace(); EditorGUILayout.EndHorizontal(); } EditorGUILayout.Space(); EditorGUILayout.BeginHorizontal(m_dark_style); { GUILayout.FlexibleSpace(); EditorGUILayout.LabelField("Smoothness: ", m_dark_style, GUILayout.MaxWidth(90.0f)); EditorGUILayout.EndHorizontal(); } EditorGUILayout.Space(); } else { EditorGUILayout.BeginHorizontal(m_dark_style); { m_dark_style.normal.background = m_wbg; EditorGUILayout.BeginVertical(m_dark_style); { EditorGUI.BeginChangeCheck(); { mp_metallic_texture2d.objectReferenceValue = EditorGUILayout.ObjectField( mp_metallic_texture2d.objectReferenceValue, typeof(Texture2D), false, GUILayout.MaxWidth(30.0f)); if (Application.isPlaying && EditorGUI.EndChangeCheck()) { rove_material.SetTexture(RoveAtlas.TYPE_METALLIC, (Texture2D)mp_metallic_texture2d.objectReferenceValue); serializedObject.Update(); } } EditorGUILayout.EndVertical(); } m_dark_style.normal.background = m_bg; EditorGUILayout.LabelField("Smoothness: ", m_dark_style, GUILayout.MaxWidth(90.0f)); GUILayout.FlexibleSpace(); EditorGUILayout.EndHorizontal(); } EditorGUILayout.Space(); } EditorGUILayout.BeginHorizontal(m_dark_style); { GUILayout.FlexibleSpace(); EditorGUILayout.LabelField("Sources:", m_dark_style, GUILayout.MaxWidth(90.0f)); EditorGUILayout.EndHorizontal(); } EditorGUILayout.Space(); EditorGUILayout.BeginHorizontal(m_dark_style); { m_dark_style.normal.background = m_wbg; EditorGUILayout.BeginVertical(m_dark_style); { EditorGUI.BeginChangeCheck(); { mp_normal_texture2d.objectReferenceValue = EditorGUILayout.ObjectField( mp_normal_texture2d.objectReferenceValue, typeof(Texture2D), false, GUILayout.MaxWidth(30.0f)); if (Application.isPlaying && EditorGUI.EndChangeCheck()) { rove_material.SetTexture(RoveAtlas.TYPE_NORMAL, (Texture2D)mp_normal_texture2d.objectReferenceValue); serializedObject.Update(); } } EditorGUILayout.EndVertical(); } m_dark_style.normal.background = m_bg; EditorGUILayout.LabelField("Normal Map: ", m_dark_style, GUILayout.MaxWidth(90.0f)); GUILayout.FlexibleSpace(); EditorGUILayout.EndHorizontal(); } EditorGUILayout.Space(); EditorGUILayout.BeginHorizontal(m_dark_style); { m_dark_style.normal.background = m_wbg; EditorGUILayout.BeginVertical(m_dark_style); { EditorGUI.BeginChangeCheck(); { mp_emission_texture2d.objectReferenceValue = EditorGUILayout.ObjectField( mp_emission_texture2d.objectReferenceValue, typeof(Texture2D), false, GUILayout.MaxWidth(30.0f)); if (Application.isPlaying && EditorGUI.EndChangeCheck()) { rove_material.SetTexture(RoveAtlas.TYPE_EMISSION, (Texture2D)mp_emission_texture2d.objectReferenceValue); serializedObject.Update(); } } EditorGUILayout.EndVertical(); } m_dark_style.normal.background = m_bg; EditorGUILayout.LabelField("Emission: ", m_dark_style, GUILayout.MaxWidth(90.0f)); GUILayout.FlexibleSpace(); EditorGUILayout.EndHorizontal(); } EditorGUILayout.Space(); EditorGUILayout.BeginHorizontal(m_dark_style); { EditorGUILayout.LabelField("Double-sided: ", m_dark_style, GUILayout.MaxWidth(90.0f)); EditorGUILayout.EndHorizontal(); } EditorGUILayout.EndVertical(); } /// EditorGUILayout.BeginVertical(m_dark_style); { EditorGUILayout.BeginHorizontal(m_dark_style); { EditorGUI.BeginChangeCheck(); { mp_albedo.colorValue = EditorGUILayout.ColorField( mp_albedo.colorValue, GUILayout.MaxWidth(50.0f)); if (Application.isPlaying && EditorGUI.EndChangeCheck()) { rove_material.SetAlbedo(mp_albedo.colorValue); } } GUILayout.FlexibleSpace(); EditorGUILayout.EndHorizontal(); } EditorGUILayout.Space(); if (mp_glass.floatValue == 0.0f) { EditorGUILayout.BeginHorizontal(m_dark_style); { EditorGUI.BeginChangeCheck(); { mp_metallic.floatValue = EditorGUILayout.Slider(mp_metallic.floatValue, 0.0f, 1.0f); if (Application.isPlaying && EditorGUI.EndChangeCheck()) { rove_material.SetMetallic(mp_metallic.floatValue); } } GUILayout.FlexibleSpace(); EditorGUILayout.EndHorizontal(); } EditorGUILayout.Space(); } EditorGUILayout.BeginHorizontal(m_dark_style); { EditorGUI.BeginChangeCheck(); { mp_smoothness.floatValue = EditorGUILayout.Slider(mp_smoothness.floatValue, 0.0f, 1.0f); if (Application.isPlaying && EditorGUI.EndChangeCheck()) { rove_material.SetSmoothness(mp_smoothness.floatValue); } } GUILayout.FlexibleSpace(); EditorGUILayout.EndHorizontal(); } EditorGUILayout.Space(); EditorGUILayout.BeginHorizontal(m_dark_style); { int choice = 0; if (mp_glass.floatValue == 0.0f) { choice = EditorGUILayout.Popup(choice, m_available_sources); } else { choice = EditorGUILayout.Popup(choice, m_available_glass_sources); } GUILayout.FlexibleSpace(); EditorGUILayout.EndHorizontal(); } EditorGUILayout.Space(); EditorGUILayout.BeginHorizontal(m_dark_style); { int choice = 0; choice = EditorGUILayout.Popup(choice, m_available_tangents); GUILayout.FlexibleSpace(); EditorGUILayout.EndHorizontal(); } EditorGUILayout.Space(); EditorGUILayout.BeginHorizontal(m_dark_style); { EditorGUI.BeginChangeCheck(); { mp_emission.floatValue = EditorGUILayout.Slider(mp_emission.floatValue, 0.0f, 1.0f); if (Application.isPlaying && EditorGUI.EndChangeCheck()) { rove_material.SetEmission(mp_emission.floatValue); } } GUILayout.FlexibleSpace(); EditorGUILayout.EndHorizontal(); } EditorGUILayout.Space(); EditorGUILayout.BeginHorizontal(m_dark_style); { EditorGUI.BeginChangeCheck(); { mp_double_sided.boolValue = EditorGUILayout.Toggle(mp_double_sided.boolValue); if (Application.isPlaying && EditorGUI.EndChangeCheck()) { rove_material.SetDoubleSided(mp_double_sided.boolValue); } } GUILayout.FlexibleSpace(); EditorGUILayout.EndHorizontal(); } EditorGUILayout.EndVertical(); } EditorGUILayout.EndHorizontal(); } GUILayout.Space(15); } EditorGUILayout.EndVertical(); } EditorGUILayout.EndVertical(); } serializedObject.ApplyModifiedProperties(); return((RoveMaterial)target); }
/// public override void OnInspectorGUI() { serializedObject.Update(); RoveObject rove_object = (RoveObject)target; 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("Object", 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); { GUILayout.Space(15); m_dark_style.fontSize = 17; m_dark_style.alignment = TextAnchor.MiddleCenter; EditorGUILayout.LabelField("Sub-Mesh Materials", m_dark_style); m_dark_style.fontSize = 12; m_dark_style.alignment = TextAnchor.MiddleLeft; GUILayout.Space(15); for (int m = 0; m < mp_materials.arraySize; ++m) { SerializedProperty p_material = mp_materials.GetArrayElementAtIndex(m); RoveMaterial material = (RoveMaterial)p_material.objectReferenceValue; if (material == null) { EditorGUILayout.BeginVertical("box"); { EditorGUILayout.BeginVertical(m_dark_style); { Rect drop_rect = EditorGUILayout.BeginHorizontal(m_dark_style); { m_dark_style.normal.background = m_gbg; EditorGUILayout.BeginHorizontal(m_dark_style); { GUIStyle button_style = new GUIStyle(GUI.skin.button); button_style.fontSize = 13; button_style.alignment = TextAnchor.MiddleLeft; button_style.normal.background = m_gbg; button_style.normal.textColor = Color.white; button_style.active.background = m_gbg; button_style.active.textColor = Color.white; if (GUILayout.Button("Default Material", button_style)) { } if (GUILayout.Button("Select", GUILayout.MaxWidth(75.0f))) { EditorGUIUtility.ShowObjectPicker <RoveMaterial>(target, false, " ", m); } EditorGUILayout.EndHorizontal(); } m_dark_style.normal.background = m_bg; EditorGUILayout.EndHorizontal(); } if (Event.current.type == EventType.DragUpdated) { if (drop_rect.Contains(Event.current.mousePosition)) { if (DragAndDrop.objectReferences.Length == 1 && (DragAndDrop.objectReferences[0].GetType()) == typeof(RoveMaterial)) { DragAndDrop.visualMode = DragAndDropVisualMode.Link; } else { DragAndDrop.visualMode = DragAndDropVisualMode.Rejected; } } } else if (Event.current.type == EventType.DragPerform) { if (drop_rect.Contains(Event.current.mousePosition)) { if (DragAndDrop.objectReferences.Length == 1 && (DragAndDrop.objectReferences[0].GetType()) == typeof(RoveMaterial)) { DragAndDrop.AcceptDrag(); RoveMaterial dropped_material = (RoveMaterial)DragAndDrop.objectReferences[0]; p_material.objectReferenceValue = dropped_material; rove_object.m_materials[m] = dropped_material; if (Application.isPlaying) { rove_object.ChangeSubMeshMaterial(m, dropped_material); } } } } else if (Event.current.commandName == "ObjectSelectorUpdated") { if (EditorGUIUtility.GetObjectPickerControlID() == m) { RoveMaterial chosen_material = (RoveMaterial)EditorGUIUtility.GetObjectPickerObject(); p_material.objectReferenceValue = chosen_material; rove_object.m_materials[m] = chosen_material; if (Application.isPlaying) { rove_object.ChangeSubMeshMaterial(m, chosen_material); } } } EditorGUILayout.EndVertical(); } EditorGUILayout.EndVertical(); } } else { RoveMaterialEditor material_editor = (RoveMaterialEditor)RoveMaterialEditor.CreateEditor( material, typeof(RoveMaterialEditor)); RoveMaterial new_material = material_editor.InspectorProperties(true, m); if (new_material != material) { p_material.objectReferenceValue = new_material; rove_object.m_materials[m] = new_material; if (Application.isPlaying) { rove_object.ChangeSubMeshMaterial(m, new_material); } } } } EditorGUILayout.EndVertical(); } serializedObject.ApplyModifiedProperties(); }
/// public uint AddMaterial(RoveMaterial material) { int index = 0; for (int m = 0; m < m_materials.Count; ++m) { if (material == m_materials[m]) { index = m + 1; } } if (index == 0) { index = m_materials.Count + 1; float[] albedo = new float[4]; albedo[0] = material.m_albedo.r; albedo[1] = material.m_albedo.g; albedo[2] = material.m_albedo.b; albedo[3] = material.m_albedo.a; // Map flags and bounds will be set during atlas processing. material.m_map_flags = 0x0; float[] albedo_bounds = new float[4]; float[] normal_bounds = new float[4]; float[] metallic_bounds = new float[4]; float[] emission_bounds = new float[4]; for (uint m = 0; m < ATLAS_COUNT; ++m) { if (material.m_textures[m].m_texture == null) { material.m_textures[m].m_status = RoveTexture.STATUS_EMPTY; } else { material.m_textures[m].m_status = RoveTexture.STATUS_NEW; } } uint mat_id = RoveInternal.AddMaterial(material.m_map_flags, material.m_double_sided, albedo, material.m_metallic, material.m_smoothness, material.m_glass, material.m_emission, albedo_bounds, normal_bounds, metallic_bounds, emission_bounds); if ((mat_id == RoveSetup.ERROR) || (mat_id != index)) { Debug.LogError("Rove: Failed to add material; check 'Rove/Data/rove.log' " + "for details"); return(RoveSetup.ERROR); } material.m_id = mat_id; m_materials.Add(material); for (int a = 0; a < ATLAS_COUNT; ++a) { m_atlases[a].m_dirty = true; } } return((uint)index); }
/// 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); }