Exemplo n.º 1
0
    ///
    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;
    }
Exemplo n.º 2
0
 ///
 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;
 }
Exemplo n.º 3
0
 ///
 void Start()
 {
     m_rove_setup = RoveSetup.GetAPIComponent <RoveSetup>();
     if (!m_rove_setup)
     {
         Debug.LogError("Rove: Could not find an active RoveSetup component.");
         Destroy(this);
         return;
     }
     m_global_framecount = 0;
 }
Exemplo n.º 4
0
 ///
 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;
     }
 }
Exemplo n.º 5
0
 ///
 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_sun_light = this.GetComponent <Light>();
     if (!m_sun_light)
     {
         Debug.LogError("Rove: Could not find a light component for sun.");
         Destroy(this);
         return;
     }
     m_atlas_material = (Material)Resources.Load("RoveAtlasMaterial");
     gameObject.transform.hasChanged = true;
 }
Exemplo n.º 6
0
    ///
    public override void OnInspectorGUI()
    {
        serializedObject.Update();
        RoveSetup rove_setup = (RoveSetup)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("Setup", 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.BeginHorizontal(m_dark_style);
        {
            GUILayout.Space(15);
            if (!Application.isPlaying && GUILayout.Button("Setup Scene"))
            {
                MeshRenderer[] found_renderers =
                    FindObjectsOfType(typeof(MeshRenderer)) as MeshRenderer[];
                for (uint r = 0; r < found_renderers.Length; ++r)
                {
                    GameObject o = found_renderers[r].gameObject;
                    if (!o.GetComponent(typeof(RoveObject)))
                    {
                        o.AddComponent(typeof(RoveObject));
                    }
                }
            }
            if (GUILayout.Button("Enable Rove"))
            {
                RoveSetup.EnableRove();
            }
            if (GUILayout.Button("Disable Rove"))
            {
                RoveSetup.DisableRove();
            }
            GUILayout.Space(15);
            EditorGUILayout.EndHorizontal();
        }
        EditorGUILayout.BeginVertical("box");
        {
            EditorGUILayout.BeginVertical(m_dark_style);
            {
                EditorGUILayout.Space();
                EditorGUILayout.Space();
                if (!Application.isPlaying)
                {
                    EditorGUILayout.BeginHorizontal(m_dark_style);
                    {
                        EditorGUILayout.LabelField("Optimization Level: ", m_dark_style,
                                                   GUILayout.MaxWidth(150.0f));
                        GUILayout.FlexibleSpace();
                        mp_optimization_level.intValue =
                            EditorGUILayout.Popup(mp_optimization_level.intValue,
                                                  m_available_optimizations);
                        EditorGUILayout.EndHorizontal();
                    }
                    EditorGUILayout.Space();
                }
                EditorGUILayout.BeginHorizontal(m_dark_style);
                {
                    EditorGUILayout.LabelField("Downsample: ", m_dark_style,
                                               GUILayout.MaxWidth(150.0f));
                    GUILayout.FlexibleSpace();
                    EditorGUI.BeginChangeCheck();
                    {
                        mp_target_downsample.intValue =
                            EditorGUILayout.IntField(mp_target_downsample.intValue);
                        if (mp_target_downsample.intValue < 0)
                        {
                            mp_target_downsample.intValue = 0;
                        }
                        if (Application.isPlaying && EditorGUI.EndChangeCheck())
                        {
                            serializedObject.ApplyModifiedProperties();
                            rove_setup.SetTargetDownsample(mp_target_downsample.intValue);
                        }
                    }
                    EditorGUILayout.EndHorizontal();
                }
                EditorGUILayout.Space();
                EditorGUILayout.BeginHorizontal(m_dark_style);
                {
                    EditorGUILayout.LabelField("Max bounces: ", m_dark_style,
                                               GUILayout.MaxWidth(150.0f));
                    GUILayout.FlexibleSpace();
                    EditorGUI.BeginChangeCheck();
                    {
                        mp_max_bounces.intValue =
                            EditorGUILayout.IntField(mp_max_bounces.intValue);
                        if (mp_max_bounces.intValue <= 0)
                        {
                            mp_max_bounces.intValue = 1;
                        }
                        if (Application.isPlaying && EditorGUI.EndChangeCheck())
                        {
                            rove_setup.SetMaxBounces(mp_max_bounces.intValue);
                        }
                    }
                    EditorGUILayout.EndHorizontal();
                }
                EditorGUILayout.Space();
                EditorGUILayout.BeginHorizontal(m_dark_style);
                {
                    EditorGUILayout.LabelField("SPP/Frame: ", m_dark_style,
                                               GUILayout.MaxWidth(150.0f));
                    GUILayout.FlexibleSpace();
                    EditorGUI.BeginChangeCheck();
                    {
                        mp_samples_per_frame.intValue =
                            EditorGUILayout.IntField(mp_samples_per_frame.intValue);
                        if (mp_samples_per_frame.intValue <= 0)
                        {
                            mp_samples_per_frame.intValue = 1;
                        }
                        if (Application.isPlaying && EditorGUI.EndChangeCheck())
                        {
                            rove_setup.SetSamplesPerFrame(mp_samples_per_frame.intValue);
                        }
                    }
                    EditorGUILayout.EndHorizontal();
                }
                EditorGUILayout.Space();
                if (!Application.isPlaying)
                {
                    EditorGUILayout.BeginHorizontal(m_dark_style);
                    {
                        EditorGUILayout.LabelField("Force OpenCL: ", m_dark_style,
                                                   GUILayout.MaxWidth(150.0f));
                        GUILayout.FlexibleSpace();
                        mp_force_opencl.boolValue =
                            EditorGUILayout.Toggle(mp_force_opencl.boolValue);
                        EditorGUILayout.EndHorizontal();
                    }
                }
                EditorGUILayout.Space();
                if (!Application.isPlaying)
                {
                    EditorGUILayout.BeginHorizontal(m_dark_style);
                    {
                        EditorGUILayout.LabelField("Allow iGPU: ", m_dark_style,
                                                   GUILayout.MaxWidth(150.0f));
                        GUILayout.FlexibleSpace();
                        mp_use_integrated_device.boolValue =
                            EditorGUILayout.Toggle(mp_use_integrated_device.boolValue);
                        EditorGUILayout.EndHorizontal();
                    }
                }
                EditorGUILayout.Space();
                EditorGUILayout.EndVertical();
            }
            EditorGUILayout.EndVertical();
        }
        if (Application.isPlaying)
        {
            EditorGUILayout.BeginVertical("box");
            {
                EditorGUILayout.BeginVertical(m_dark_style);
                {
                    GUILayout.Space(15);
                    m_dark_style.fontSize  = 16;
                    m_dark_style.alignment = TextAnchor.MiddleCenter;
                    EditorGUILayout.LabelField("System Info", m_dark_style);
                    m_dark_style.fontSize  = 12;
                    m_dark_style.alignment = TextAnchor.MiddleLeft;
                    GUILayout.Space(15);
                    EditorGUILayout.LabelField("CPU threads: " +
                                               rove_setup.m_system_info.m_thread_count +
                                               ".",
                                               m_dark_style);
                    if (rove_setup.m_system_info.m_compute_api == RoveSetup.CL)
                    {
                        EditorGUILayout.LabelField("Compute API: OpenCL.", m_dark_style);
                    }
                    else if (rove_setup.m_system_info.m_compute_api == RoveSetup.CUDA)
                    {
                        EditorGUILayout.LabelField("Compute API: CUDA.", m_dark_style);
                    }
                    EditorGUILayout.LabelField("Active device count: " +
                                               rove_setup.m_system_info.m_device_count + ".",
                                               m_dark_style);
                    EditorGUILayout.LabelField("---", m_dark_style);
                    for (uint d = 0; d < rove_setup.m_system_info.m_device_count; ++d)
                    {
                        EditorGUILayout.LabelField(
                            d + ": " + rove_setup.m_system_info.m_device_names[d],
                            m_dark_style);
                    }
                    GUILayout.Space(15);
                    EditorGUILayout.EndVertical();
                }
                EditorGUILayout.EndVertical();
            }
        }
        serializedObject.ApplyModifiedProperties();
    }
Exemplo n.º 7
0
    ///
    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);
    }
Exemplo n.º 8
0
    ///
    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);
    }