Search Unity

Is it possible to link the transforms (Dir) of an object, into the shader of another object ?

Discussion in 'Shaders' started by Nova-Shade, Mar 24, 2019.

  1. Nova-Shade

    Nova-Shade

    Joined:
    Oct 15, 2015
    Posts:
    136
    Hello !

    I have a custom Planet shader completly emissive and at the moment it has the Light Dir linked to it to dictate the direction of the Illumination.



    Since it's emissive only, the Planet object recieves no pixel light at all and I think it's quite sad to use a directional light if there is no light out of it.
    So here is what I wish I could do : Using a game object instead of the light and link it to the Planet shader.
    I know how to create an object field in my custom material editor , but it looks like there is no nodes for the game object transforms. Would it work with a custom expression node ?

    Edit : I have began a custom node with this , but as I was expecting its just all white , illuminated from all directions :

    Code (CSharp):
    1. GameObject lightSource = GameObject.Find ("LightSource")
    2.  
    3. Transform lightRotation = lightSource.GetComponent<Transform>()rotation.xyz
    4.  
    5. Out = LightRotation
    Thanks you for reading and for any further help on the subject.
     
    Last edited: Mar 24, 2019
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,352
    You can’t use a non-texture object as a material property or access those objects in a shader. Custom nodes can only access what can be passed in as a material property or is already available to the shader.

    The solution is you need to have a custom Vector3 property that you set via a c# script. You want to get your object transform’s forward vector and set that on your material.