void Reset()
    {
        _AccelerationStructure.Update();
        PathTracingShader.SetVector("_AmbientColor", AmbientColor.gamma);
        Matrix4x4 frustum = Matrix4x4.identity;

        frustum.SetRow(0, _Camera.ViewportToWorldPoint(new Vector3(0, 1, _Camera.farClipPlane)).normalized);
        frustum.SetRow(1, _Camera.ViewportToWorldPoint(new Vector3(1, 1, _Camera.farClipPlane)).normalized);
        frustum.SetRow(2, _Camera.ViewportToWorldPoint(new Vector3(0, 0, _Camera.farClipPlane)).normalized);
        frustum.SetRow(3, _Camera.ViewportToWorldPoint(new Vector3(1, 0, _Camera.farClipPlane)).normalized);
        PathTracingShader.SetMatrix("_Frustum", frustum);
        PathTracingShader.SetVector("_WorldSpaceCameraPos", _Camera.transform.position);
        _ModelMatrix = _Camera.transform.localToWorldMatrix;
        _Frame       = 0;
    }
Beispiel #2
0
    private void SetShaderParameters()
    {
        // for camera
        RayTracingShader.SetTexture(0, "_SkyboxTexture", SkyboxTexture);
        RayTracingShader.SetMatrix("_CameraToWorld", _camera.cameraToWorldMatrix);
        RayTracingShader.SetMatrix("_CameraInverseProjection", _camera.projectionMatrix.inverse);
        // light
        Vector3 l = DirectionalLight.transform.forward;

        RayTracingShader.SetVector("_DirectionalLight", new Vector4(l.x, l.y, l.z, DirectionalLight.intensity));
        // for antialiasing
        RayTracingShader.SetVector("_PixelOffset", new Vector2(Random.value, Random.value));
        // Spheres
        if (_sphereBuffer != null)
        {
            RayTracingShader.SetBuffer(0, "_Spheres", _sphereBuffer);
        }
    }
    public void OnRenderImage(RenderTexture src, RenderTexture dest)
    {
        var cam = Camera.main;

        if (!CheckResources(cam))
        {
            Graphics.Blit(src, dest);
            return;
        }

        //-- shader pass "RTPass" in material shaders
        rtShader.SetShaderPass("RTPass");

        //- set global variables, used in all material shaders
        Shader.SetGlobalVector(ShaderID._T_minmax, new Vector2(tMin, tMax));

        //-- set local RT raygen shader variables
        _rtAccStructure.Build();
        rtShader.SetAccelerationStructure(ShaderID._SceneAccelerationStructure, _rtAccStructure);

        rtShader.SetTexture(ShaderID._RenderTarget, _rtTargetTexture);
        rtShader.SetMatrix(ShaderID._InvViewMatrix, cam.cameraToWorldMatrix);
        rtShader.SetTexture(ShaderID._EnvironmentTex, environmentCubemap);
        rtShader.SetFloat(ShaderID._EnvironmentGamma, Mathf.GammaToLinearSpace(environmentExposure));
        rtShader.SetFloat(ShaderID._ShadowBrightness, shadowBrightness);

        var camOrigin = cam.transform.position;
        var frustumBottomLeftDirWS  = (cam.ViewportToWorldPoint(new Vector3(0, 0, cam.nearClipPlane)) - camOrigin).normalized;
        var frustumBottomRightDirWS = (cam.ViewportToWorldPoint(new Vector3(1, 0, cam.nearClipPlane)) - camOrigin).normalized;
        var frustumTopLeftDirWS     = (cam.ViewportToWorldPoint(new Vector3(0, 1, cam.nearClipPlane)) - camOrigin).normalized;

        rtShader.SetVector(ShaderID._FrustumBottomLeftDirWS, frustumBottomLeftDirWS);
        rtShader.SetVector(ShaderID._FrustumHorizDirWS, frustumBottomRightDirWS - frustumBottomLeftDirWS);
        rtShader.SetVector(ShaderID._FrustumVertDirWS, frustumTopLeftDirWS - frustumBottomLeftDirWS);

        //-- dispatch ray tracing
        rtShader.Dispatch("RaygenShader", cam.pixelWidth, cam.pixelHeight, 1);
        Graphics.Blit(_rtTargetTexture, dest);
    }
