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):
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..

Related Posts
11 Comments + Add Comment
Leave a comment
Recent posts
- Favorites in PackageManager
- LudumDare59 : Signal
- Unity Editor: Tree Generator
- Leaf/Foliage Generator Tools (Runs in Browser)
- Testing Unity AI Beta
- Ways to Support UnityCoder Development
- Using UI Slider to Create 5-Star Rating Element
- Game Music Library For Unity (editor plugin)
- Fontastic : Easily Test Fonts in Unity Editor!
- GeoTiff Importer & Terrain Generator for Unity
- Create Baked DropShadow for UI images
- .JP2 Ortho Image Converter to PNG/JPG/TIFF
Recent Comments
- on Mesh Exploder (sources)
- on Sprite Sheet Flip Book Shader
- on Sprite Sheet Flip Book Shader
- on [Asset Store] PolygonCollider2D Optimizer
- on Trajectory Test Scene 2.0
- on Vector3 maths for dummies!
- on UnityHub 3.6.0: Remove Version Control & Cloud Dashboard columns
- on Using RenderDoc with Unity (graphics debugger)
Coin:
CUgDSbRqFcAumDSAcdKDvuXsw26VdkJe8C8WGUQHBAGS
An article by













Seems buggy.
Which part?
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);