1
+ Shader "Sample Texture"
2
+ {
3
+ Properties
4
+ {
5
+ _MainTex ( "Texture" , 2D ) = "white" {}
6
+ }
7
+ Subshader
8
+ {
9
+ Tags { "RenderType" = "Transparent" "Queue" = "Transparent" }
10
+ Pass
11
+ {
12
+ ZWrite Off
13
+ Blend SrcAlpha OneMinusSrcAlpha
14
+ CGPROGRAM
15
+ #pragma vertex vertex_shader
16
+ #pragma fragment pixel_shader
17
+ #pragma target 3.0
18
+
19
+ float4 _LightColor0 ;
20
+ sampler2D _MainTex;
21
+
22
+ struct custom_type
23
+ {
24
+ float4 screen_vertex : SV_POSITION ;
25
+ float3 world_vertex : TEXCOORD1 ;
26
+ };
27
+
28
+ float3x3 rotation ( float x )
29
+ {
30
+ return float3x3
31
+ (
32
+ 1.0 ,0.0 ,0.0 ,
33
+ 0.0 ,cos (x),-sin (x),
34
+ 0.0 ,sin (x),cos (x)
35
+ );
36
+ }
37
+
38
+ float cuboid (float3 p,float3 c,float3 s)
39
+ {
40
+ float3 d = abs (p-c)-s;
41
+ return float (max (max (d.x,d.y),d.z));
42
+ }
43
+
44
+ float map (float3 p)
45
+ {
46
+ p = mul (rotation (_Time .g ),p);
47
+ return cuboid (p,float3 (0.0 ,0.0 ,0.0 ),float3 (1.0 ,1.0 ,1.0 ));
48
+ }
49
+
50
+ float3 set_normal (float3 p)
51
+ {
52
+ float3 x = float3 (0.01 ,0.00 ,0.00 );
53
+ float3 y = float3 (0.00 ,0.01 ,0.00 );
54
+ float3 z = float3 (0.00 ,0.00 ,0.01 );
55
+ return normalize (float3 (map (p+x)-map (p-x),map (p+y)-map (p-y),map (p+z)-map (p-z)));
56
+ }
57
+
58
+ float3 set_texture ( float3 pos, float3 nor )
59
+ {
60
+ float3 w = nor*nor;
61
+ return (w.x*tex2Dlod (_MainTex,float4 (pos.yz,0.0 ,0.0 ) ) + w.y*tex2Dlod (_MainTex,float4 (pos.zx,0.0 ,0.0 ))+ w.z*tex2Dlod (_MainTex,float4 (pos.xy,0.0 ,0.0 )) / (w.x+w.y+w.z));
62
+ }
63
+
64
+ float3 lighting (float3 p)
65
+ {
66
+ float3 AmbientLight = UNITY_LIGHTMODEL_AMBIENT ;
67
+ float3 LightDirection = normalize (_WorldSpaceLightPos0 .xyz);
68
+ float3 LightColor = _LightColor0 .xyz;
69
+ float3 NormalDirection = set_normal (p);
70
+ return (max (dot (LightDirection, NormalDirection),0.0 ) * LightColor + AmbientLight)*set_texture (p,NormalDirection);;
71
+ }
72
+
73
+ float4 raymarch (float3 ro, float3 rd)
74
+ {
75
+ for (int i=0 ; i<128 ; i++)
76
+ {
77
+ float t = map (ro);
78
+ if (distance (ro,t*rd)>250 ) break ;
79
+ if (t < 0.001 ) return float4 (lighting (ro),1.0 );
80
+ ro+=t*rd;
81
+ }
82
+ return float4 (0.0 ,0.0 ,0.0 ,0.0 );
83
+ }
84
+
85
+ custom_type vertex_shader (float4 vertex : POSITION )
86
+ {
87
+ custom_type vs;
88
+ vs.screen_vertex = mul (UNITY_MATRIX_MVP , vertex);
89
+ vs.world_vertex = mul (_Object2World , vertex);
90
+ return vs;
91
+ }
92
+
93
+ float4 pixel_shader (custom_type ps ) : SV_TARGET
94
+ {
95
+ float3 worldPosition = ps.world_vertex;
96
+ float3 viewDirection = normalize (ps.world_vertex - _WorldSpaceCameraPos .xyz);
97
+ return raymarch (worldPosition,viewDirection);
98
+ }
99
+
100
+ ENDCG
101
+
102
+ }
103
+ }
104
+ }
0 commit comments