static void SetupSpotLight(VolumetricLight light, CommandBuffer cb, Vector3 lightPos, bool drawShadows) { float angleScale = Mathf.Tan((light.spotAngle + 1) * 0.5f * Mathf.Deg2Rad) * light.range; Quaternion rotation = light.transform.rotation; Matrix4x4 world = Matrix4x4.TRS(lightPos, rotation, new Vector3(angleScale, angleScale, light.range)); Matrix4x4 view = Matrix4x4.TRS(lightPos, rotation, Vector3.one).inverse; Vector3 fwd = light.transform.forward; // cosAngle, planeD, Vector2 extraParams = new Vector2(Mathf.Cos((light.spotAngle + 1) * 0.5f * Mathf.Deg2Rad), -Vector3.Dot((lightPos + fwd * light.lightC.range), fwd)); cb.SetGlobalVector(_ConeAxis, fwd); SetNonDirectionalParams(light, cb, lightPos, ref world, drawShadows, extraParams); if (light.cookie != null) { Matrix4x4 proj = Matrix4x4.Perspective(light.spotAngle, 1, 0, 1); cb.SetGlobalMatrix(_MyLightMatrix0, spotClipNeg * proj * view); } if (drawShadows) { Matrix4x4 proj = Matrix4x4.Perspective(light.spotAngle, 1, SystemInfo.usesReversedZBuffer ? light.range : light.lightC.shadowNearPlane, SystemInfo.usesReversedZBuffer ? light.lightC.shadowNearPlane : light.range); Matrix4x4 m = spotClip * proj; m[0, 2] *= -1; m[1, 2] *= -1; m[2, 2] *= -1; m[3, 2] *= -1; cb.SetGlobalMatrix(_MyWorld2Shadow, m * view); } cb.DrawMesh(VolumetricLightUtils.spotlightMesh, world, VolumetricLightUtils.GetMaterial(LightType.Spot), 0, IsCameraInSpotLightBounds(light, fwd) ? 0 : 1); }
static void SetupDirectionalLight(VolumetricLight light, CommandBuffer cb, bool drawShadows) { if (drawShadows) { cb.SetGlobalTexture(_World2ShadowTex, light.world2ShadowCache.world2ShadowTex); } cb.Blit(null as Texture, _RenderTarget, VolumetricLightUtils.GetMaterial(LightType.Directional), 0); }
static void SetupPointLight(VolumetricLight light, CommandBuffer cb, Vector3 lightPos, bool drawShadows) { Matrix4x4 world = Matrix4x4.TRS(lightPos, Quaternion.identity, Vector3.one * light.range * 2.0f); SetNonDirectionalParams(light, cb, lightPos, ref world, drawShadows, Vector2.zero); //view if (light.cookie != null) { cb.SetGlobalMatrix(_MyLightMatrix0, Matrix4x4.TRS(lightPos, light.transform.rotation, Vector3.one).inverse); } cb.DrawMesh(RenderUtils.GetMesh(PrimitiveType.Sphere), world, VolumetricLightUtils.GetMaterial(LightType.Point), 0, IsCameraInPointLightBounds(light) ? 0 : 1); }