Search Unity

  1. We are migrating the Unity Forums to Unity Discussions. On July 12, the Unity Forums will become read-only. On July 15, Unity Discussions will become read-only until July 18, when the new design and the migrated forum contents will go live. Read our full announcement for more information and let us know if you have any questions.

Apply shader to all objects in the scene

Discussion in 'Shaders' started by goo876, Oct 1, 2018.

  1. goo876

    goo876

    Joined:
    Aug 6, 2018
    Posts:
    8
    I would like to render a scene using a specific geometry and fragment shader for each object in that scene. The problem is that all the objects already have materials and textures of their own. My shader would need to access those material's properties (each texture channel), together with normals, world position coordinates etc.

    What would be the best way to achieve this? Replacing all the materials in the scene with my shader+original material by hand is not possible, as there are a lot of assemblies with child objects which kind of break when I try to modify them.
     
  2. Invertex

    Invertex

    Joined:
    Nov 7, 2013
    Posts:
    1,560
  3. goo876

    goo876

    Joined:
    Aug 6, 2018
    Posts:
    8
    Thank you, I tried them and work as expected, both geometry and fragment shaders.

    However, I encountered another problem: some objects in my scene have textures, and others don't (only use the Albedo color). In the replacement fragment shader, I can either use the _Color or _MainTex property for the output pixel value. Is there any way of using the texture when the object has a texture, and revert to its color when there's no texture assigned to it?
     
    Last edited: Oct 2, 2018
  4. goo876

    goo876

    Joined:
    Aug 6, 2018
    Posts:
    8
    Is this really not possible?

    I was thinking to test in my script if the material has a texture, and if not to create one with the existing color. But this seems too over-the-top for what it's supposed to do...
     
  5. sacb0y

    sacb0y

    Joined:
    May 9, 2016
    Posts:
    1,012
    I doubt you still need help but just in case other people find this post, the solution is to multiply the color and maintex values in your shader.