Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

3d render inside inspector always black

Discussion in 'Scripting' started by rogermiranda1000, Jan 24, 2022.

  1. rogermiranda1000

    rogermiranda1000

    Joined:
    Nov 5, 2016
    Posts:
    5
    I'm trying to display a 3d object inside the inspector, but it only shows black pixels.
    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using UnityEditor;
    4. using System.Collections;
    5. using System.Collections.Generic;
    6. using System.Threading;
    7.  
    8. [CustomEditor(typeof(Biomes))]
    9. public class BiomesEditor : Editor {
    10.     private static int size = 256;
    11.  
    12.     // from https://docs.unity3d.com/ScriptReference/RenderTexture-active.html
    13.     static public Texture2D GetRTPixels(RenderTexture rt) {
    14.         RenderTexture currentActiveRT = RenderTexture.active;
    15.         RenderTexture.active = rt;
    16.         tex.ReadPixels(new Rect(0, 0, tex.width, tex.height), 0, 0);
    17.         tex.Apply();
    18.         RenderTexture.active = currentActiveRT;
    19.         return tex;
    20.     }
    21.  
    22.     private void RenderBiomesPreview(Biomes edit) {
    23.         RenderTexture render = new RenderTexture(size, size, 16, RenderTextureFormat.ARGB32);
    24.         if (!render.Create()) return;
    25.         List<GameObject> destroyables = new List<GameObject>();
    26.         destroyables.Add(new GameObject());
    27.         Camera camera = destroyables[0].AddComponent<Camera>();
    28.         destroyables[0].transform.position = new Vector3(0f, 1000f, -5f);
    29.         camera.targetTexture = render;
    30.         destroyables.Add(Object.Instantiate(edit.cube));
    31.         destroyables[1].transform.position = new Vector3(0f, 1000f, 0f);
    32.  
    33.  
    34.         EditorGUI.BeginDisabledGroup(true);
    35.         Texture2D render_texture = BiomesEditor.GetRTPixels(render);
    36.         EditorGUILayout.ObjectField(render_texture, typeof(Texture2D), false, GUILayout.Width(render_texture.width), GUILayout.Height(render_texture.height));
    37.         EditorGUI.EndDisabledGroup();
    38.  
    39.         render.Release();
    40.         foreach (GameObject obj in destroyables) Object.DestroyImmediate(obj);
    41.     }
    42.  
    43.     public override void OnInspectorGUI() {
    44.         Biomes edit = (Biomes)target;
    45.  
    46.         DrawDefaultInspector();
    47.  
    48.         RenderBiomesPreview(edit);
    49.     }
    50. }
    51.  
    Relevant fragment from Biomes.cs:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. [CreateAssetMenu]
    6. public class Biomes : ScriptableObject {
    7.     public GameObject cube;
    8. }
    'cube' it's just an instance of a 3d cube.
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,946
    I've never rendered in this context but unless your materials are unlit, I imagine you might need a light.
     
  3. rogermiranda1000

    rogermiranda1000

    Joined:
    Nov 5, 2016
    Posts:
    5
    I don't have any lightsource in the scene, so Unity should use global illuminance.
    I have created a light in the camera position just in case, and the results were the same.
     
  4. rogermiranda1000

    rogermiranda1000

    Joined:
    Nov 5, 2016
    Posts:
    5
    Also, if you remove the line that deletes all the objects created (
    foreach (GameObject obj in destroyables) Object.DestroyImmediate(obj);
    ) and you go to the camera you can see the block.
     
  5. rogermiranda1000

    rogermiranda1000

    Joined:
    Nov 5, 2016
    Posts:
    5
    OK, I had to call
    camera.Render()
    before mapping the RenderTexture into a Texture2D
     
  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,946
    Oh... yeah. Cameras like that call a lot. :)
     
  7. orionsyndrome

    orionsyndrome

    Joined:
    May 4, 2014
    Posts:
    3,043
    silly cameras, always fussy