Search Unity

Per GameObject shader variables?

Discussion in 'Shaders' started by eXonius, Oct 25, 2016.

  1. eXonius

    eXonius

    Joined:
    Feb 2, 2016
    Posts:
    207
    Can I send the GameObject.transform.location vector to a shader so that the shader can take the position of the GameObject currently being rendered into consideration?

    How would I do that? Thanks.
     
  2. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    9,413
  3. eXonius

    eXonius

    Joined:
    Feb 2, 2016
    Posts:
    207
    So variables set that way are bound to the material?

    I'm not sure I understand, because I have several GameObjects using the same material, but I need the shader to take the position of the GameObject it's currently rendering into consideration. Can I set a float for one GameObject and then change it before the next GameObject is rendered?

    Can I do that using SetFloat or is there another way?
     
    Last edited: Oct 25, 2016
  4. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    9,413
    Do you have lots of gameobjects, that couldn't use separate materials for them?

    What kind of effect you actually want to do?
     
  5. eXonius

    eXonius

    Joined:
    Feb 2, 2016
    Posts:
    207
    I want to offset a texture lookup index based on the current GameObject's position.

    Specifically I have multiple terrains tiled together, and using the default terrain shader there will be visible seams between the terrains. I thought I might be able to fix this by using an offset value for each terrain based on its position.
     
  6. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    9,413
  7. eXonius

    eXonius

    Joined:
    Feb 2, 2016
    Posts:
    207
    Yes I'm using SetNeighbours. As it says it only helps for the LOD.

    I've already modified the terrain shaders to use the World UV positions instead, and that works but it doesn't let me control the scale of each individual SplatPrototype anymore, that's why I want another solution.
     
  8. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,343
    As long as the gameobject in question is the same position as the renderer component then the shader already has this information, assuming it's not being batched (and I don't think Terrain gets batched?).

    Either way float3(unity_ObjectToWorld[0].w, unity_ObjectToWorld[1].w, unity_ObjectToWorld[2].w) will give you the world position of the object.
     
  9. eXonius

    eXonius

    Joined:
    Feb 2, 2016
    Posts:
    207
    Thanks bgolus I'll try that!