Search Unity

[SOLVED] Project caustics texture from light direction

Discussion in 'Shaders' started by Flohneh, Aug 24, 2019.

  1. Flohneh

    Flohneh

    Joined:
    Aug 10, 2018
    Posts:
    79
    I basically have a caustics texture that i want to project coming from the light direction of my main directional light. Planar Mapping should be the solution to this, for example using just this line of code to project it from the xy plane of the world works pretty straight forward.
    Code (CSharp):
    1. fixed caustics = tex2D(_CausticsTex, IN.worldPos.xy);
    I thought of replacing the world position with _WorldSpaceLightPos0.xy, which should be the light direction vectors, but it just results in a glitchy color change. Does somebody has an idea how to solve this?
     
  2. tmcthee

    tmcthee

    Joined:
    Mar 8, 2013
    Posts:
    58
    Could you create the correct UVs in your graphics package? Maya, blender whatever?
     
    Flohneh likes this.
  3. mouurusai

    mouurusai

    Joined:
    Dec 2, 2011
    Posts:
    281
    Why you not use light cookie?
    I think you can use "unity_WorldToLight" matrix to project texture coords.
     
    Last edited: Aug 24, 2019
    Flohneh likes this.
  4. Flohneh

    Flohneh

    Joined:
    Aug 10, 2018
    Posts:
    79
    That's not possible, because it should be a seamless projection across the objects, ignoring their original uvs.

    I also thought about it, but for example, i cant mask a light cookie to under beneath a give surface, in my case, the water surface. (Atleast i don't know any way how) Also I'm nor sure how light cookies play with batching and so on.

    That's why i want to have the caustics on each object themself and just blend it in based on an height number from the watersurface. And the light direction as projection base would be perfect for giving it directionality.
     
  5. Flohneh

    Flohneh

    Joined:
    Aug 10, 2018
    Posts:
    79
    Ok, so i tried around with unity_WorldToLight and it goes into the right direction, but still has two errors- So this is the code im using now:
    Code (CSharp):
    1. float2 lightSpaceUVs = mul(unity_WorldToLight , float4(IN.worldPos, 1)).xy;
    First, it seems to work ok, but when i look with my camera from a specific angle on it, theres some glitching happening? I noticed this in general with light pos stuff-

    And secondly, theres some weird stuff with the definiton of unity_WorldToLight. I need to define the float4x4 unity_WorldToLight for it to be declared, because else he wouldnt find the matrix. Then he says tho, that its a redefiniton. Now when i add #inculde "Autolight.cginc", where this is supposed to be and i delete the redef in my shader, he just can't find it anymore-

    (Also this only seems to work in deferred, as in forward, the texture doesn't get moved at all-)
     
    Last edited: Aug 27, 2019
  6. mouurusai

    mouurusai

    Joined:
    Dec 2, 2011
    Posts:
    281
    I was try use that matrix, no luck, I have no clue how to use it without cookie.
    Pass worldToLocalMatrix from script, it's do the job if you have only one light source.
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. [ExecuteInEditMode]
    4. public class SetLightMatrix : MonoBehaviour
    5. {
    6.     void Update()
    7.     {
    8.         Matrix4x4 mat = transform.worldToLocalMatrix;
    9.         Shader.SetGlobalMatrix("PrjMatrix", mat);
    10.     }
    11. }
     
    Flohneh likes this.
  7. Flohneh

    Flohneh

    Joined:
    Aug 10, 2018
    Posts:
    79
    @mouurusai Works like a charm! Used it for this caustic effect of mine ouo b

    Caustics_2_Screenshot.png