Search Unity

Question Gradient shader for 3D mesh

Discussion in 'Shaders' started by magebuzzcutt, Jan 28, 2023.

  1. magebuzzcutt

    magebuzzcutt

    Joined:
    Jun 20, 2021
    Posts:
    17
    I'm trying to create a custom shader which gives a sphere a sort of gradient look. I've attached an image of my attempt at trying to create it. The image on the left is the shader attached to a plane, and the one on the right is the shader attached onto a sphere mesh.

    The code for it:
    Code (CSharp):
    1.  float gradientVal= floor(saturate(1-length(input.positionOS.xyz))/0.2)*0.2;
    2. return float4(gradientVal.xxxx)
    So I use the one minus length of the object space coordinates to get the color to fade towards the edges. But I'm not sure why it works for the plane but not the sphere mesh? Is there something I am missing or should I be trying another approach?

    TIA
     

    Attached Files:

  2. Olmi

    Olmi

    Joined:
    Nov 29, 2012
    Posts:
    1,553
    Your plane has a shader effect which basically displays a stepped gradient of the distance to the center of the quad. But what are you expecting to see on your sphere, are you expecting something to happen in 3D space? Perhaps you could show an example image?
     
  3. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    9,408
    sphere surface would be same distance everywhere,
    test adding offset to that value, to check if it works
    Code (CSharp):
    1. input.positionOS.xyz+float3(0.5,0,0)
    then could try using light direction as offset,
    so you get the bright spot towards light, or towards camera if take camdirection.
    or just manually feeding vector3 property to set direction