Search Unity

control multi-colored fresnel shader

Discussion in 'Shaders' started by lego082, Jul 6, 2018.

  1. lego082

    lego082

    Joined:
    Jun 19, 2018
    Posts:
    6
    hello there,

    i would like to achieve something like what i did here in c4d in unity.
    fresneltest03.jpg
    my current shader graph setup looks like this.
    Bildschirmfoto 2018-07-06 um 14.46.56.png

    i would like to use 4 colors instead of just 2 with more sharp edges. i figured i might use masks for this?
    additionally i need to control the proportion/position of the colors via c#.

    some advice would be much appreciated. thanks in advance!
     
  2. Johannski

    Johannski

    Joined:
    Jan 25, 2014
    Posts:
    826
    I would recommend using an actual texture for it instead of multiple colors, this way you can create a lot of different color ramps without the need of adding a lot of colors. Also sharp edges are really easy to achieve with a color ramp. You just need to feed the fresnel output to a texture lookup (either u or v or both, depending on how you want to build your texture). You could even screenshot your c4d square and use that to get a perfect match :)
     
    lego082 and bgolus like this.
  3. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,352
    If you want to be able to modify it from script, you can either construct the ramp texture in c# and update it each frame as needed, or construct multiple ramps and swap them out, or even create a texture with multiple ramps and modify the lookup UVs to offset it so it samples from the correct ramp (using a material property float value for one component of the UV, and the fresnel dot product for the other).
     
    lego082 likes this.
  4. lego082

    lego082

    Joined:
    Jun 19, 2018
    Posts:
    6
    thanks a lot. texture works great.
    to get it dynamic would i need a script that constantly updates the png texture or what should it put out?
     
  5. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,352
    The PNG is just an image format for transferring data between programs. Once imported, Unity never uses PNG, or JPG, or really any image format you're likely familiar with, but instead uses internal image formats designed for use on GPUs. In script you can create a Texture2D and directly assign the color values for each pixel, and Unity will upload that texture to the GPU.

    The Create() function in the below link is all you need to create the initial texture. Then just assign it to your material. To update the texture just update the gradient and run the evaluate & SetPixel loop on the same Texture2D. I'd also be more efficient to use SetPixels() rather than SetPixel(), but the below is still a valid example.

    https://gist.github.com/mattatz/a8c697e212b653ca9ce966797bb93a3c
     
    lego082 likes this.
  6. lego082

    lego082

    Joined:
    Jun 19, 2018
    Posts:
    6
    thanks alot!