/
ScaledPlanetScattering.shader
195 lines (152 loc) · 6.25 KB
/
ScaledPlanetScattering.shader
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
Shader "Scatterer/ScaledPlanetScattering" {
SubShader
{
Tags {"QUEUE"="Geometry+1" "IgnoreProjector"="True" }
Pass { //extinction pass
ZWrite Off
ZTest LEqual
Cull Back
Offset -0.05, -0.05
Blend DstColor Zero //multiplicative
CGPROGRAM
#include "UnityCG.cginc"
#pragma glsl
#pragma target 3.0
#pragma vertex vert
#pragma fragment frag
#include "../CommonAtmosphere.cginc"
#include "../EclipseCommon.cginc"
#include "../RingCommon.cginc"
#define useAnalyticSkyTransmittance
#pragma multi_compile ECLIPSES_OFF ECLIPSES_ON
#pragma multi_compile RINGSHADOW_OFF RINGSHADOW_ON
uniform float _Alpha_Global;
uniform float extinctionTint;
uniform float extinctionThickness;
uniform float3 _Sun_WorldSunDir;
uniform float renderScattering;
uniform float flatScaledSpaceModel;
struct v2f
{
float4 pos: SV_POSITION;
float3 worldPos : TEXCOORD0;
float3 planetOrigin: TEXCOORD1;
};
v2f vert(appdata_base v)
{
v2f OUT;
OUT.pos = UnityObjectToClipPos(v.vertex);
OUT.worldPos = mul(unity_ObjectToWorld, v.vertex);
OUT.planetOrigin = mul (unity_ObjectToWorld, float4(0,0,0,1)).xyz;
OUT.pos = (renderScattering == 1.0) ? OUT.pos : float4(2.0, 2.0, 2.0, 1.0); //outside clip space => cull vertex
return OUT;
}
half4 frag(v2f IN): COLOR
{
float3 extinction=0.0;
float3 WCP = _WorldSpaceCameraPos ;
float3 planetSurfacePosition = IN.worldPos-IN.planetOrigin;
float3 planetSurfaceScatteringPosition = (flatScaledSpaceModel == 1.0) ? normalize(planetSurfacePosition) * Rg * 1.0001 : planetSurfacePosition * 6005; //transform to scaledspace here,
//6005 instead of 6000 due to precision issues, same with 1.0008
extinction = getExtinction((WCP-IN.planetOrigin)*6000, planetSurfaceScatteringPosition, 1.0, 1.0, 1.0);
#if defined (ECLIPSES_ON)
extinction*= getEclipseShadows(IN.worldPos*6000);
#endif
#if defined (RINGSHADOW_ON)
extinction *= getRingShadow(IN.worldPos*6000, _Sun_WorldSunDir, IN.planetOrigin*6000);
#endif
float average=(extinction.r+extinction.g+extinction.b)/3;
extinction = extinctionTint * extinction + (1-extinctionTint) * float3(average,average,average);
extinction= max(float3(0.0,0.0,0.0), (float3(1.0,1.0,1.0)*(1-extinctionThickness) + extinctionThickness*extinction) );
return float4(extinction,1.0);
}
ENDCG
}
Pass { //scattering pass
ZWrite Off
ZTest LEqual
Cull Back
Offset -0.05, -0.05
Blend OneMinusDstColor One //soft additive
CGPROGRAM
#include "UnityCG.cginc"
#pragma glsl
#pragma target 3.0
#pragma vertex vert
#pragma fragment frag
#include "../CommonAtmosphere.cginc"
#include "../EclipseCommon.cginc"
#include "../RingCommon.cginc"
#pragma multi_compile ECLIPSES_OFF ECLIPSES_ON
#pragma multi_compile PLANETSHINE_OFF PLANETSHINE_ON
#pragma multi_compile RINGSHADOW_OFF RINGSHADOW_ON
//#pragma fragmentoption ARB_precision_hint_nicest
uniform float _Alpha_Global;
uniform float3 _Sun_WorldSunDir;
uniform float _ScatteringExposure;
uniform float renderScattering;
#if defined (PLANETSHINE_ON)
uniform float4x4 planetShineSources;
uniform float4x4 planetShineRGB;
#endif
//sampler2D _MainTex;
uniform float flatScaledSpaceModel;
struct v2f
{
float4 pos: SV_POSITION;
float3 worldPos : TEXCOORD0;
float3 planetOrigin: TEXCOORD1;
};
v2f vert(appdata_base v)
{
v2f OUT;
OUT.pos = UnityObjectToClipPos(v.vertex);
OUT.worldPos = mul(unity_ObjectToWorld, v.vertex);
OUT.planetOrigin = mul (unity_ObjectToWorld, float4(0,0,0,1)).xyz;
OUT.pos = (renderScattering == 1.0) ? OUT.pos : float4(2.0, 2.0, 2.0, 1.0); //outside clip space => cull vertex
return OUT;
}
half4 frag(v2f IN): COLOR
{
float3 inscatter=0.0;
float3 extinction=0.0;
float3 WCP = _WorldSpaceCameraPos;
float3 planetSurfacePosition = IN.worldPos-IN.planetOrigin;
float3 planetSurfaceScatteringPosition = (flatScaledSpaceModel == 1.0) ? normalize(planetSurfacePosition) * Rg * 1.0001 : planetSurfacePosition * 6005; //transform to scaledspace here,
//6005 instead of 6000 due to precision issues, same with 1.0008
inscatter= InScattering2((WCP-IN.planetOrigin)*6000, planetSurfaceScatteringPosition, _Sun_WorldSunDir,extinction);
#if defined (ECLIPSES_ON)
inscatter *= getEclipseShadows(IN.worldPos*6000);
#endif
#if defined (RINGSHADOW_ON)
inscatter *= getRingShadow(IN.worldPos*6000, _Sun_WorldSunDir, IN.planetOrigin*6000);
#endif
///////////////////PLANETSHINE///////////////////////////////
//#if defined (PLANETSHINE_ON)
// float3 inscatter2=0;
// float intensity=1;
// for (int i=0; i<4; ++i)
// {
// if (planetShineRGB[i].w == 0) break;
//
// //if source is not a sun compute intensity of light from angle to light source
// intensity=1;
// if (planetShineSources[i].w != 1.0f)
// {
//// intensity = 0.5f*(1-dot(normalize(planetShineSources[i].xyz - worldPos),WSD));
// intensity = 0.57f*max((0.75-dot(normalize(planetShineSources[i].xyz - planetSurfacePosition),WSD)),0);
// }
//
// inscatter2+=SkyRadiance3(WCP - IN.planetOrigin, d, normalize(planetShineSources[i].xyz))
// *planetShineRGB[i].xyz*planetShineRGB[i].w*intensity;
// }
//
// finalColor+=inscatter2;
//#endif
/////////////////////////////////////////////////////////////
return float4(hdr(inscatter,_ScatteringExposure),1.0);
}
ENDCG
}
}
}