private void VolumeLightMgr_PreRenderEvent(VolumeLightMgr renderer, Matrix4x4 viewProj) { if (_light == null || _shadowCommandBuffer == null) { return; } _material.SetVector("_CameraForward", VolumeLightMgr.Instance._camera.transform.forward); _material.SetInt("_SampleCount", sampleCount); _material.SetFloat("_Bias", depthBias); _material.SetVector("_MieG", new Vector4(1 - (MieG * MieG), 1 + (MieG * MieG), 2 * MieG, 1.0f / (4.0f * Mathf.PI))); _material.SetVector("_VolumetricLight", new Vector4(ScatteringCoef, ExtinctionCoef, _light.range, 1.0f - SkyboxExtinctionCoef)); //_material.SetVector("_LightPos", new Vector4(this.transform.position.x, this.transform.position.y, this.transform.position.z, 1)); _material.SetTexture("_CameraDepthTexture", renderer.GetVolumeLightDepthBuffer()); _material.SetVector("_halfResolution", new Vector2(renderer.GetVolumeLightBuffer().width, renderer.GetVolumeLightBuffer().height) / 2); _material.SetFloat("_Strength", strength); _material.SetFloat("_Fallback", fallback); SetupSpotLight(renderer, viewProj); }
//public Mesh SpotLightMesh; private void SetupSpotLight(VolumeLightMgr renderer, Matrix4x4 viewProj) { _shadowCommandBuffer.Clear(); //如果CustomLightDepthTex不为空,就使用指定的DepthTex,否则使用unity灯光自带的 if (useCustomLightDepthMap) { _material.SetTexture("_LightDepthTexture", customLightDepthTex); } else { //拷贝depth map RenderTargetIdentifier shadowmap = BuiltinRenderTextureType.CurrentActive; //设置当前深度图的采样模式 _shadowCommandBuffer.SetShadowSamplingMode(shadowmap, ShadowSamplingMode.RawDepth); _shadowCommandBuffer.SetGlobalTexture("_LightDepthTexture", shadowmap); } //------------------------------------------------------ int pass = GetCameraInSpotLightBounds(VolumeLightMgr.Instance._camera); Mesh mesh = VolumeLightMgr.GetSpotLightMesh(); float scale = _light.range; float angleScale = Mathf.Tan((_light.spotAngle + 1) * 0.5f * Mathf.Deg2Rad) * _light.range; Matrix4x4 world = Matrix4x4.TRS(_light.transform.position, _light.transform.rotation, new Vector3(angleScale, angleScale, scale)); Matrix4x4 view = Matrix4x4.TRS(_light.transform.position, _light.transform.rotation, Vector3.one).inverse; //NDC 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(_light.spotAngle, 1, 0, 1); _material.SetMatrix("_MyLightMatrix0", clip * proj * view); _material.SetMatrix("_WorldViewProj", viewProj * world); _material.SetVector("_LightPos", new Vector4(_light.transform.position.x, _light.transform.position.y, _light.transform.position.z, 1.0f / (_light.range * _light.range))); _material.SetVector("_LightColor", _light.color * _light.intensity); Vector3 apex = _light.transform.position; Vector3 axis = _light.transform.forward; // plane equation ax + by + cz + d = 0; precompute d here to lighten the shader Vector3 center = apex + axis * _light.range; float d = -Vector3.Dot(center, axis); _material.SetFloat("_PlaneD", d); _material.SetFloat("_CosAngle", Mathf.Cos((_light.spotAngle + 1) * 0.5f * Mathf.Deg2Rad)); _material.SetVector("_ConeApex", new Vector4(apex.x, apex.y, apex.z)); _material.SetVector("_ConeAxis", new Vector4(axis.x, axis.y, axis.z)); _material.EnableKeyword("SPOT"); _material.DisableKeyword("NOISE"); if (_light.cookie == null) { _material.SetTexture("_LightCookieTex", VolumeLightMgr.GetDefaultSpotCookie()); } else { _material.SetTexture("_LightCookieTex", _light.cookie); } clip = Matrix4x4.TRS(new Vector3(0.5f, 0.5f, 0.5f), Quaternion.identity, new Vector3(0.5f, 0.5f, 0.5f)); if (VolumeLightMgr._reversedZ) { proj = Matrix4x4.Perspective(_light.spotAngle, 1, _light.range, _light.shadowNearPlane); _material.DisableKeyword("XIAO_MI"); } else { proj = Matrix4x4.Perspective(_light.spotAngle, 1, _light.shadowNearPlane, _light.range); if (Application.platform == RuntimePlatform.Android) { _material.EnableKeyword("XIAO_MI"); } } Matrix4x4 m = clip * proj; m[0, 2] *= -1; m[1, 2] *= -1; m[2, 2] *= -1; m[3, 2] *= -1; //view = _light.transform.worldToLocalMatrix; _material.SetMatrix("_MyWorld2Shadow", m * view); _material.EnableKeyword("SHADOWS_DEPTH"); if (isNoise) { _material.SetTexture("_NoiseTex", noiseTex); _material.SetVector("_NoiseVelocity", new Vector4(NoiseVelocity.x * 1.0f / NoiseScale.x, NoiseVelocity.y * 1.0f / NoiseScale.y)); _material.SetVector("_NoiseData", new Vector4(1.0f / NoiseScale.x, 1.0f / NoiseScale.y, NoiseIntensity, NoiseIntensityOffset)); _material.EnableKeyword("NOISE"); } else { _material.DisableKeyword("NOISE"); } if (contrast) { //_material.SetFloat("_Contrast", contrast); _material.EnableKeyword("CONTRAST"); } else { _material.DisableKeyword("CONTRAST"); } _shadowCommandBuffer.SetRenderTarget(renderer.GetVolumeLightBuffer()); //_shadowCommandBuffer.ClearRenderTarget(false, true, Color.white); _shadowCommandBuffer.DrawMesh(mesh, world, _material, 0, pass); //if (CustomRenderEvent != null) // CustomRenderEvent(renderer, this, _commandBuffer, viewProj); }