1
+ Shader "Orthogonal sphere"
2
+ {
3
+ Properties
4
+ {
5
+ _lightcolor( "Light Color" , Color ) = (0.0 ,1.0 ,0.0 ,1.0 )
6
+ _lightposition( "Light Position" , Vector ) = (8.0 ,0.0 ,10.0 ,1.0 )
7
+ }
8
+ Subshader
9
+ {
10
+ Pass
11
+ {
12
+ CGPROGRAM
13
+ #pragma vertex vertex_shader
14
+ #pragma fragment pixel_shader
15
+ #pragma target 2.0
16
+
17
+ float4 _lightcolor, _lightposition;
18
+
19
+ struct type
20
+ {
21
+ float4 vertex : SV_POSITION ;
22
+ float2 uv : TEXCOORD0 ;
23
+ };
24
+
25
+ float3 sphere (float2 p, float s)
26
+ {
27
+ float z = sqrt (abs (s*s-dot (p,p)));
28
+ return float3 (p,z);
29
+ }
30
+
31
+ float3 map (float2 p)
32
+ {
33
+ return sphere (p,0.5 );
34
+ }
35
+
36
+ float4 lighting (float2 p)
37
+ {
38
+ float3 AmbientLight = float3 (0.0 ,0.0 ,0.0 );
39
+ float3 LightDirection = normalize (_lightposition);
40
+ float3 NormalDirection = normalize (map (p));
41
+ float3 LightColor = _lightcolor.xyz;
42
+ float3 DiffuseColor = max (dot (NormalDirection,LightDirection),0.0 )*LightColor+AmbientLight;
43
+ return float4 (DiffuseColor,1.0 );
44
+ }
45
+
46
+ type vertex_shader (float4 vertex:POSITION , float2 uv:TEXCOORD0 )
47
+ {
48
+ type vs;
49
+ vs.vertex = mul (UNITY_MATRIX_MVP ,vertex);
50
+ vs.uv = uv;
51
+ return vs;
52
+ }
53
+
54
+ float4 pixel_shader (type ps) : SV_TARGET
55
+ {
56
+ float2 uv = 2.0 *ps.uv.xy-1.0 ;
57
+ return lighting (uv);
58
+ }
59
+ ENDCG
60
+ }
61
+ }
62
+ }
0 commit comments