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; }
private void UpdateParameters() { // update raytracing scene, in case something moved _rtas.Update(); // frustum corners for current camera transform Vector3 bottomLeft = _camera.ViewportToWorldPoint(new Vector3(0, 0, _camera.farClipPlane)).normalized; Vector3 topLeft = _camera.ViewportToWorldPoint(new Vector3(0, 1, _camera.farClipPlane)).normalized; Vector3 bottomRight = _camera.ViewportToWorldPoint(new Vector3(1, 0, _camera.farClipPlane)).normalized; Vector3 topRight = _camera.ViewportToWorldPoint(new Vector3(1, 1, _camera.farClipPlane)).normalized; // update camera, environment parameters _rayTracingShader.SetVector("_SkyColor", SkyColor.gamma); _rayTracingShader.SetVector("_GroundColor", GroundColor.gamma); _rayTracingShader.SetVector("_TopLeftFrustumDir", topLeft); _rayTracingShader.SetVector("_TopRightFrustumDir", topRight); _rayTracingShader.SetVector("_BottomLeftFrustumDir", bottomLeft); _rayTracingShader.SetVector("_BottomRightFrustumDir", bottomRight); _rayTracingShader.SetVector("_CameraPos", _camera.transform.position); _cameraWorldMatrix = _camera.transform.localToWorldMatrix; // reset accumulation frame counter _frameIndex = 0; }
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); }
/// <summary> /// Ray tracing to check whether plane1 can see plane2 by GPU. /// </summary> /// <param name="plane1">the plane1.</param> /// <param name="plane2">the plane2.</param> /// <returns>the result.</returns> public bool RayTracingPlane2PlaneGPU(Plane plane1, Plane plane2) { var viewRect = new Rect(0F, 0F, math.ceil(plane1.size.x / rayTracingStep.x), math.ceil(plane1.size.y / rayTracingStep.x)); var renderTarget = RequireRenderTarget(ref viewRect); var resultBuffer = RequireResultBuffer(); bakeRayTraceShader.SetShaderPass("PVS"); bakeRayTraceShader.SetAccelerationStructure(_accelerationStructureShaderId, accelerationStructure); bakeRayTraceShader.SetTexture(_resultShaderId, renderTarget); bakeRayTraceShader.SetVector(_viewLeftBottomCornerShaderId, plane1.leftBottomCorner); bakeRayTraceShader.SetVector(_viewRightDirShaderId, plane1.rightDir); bakeRayTraceShader.SetVector(_viewUpDirShaderId, plane1.upDir); bakeRayTraceShader.SetVector(_viewSizeShaderId, plane1.size); bakeRayTraceShader.SetVector(_targetLeftBottomCornerShaderId, plane2.leftBottomCorner); bakeRayTraceShader.SetVector(_targetRightDirShaderId, plane2.rightDir); bakeRayTraceShader.SetVector(_targetUpDirShaderId, plane2.upDir); bakeRayTraceShader.SetVector(_targetSizeShaderId, plane2.size); bakeRayTraceShader.SetVector(_rayTracingStepShaderId, rayTracingStep); bakeRayTraceShader.Dispatch("BakeRaygenShader", (int)viewRect.width, (int)viewRect.height, 1); var data = new byte[] { 0, 0, 0, 0 }; resultBuffer.SetData(data); var kernelId = countPixelComputeShader.FindKernel("CSMain"); countPixelComputeShader.SetTexture(kernelId, _pixelsShaderId, renderTarget); countPixelComputeShader.SetInt(_widthShaderId, (int)viewRect.width); countPixelComputeShader.SetInt(_heightShaderId, (int)viewRect.height); countPixelComputeShader.SetBuffer(kernelId, _resultShaderId, resultBuffer); countPixelComputeShader.GetKernelThreadGroupSizes(kernelId, out var grpSizeX, out var grpSizeY, out var _); countPixelComputeShader.Dispatch(kernelId, (int)math.ceil(viewRect.width / grpSizeX), (int)math.ceil(viewRect.height / grpSizeY), 1); resultBuffer.GetData(data); return(data[0] > 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); } }
void SetShaderProperties() { Vector3 bottomLeft = cam.ViewportToWorldPoint(new Vector3(0, 0, 100000)).normalized; Vector3 topLeft = cam.ViewportToWorldPoint(new Vector3(0, 1, 100000)).normalized; Vector3 bottomRight = cam.ViewportToWorldPoint(new Vector3(1, 0, 100000)).normalized; Vector3 topRight = cam.ViewportToWorldPoint(new Vector3(1, 1, 100000)).normalized; shader.SetVector("_TopLeftFrustumDir", topLeft); shader.SetVector("_TopRightFrustumDir", topRight); shader.SetVector("_BottomLeftFrustumDir", bottomLeft); shader.SetVector("_BottomRightFrustumDir", bottomRight); shader.SetVector("_Sun", sun.transform.forward); shader.SetVector("_WSCP", transform.position); shader.SetFloat("_RayLength", rayLength); combine.SetTexture("_CS", cs); shader.SetAccelerationStructure("_RTAS", _RTAS); combine.SetColor("_Ambient", RenderSettings.ambientLight); }