2using System.Collections;
5[RequireComponent(typeof(Light))]
18 void UpdateShadowmap()
25 if (m_ShadowmapCamera ==
null)
27 GameObject go =
new GameObject(
"Depth Camera");
28 go.AddComponent(typeof(Camera));
29 m_ShadowmapCamera = go.GetComponent<Camera>();
30 go.hideFlags = HideFlags.HideAndDontSave;
31 m_ShadowmapCamera.enabled =
false;
32 m_ShadowmapCamera.clearFlags = CameraClearFlags.SolidColor;
34 Transform cam = m_ShadowmapCamera.transform;
35 cam.position = transform.position;
36 cam.rotation = transform.rotation;
40 m_ShadowmapCamera.orthographic =
true;
41 m_ShadowmapCamera.nearClipPlane = 0;
42 m_ShadowmapCamera.farClipPlane =
m_Size.z;
43 m_ShadowmapCamera.orthographicSize =
m_Size.y * 0.5f;
48 m_ShadowmapCamera.orthographic =
false;
49 m_ShadowmapCamera.nearClipPlane =
m_SpotNear * m_Light.range;
50 m_ShadowmapCamera.farClipPlane =
m_SpotFar * m_Light.range;
51 m_ShadowmapCamera.fieldOfView = m_Light.spotAngle;
52 m_ShadowmapCamera.aspect = 1.0f;
54 m_ShadowmapCamera.renderingPath = RenderingPath.Forward;
55 m_ShadowmapCamera.targetTexture = m_Shadowmap;
57 m_ShadowmapCamera.backgroundColor =
Color.white;
59 m_ShadowmapCamera.RenderWithShader(
m_DepthShader,
"RenderType");
63 m_ShadowmapCamera.targetTexture = m_ColorFilter;
69 m_ShadowmapDirty =
false;
72 void RenderCoords(
int width,
int height, Vector4 lightPos)
74 SetFrustumRays(m_CoordMaterial);
76 RenderBuffer[] buffers = {m_CoordEpi.colorBuffer, m_DepthEpi.colorBuffer};
77 Graphics.SetRenderTarget(buffers, m_DepthEpi.depthBuffer);
78 m_CoordMaterial.SetVector(
"_LightPos", lightPos);
79 m_CoordMaterial.SetVector(
"_CoordTexDim",
new Vector4(m_CoordEpi.width, m_CoordEpi.height, 1.0f / m_CoordEpi.width, 1.0f / m_CoordEpi.height));
80 m_CoordMaterial.SetVector(
"_ScreenTexDim",
new Vector4(width, height, 1.0f / width, 1.0f / height));
81 m_CoordMaterial.SetPass(0);
85 void RenderInterpolationTexture(Vector4 lightPos)
87 Graphics.SetRenderTarget(m_InterpolationEpi.colorBuffer, m_RaymarchedLightEpi.depthBuffer);
88 if (!m_DX11Support && (Application.platform == RuntimePlatform.WindowsEditor || Application.platform == RuntimePlatform.WindowsPlayer))
92 m_DepthBreaksMaterial.SetPass(1);
97 GL.Clear(
true,
true,
new Color(0, 0, 0, 1));
100 m_DepthBreaksMaterial.SetFloat(
"_DepthThreshold", GetDepthThresholdAdjusted());
101 m_DepthBreaksMaterial.SetTexture(
"_DepthEpi", m_DepthEpi);
102 m_DepthBreaksMaterial.SetVector(
"_DepthEpiTexDim",
new Vector4(m_DepthEpi.width, m_DepthEpi.height, 1.0f / m_DepthEpi.width, 1.0f / m_DepthEpi.height));
103 m_DepthBreaksMaterial.SetPass(0);
104 RenderQuadSections(lightPos);
107 void InterpolateAlongRays(Vector4 lightPos)
109 Graphics.SetRenderTarget(m_InterpolateAlongRaysEpi);
111 m_InterpolateAlongRaysMaterial.SetTexture(
"_InterpolationEpi", m_InterpolationEpi);
112 m_InterpolateAlongRaysMaterial.SetTexture(
"_RaymarchedLightEpi", m_RaymarchedLightEpi);
113 m_InterpolateAlongRaysMaterial.SetVector(
"_RaymarchedLightEpiTexDim",
new Vector4(m_RaymarchedLightEpi.width, m_RaymarchedLightEpi.height, 1.0f / m_RaymarchedLightEpi.width, 1.0f / m_RaymarchedLightEpi.height));
114 m_InterpolateAlongRaysMaterial.SetPass(0);
115 RenderQuadSections(lightPos);
118 void RenderSamplePositions(
int width,
int height, Vector4 lightPos)
120 InitRenderTexture (ref m_SamplePositions, width, height, 0, RenderTextureFormat.ARGB32,
false);
122 m_SamplePositions.enableRandomWrite =
true;
123 m_SamplePositions.filterMode = FilterMode.Point;
125 Graphics.SetRenderTarget (m_SamplePositions);
126 GL.Clear (
false,
true,
new Color(0,0,0,1));
129 Graphics.SetRandomWriteTarget(1, m_SamplePositions);
133 Graphics.SetRenderTarget(m_RaymarchedLightEpi);
135 m_SamplePositionsMaterial.SetVector(
"_OutputTexDim",
new Vector4(width-1, height-1, 0, 0));
136 m_SamplePositionsMaterial.SetVector(
"_CoordTexDim",
new Vector4(m_CoordEpi.width, m_CoordEpi.height, 0, 0));
137 m_SamplePositionsMaterial.SetTexture(
"_Coord", m_CoordEpi);
138 m_SamplePositionsMaterial.SetTexture(
"_InterpolationEpi", m_InterpolationEpi);
142 m_SamplePositionsMaterial.SetFloat(
"_SampleType", 1);
143 m_SamplePositionsMaterial.SetVector(
"_Color",
new Vector4(0.4f, 0.4f, 0, 0));
144 m_SamplePositionsMaterial.SetPass(0);
148 m_SamplePositionsMaterial.SetFloat(
"_SampleType", 0);
149 m_SamplePositionsMaterial.SetVector(
"_Color",
new Vector4(1, 0, 0, 0));
150 m_SamplePositionsMaterial.SetPass(0);
151 RenderQuadSections(lightPos);
156 void ShowSamples(
int width,
int height, Vector4 lightPos)
158 bool showSamples =
m_ShowSamples && m_DX11Support && m_SamplePositionsShaderCompiles;
159 SetKeyword(showSamples,
"SHOW_SAMPLES_ON",
"SHOW_SAMPLES_OFF");
161 RenderSamplePositions(width, height, lightPos);
166 void Raymarch(
int width,
int height, Vector4 lightPos)
168 SetFrustumRays(m_RaymarchMaterial);
170 int shadowmapWidth = m_Shadowmap.width;
171 int shadowmapHeight = m_Shadowmap.height;
173 Graphics.SetRenderTarget(m_RaymarchedLightEpi.colorBuffer, m_RaymarchedLightEpi.depthBuffer);
174 GL.Clear(
false,
true,
new Color(0, 0, 0, 1));
175 m_RaymarchMaterial.SetTexture(
"_Coord", m_CoordEpi);
176 m_RaymarchMaterial.SetTexture(
"_InterpolationEpi", m_InterpolationEpi);
177 m_RaymarchMaterial.SetTexture(
"_Shadowmap", m_Shadowmap);
179 brightness *= m_Light.intensity;
180 m_RaymarchMaterial.SetFloat(
"_Brightness", brightness);
181 m_RaymarchMaterial.SetFloat(
"_Extinction", -
m_Extinction);
182 m_RaymarchMaterial.SetVector(
"_ShadowmapDim",
new Vector4(shadowmapWidth, shadowmapHeight, 1.0f / shadowmapWidth, 1.0f / shadowmapHeight));
183 m_RaymarchMaterial.SetVector(
"_ScreenTexDim",
new Vector4(width, height, 1.0f / width, 1.0f / height));
184 m_RaymarchMaterial.SetVector(
"_LightColor", m_Light.color.linear);
186 SetKeyword(
m_Colored,
"COLORED_ON",
"COLORED_OFF");
187 m_RaymarchMaterial.SetTexture(
"_ColorFilter", m_ColorFilter);
189 m_RaymarchMaterial.SetTexture(
"_AttenuationCurveTex", m_AttenuationCurveTex);
190 Texture cookie = m_Light.cookie;
191 SetKeyword(cookie !=
null,
"COOKIE_TEX_ON",
"COOKIE_TEX_OFF");
193 m_RaymarchMaterial.SetTexture(
"_Cookie", cookie);
194 m_RaymarchMaterial.SetPass(0);
196 RenderQuadSections(lightPos);
202 if (!m_MinRequirements || !CheckCamera() || !IsVisible())
206 RenderBuffer depthBuffer = Graphics.activeDepthBuffer;
207 RenderBuffer colorBuffer = Graphics.activeColorBuffer;
209 Vector4 lightPos = GetLightViewportPos();
210 bool lightOnScreen = lightPos.x >= -1 && lightPos.x <= 1 && lightPos.y >= -1 && lightPos.y <= 1;
211 SetKeyword(lightOnScreen,
"LIGHT_ON_SCREEN",
"LIGHT_OFF_SCREEN");
212 int width = Screen.width;
213 int height = Screen.height;
217 SetKeyword(
directional,
"DIRECTIONAL_SHAFTS",
"SPOT_SHAFTS");
218 RenderCoords(width, height, lightPos);
219 RenderInterpolationTexture(lightPos);
220 Raymarch(width, height, lightPos);
221 InterpolateAlongRays(lightPos);
223 ShowSamples(width, height, lightPos);
226 SetFrustumRays(m_FinalInterpolationMaterial);
227 m_FinalInterpolationMaterial.SetTexture(
"_InterpolationEpi", m_InterpolationEpi);
228 m_FinalInterpolationMaterial.SetTexture(
"_DepthEpi", m_DepthEpi);
229 m_FinalInterpolationMaterial.SetTexture(
"_Shadowmap", m_Shadowmap);
230 m_FinalInterpolationMaterial.SetTexture(
"_Coord", m_CoordEpi);
231 m_FinalInterpolationMaterial.SetTexture(
"_SamplePositions", m_SamplePositions);
232 m_FinalInterpolationMaterial.SetTexture(
"_RaymarchedLight", m_InterpolateAlongRaysEpi);
233 m_FinalInterpolationMaterial.SetVector(
"_CoordTexDim",
new Vector4(m_CoordEpi.width, m_CoordEpi.height, 1.0f / m_CoordEpi.width, 1.0f / m_CoordEpi.height));
234 m_FinalInterpolationMaterial.SetVector(
"_ScreenTexDim",
new Vector4(width, height, 1.0f / width, 1.0f / height));
235 m_FinalInterpolationMaterial.SetVector(
"_LightPos", lightPos);
236 m_FinalInterpolationMaterial.SetFloat(
"_DepthThreshold", GetDepthThresholdAdjusted());
237 bool renderAsQuad =
directional || IntersectsNearPlane();
238 m_FinalInterpolationMaterial.SetFloat(
"_ZTest", (
float)(renderAsQuad ?
UnityEngine.Rendering.CompareFunction.Always :
UnityEngine.Rendering.CompareFunction.Less));
239 SetKeyword(renderAsQuad,
"QUAD_SHAFTS",
"FRUSTUM_SHAFTS");
241 Graphics.SetRenderTarget(colorBuffer, depthBuffer);
242 m_FinalInterpolationMaterial.SetPass(0);
void UpdateCameraDepthMode()
float m_MinDistFromCamera
bool CheckMinRequirements()
LayerMask m_ColorFilterMask
bool m_AttenuationCurveOn
float m_BrightnessColored
bool m_ShowInterpolatedSamples
float m_ShowSamplesBackgroundFade
LightShaftsShadowmapMode m_ShadowmapMode
Shader m_ColorFilterShader