public static void Foam(CommandBuffer cmd, FluidRenderTargets renderTargets, ObiParticleRenderer[] renderers) { foreach (ObiParticleRenderer renderer in renderers) { if (renderer != null) { ObiFoamGenerator foamGenerator = renderer.GetComponent <ObiFoamGenerator>(); if (foamGenerator != null && foamGenerator.advector != null && foamGenerator.advector.Particles != null) { ParticleSystemRenderer psRenderer = foamGenerator.advector.Particles.GetComponent <ParticleSystemRenderer>(); if (psRenderer != null) { cmd.DrawRenderer(psRenderer, psRenderer.material); } } } } cmd.SetGlobalTexture("_Foam", renderTargets.foam); }
public void SetupFluidRenderingCommandBuffer() { renderFluid.Clear(); if (particleRenderers == null || fluidMaterial == null) { return; } int refraction = Shader.PropertyToID("_Refraction"); int foam = Shader.PropertyToID("_Foam"); int depth = Shader.PropertyToID("_FluidDepthTexture"); int thickness1 = Shader.PropertyToID("_FluidThickness1"); int thickness2 = Shader.PropertyToID("_FluidThickness2"); int smoothDepth = Shader.PropertyToID("_FluidSurface"); int normals = Shader.PropertyToID("_FluidNormals"); // refraction (background), foam and fluid depth buffers: renderFluid.GetTemporaryRT(refraction, -1, -1, 0, FilterMode.Bilinear); renderFluid.GetTemporaryRT(foam, -1, -1, 0, FilterMode.Bilinear); renderFluid.GetTemporaryRT(depth, -1, -1, 24, FilterMode.Point, RenderTextureFormat.Depth); // thickness/color, surface depth and normals buffers: renderFluid.GetTemporaryRT(thickness1, -2, -2, 16, FilterMode.Bilinear, RenderTextureFormat.ARGBHalf); renderFluid.GetTemporaryRT(thickness2, -2, -2, 0, FilterMode.Bilinear, RenderTextureFormat.ARGBHalf); renderFluid.GetTemporaryRT(smoothDepth, -1, -1, 0, FilterMode.Point, RenderTextureFormat.RFloat); renderFluid.GetTemporaryRT(normals, -1, -1, 0, FilterMode.Bilinear, RenderTextureFormat.ARGBHalf); // Copy screen contents to refract them later. renderFluid.Blit(BuiltinRenderTextureType.CurrentActive, refraction); renderFluid.SetRenderTarget(depth); // fluid depth renderFluid.ClearRenderTarget(true, true, Color.clear); //clear // draw fluid depth texture: foreach (ObiParticleRenderer renderer in particleRenderers) { if (renderer != null) { foreach (Mesh mesh in renderer.ParticleMeshes) { renderFluid.DrawMesh(mesh, Matrix4x4.identity, renderer.ParticleMaterial, 0, 0); } } } // draw fluid thickness and color: renderFluid.SetRenderTarget(thickness1); renderFluid.ClearRenderTarget(true, true, thicknessBufferClear); foreach (ObiParticleRenderer renderer in particleRenderers) { if (renderer != null) { foreach (Mesh mesh in renderer.ParticleMeshes) { renderFluid.DrawMesh(mesh, Matrix4x4.identity, thickness_Material, 0, 0); renderFluid.DrawMesh(mesh, Matrix4x4.identity, colorMaterial, 0, 0); } } } // blur fluid thickness: renderFluid.Blit(thickness1, thickness2, thickness_Material, 1); renderFluid.Blit(thickness2, thickness1, thickness_Material, 2); // draw foam: renderFluid.SetRenderTarget(foam); renderFluid.ClearRenderTarget(true, true, Color.clear); foreach (ObiParticleRenderer renderer in particleRenderers) { if (renderer != null) { ObiFoamGenerator foamGenerator = renderer.GetComponent <ObiFoamGenerator>(); if (foamGenerator != null && foamGenerator.advector != null && foamGenerator.advector.Particles != null) { ParticleSystemRenderer psRenderer = foamGenerator.advector.Particles.GetComponent <ParticleSystemRenderer>(); if (psRenderer != null) { renderFluid.DrawRenderer(psRenderer, psRenderer.material); } } } } // blur fluid surface: renderFluid.Blit(depth, smoothDepth, depth_BlurMaterial); renderFluid.ReleaseTemporaryRT(depth); // reconstruct normals from smoothed depth: renderFluid.Blit(smoothDepth, normals, normal_ReconstructMaterial); // render fluid: renderFluid.SetGlobalTexture("_FluidDepth", depth); renderFluid.SetGlobalTexture("_Foam", foam); renderFluid.SetGlobalTexture("_Refraction", refraction); renderFluid.SetGlobalTexture("_Thickness", thickness1); renderFluid.SetGlobalTexture("_Normals", normals); renderFluid.Blit(smoothDepth, BuiltinRenderTextureType.CameraTarget, fluidMaterial); }
// Update is called once per frame public static void SetupCommandBuffer(CommandBuffer cmd, RenderTargetIdentifier cameraTarget, FluidRenderTargets renderTargets, Material depth_BlurMaterial, Material normal_ReconstructMaterial, Material thickness_Material, Material colorMaterial, Material fluidMaterial, ObiParticleRenderer[] renderers) { // Copy screen contents to refract them later. cmd.Blit(cameraTarget, renderTargets.refraction); cmd.SetRenderTarget(renderTargets.depth); // fluid depth cmd.ClearRenderTarget(true, true, Color.clear); //clear // draw fluid depth texture: foreach (ObiParticleRenderer renderer in renderers) { if (renderer != null) { foreach (Mesh mesh in renderer.ParticleMeshes) { if (renderer.ParticleMaterial != null) { cmd.DrawMesh(mesh, Matrix4x4.identity, renderer.ParticleMaterial, 0, 0); } } } } // draw fluid thickness and color: cmd.SetRenderTarget(renderTargets.thickness1); cmd.ClearRenderTarget(true, true, thicknessBufferClear); foreach (ObiParticleRenderer renderer in renderers) { if (renderer != null) { cmd.SetGlobalColor("_ParticleColor", renderer.particleColor); cmd.SetGlobalFloat("_RadiusScale", renderer.radiusScale); foreach (Mesh mesh in renderer.ParticleMeshes) { cmd.DrawMesh(mesh, Matrix4x4.identity, thickness_Material, 0, 0); cmd.DrawMesh(mesh, Matrix4x4.identity, colorMaterial, 0, 0); } } } // blur fluid thickness: cmd.Blit(renderTargets.thickness1, renderTargets.thickness2, thickness_Material, 1); cmd.Blit(renderTargets.thickness2, renderTargets.thickness1, thickness_Material, 2); // draw foam: cmd.SetRenderTarget(renderTargets.foam); cmd.ClearRenderTarget(true, true, Color.clear); foreach (ObiParticleRenderer renderer in renderers) { if (renderer != null) { ObiFoamGenerator foamGenerator = renderer.GetComponent <ObiFoamGenerator>(); if (foamGenerator != null && foamGenerator.advector != null && foamGenerator.advector.Particles != null) { ParticleSystemRenderer psRenderer = foamGenerator.advector.Particles.GetComponent <ParticleSystemRenderer>(); if (psRenderer != null) { cmd.DrawRenderer(psRenderer, psRenderer.material); } } } } // blur fluid surface: cmd.Blit(renderTargets.depth, renderTargets.smoothDepth, depth_BlurMaterial); // reconstruct normals from smoothed depth: cmd.Blit(renderTargets.smoothDepth, renderTargets.normals, normal_ReconstructMaterial); // render fluid: cmd.SetGlobalTexture("_FluidDepth", renderTargets.depth); cmd.SetGlobalTexture("_Foam", renderTargets.foam); cmd.SetGlobalTexture("_Refraction", renderTargets.refraction); cmd.SetGlobalTexture("_Thickness", renderTargets.thickness1); cmd.SetGlobalTexture("_Normals", renderTargets.normals); cmd.Blit(renderTargets.smoothDepth, cameraTarget, fluidMaterial); }