Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Mesh and Fluid Problem
#47
(27-12-2019, 10:22 AM)josemendez Wrote: Ok, I think it's done. It all boils down to modifying the SimpleRenderer so that it shows per-particle colors, instead of using a flat color for the entire surface of the fluid. You can find the required files attached:
  • ObiSimpleFluidRendererColored.cs (the renderer you need to use)
  • FluidColorsBlendSimple.shader (shader for the color material)
  • SimpleFluidColored.shader (shader for the fluid material)

Here's the FluidMixing scene using these files, running at roughly 50 fps in an iPhone 7. Note we set the ParticleRenderer's radius scale to 1.8 to ensure good particle coverage.:


A short explanation of what needed to be done and why:

We start from the ObiSimpleFluidRenderer as it has better mobile compatibility, so we make a copy and call it ObiSimpleFluidRendererColored. We need to bring in the standard renderer's capability of rendering individual particle colors, so let's look into it and figure out how it's done. The comment on line 138 of ObiFluidRenderer.cs says:

Code:
// draw fluid thickness and color:

Predictably enough, the following lines set a render target, clear it to a color (1,1,1,0), and then we iterate over all particle renderers drawing their meshes into this render target 2 times: one using the thicknessMaterial, and a second time using the colorMaterial:

Code:
renderFluid.DrawMesh(mesh,Matrix4x4.identity,thickness_Material,0,0);
renderFluid.DrawMesh(mesh,Matrix4x4.identity,colorMaterial,0,0);

If you take a look at the equivalent code in ObiSimpleFluidRenderer, you will see 2 differences:
  • It clears the render target to pitch black (0,0,0,0). In the standard fluid renderer, (1,1,1,0) is used instead because particle color blending is multiplicative, and thickness blending is additive. For multiplication "1" is the neutral value, and for addition, "0" is.
  • It does not render the particles a second time using the colorMaterial.

So it seems like a good idea™ to copy these lines over to our simple renderer. Also, we'll need to copy the declaration of the colorMaterial used to render the particle colors. That's all we need to do in ObiSimpleFluidRendererColored.cs.

Now for the shaders. The shader used by thicknessMaterial is Hidden/FluidThickness. If we take a look into it, we'll see it only writes to the alpha channel of the render target, as specified in line 16:

Code:
ColorMask A

Similarly, the shader used by colorMaterial only writes to the RGB channels:

Code:
ColorMask RGB

So with the modifications we did in our simple renderer, thickness is being stored in the alpha channel of the _Thickness render target, and particle color in the red, green and blue channels. All that's left to do is read the rgb channels in our fluid shader. So we make a copy of SimpleFluid.shader, call it SimpleFluidColored.shader, and slightly modify its last line so that it modulates the final color using the particle color we read from the rgb channels of _Thickness:

Code:
return output * _Color * half4(fluid.rgb,1);

If we tried now, things *should* run. However if we take a look at the shader used to draw the colors into the _Thickness render target, we'd see it reads/writes from the depth buffer, which we aren't using at all because we're in 2D. So we can get rid of most of the zbuffer-related code in FluidColorsBlend and get a nice speedup.

That's it! all it took was modifying 3 lines of the simple fluid renderer, 1 line of the fluid shader, and optionally a few lines of the color blending shader.

