private void Awake() { s_instance = this; cam = GetComponent <Camera>(); compositeResultBufferMtrl = new Material(Shader.Find("Hidden/JCDeferredShading/CompositeResultBuffer")); ssrMtrl = new Material(Shader.Find("Hidden/JCDeferredShading/ScreenSpaceReflection")); frontFaceDepthShader = Shader.Find("Hidden/JCDeferredShading/FrontFaceDepth"); backFaceDepthShader = Shader.Find("Hidden/JCDeferredShading/BackFaceDepth"); shaderPropId_diffuseBuffer = Shader.PropertyToID("_DiffuseBuffer"); shaderPropId_normalBuffer = Shader.PropertyToID("_NormalBuffer"); shaderPropId_positionBuffer = Shader.PropertyToID("_PositionBuffer"); shaderPropId_resultBuffer = Shader.PropertyToID("_ResultBuffer"); shaderPropId_ssrBuffer = Shader.PropertyToID("_SSRBuffer"); shaderPropId_doubleFaceDepthBuffer = Shader.PropertyToID("_DoubleFaceDepthBuffer"); shaderPropId_ssrVMatrix = Shader.PropertyToID("_SSR_V_MATRIX"); shaderPropId_ssrPMatrix = Shader.PropertyToID("_SSR_P_MATRIX"); shaderPropId_screenPixelSize = Shader.PropertyToID("_ScreenPixelSize"); shaderPropId_dirLightDir = Shader.PropertyToID("_DirLightDir"); shaderPropId_dirLightColor = Shader.PropertyToID("_DirLightColor"); shaderPropId_pointLightPos = Shader.PropertyToID("_PointLightPos"); shaderPropId_pointLightColor = Shader.PropertyToID("_PointLightColor"); shaderPropId_pointLightRange = Shader.PropertyToID("_PointLightRange"); mrtGBuffer = new JCDSRenderTexture( 3, Screen.width, Screen.height, JCDSRenderTexture.ValueToMask(null), RenderTextureFormat.ARGBHalf, FilterMode.Point, false ); resultRT = new JCDSRenderTexture( 1, Screen.width, Screen.height, JCDSRenderTexture.ValueToMask(new bool[] { true }), RenderTextureFormat.ARGB32, FilterMode.Point, false ); ssrRT = new JCDSRenderTexture( 1, Screen.width, Screen.height, JCDSRenderTexture.ValueToMask(null), RenderTextureFormat.ARGB32, FilterMode.Point, false ); doubleFaceDepthRT = new JCDSRenderTexture( 1, Screen.width, Screen.height, JCDSRenderTexture.ValueToMask(new bool[] { true }), RenderTextureFormat.ARGBFloat, FilterMode.Point, false ); CollectLights(); CreateDoubleFaceDepthCamera(); }
public static void SetMultipleRenderTargets(Camera camera, JCDSRenderTexture colorBuffers, JCDSRenderTexture depthBuffer, int depthBufferIndex) { if (camera == null || colorBuffers == null || depthBuffer == null) { throw new System.ArgumentNullException(); } camera.SetTargetBuffers(colorBuffers.GetColorBuffers(), depthBuffer.GetDepthBuffer(depthBufferIndex)); }
private void OnPreRender() { mrtGBuffer.ResetSize(Screen.width, Screen.height); resultRT.ResetSize(Screen.width, Screen.height); ssrRT.ResetSize(Screen.width, Screen.height); doubleFaceDepthRT.ResetSize(Screen.width, Screen.height); JCDSRenderTexture.StoreCurrentActiveBuffers(); JCDSRenderTexture.SetMultipleRenderTargets(cam, mrtGBuffer, resultRT, 0); }
private void OnGUI_DrawRTs(JCDSRenderTexture rt, ref Rect rect, bool isNewColumn) { int numRTs = rt.numRTs; for (int i = 0; i < numRTs; ++i) { GUI.DrawTexture(rect, rt.GetRenderTexture(i), ScaleMode.ScaleToFit, false); rect.y += rect.height; } if (isNewColumn) { rect.x += rect.width; rect.y = 0; } }
private void OnDestroy() { if (mrtGBuffer != null) { mrtGBuffer.Destroy(); mrtGBuffer = null; } if (resultRT != null) { resultRT.Destroy(); resultRT = null; } if (ssrRT != null) { ssrRT.Destroy(); ssrRT = null; } if (doubleFaceDepthRT != null) { doubleFaceDepthRT.Destroy(); doubleFaceDepthRT = null; } if (compositeResultBufferMtrl != null) { Material.Destroy(compositeResultBufferMtrl); compositeResultBufferMtrl = null; } if (ssrMtrl != null) { Material.Destroy(ssrMtrl); ssrMtrl = null; } frontFaceDepthShader = null; backFaceDepthShader = null; s_instance = null; }
private void OnPostRender() { doubleFaceDepthRT.SetActiveRenderTexture(0); JCDSRenderTexture.ClearActiveRenderTexture(true, true, Color.black, 1.0f); JCDSRenderTexture.SetMultipleRenderTargets(doubleFaceDepthCam, doubleFaceDepthRT, doubleFaceDepthRT, 0); doubleFaceDepthCam.RenderWithShader(frontFaceDepthShader, null); JCDSRenderTexture.ClearActiveRenderTexture(true, false, Color.black, 1.0f); doubleFaceDepthCam.RenderWithShader(backFaceDepthShader, null); resultRT.SetActiveRenderTexture(0); JCDSRenderTexture.ClearActiveRenderTexture(true, true, Color.black, 0.0f); compositeResultBufferMtrl.SetTexture(shaderPropId_diffuseBuffer, mrtGBuffer.GetRenderTexture(0)); compositeResultBufferMtrl.SetTexture(shaderPropId_normalBuffer, mrtGBuffer.GetRenderTexture(1)); compositeResultBufferMtrl.SetTexture(shaderPropId_positionBuffer, mrtGBuffer.GetRenderTexture(2)); compositeResultBufferMtrl.SetTexture(shaderPropId_resultBuffer, resultRT.GetRenderTexture(0)); compositeResultBufferMtrl.SetTexture(shaderPropId_ssrBuffer, ssrRT.GetRenderTexture(0)); int numDirLights = dirLights == null ? 0 : dirLights.Length; for (int i = 0; i < numDirLights; ++i) { Light light = dirLights[i]; if (light != null && light.enabled && light.gameObject.activeSelf) { Vector3 dir = -light.transform.forward; compositeResultBufferMtrl.SetVector(shaderPropId_dirLightDir, new Vector4(dir.x, dir.y, dir.z, light.intensity)); compositeResultBufferMtrl.SetColor(shaderPropId_dirLightColor, light.color); DrawScreenQuad(compositeResultBufferMtrl, 0, false, false); } } int numPointLights = pointLights == null ? 0 : pointLights.Length; for (int i = 0; i < numPointLights; ++i) { Light light = pointLights[i]; if (light != null && light.enabled && light.gameObject.activeSelf) { compositeResultBufferMtrl.SetVector(shaderPropId_pointLightPos, light.transform.position); compositeResultBufferMtrl.SetColor(shaderPropId_pointLightColor, light.color); compositeResultBufferMtrl.SetVector(shaderPropId_pointLightRange, new Vector4(1.0f / light.range, light.intensity, 0, 0)); compositeResultBufferMtrl.SetPass(2); Graphics.DrawMeshNow(pointLightMesh, Matrix4x4.TRS(light.transform.position, Quaternion.identity, Vector3.one * light.range * 2)); } } ssrMtrl.SetTexture(shaderPropId_diffuseBuffer, mrtGBuffer.GetRenderTexture(0)); ssrMtrl.SetTexture(shaderPropId_normalBuffer, mrtGBuffer.GetRenderTexture(1)); ssrMtrl.SetTexture(shaderPropId_positionBuffer, mrtGBuffer.GetRenderTexture(2)); ssrMtrl.SetTexture(shaderPropId_resultBuffer, resultRT.GetRenderTexture(0)); ssrMtrl.SetTexture(shaderPropId_doubleFaceDepthBuffer, doubleFaceDepthRT.GetRenderTexture(0)); ssrMtrl.SetMatrix(shaderPropId_ssrVMatrix, cam.worldToCameraMatrix); ssrMtrl.SetMatrix(shaderPropId_ssrPMatrix, cam.projectionMatrix); ssrMtrl.SetVector(shaderPropId_screenPixelSize, new Vector2(Screen.width, Screen.height)); ssrRT.SetActiveRenderTexture(0); JCDSRenderTexture.ClearActiveRenderTexture(true, true, Color.black, 1.0f); DrawScreenQuad(ssrMtrl, 0, false, false); JCDSRenderTexture.ResetActiveRenderTexture(); DrawScreenQuad(compositeResultBufferMtrl, 3, true, true); JCDSRenderTexture.RestoreCurrentActiveBuffers(cam); }