Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question How to spawn balls with same emissive intensity and random color.

Discussion in 'Shaders' started by sctiendat, Apr 17, 2021.

  1. sctiendat

    sctiendat

    Joined:
    Mar 14, 2020
    Posts:
    22
    Hello,
    I searched many threads but I can't find any correct solution.
    I need to spawn many balls, random it's color, but they have to same Intensity Emission.
    Many threads about change Intensity Emission at runtime, but all of them can't have same value.
    Please help me, thank you!
     
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,329
    There are kind of two questions / answers here. The simple question and answer is you want to use this function:
    https://docs.unity3d.com/ScriptReference/Random.ColorHSV.html

    That’ll return a random color within the ranges specified. If you want something to have a constant intensity, use the form of the function without a
    valueMin/Max
    range and then multiply the resulting
    Color
    by whatever intensity over “1.0” you want. That’ll give you the exact same intensity for every random color.

    The second, harder question is how do you ensure it looks like the same intensity. This is actually a different question, because the first function will return values that really are the same gamma space intensity, but they will not look the same because human vision does not perceive all colors as having the same intensity.

    To fix that there are two ways I can think of the work around the problem. One simplistic (and less accurate) way would be to divide the color by it’s .grayscale to try to balance the starting perceptual intensity before multiplying your additional intensity. This might work “well enough”, but it won’t be perfect. The more accurate option would be to random the color in a different color space, one that is already perceptually constant in intensity when modifying the hue. Oklab would be an excellent choice.
    https://bottosson.github.io/posts/oklab/

    Also, depending on what post processing you’re doing, it may not treat all colors the same either, which adds another layer of *shrug*.
     
    sctiendat likes this.
  3. sctiendat

    sctiendat

    Joined:
    Mar 14, 2020
    Posts:
    22
    Thank you so much!
    Maybe it's a quite difficult problem. So I think I will use an easier solution. I create many materials and save them. Then when I need to random, I wil random one of them.
    But thank you again!