1
+ /*Set Main Camera to following position ( 0 ,50 ,0 ).
2
+ In Unity3D editor, add 3D Object/Quad to Main Camera, then bind material with shader to the quad . Set quad position at ( x=0 ; y=0 ; z=0.4 ;).
3
+ Add fly script to Main Camera. Play.
4
+ Or just bind material with shader to any gameobject to create volumetric effect :) */
5
+
6
+ Shader "City"
7
+ {
8
+ Subshader
9
+ {
10
+ Pass
11
+ {
12
+ CGPROGRAM
13
+ #pragma vertex vertex_shader
14
+ #pragma fragment pixel_shader
15
+ #pragma target 3.0
16
+
17
+ struct custom_type
18
+ {
19
+ float4 screen_vertex : SV_POSITION ;
20
+ float3 world_vertex : TEXCOORD1 ;
21
+ };
22
+
23
+ float hash (float2 n)
24
+ {
25
+ return frac (sin (dot (n, float2 (12.9898 , 4.1414 )))*43758.5453 );
26
+ }
27
+
28
+ float map (float3 p)
29
+ {
30
+ float2 u = floor (p.xz*0.005 *64.0 )/64.0 ;
31
+ float h = hash (u);
32
+ h = p.y - lerp (0.0 ,8.0 ,pow (h,2.0 ));
33
+ return max ( min ( h, 0.1 ), p.y-8.0 );
34
+ }
35
+
36
+ float4 color (float3 ro)
37
+ {
38
+ float m = ro.y/8.0 ;
39
+ float4 buildings = float4 (m,m,m,1.0 );
40
+ float4 grass = float4 (0 ,0.05 ,0 ,1 );
41
+ return lerp (buildings,grass,step (ro.y,0.1 ));
42
+ }
43
+
44
+ float4 raymarch (float3 ro, float3 rd)
45
+ {
46
+ for (int i=0 ; i<512 ; i++)
47
+ {
48
+ float t = map (ro);
49
+ if (ro.x>300.0 || ro.x<-300.0 || ro.z>300.0 || ro.z<-300.0 ) break ;
50
+ if ( t<0.001 ) return color (ro);
51
+ ro+=t*rd;
52
+ }
53
+ return float4 (0.0 ,0.0 ,1.0 ,0.0 );
54
+ }
55
+
56
+ custom_type vertex_shader (float4 vertex : POSITION )
57
+ {
58
+ custom_type vs;
59
+ vs.screen_vertex = mul (UNITY_MATRIX_MVP ,vertex);
60
+ vs.world_vertex = mul (_Object2World , vertex);
61
+ return vs;
62
+ }
63
+
64
+ float4 pixel_shader (custom_type ps ) : SV_TARGET
65
+ {
66
+ float3 worldPosition = ps.world_vertex;
67
+ float3 viewDirection = normalize (ps.world_vertex-_WorldSpaceCameraPos .xyz);
68
+ return raymarch (worldPosition,viewDirection);
69
+ }
70
+
71
+ ENDCG
72
+
73
+ }
74
+ }
75
+ }
0 commit comments