Oct
2
2012

Fake Godrays Shader

Godrays shader on a plane texture.. since no RenderToTexturein indie. ( Converted to unity from this webgl shader)
Should try it as a camera shader also..

Features:
– Adjustable values: effect xy position, exposure, decay, density, weight, clamp,  samples (screenshot is using 100 samples)

Webplayer:
http://unitycoder.com/upload/demos/fake_godrays_shader_unity/

Source (shader):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
Shader "UnityCoder/Rays2"
{
    Properties
    {
        tDiffuse ("Base (RGB)", 2D) = "white" {}
        fX ("fX", Float) = 0.5 // you can feed mouse xpos here with script: var mpos:Vector3 = Camera.main.ScreenToViewportPoint(Input.mousePosition); renderer.material.SetFloat( "fX", mpos.x);
        fY ("fY", Float) = 0.5 //  mouse ypos
        fExposure ("fExposure", Float) = 0.6
        fDecay ("fDecay", Float) = 0.93
        fDensity ("fDensity", Float) = 0.96
        fWeight ("fWeight", Float) = 0.4
        fClamp ("fClamp", Float) = 1.0
        //iSamples ("iSamples", Int) = 20
    }
    SubShader {
        Tags { "RenderType"="Opaque" }
        LOD 200
        Cull Off
         
        CGPROGRAM
        #pragma target 3.0
        #pragma surface surf Lambert
 
        sampler2D tDiffuse;
        float fX,fY,fExposure,fDecay,fDensity,fWeight,fClamp,iSamples;
 
        struct Input {
            float2 uvtDiffuse;
            float4 screenPos;
        };
 
        void surf (Input IN, inout SurfaceOutput o)
        {
            int iSamples=100;
            float2 vUv = IN.uvtDiffuse;
            //vUv *= float2(1,1); // repeat?
            float2 deltaTextCoord = float2(vUv - float2(fX,fY));
            deltaTextCoord *= 1.0 /  float(iSamples) * fDensity;
            float2 coord = vUv;
            float illuminationDecay = 1.0;
            float4 FragColor = float4(0.0);
            for(int i=0; i < iSamples ; i++)
            {
                coord -= deltaTextCoord;
                float4 texel = tex2D(tDiffuse, coord);
                texel *= illuminationDecay * fWeight;
                FragColor += texel;
                illuminationDecay *= fDecay;
            }
            FragColor *= fExposure;
            FragColor = clamp(FragColor, 0.0, fClamp);
            float4 c = FragColor;
            o.Albedo = c.rgb;
            o.Alpha = c.a;
             
        }
        ENDCG
    }
    FallBack "Diffuse"
}

Image#2: Texture used for the plane, you can use any texture..


11 Comments + Add Comment

  • Seems buggy.

  • Does not run in 3.5.6f4 Engine/Webplayer setup, get corrupted error

    • I’ll check on what version it was build.. (atleast with latest 4.x webplayer it works)

    • Maybe you can report a bug to unity..(or if anyone else can confirm that its working or not?)

  • Is it working on Android and iOS?

    • Not tested.. i’ll try to test it over next week.

  • Any news regarding Android/iOS?

    • Sorry forgot to test ipad, but on old galaxy s android it was too slow ~5fps
      (with 40 samples..webplayer had 100samples i think, and with much less samples (like 16..32..) it starts to look bad)

      So no use in mobiles.

  • Hi,
    I am trying to do something similar but the shader does not work with unity 2018.2 says compile error

    Shader error in ‘UnityCoder/Rays2’: incorrect number of arguments to numeric-type constructor at line 41 (on d3d11)

    What i want is like rays coming out of a perticular point (alpha image soft of rays for hologram’s light emmision point in the shape of hologram) please help if you can 🙂 Thank you in advance

    • for the error, try:
      float4 FragColor = float4(0,0,0,0);

Leave a comment to mgear

Connect

Twitter View LinkedIn profile Youtube Github Join Discord Twitch Instagram BlueSky

UnityLauncherPro

Get UnityLauncherPRO and work faster with Unity Projects!
*free unity hub alternative

@unitycoder_com

Subscribe to Blog via Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.