private void Update() { if (_lpvTex3D.r != null && _lpvTex3D.r.width != lpvGridResolution) { DestroyTargets(); } if (_lpvTex3D.r == null) { if (lpvGridResolution == 0) { return; } _tex3d_accum = CreateTarget(); _tex3d_gv = CreateTex3dGV(); _lpvTex3D_prev = CreateShRGB(); _lpvTex3D = CreateShRGB(); } _tex3d_gv.Create(); if (!GetComponent <RSMTest>().UpdateShadowsFull(out RenderTexture shadow_texture, out RenderTexture shadow_texture_color, out Matrix4x4 world_to_light_local_matrix, _tex3d_gv)) { return; } _lpvTex3D.Create(); _tex3d_accum.Create(); lpvCompute.SetInt("GridResolution", lpvGridResolution); lpvCompute.SetMatrix("WorldToLightLocalMatrix", world_to_light_local_matrix); int clear_kernel = lpvCompute.FindKernel("ClearKernel"); SetShRGBTextures(clear_kernel, "", _lpvTex3D); lpvCompute.SetTexture(clear_kernel, "LPV_accum", _tex3d_accum); int thread_groups = (lpvGridResolution + 3) / 4; lpvCompute.Dispatch(clear_kernel, thread_groups, thread_groups, thread_groups); #if false GetInitialBorderColor(out var border_sh_r, out var border_sh_g, out var border_sh_b); int border_kernel = lpvCompute.FindKernel("BorderKernel"); SetShRGBTextures(border_kernel, "", _lpvTex3D); //SetShRGBTextures(border_kernel, "_accum", _lpvTex3D_accum); lpvCompute.SetVector("LPV_sh_r", border_sh_r); lpvCompute.SetVector("LPV_sh_g", border_sh_g); lpvCompute.SetVector("LPV_sh_b", border_sh_b); thread_groups = (lpvGridResolution + 7) / 8; lpvCompute.Dispatch(border_kernel, thread_groups, thread_groups, 1); #endif int inject_kernel = lpvCompute.FindKernel("InjectKernel"); SetShRGBTextures(inject_kernel, "", _lpvTex3D); lpvCompute.SetTexture(inject_kernel, "ShadowTexture", shadow_texture); lpvCompute.SetTexture(inject_kernel, "ShadowTextureColor", shadow_texture_color); thread_groups = (lpvGridResolution + 7) / 8; lpvCompute.Dispatch(inject_kernel, thread_groups, thread_groups, 1); shadow_texture.Release(); shadow_texture_color.Release(); int propagate_step_kernel = lpvCompute.FindKernel("PropagateStepKernel"); lpvCompute.SetTexture(propagate_step_kernel, "LPV_accum", _tex3d_accum); lpvCompute.SetTexture(propagate_step_kernel, "LPV_gv", _tex3d_gv); thread_groups = (lpvGridResolution + 3) / 4; _lpvTex3D_prev.Create(); for (int i = 0; i < propagateSteps; i++) { Swap(ref _lpvTex3D_prev, ref _lpvTex3D); SetShRGBTextures(propagate_step_kernel, "_prev", _lpvTex3D_prev); SetShRGBTextures(propagate_step_kernel, "", _lpvTex3D); lpvCompute.SetInt("PropagationStep", i); lpvCompute.Dispatch(propagate_step_kernel, thread_groups, thread_groups, thread_groups); } _lpvTex3D_prev.Release(); _lpvTex3D.Release(); int clear_border_kernel = lpvCompute.FindKernel("ClearBorderKernel"); lpvCompute.SetTexture(clear_border_kernel, "LPV_accum", _tex3d_accum); thread_groups = (lpvGridResolution + 7) / 8; lpvCompute.Dispatch(clear_border_kernel, thread_groups, thread_groups, thread_groups); Shader.SetGlobalTexture("_LPV_accum", _tex3d_accum); Shader.SetGlobalFloat("_LPV_GridCellSize", lpvGridCellSize); if (!drawGizmosGV) { _tex3d_gv.Release(); } }
void DestroyTarget(ref ShRGB sh) { DestroyTarget(ref sh.r); DestroyTarget(ref sh.g); DestroyTarget(ref sh.b); }
void SetShRGBTextures(int kernel, string suffix, ShRGB sh) { lpvCompute.SetTexture(kernel, "LPV_r" + suffix, sh.r); lpvCompute.SetTexture(kernel, "LPV_g" + suffix, sh.g); lpvCompute.SetTexture(kernel, "LPV_b" + suffix, sh.b); }