-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRaymarching.cginc
More file actions
96 lines (71 loc) · 2.12 KB
/
Copy pathRaymarching.cginc
File metadata and controls
96 lines (71 loc) · 2.12 KB
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#ifndef RAYMARCHING
#define RAYMARCHING
#include "UnityCG.cginc"
#include "UnityLightingCommon.cginc"
float GetPosHitpos(float4 vertex, out float3 cameraPos,out float3 hitPos){
#if WORLDSPACE
cameraPos = _WorldSpaceCameraPos;
hitPos = mul(unity_ObjectToWorld, vertex);
#else
cameraPos = mul(unity_WorldToObject, float4(_WorldSpaceCameraPos,1));
hitPos=vertex;
#endif
return 0;
}
float3 GetNormal(float3 p) {
float2 e = float2(1e-2, 0);
float3 n = GetDist(p) - float3(
GetDist(p-e.xyy),
GetDist(p-e.yxy),
GetDist(p-e.yyx)
);
return normalize(n);
}
float Raymarch(float3 ro, float3 rd) {
float dO = 0;
float dS;
for (int i = 0; i < MAX_STEPS; i++) {
float3 p = ro + rd * dO;
dS = GetDist(p);
dO += dS;
if (dS<SURF_DIST || dO>MAX_DIST) break;
}
return dO;
}
//LIGHTING
//====================
float3 LightColor(float3 p,float3 n,float3 lightPos,float3 lightColor,float attenuation){
#if !WORLDSPACE
p=mul(unity_ObjectToWorld,p);
n=UnityObjectToWorldNormal(n);
#endif
float3 lightVec = p-lightPos;
float3 lightDir = normalize(lightVec);
float light = saturate(dot(n,lightPos.xyz));
light/=1+attenuation*dot(lightVec,lightVec);
return lightColor*light;
}
float3 CalculateLighting(float3 p,float3 n,float3 col){
float3 lightColor=1;
#if LIGHTING
lightColor=0;
float3 lightPos=_WorldSpaceLightPos0;
lightColor+=LightColor(p,n,lightPos,_LightColor0,1);
/*float fac;
for(int i=0;i<4;i++){
lightPos=(unity_4LightPosX0[i], unity_4LightPosY0[i], unity_4LightPosZ0[i]);
lightColor+=LightColor(p,n,lightPos,unity_LightColor[i],unity_4LightAtten0[i]);
}*/
lightColor+=ShadeSH9(half4(n,1));
#endif
return lightColor*col;
}
float GetDepth(float3 p, float d){
#if WORLDSPACE==0
p=mul(unity_ObjectToWorld,float4(p,1));
#endif
float3 viewPos = mul(UNITY_MATRIX_V, float4(p, 1.0)).xyz;
float linearZ = -viewPos.z;
return (1.0 - _ZBufferParams.w * linearZ) / (linearZ * _ZBufferParams.z);
}
#endif