Search Unity

Question How to get color

Discussion in 'Shader Graph' started by Wenon, Feb 20, 2021.

  1. Wenon

    Wenon

    Joined:
    Mar 18, 2018
    Posts:
    19
    Hello, Im new with shader graph and I'm trying to get color via script.
    unknown.png

    I tried with material.GetColor("_Color") or just material.Color but unity says that shader do not constain _Color parameter. How can I get color from master node?
     
  2. sandolkakos

    sandolkakos

    Joined:
    Jun 3, 2009
    Posts:
    284
    It has just some hours since I started to play with ShaderGraph, but I would say the issue for you is that your Shader does not have a property called: "_Color"

    upload_2021-2-20_20-56-20.png
     
  3. Wenon

    Wenon

    Joined:
    Mar 18, 2018
    Posts:
    19
    But this is just input, I need color made by shader, final color in master.
     
  4. sandolkakos

    sandolkakos

    Joined:
    Jun 3, 2009
    Posts:
    284
    But that "Color(3) input" (as far as I know) is only the port where the Master Node will receive everything you did in order to show the final result of the shader, which could be a color (yellow, pink, red, etc..) or a texture with a lot of complex pixels changes.

    What specifically would you like to do with that info in your code?
     
  5. Wenon

    Wenon

    Joined:
    Mar 18, 2018
    Posts:
    19
    I need a color generated by a noise in shader. Im arleady using mathf.perlinnoise and some other plugin with noise from github but it starting to be very heavy if I need a lot of values from noises. I thing noise generaded in shader (so by gpu) would be much faster.
     
  6. sandolkakos

    sandolkakos

    Joined:
    Jun 3, 2009
    Posts:
    284
    I'm not an expert in Shaders and didn't understand exactly what you want to do with the noise result, but if you want to provide more details, I can try to help while another experienced person comes to help you.
     
  7. JG-Denver

    JG-Denver

    Joined:
    Jan 4, 2013
    Posts:
    77
    I would use one of the Shader Graph Noise nodes, there are three:
    Gradient Noise
    Simple Noise
    Voronoi

    Gradient is Perlin.

    That will give you a single floating point value, so you will probably want three generators one for each color. Then play around with the parameters until you get the amount of noise or smoothed noise you want for your effect.

    You may need to combine the three outputs from the Noise nodes into one vector 3 before passing it to the color, which can be done with the Combine node.

    Let me know how this works out for you.
     
  8. Wenon

    Wenon

    Joined:
    Mar 18, 2018
    Posts:
    19
    Yes, it work for me but the problem is that I don't know how to get that color or other value in my script from shader. In shader graph I can set only input parameter like vector, color etc but there is nothing like output parameter. For example if I want to create mesh plane with vertices height depends on noise from shader. But I can nothing get from shader what shader generated.
     
  9. sandolkakos

    sandolkakos

    Joined:
    Jun 3, 2009
    Posts:
    284
    JG-Denver likes this.
  10. JG-Denver

    JG-Denver

    Joined:
    Jan 4, 2013
    Posts:
    77
    A Shader Graph shader is intended for producing a camera rendered output with two parts a vertex shader for your mesh, first, and a fragment shader for rendered pixel colors, last. Those are the outputs for shader graph, but the end result is a texture of the render.

    You could set up a camera, put the material with the shader on a quad or whatever in front of the camera to a render texture than save off that result if you wanted a map of the end results. However, this seems like a complicated approach to something that might be simple. If you just want the GPU to calculate some values, see Compute Shaders (not Shader Graph).

    You are also talking about color and then about a mesh. So first I think it would be best to understand what you are trying to accomplish overall and then I can give you some direction. For instance, if you just want to randomly displace the mesh for a plane, that would be with the vertex shader outputs. Let me know what you are trying to do.
     
    sandolkakos likes this.