private void PreRenderEvent(VolumeLightManager renderer, Matrix4x4 viewProj) { if (thisLight.enabled == false || direMaterial == null) { return; } direMaterial.SetVector("_CameraForward", Camera.current.transform.forward); direMaterial.SetInt("_SampleCount", SampleCount); direMaterial.SetVector("_NoiseVelocity", new Vector4(NoiseVelocity.x, NoiseVelocity.y) * NoiseScale); direMaterial.SetVector("_NoiseData", new Vector4(NoiseScale, NoiseIntensity, NoiseIntensityOffset)); direMaterial.SetVector("_MieG", new Vector4(1 - (HenyeyGreensteinG * HenyeyGreensteinG), 1 + (HenyeyGreensteinG * HenyeyGreensteinG), 2 * HenyeyGreensteinG, 1.0f / (4.0f * Mathf.PI))); direMaterial.SetVector("_VolumetricLight", new Vector4(ScatteringCoef, ExtinctionCoef, thisLight.range, 1.0f - SkyboxExtinctionCoef)); direMaterial.SetTexture("_CameraDepthTexture", renderer.GetVolumeLightDepthBuffer()); direMaterial.SetFloat("_ZTest", (int)UnityEngine.Rendering.CompareFunction.Always); if (HeightFog) { direMaterial.EnableKeyword("HEIGHT_FOG"); direMaterial.SetVector("_HeightFog", new Vector4(GroundLevel, HeightScale)); } else { direMaterial.DisableKeyword("HEIGHT_FOG"); } SetupDirectionalLight(renderer, viewProj); }
private void SetupDirectionalLight(VolumeLightManager renderer, Matrix4x4 viewProj) { commandBuffer.Clear(); int pass = 0; direMaterial.SetPass(pass); if (Noise) { direMaterial.EnableKeyword("NOISE"); } else { direMaterial.DisableKeyword("NOISE"); } direMaterial.SetVector("_LightDir", new Vector4(thisLight.transform.forward.x, thisLight.transform.forward.y, thisLight.transform.forward.z, 1.0f / (thisLight.range * thisLight.range))); direMaterial.SetVector("_LightColor", thisLight.color * thisLight.intensity); direMaterial.SetFloat("_MaxRayLength", MaxRayLength); if (thisLight.cookie == null) { direMaterial.EnableKeyword("DIRECTIONAL"); direMaterial.DisableKeyword("DIRECTIONAL_COOKIE"); } else { direMaterial.EnableKeyword("DIRECTIONAL_COOKIE"); direMaterial.DisableKeyword("DIRECTIONAL"); direMaterial.SetTexture("_LightTexture0", thisLight.cookie); } _frustumCorners[0] = Camera.current.ViewportToWorldPoint(new Vector3(0, 0, Camera.current.farClipPlane)); _frustumCorners[2] = Camera.current.ViewportToWorldPoint(new Vector3(0, 1, Camera.current.farClipPlane)); _frustumCorners[3] = Camera.current.ViewportToWorldPoint(new Vector3(1, 1, Camera.current.farClipPlane)); _frustumCorners[1] = Camera.current.ViewportToWorldPoint(new Vector3(1, 0, Camera.current.farClipPlane)); #if UNITY_5_4_OR_NEWER direMaterial.SetVectorArray("_FrustumCorners", _frustumCorners); #else _material.SetVector("_FrustumCorners0", _frustumCorners[0]); _material.SetVector("_FrustumCorners1", _frustumCorners[1]); _material.SetVector("_FrustumCorners2", _frustumCorners[2]); _material.SetVector("_FrustumCorners3", _frustumCorners[3]); #endif Texture nullTexture = null; if (thisLight.shadows != LightShadows.None) { direMaterial.EnableKeyword("SHADOWS_DEPTH"); commandBuffer.Blit(nullTexture, renderer.GetVolumeLightBuffer(), direMaterial, pass); } else { direMaterial.DisableKeyword("SHADOWS_DEPTH"); renderer.commandBuffers[0].Blit(nullTexture, renderer.GetVolumeLightBuffer(), direMaterial, pass); } }
private void SetupPointLight(VolumeLightManager renderer, Matrix4x4 viewProj) { commandBuffer.Clear(); int pass = 1; if (!IsCameraInPointLightBounds()) { pass = 1; } poinMaterial.SetPass(pass); float scale = thisLight.range * 2.0f; Matrix4x4 world = Matrix4x4.TRS(transform.position, thisLight.transform.rotation, new Vector3(scale, scale, scale)); poinMaterial.SetMatrix("_WorldViewProj", viewProj * world); poinMaterial.SetMatrix("_WorldView", Camera.current.worldToCameraMatrix * world); if (Noise) { poinMaterial.EnableKeyword("NOISE"); } else { poinMaterial.DisableKeyword("NOISE"); } poinMaterial.SetVector("_LightPos", new Vector4(thisLight.transform.position.x, thisLight.transform.position.y, thisLight.transform.position.z, 1.0f / (thisLight.range * thisLight.range))); poinMaterial.SetColor("_LightColor", thisLight.color * thisLight.intensity); if (thisLight.cookie == null) { poinMaterial.EnableKeyword("POINT"); poinMaterial.DisableKeyword("POINT_COOKIE"); } else { Matrix4x4 view = Matrix4x4.TRS(thisLight.transform.position, thisLight.transform.rotation, Vector3.one).inverse; poinMaterial.SetMatrix("_MyLightMatrix0", view); poinMaterial.EnableKeyword("POINT_COOKIE"); poinMaterial.DisableKeyword("POINT"); poinMaterial.SetTexture("_LightTexture0", thisLight.cookie); } bool forceShadowsOff = false; if ((thisLight.transform.position - Camera.current.transform.position).magnitude >= QualitySettings.shadowDistance) { forceShadowsOff = true; } if (thisLight.shadows != LightShadows.None && forceShadowsOff == false) { poinMaterial.EnableKeyword("SHADOWS_CUBE"); commandBuffer.SetGlobalTexture("_ShadowMapTexture", BuiltinRenderTextureType.CurrentActive); commandBuffer.SetRenderTarget(renderer.GetVolumeLightBuffer()); commandBuffer.DrawMesh(sphereMesh, world, poinMaterial, 0, pass); } else { poinMaterial.DisableKeyword("SHADOWS_CUBE"); } }
private void SetupSpotLight(VolumeLightManager renderer, Matrix4x4 viewProj) { commandBuffer.Clear(); int pass = 0; if (!IsCameraInSpotLightBounds()) { pass = 1; } float scale = thisLight.range; float angleScale = Mathf.Tan((thisLight.spotAngle + 1) * 0.5f * Mathf.Deg2Rad) * thisLight.range; Matrix4x4 world = Matrix4x4.TRS(transform.position, transform.rotation, new Vector3(angleScale, angleScale, scale)); Matrix4x4 view = Matrix4x4.TRS(thisLight.transform.position, thisLight.transform.rotation, Vector3.one).inverse; Matrix4x4 clip = Matrix4x4.TRS(new Vector3(0.5f, 0.5f, 0.0f), Quaternion.identity, new Vector3(-0.5f, -0.5f, 1.0f)); Matrix4x4 proj = Matrix4x4.Perspective(thisLight.spotAngle, 1, 0, 1); spotMaterial.SetMatrix("_MyLightMatrix0", clip * proj * view); spotMaterial.SetMatrix("_WorldViewProj", viewProj * world); spotMaterial.SetVector("_LightPos", new Vector4(thisLight.transform.position.x, thisLight.transform.position.y, thisLight.transform.position.z, 1.0f / (thisLight.range * thisLight.range))); spotMaterial.SetVector("_LightColor", thisLight.color * thisLight.intensity); Vector3 apex = transform.position; Vector3 axis = transform.forward; Vector3 center = apex + axis * thisLight.range; float d = -Vector3.Dot(center, axis); spotMaterial.SetFloat("_PlaneD", d); spotMaterial.SetFloat("_CosAngle", Mathf.Cos((thisLight.spotAngle + 1) * 0.5f * Mathf.Deg2Rad)); spotMaterial.SetVector("_ConeApex", new Vector4(apex.x, apex.y, apex.z)); spotMaterial.SetVector("_ConeAxis", new Vector4(axis.x, axis.y, axis.z)); spotMaterial.EnableKeyword("SPOT"); if (Noise) { spotMaterial.EnableKeyword("NOISE"); } else { spotMaterial.DisableKeyword("NOISE"); } spotMaterial.SetTexture("_LightTexture0", renderer.spotTexutre); bool forceShadowsOff = false; if ((thisLight.transform.position - Camera.current.transform.position).magnitude >= QualitySettings.shadowDistance) { forceShadowsOff = true; } if (thisLight.shadows != LightShadows.None && forceShadowsOff == false) { clip = Matrix4x4.TRS(new Vector3(0.5f, 0.5f, 0.5f), Quaternion.identity, new Vector3(0.5f, 0.5f, 0.5f)); if (_reversedZ) { proj = Matrix4x4.Perspective(thisLight.spotAngle, 1, thisLight.range, thisLight.shadowNearPlane); } else { proj = Matrix4x4.Perspective(thisLight.spotAngle, 1, thisLight.shadowNearPlane, thisLight.range); } Matrix4x4 m = clip * proj; m[0, 2] *= -1; m[1, 2] *= -1; m[2, 2] *= -1; m[3, 2] *= -1; spotMaterial.SetMatrix("_MyWorld2Shadow", m * view); spotMaterial.SetMatrix("_WorldView", m * view); spotMaterial.EnableKeyword("SHADOWS_DEPTH"); commandBuffer.SetGlobalTexture("_ShadowMapTexture", BuiltinRenderTextureType.CurrentActive); commandBuffer.SetRenderTarget(renderer.GetVolumeLightBuffer()); commandBuffer.DrawMesh(spotMesh, world, spotMaterial, 0, pass); renderer.commandBuffers[0].DrawMesh(spotMesh, world, spotMaterial, 0, pass); } else { spotMaterial.DisableKeyword("SHADOWS_DEPTH"); renderer.commandBuffers[0].DrawMesh(spotMesh, world, spotMaterial, 0, pass); } }