protected override void OnPreRenderImage(ComputeShader compute, RenderTexture src, RenderTexture dst) { var sdfShapes = RayMarchedShape.GetShapes(); int numShapes = sdfShapes.Count; if (m_shapes == null || m_shapes.count != numShapes) { if (m_shapes != null) { m_shapes.Dispose(); } m_shapes = new ComputeBuffer(Mathf.Max(1, numShapes), SdfShape.Stride); } m_shapes.SetData(sdfShapes); var camera = GetComponent <Camera>(); compute.SetTexture(m_const.Kernel, m_const.Src, src); compute.SetTexture(m_const.Kernel, m_const.Dst, dst); compute.SetMatrix(m_const.CameraInverseProjection, camera.projectionMatrix.inverse); compute.SetMatrix(m_const.CameraToWorld, camera.cameraToWorldMatrix); compute.SetVector(m_const.CameraPosition, camera.transform.position); compute.SetInts(m_const.ScreenSize, new int[] { camera.pixelWidth, camera.pixelHeight }); compute.SetBuffer(m_const.Kernel, m_const.SdfShapes, m_shapes); compute.SetInt(m_const.NumSdfShapes, numShapes); compute.SetVector(m_const.RayMarchParams, new Vector4(MaxRaySteps, RayHitThreshold, MaxRayDistance, Time.time)); compute.SetFloat(m_const.BlendDistance, BlendDistance); compute.SetVector(m_const.BackgroundColor, new Vector4(BackgroundColor.r, BackgroundColor.g, BackgroundColor.b, BackgroundColor.a)); compute.SetVector(m_const.MissColor, new Vector4(MissColor.r, MissColor.g, MissColor.b, MissColor.a)); }
protected override void OnPreRenderImage(ComputeShader compute, RenderTexture src, RenderTexture dst) { // validate SDF shapes buffer var sdfShapes = RayMarchedShape.GetShapes(); int numShapes = sdfShapes.Count; if (m_shapes == null || m_shapes.count != numShapes) { if (m_shapes != null) { m_shapes.Dispose(); m_shapes = null; } m_shapes = new ComputeBuffer(Mathf.Max(1, numShapes), SdfShape.Stride); } // validate SDF temp buffer if (m_tempBuffer == null || m_tempBuffer.width != src.width || m_tempBuffer.height != src.height) { if (m_tempBuffer != null) { DestroyImmediate(m_tempBuffer); m_tempBuffer = null; } m_tempBuffer = new RenderTexture(src.width, src.height, 0, RenderTextureFormat.ARGBFloat); m_tempBuffer.enableRandomWrite = true; m_tempBuffer.Create(); } // validate AABB tree buffer if (m_aabbTree == null || m_aabbTree.count != RayMarchedShape.AabbTreeCapacity) { if (m_aabbTree != null) { m_aabbTree.Dispose(); m_aabbTree = null; } m_aabbTree = new ComputeBuffer(RayMarchedShape.AabbTreeCapacity, AabbTree <RayMarchedShape> .NodePod.Stride); } // validate heat map buffer if (m_heatMap == null || m_heatMap.width != src.width || m_heatMap.height != src.height) { if (m_heatMap != null) { DestroyImmediate(m_heatMap); m_heatMap = null; } m_heatMap = new RenderTexture(src.width, src.height, 0, RenderTextureFormat.RFloat); m_heatMap.enableRandomWrite = true; m_heatMap.Create(); } // fill buffers m_shapes.SetData(sdfShapes); RayMarchedShape.FillAabbTree(m_aabbTree, AabbTree <RayMarchedShape> .FatBoundsRadius - BlendDistance); if (m_const.Kernels == null) { InitShaderConstants(); } foreach (int kernel in m_const.Kernels) { compute.SetTexture(kernel, m_const.Src, src); compute.SetTexture(kernel, m_const.Dst, dst); compute.SetBuffer(kernel, m_const.SdfShapes, m_shapes); compute.SetTexture(kernel, m_const.TempBuffer, m_tempBuffer); compute.SetBuffer(kernel, m_const.AabbTree, m_aabbTree); compute.SetTexture(kernel, m_const.HeatMap, m_heatMap); } var camera = GetComponent <Camera>(); compute.SetMatrix(m_const.CameraInverseProjection, camera.projectionMatrix.inverse); compute.SetMatrix(m_const.CameraToWorld, camera.cameraToWorldMatrix); compute.SetVector(m_const.CameraPosition, camera.transform.position); compute.SetInts(m_const.ScreenSize, new int[] { camera.pixelWidth, camera.pixelHeight }); compute.SetInt(m_const.NumSdfShapes, numShapes); compute.SetVector(m_const.RayMarchParams, new Vector4(MaxRaySteps, RayHitThreshold, MaxRayDistance, Time.time)); compute.SetFloat(m_const.BlendDistance, BlendDistance); compute.SetVector(m_const.BackgroundColor, new Vector4(BackgroundColor.r, BackgroundColor.g, BackgroundColor.b, BackgroundColor.a)); compute.SetVector(m_const.MissColor, new Vector4(MissColor.r, MissColor.g, MissColor.b, MissColor.a)); compute.SetBool(m_const.UseAabbTree, UseBoundingVolumes); compute.SetInt(m_const.AabbTreeRoot, RayMarchedShape.AabbTreeRoot); compute.SetVector(m_const.HeatColorCool, new Vector4(HeatColorCool.r, HeatColorCool.g, HeatColorCool.b, HeatColorCool.a)); compute.SetVector(m_const.HeatColorMedium, new Vector4(HeatColorMedium.r, HeatColorMedium.g, HeatColorMedium.b, HeatColorMedium.a)); compute.SetVector(m_const.HeatColorHot, new Vector4(HeatColorHot.r, HeatColorHot.g, HeatColorHot.b, HeatColorHot.a)); compute.SetFloat(m_const.HeatAlpha, HeatAlpha); }
protected override void OnPreRenderImage(ComputeShader compute, RenderTexture src, RenderTexture dst) { var sdfShapes = RayMarchedShape.GetShapes(); int numShapes = sdfShapes.Count; if (m_shapes == null || m_shapes.count != numShapes) { if (m_shapes != null) { m_shapes.Dispose(); } m_shapes = new ComputeBuffer(Mathf.Max(1, numShapes), SdfShape.Stride); } if (m_heatMap == null || m_heatMap.width != src.width || m_heatMap.height != src.height) { if (m_heatMap != null) { DestroyImmediate(m_heatMap); m_heatMap = null; } m_heatMap = new RenderTexture(src.width, src.height, 0, RenderTextureFormat.RFloat); m_heatMap.enableRandomWrite = true; m_heatMap.Create(); } m_shapes.SetData(sdfShapes); var camera = GetComponent <Camera>(); if (m_const.Kernels == null) { InitShaderConstants(); } foreach (int kernel in m_const.Kernels) { compute.SetTexture(kernel, m_const.Src, src); compute.SetTexture(kernel, m_const.Dst, dst); compute.SetTexture(kernel, m_const.HeatMap, m_heatMap); compute.SetBuffer(kernel, m_const.SdfShapes, m_shapes); } compute.SetMatrix(m_const.CameraInverseProjection, camera.projectionMatrix.inverse); compute.SetMatrix(m_const.CameraToWorld, camera.cameraToWorldMatrix); compute.SetVector(m_const.CameraPosition, camera.transform.position); compute.SetInts(m_const.ScreenSize, new int[] { camera.pixelWidth, camera.pixelHeight }); compute.SetInt(m_const.NumSdfShapes, numShapes); compute.SetVector(m_const.RayMarchParams, new Vector4(MaxRaySteps, RayHitThreshold, MaxRayDistance, Time.time)); compute.SetFloat(m_const.BlendDistance, BlendDistance); compute.SetVector(m_const.BackgroundColor, new Vector4(BackgroundColor.r, BackgroundColor.g, BackgroundColor.b, BackgroundColor.a)); compute.SetVector(m_const.MissColor, new Vector4(MissColor.r, MissColor.g, MissColor.b, MissColor.a)); compute.SetVector(m_const.HeatColorCool, new Vector4(HeatColorCool.r, HeatColorCool.g, HeatColorCool.b, HeatColorCool.a)); compute.SetVector(m_const.HeatColorMedium, new Vector4(HeatColorMedium.r, HeatColorMedium.g, HeatColorMedium.b, HeatColorMedium.a)); compute.SetVector(m_const.HeatColorHot, new Vector4(HeatColorHot.r, HeatColorHot.g, HeatColorHot.b, HeatColorHot.a)); compute.SetFloat(m_const.HeatAlpha, HeatAlpha); }