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

Multi Directional Color Shader

Discussion in 'Shaders' started by sk8terboy4, Feb 3, 2016.

  1. sk8terboy4

    sk8terboy4

    Joined:
    Mar 29, 2013
    Posts:
    29
    Hello,
    I'm trying to code a shader that allows me to change 3 colors each in different directions. My game is a isometric game and I want one color to project on the left side of a cube, another to project on the right side, and another to project on the top. How will I go about coding a shader like this?
    Thanks.
     
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,329
    By left and right do you mean side and front? You can use the object's surface normals to choose a color.
     
  3. rageingnonsense

    rageingnonsense

    Joined:
    Dec 3, 2014
    Posts:
    99
    Will your cube ever rotate? An idea that comes to mind would be to pass 3 vectors to your shader representing up, left, and right (or even just two). Then perform some dot product tests on the normal of the vertex to decide what color to use there.

    Another idea would be to pass a second texture with masks for the areas that can have a dynamic color change, with distinct colors for each "side" (for instance, any area of the cube that should be considered left has [255,0,0] in this texture). Then test for the existence of these color when sampling the texture. At that point you will know what color you want to use there. You can have three separate color parameters for the shader, and if those need to change you can change them from your c#/js script.

    The second approach will be better if your cube rotates, and the sides are always relative to the cube (also easier). The first option will be better if it is always relative to the world coordinates (Vector3.up), but I coudl see problems with this if the cube will rotate.
     
  4. sk8terboy4

    sk8terboy4

    Joined:
    Mar 29, 2013
    Posts:
    29
    Thanks I was able to solve my problem.