If you want to further hide the particle-based structure of the fluid, you could also perform a blurring pass of the _Thickness render target before using it to render the final image. This would be a very simplified version of what the screen space curvature shader used in the standard renderer does (the curvature shader has to deal with depth discontinuities and depth-dependent blurring kernel size, both of which aren't needed in 2D).

kind regards,

Ok, how to remove https://i.imgur.com/31xFyHW.png black color while mixing?
During the gradient - https://i.imgur.com/FbgHTDy.png
Reply


Messages In This Thread
Mesh and Fluid Problem - by sc00b - 12-12-2019, 11:32 AM
RE: Mesh and Fluid Problem - by josemendez - 12-12-2019, 11:34 AM
RE: Mesh and Fluid Problem - by sc00b - 12-12-2019, 01:57 PM
RE: Mesh and Fluid Problem - by josemendez - 12-12-2019, 02:19 PM
RE: Mesh and Fluid Problem - by sc00b - 12-12-2019, 02:22 PM
RE: Mesh and Fluid Problem - by josemendez - 12-12-2019, 02:23 PM
RE: Mesh and Fluid Problem - by sc00b - 12-12-2019, 02:32 PM
RE: Mesh and Fluid Problem - by josemendez - 12-12-2019, 02:42 PM
RE: Mesh and Fluid Problem - by sc00b - 12-12-2019, 08:20 PM
RE: Mesh and Fluid Problem - by josemendez - 13-12-2019, 08:27 AM
RE: Mesh and Fluid Problem - by sc00b - 13-12-2019, 08:30 AM
RE: Mesh and Fluid Problem - by josemendez - 13-12-2019, 08:44 AM
RE: Mesh and Fluid Problem - by sc00b - 13-12-2019, 08:49 AM
RE: Mesh and Fluid Problem - by josemendez - 13-12-2019, 08:53 AM
RE: Mesh and Fluid Problem - by sc00b - 13-12-2019, 09:10 AM
RE: Mesh and Fluid Problem - by josemendez - 13-12-2019, 09:26 AM
RE: Mesh and Fluid Problem - by sc00b - 13-12-2019, 09:41 AM
RE: Mesh and Fluid Problem - by josemendez - 13-12-2019, 09:47 AM
RE: Mesh and Fluid Problem - by sc00b - 13-12-2019, 09:56 AM
RE: Mesh and Fluid Problem - by josemendez - 13-12-2019, 10:01 AM
RE: Mesh and Fluid Problem - by sc00b - 13-12-2019, 10:08 AM
RE: Mesh and Fluid Problem - by josemendez - 13-12-2019, 10:10 AM
RE: Mesh and Fluid Problem - by sc00b - 16-12-2019, 10:07 PM
RE: Mesh and Fluid Problem - by josemendez - 17-12-2019, 08:02 AM
RE: Mesh and Fluid Problem - by sc00b - 17-12-2019, 08:37 AM
RE: Mesh and Fluid Problem - by josemendez - 17-12-2019, 08:48 AM
RE: Mesh and Fluid Problem - by sc00b - 17-12-2019, 08:51 AM
RE: Mesh and Fluid Problem - by josemendez - 17-12-2019, 08:53 AM
RE: Mesh and Fluid Problem - by sc00b - 17-12-2019, 01:07 PM
RE: Mesh and Fluid Problem - by josemendez - 17-12-2019, 02:07 PM
RE: Mesh and Fluid Problem - by sc00b - 17-12-2019, 06:14 PM
RE: Mesh and Fluid Problem - by josemendez - 17-12-2019, 06:37 PM
RE: Mesh and Fluid Problem - by sc00b - 21-12-2019, 09:20 AM
RE: Mesh and Fluid Problem - by josemendez - 21-12-2019, 03:59 PM
RE: Mesh and Fluid Problem - by sc00b - 22-12-2019, 06:55 AM
RE: Mesh and Fluid Problem - by josemendez - 22-12-2019, 06:57 PM
RE: Mesh and Fluid Problem - by sc00b - 23-12-2019, 05:44 PM
RE: Mesh and Fluid Problem - by sc00b - 25-12-2019, 02:14 PM
RE: Mesh and Fluid Problem - by josemendez - 26-12-2019, 10:40 AM
RE: Mesh and Fluid Problem - by sc00b - 26-12-2019, 04:47 PM
RE: Mesh and Fluid Problem - by josemendez - 26-12-2019, 06:25 PM
RE: Mesh and Fluid Problem - by sc00b - 26-12-2019, 07:26 PM
RE: Mesh and Fluid Problem - by josemendez - 26-12-2019, 09:51 PM
RE: Mesh and Fluid Problem - by sc00b - 26-12-2019, 10:25 PM
RE: Mesh and Fluid Problem - by josemendez - 27-12-2019, 10:22 AM
RE: Mesh and Fluid Problem - by sc00b - 27-12-2019, 01:07 PM
RE: Mesh and Fluid Problem - by josemendez - 27-12-2019, 01:12 PM
RE: Mesh and Fluid Problem - by sc00b - 27-12-2019, 02:09 PM
RE: Mesh and Fluid Problem - by josemendez - 27-12-2019, 02:33 PM
RE: Mesh and Fluid Problem - by sc00b - 27-12-2019, 09:44 PM
RE: Mesh and Fluid Problem - by josemendez - 27-12-2019, 10:09 PM
RE: Mesh and Fluid Problem - by sc00b - 27-12-2019, 11:13 PM
RE: Mesh and Fluid Problem - by josemendez - 27-12-2019, 11:19 PM
RE: Mesh and Fluid Problem - by sc00b - 27-12-2019, 11:28 PM
RE: Mesh and Fluid Problem - by josemendez - 28-12-2019, 12:05 AM
RE: Mesh and Fluid Problem - by sc00b - 28-12-2019, 07:46 PM
RE: Mesh and Fluid Problem - by josemendez - 28-12-2019, 08:46 PM
RE: Mesh and Fluid Problem - by sc00b - 28-12-2019, 09:49 PM
RE: Mesh and Fluid Problem - by josemendez - 29-12-2019, 12:28 AM
RE: Mesh and Fluid Problem - by sc00b - 29-12-2019, 11:54 AM
RE: Mesh and Fluid Problem - by josemendez - 29-12-2019, 01:17 PM
RE: Mesh and Fluid Problem - by sc00b - 29-12-2019, 02:36 PM
RE: Mesh and Fluid Problem - by sc00b - 09-01-2020, 03:23 PM
RE: Mesh and Fluid Problem - by josemendez - 09-01-2020, 03:35 PM
RE: Mesh and Fluid Problem - by sc00b - 09-01-2020, 04:50 PM
RE: Mesh and Fluid Problem - by josemendez - 09-01-2020, 05:10 PM
RE: Mesh and Fluid Problem - by sc00b - 09-01-2020, 05:24 PM
RE: Mesh and Fluid Problem - by josemendez - 09-01-2020, 06:00 PM
RE: Mesh and Fluid Problem - by BelieveShuai - 21-12-2019, 01:38 PM