Beispiel #4
0
    void DispatchRays()
    {
        // update frame index and start path tracer
        _rayTracingShader.SetInt("_FrameIndex", _frameIndex);
        _rayTracingShader.SetShaderPass("DxrPass");         //

        // using e.g. ForwardDXR here will require binding all variables correctly, e.g. _EnvLightDatasRT
        // _rayTracingShader.SetShaderPass("ForwardDXR");
        // var lightCluster = HDRenderPipeline.currentPipeline.RequestLightCluster();
        // Shader.SetGlobalBuffer(HDShaderIDs._RaytracingLightCluster, lightCluster.GetCluster());
        // Shader.SetGlobalBuffer(HDShaderIDs._LightDatasRT, lightCluster.GetLightDatas());
        // Shader.SetGlobalBuffer(HDShaderIDs._EnvLightDatasRT, lightCluster.GetEnvLightDatas());

        _rayTracingShader.SetFloat("_JitterAmount", resultTexture == ResultTexture.DxrTarget ? 0 : 1);
        _rayTracingShader.SetBool("_UseLensData", lensRenderer && lensRenderer.isActiveAndEnabled);
        if (lensRenderer)
        {
            _rayTracingShader.SetMatrix("_DataProviderTransform", lensRenderer.transform.localToWorldMatrix);
            _rayTracingShader.SetTexture("_PositionData", lensRenderer.lensPositionTex);
            _rayTracingShader.SetTexture("_DirectionData", lensRenderer.lensDirectionTex);
        }
        else
        {
            _rayTracingShader.SetTexture("_PositionData", Texture2D.whiteTexture);
            _rayTracingShader.SetTexture("_DirectionData", Texture2D.whiteTexture);
        }

        // start one thread for each pixel on screen
        _rayTracingShader.Dispatch("MyRaygenShader", _dxrTarget.width, _dxrTarget.height, 1, _camera);

        // update accumulation material
        _accumulationMaterial.SetTexture("_CurrentFrame", _dxrTarget);
        _accumulationMaterial.SetTexture("_Accumulation", _accumulationTarget1);
        _accumulationMaterial.SetInt("_FrameIndex", _frameIndex++);

        // accumulate current raytracing result
        Graphics.Blit(_dxrTarget, _accumulationTarget2, _accumulationMaterial);

        // switch accumulate textures
        var temp = _accumulationTarget1;

        _accumulationTarget1 = _accumulationTarget2;
        _accumulationTarget2 = temp;

        // // apply DLSS to the _dxrTarget
        // var cmd = new CommandBuffer();
        // var initData = new DLSSCommandInitializationData()
        // {
        //  featureFlags = DLSSFeatureFlags.DoSharpening,
        //  inputRTHeight = (uint)_dxrTarget.height,
        //  inputRTWidth = (uint)_dxrTarget.width,
        //  outputRTWidth = (uint)(2 * _dxrTarget.width),
        //  outputRTHeight = (uint)(2 * _dxrTarget.height),
        //  quality = DLSSQuality.MaximumPerformance
        // };
        //
        // var context = _nvidiaDevice.CreateFeature(cmd, initData);
        // var textureTable = new DLSSTextureTable()
        // {
        //  colorInput = _dxrTarget,
        //  colorOutput = null,
        //  depth = null,
        //  motionVectors = null,
        // };
        // _nvidiaDevice.ExecuteDLSS(cmd, context, new DLSSTextureTable());

        GetComponent <MeshRenderer>().sharedMaterial.mainTexture = resultTexture == ResultTexture.DxrTarget ? _dxrTarget : _accumulationTarget1;
    }