Esempio n. 1
0
    /// <summary>
    ///
    /// </summary>
    /// <param name="renderer"></param>
    /// <param name="viewProj"></param>
    private void SetupDirectionalLight(VolumetricLightRenderer renderer, Matrix4x4 viewProj)
    {
        int pass = 4;

        _material.SetPass(pass);

        Mesh mesh = VolumetricLightRenderer.GetDirLightMesh();

        _commandBuffer.Clear();

        float     zScale = Mathf.Min(Camera.current.farClipPlane, MaxRayLength);
        float     yScale = Camera.current.farClipPlane * Mathf.Tan(Mathf.Deg2Rad * Camera.current.fieldOfView * 0.5f);
        float     xScale = yScale * Camera.current.aspect;
        Matrix4x4 world  = Matrix4x4.TRS(Camera.current.transform.position, Camera.current.transform.rotation, new Vector3(xScale, yScale, zScale));

        _material.SetMatrix("_WorldViewProj", viewProj * world);
        _material.SetMatrix("_WorldView", Camera.current.worldToCameraMatrix * world);

        if (Noise)
        {
            _material.EnableKeyword("NOISE");
        }
        else
        {
            _material.DisableKeyword("NOISE");
        }

        _material.SetVector("_LightDir", new Vector4(_light.transform.forward.x, _light.transform.forward.y, _light.transform.forward.z, 1.0f / (_light.range * _light.range)));
        _material.SetVector("_LightColor", _light.color * _light.intensity);

        if (_light.cookie == null)
        {
            _material.EnableKeyword("DIRECTIONAL");
            _material.DisableKeyword("DIRECTIONAL_COOKIE");
        }
        else
        {
            _material.EnableKeyword("DIRECTIONAL_COOKIE");
            _material.DisableKeyword("DIRECTIONAL");

            _material.SetTexture("_LightTexture0", _light.cookie);
        }

        Texture texture = null;

        if (_light.shadows != LightShadows.None)
        {
            _material.EnableKeyword("SHADOWS_DEPTH");
            //_commandBuffer.SetGlobalTexture("_ShadowMapTexture", BuiltinRenderTextureType.CurrentActive);
            if (renderer.Resolution == VolumetricLightRenderer.VolumtericResolution.Full)
            {
                _commandBuffer.SetRenderTarget(renderer.GetVolumeLightBuffer(), BuiltinRenderTextureType.CameraTarget);
            }
            else
            {
                _commandBuffer.SetRenderTarget(renderer.GetVolumeLightBuffer());
            }
            //_commandBuffer.Blit(texture, VolumetricLightRenderer.GetVolumeLightBuffer(), _material, pass);
            _commandBuffer.DrawMesh(mesh, world, _material, 0, pass);

            if (CustomRenderEvent != null)
            {
                CustomRenderEvent(renderer, this, _commandBuffer, viewProj);
            }
        }
        else
        {
            _material.DisableKeyword("SHADOWS_DEPTH");
            //globalBuffer.Blit(texture, VolumetricLightRenderer.GetVolumeLightBuffer(), _material, pass);
            renderer.GlobalCommandBuffer.DrawMesh(mesh, world, _material, 0, pass);

            if (CustomRenderEvent != null)
            {
                CustomRenderEvent(renderer, this, renderer.GlobalCommandBuffer, viewProj);
            }
        }
    }