Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Everything is black.

Discussion in 'General Graphics' started by ElectricYtArt, Jun 4, 2022.

  1. ElectricYtArt

    ElectricYtArt

    Joined:
    Apr 5, 2022
    Posts:
    2
    The day/night script I used put in a reflection probe and it's all black. I removed it and here we are.
    This is the code I used.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.Rendering;
    5.  
    6. [ExecuteInEditMode]
    7. public class SkyboxBlender : MonoBehaviour {
    8.  
    9.     [SerializeField] public enum BlendMode { Linear, Smoothstep, Maximum, Add, Substract, Multiply }
    10.     [SerializeField] public enum ProbeResolution { _16, _32, _64, _128, _256, _512, _1024, _2048 }
    11.  
    12.     //[Header("Input Skyboxes")]
    13.     [SerializeField] public Material skyBox1;
    14.     [SerializeField] public Material skyBox2;
    15.  
    16.     //[Header("Blended Skybox")]
    17.     [SerializeField] public Material blendedSkybox;
    18.     [SerializeField] [Range(0, 8)] public float exposure = 1;
    19.     [SerializeField] [Range(0, 360)] public float rotation = 0;
    20.     [SerializeField] public Color tint = Color.white;
    21.     [SerializeField] [Range(0, 1)] public float invertColors = 0;
    22.     [SerializeField] public BlendMode blendMode = BlendMode.Linear;
    23.     [SerializeField] [Range(0, 1)] public float blend = 0;
    24.  
    25.     [SerializeField] public bool bindOnStart = true;
    26.     [SerializeField] public bool updateLightingOnStart = true;
    27.     [SerializeField] public bool updateLightingEveryFrame = true;
    28.     [SerializeField] public bool updateReflectionsOnStart = true;
    29.     [SerializeField] public bool updateReflectionsEveryFrame = true;
    30.  
    31.     [SerializeField] public ProbeResolution reflectionResolution = ProbeResolution._128;
    32.  
    33.     private ReflectionProbe probeComponent = null;
    34.     private GameObject probeGameObject = null;
    35.     private Cubemap blendedCubemap = null;
    36.     private int renderId = -1;
    37.  
    38.     #region MonoBehaviour Functions
    39.  
    40.     // Use this for initialization
    41.     void Start () {
    42.  
    43.         if (bindOnStart)
    44.             BindTextures();
    45.  
    46.         //Update the material parameters
    47.         UpdateBlendedMaterialParameters();
    48.  
    49.         if (updateLightingOnStart)
    50.             UpdateLighting();
    51.  
    52.         if (updateReflectionsOnStart)
    53.             UpdateReflections();
    54.     }
    55.    
    56.     // Update is called once per frame
    57.     void Update () {
    58.  
    59.         //Update material parameters
    60.         UpdateBlendedMaterialParameters();
    61.  
    62.         //Update lighting
    63.         if (updateLightingEveryFrame)
    64.             UpdateLighting();
    65.  
    66.         //Update reflections
    67.         if (updateReflectionsEveryFrame)
    68.             UpdateReflections();
    69.     }
    70.    
    71.     /*
    72.     private void OnValidate()
    73.     {
    74.         if (!updateInEditMode)
    75.             return;
    76.  
    77.         Update();
    78.     }
    79.     */
    80.  
    81.     #endregion
    82.  
    83.     /// <summary>
    84.     /// Get the probe resolution value
    85.     /// </summary>
    86.     int GetProbeResolution(ProbeResolution probeResolution)
    87.     {
    88.         switch (probeResolution)
    89.         {
    90.             case ProbeResolution._16:
    91.                 return 16;
    92.             case ProbeResolution._32:
    93.                 return 32;
    94.             case ProbeResolution._64:
    95.                 return 64;
    96.             case ProbeResolution._128:
    97.                 return 128;
    98.             case ProbeResolution._256:
    99.                 return 256;
    100.             case ProbeResolution._512:
    101.                 return 512;
    102.             case ProbeResolution._1024:
    103.                 return 1024;
    104.             case ProbeResolution._2048:
    105.                 return 2048;
    106.             default:
    107.                 return 128;
    108.         }
    109.     }
    110.  
    111.     /// <summary>
    112.     /// Create a reflection probe gameobject and setup the cubemap for environment reflections
    113.     /// </summary>
    114.     void CreateReflectionProbe()
    115.     {
    116.         //Search for the reflection probe object
    117.         probeGameObject = GameObject.Find("Skybox Blender Reflection Probe");
    118.  
    119.         if (!probeGameObject)
    120.         {
    121.             //Create the gameobject if its not here
    122.             probeGameObject = new GameObject("Skybox Blender Reflection Probe");
    123.             probeGameObject.transform.parent = gameObject.transform;
    124.             // Use a location such that the new Reflection Probe will not interfere with other Reflection Probes in the scene.
    125.             probeGameObject.transform.position = new Vector3(0, -1000, 0);
    126.         }
    127.  
    128.         probeComponent = probeGameObject.GetComponent<ReflectionProbe>();
    129.  
    130.         if (probeComponent)
    131.         {
    132.             DestroyImmediate(probeComponent);
    133.         }
    134.  
    135.         // Create a Reflection Probe that only contains the Skybox. The Update function controls the Reflection Probe refresh.
    136.         probeComponent = probeGameObject.AddComponent<ReflectionProbe>() as ReflectionProbe;
    137.  
    138.     }
    139.  
    140.     /// <summary>
    141.     /// Update the reflection probe and cubemap
    142.     /// </summary>
    143.     public void UpdateReflectionProbe()
    144.     {
    145.         //if (!probeGameObject || !probeComponent)
    146.             CreateReflectionProbe();
    147.  
    148.         probeComponent.resolution = GetProbeResolution(reflectionResolution);
    149.         probeComponent.size = new Vector3(1, 1, 1);
    150.         probeComponent.cullingMask = 0;
    151.         probeComponent.clearFlags = ReflectionProbeClearFlags.Skybox;
    152.         probeComponent.mode = ReflectionProbeMode.Realtime;
    153.         probeComponent.refreshMode = ReflectionProbeRefreshMode.ViaScripting;
    154.         probeComponent.timeSlicingMode = ReflectionProbeTimeSlicingMode.NoTimeSlicing;
    155.  
    156.         // A cubemap is used as a default specular reflection.
    157.         blendedCubemap = new Cubemap(probeComponent.resolution, probeComponent.hdr ? TextureFormat.RGBAHalf : TextureFormat.RGBA32, true);
    158.  
    159.         //Set the render reflection mode to Custom
    160.         RenderSettings.defaultReflectionMode = DefaultReflectionMode.Custom;
    161.         RenderSettings.customReflection = blendedCubemap;
    162.     }
    163.  
    164.     /// <summary>
    165.     /// Update the scene environment lighting
    166.     /// </summary>
    167.     public void UpdateLighting()
    168.     {
    169.         DynamicGI.UpdateEnvironment();
    170.     }
    171.  
    172.     /// <summary>
    173.     /// Update the scene environment reflections
    174.     /// </summary>
    175.     public void UpdateReflections()
    176.     {
    177.         if (!probeGameObject || !probeComponent)
    178.             UpdateReflectionProbe();
    179.  
    180.         // The Update function refreshes the Reflection Probe and copies the result to the default specular reflection Cubemap.
    181.  
    182.         // The texture associated with the real-time Reflection Probe is a render target and RenderSettings.customReflection is a Cubemap. We have to check the support if copying from render targets to Textures is supported.
    183.         if ((SystemInfo.copyTextureSupport & CopyTextureSupport.RTToTexture) != 0)
    184.         {
    185.             // Wait until previous RenderProbe is finished before we refresh the Reflection Probe again.
    186.             // renderId is a token used to figure out when the refresh of a Reflection Probe is finished. The refresh of a Reflection Probe can take mutiple frames when time-slicing is used.
    187.             if (renderId == -1 || probeComponent.IsFinishedRendering(renderId))
    188.             {
    189.                 if (probeComponent.IsFinishedRendering(renderId))
    190.                 {
    191.                     //Debug.Log("probeComponent.texture.width = " + probeComponent.texture.width + " blendedCubemap.width = "+ blendedCubemap.width);
    192.                     //Debug.Log("probeComponent.texture.height = " + probeComponent.texture.height + " blendedCubemap.height = " + blendedCubemap.height);
    193.                     //Debug.Log("probeComponent.resolution = " + probeComponent.resolution);
    194.                     // After the previous RenderProbe is finished, we copy the probe's texture to the cubemap and set it as a custom reflection in RenderSettings.
    195.                     if (probeComponent.texture.width == blendedCubemap.width && probeComponent.texture.height == blendedCubemap.height)
    196.                     {
    197.                         Graphics.CopyTexture(probeComponent.texture, blendedCubemap as Texture);
    198.                         //Debug.Log("Copying");
    199.                     }
    200.  
    201.                     RenderSettings.customReflection = blendedCubemap;
    202.                 }
    203.  
    204.                 renderId = probeComponent.RenderProbe();
    205.             }
    206.         }
    207.     }
    208.  
    209.     /// <summary>
    210.     /// Get the BlendMode index from the enumeration
    211.     /// </summary>
    212.     int GetBlendModeIndex(BlendMode blendMode)
    213.     {
    214.         switch (blendMode)
    215.         {
    216.             case BlendMode.Linear:
    217.                 return 0;
    218.             case BlendMode.Smoothstep:
    219.                 return 5;
    220.             case BlendMode.Maximum:
    221.                 return 1;
    222.             case BlendMode.Add:
    223.                 return 2;
    224.             case BlendMode.Substract:
    225.                 return 3;
    226.             case BlendMode.Multiply:
    227.                 return 4;
    228.             default:
    229.                 return 0;
    230.         }
    231.     }
    232.  
    233.     /// <summary>
    234.     /// Bind the input skyboxes textures to the blended skybox
    235.     /// </summary>
    236.     public void BindTextures()
    237.     {
    238.         blendedSkybox.SetTexture("_FrontTex_1", skyBox1.GetTexture("_FrontTex"));
    239.         blendedSkybox.SetTexture("_BackTex_1", skyBox1.GetTexture("_BackTex"));
    240.         blendedSkybox.SetTexture("_LeftTex_1", skyBox1.GetTexture("_LeftTex"));
    241.         blendedSkybox.SetTexture("_RightTex_1", skyBox1.GetTexture("_RightTex"));
    242.         blendedSkybox.SetTexture("_UpTex_1", skyBox1.GetTexture("_UpTex"));
    243.         blendedSkybox.SetTexture("_DownTex_1", skyBox1.GetTexture("_DownTex"));
    244.  
    245.         blendedSkybox.SetTexture("_FrontTex_2", skyBox2.GetTexture("_FrontTex"));
    246.         blendedSkybox.SetTexture("_BackTex_2", skyBox2.GetTexture("_BackTex"));
    247.         blendedSkybox.SetTexture("_LeftTex_2", skyBox2.GetTexture("_LeftTex"));
    248.         blendedSkybox.SetTexture("_RightTex_2", skyBox2.GetTexture("_RightTex"));
    249.         blendedSkybox.SetTexture("_UpTex_2", skyBox2.GetTexture("_UpTex"));
    250.         blendedSkybox.SetTexture("_DownTex_2", skyBox2.GetTexture("_DownTex"));
    251.     }
    252.  
    253.     /// <summary>
    254.     /// Update the material parameters
    255.     /// </summary>
    256.     void UpdateBlendedMaterialParameters()
    257.     {
    258.         blendedSkybox.SetColor("_Tint", tint);
    259.         blendedSkybox.SetFloat("_Exposure", exposure);
    260.         blendedSkybox.SetFloat("_Rotation", rotation);
    261.         blendedSkybox.SetFloat("_Blend", blend);
    262.         blendedSkybox.SetInt("_BlendMode", GetBlendModeIndex(blendMode));
    263.         blendedSkybox.SetFloat("_InvertColors", invertColors);
    264.  
    265.     }
    266.  
    267. }
    I just need a way to fix the bug. I don't need the script anymore.
     
  2. DevDunk

    DevDunk

    Joined:
    Feb 13, 2020
    Posts:
    4,847
    If you disable the script does it render properly?
    What materials are used?