Screen Space Fluid Rendering for Gam es Simon Green, NVIDIA - - PowerPoint PPT Presentation

screen space fluid rendering for gam es
SMART_READER_LITE
LIVE PREVIEW

Screen Space Fluid Rendering for Gam es Simon Green, NVIDIA - - PowerPoint PPT Presentation

Screen Space Fluid Rendering for Gam es Simon Green, NVIDIA Overview Introduction Fluid Simulation for Games Screen Space Fluid Rendering Demo I ntroduction DirectX 11 and DirectCompute enable physics effects to be computed


slide-1
SLIDE 1
slide-2
SLIDE 2

Screen Space Fluid Rendering for Gam es

Simon Green, NVIDIA

slide-3
SLIDE 3

Overview

 Introduction  Fluid Simulation for Games  Screen Space Fluid Rendering  Demo

slide-4
SLIDE 4

I ntroduction

 DirectX 11 and DirectCompute enable

physics effects to be computed and rendered directly on the GPU

 DirectCompute allows flexible general

purpose computation on the GPU

 sorting, searching  spatial data structures

 DirectX 11 has good interoperability

between Compute shaders and graphics

 can render results efficiently

slide-5
SLIDE 5

Fluid Sim ulation for Gam es

 Fluids are well suited to GPU

 data parallel

 Many different techniques

 Eulerian (grid-based)  Lagrangian (particle-based)  Heightfield

 Each has its own strengths and

weaknesses

 To achieve realistic results, games need

to combine techniques

slide-6
SLIDE 6

Particle Based Fluid Sim ulation

 Smoothed particle hydrodynamics

(SPH)

 Good for spray, splashes  Easy to integrate into games

 no fixed domain  particles simple to collide with scene

 Simulation can be provided by

 Physics middleware (e.g. Bullet,

Havok, PhysX)

 or custom DirectCompute or CPU code

slide-7
SLIDE 7
slide-8
SLIDE 8

Fluid Rendering

 Rendering particle-based fluids is

difficult

 Simulation doesn’t naturally generate

a surface (no grid, no level set)

 Just get particle positions and density

 Traditionally, rendering done using

marching cubes

 Generate density field from particles  Extract polygon mesh isosurface  Can be done on GPU, but very

expensive

slide-9
SLIDE 9

Screen Space Fluid Rendering

 Inspired by “Screen Space

Meshes” paper (Müller et al)

 See: van der Laan et al “Screen

space fluid rendering with curvature flow”, I3D 2009

 Operates entirely in screen-space

 No meshes

 Only generates surface closest to

camera

slide-10
SLIDE 10

Screen Space Fluid Rendering

camera particles surface

slide-11
SLIDE 11

Screen Space Fluid Rendering - Overview

 Generate depth image of particles

 Render as spherical point sprites

 Smooth depth image

 Gaussian bilateral blur

 Calculate surface normals and

position from depth

 Shade surface

 Write depth to merge with scene

slide-12
SLIDE 12

Screen Space Fluid Rendering

Depth Image Thickness Image Background Image Depth Smoothing Particles Smoothed Depth Image Surface Shader Scene Final Shaded Image

slide-13
SLIDE 13

Rendering Particle Spheres

 Render as point sprites (quads)  Calculate quad size in vertex

shader (constant in world-space)

 Calculate sphere normal and depth

in pixel shader

 Discard pixels outside circle  Not strictly correct (perspective

projection of a sphere can be an ellipsoid)

 But works fine in practice

slide-14
SLIDE 14

PSOutput particleSpherePS( float2 texCoord : TEXCOORD0, float3 eyeSpacePos : TEXCOORD1, float sphereRadius : TEXCOORD2, float4 color : COLOR0) { PSOutput OUT; // calculate eye-space sphere normal from texture coordinates float3 N; N.xy = texCoord*2.0-1.0; float r2 = dot(N.xy, N.xy); if (r2 > 1.0) discard; // kill pixels outside circle N.z = -sqrt(1.0 - r2); // calculate depth float4 pixelPos = float4(eyeSpacePos + N*sphereRadius, 1.0); float4 clipSpacePos = mul(pixelPos, ProjectionMatrix); OUT.fragDepth = clipSpacePos.z / clipSpacePos.w; float diffuse = max(0.0, dot(N, lightDir)); OUT.fragColor = diffuse * color; return OUT; }

Rendering Particle Spheres

1 1 r

slide-15
SLIDE 15

Point Sprite Spheres

slide-16
SLIDE 16

Sphere Depth

slide-17
SLIDE 17

Calculating Norm als

 Store eye-space sphere depth to

floating point render target

 Can calculate eye-space position

from UV coordinates and depth

 Use partial differences of depth to

calculate normal

 Look at neighbouring pixels

 Have to be careful at edges

 Normal may not be well-defined  At edges, use difference in opposite

direction (hack!)

slide-18
SLIDE 18

Calculating Norm als ( code)

// read eye-space depth from texture float depth = tex2D(depthTex, texCoord).x; if (depth > maxDepth) { discard; return; } // calculate eye-space position from depth float3 posEye = uvToEye(texCoord, depth); // calculate differences float3 ddx = getEyePos(depthTex, texCoord + float2(texelSize, 0)) - posEye; float3 ddx2 = posEye - getEyePos(depthTex, texCoord + vec2(-texelSize, 0)); if (abs(ddx.z) > abs(ddx2.z)) { ddx = ddx2; } float3 ddy = getEyePos(depthTex, texCoord[0] + vec2(0, texelSize)) - posEye; float3 ddy2 = surfacePosEye - getEyePos(depthTex, texCoord + vec2(0, -texelSize)); if (abs(ddy2.z) < abs(ddy.z)) { ddy = ddy2; } // calculate normal vec3 n = cross(ddx, ddy); n = normalize(n);

ddx ddy

n

slide-19
SLIDE 19

Sphere Normals Calculated From Depth

slide-20
SLIDE 20

Sm oothing

 By blurring the depth image, we

can smooth the surface

 Use Gaussian blur  Needs to be view-invariant

 Constant width in world space  -> Variable in screen-space space

 Calculate filter width in shader

 Clamped to maximum radius in screen

space (e.g. 50 pixels) for performance

slide-21
SLIDE 21

Sphere Depth

slide-22
SLIDE 22

Naively Smoothed Depth

slide-23
SLIDE 23

Calculated Normal

slide-24
SLIDE 24

Diffuse Shaded Surface

slide-25
SLIDE 25

Bilateral Filter

 Problem: we want to preserve the

silhouette edges in depth image

 So particles don’t get blended into

background surfaces

 Solution: Bilateral Filter

 Edge-preserving smoothing filter  Called “Surface Blur” in Photoshop  Regular Gaussian filter is based only

  • n only distance in image domain

 Bilateral filter also looks at difference

in range (image values)

 Two sets of weights

slide-26
SLIDE 26

Bilateral Filter Code

float depth = tex2D(depthSampler, texcoord).x; float sum = 0; float wsum = 0; for(float x=-filterRadius; x<=filterRadius; x+=1.0) { float sample = tex2D(depthSampler, texcoord + x*blurDir).x; // spatial domain float r = x * blurScale; float w = exp(-r*r); // range domain float r2 = (sample - depth) * blurDepthFalloff; float g = exp(-r2*r2); sum += sample * w * g; wsum += w * g; } if (wsum > 0.0) { sum /= wsum; } return sum;

Note – not optimized!

slide-27
SLIDE 27

Sphere Depth

slide-28
SLIDE 28

Bilateral Filtered Depth

slide-29
SLIDE 29

Diffuse Shaded Surface

slide-30
SLIDE 30

Bilateral Filter

 Bilateral filter is not strictly

separable

 Can’t separate into X and Y blur

passes

 Non-separable 2D filter is very

expensive

 But we can get away with

separating, with some artifacts

 Artifacts not very visible once other

shading added

slide-31
SLIDE 31

Diffuse Shaded Surface Using Separated Bilateral Filter

slide-32
SLIDE 32

Surface Shading

 Why not just blur normals?  We also calculate eye-space

surface position from the smoothed depth

 Important for accurate specular

reflections

 Once we have a per-pixel surface

normal and position, can shade as usual

slide-33
SLIDE 33
slide-34
SLIDE 34
slide-35
SLIDE 35
slide-36
SLIDE 36

Diffuse Shading – dot(N, L)

slide-37
SLIDE 37

Wrapped Diffuse Shading – dot(N,L)* 0.5+ 0.5

slide-38
SLIDE 38

Specular (Blinn-Phong)

slide-39
SLIDE 39

Fresnel

 Surfaces are more reflective at

glancing angles

 Schlick's approximation  θ is incident angle

 cos(θ) =dot(N, V)

 R0 is the reflectance at normal

incidence

 Can vary exponent for visual effect

slide-40
SLIDE 40

Fresnel Approximation

slide-41
SLIDE 41

Cubemap Reflection

slide-42
SLIDE 42

Cubemap Reflection * Fresnel

slide-43
SLIDE 43

Final Opaque Surface with Reflections

slide-44
SLIDE 44

Thickness Shading

 Fluids are often transparent  Screen-space surface rendering

  • nly generates surface nearest

camera

 Looks strange with transparency  Can’t see surfaces behind front

 Solution – shade fluid as semi-

  • paque using thickness through

volume to attenuate color

slide-45
SLIDE 45

Generating Thickness

 Render particles using additive

blending (no depth test)

 Store in off-screen render target  Render smooth Gaussian splats  or just discs, and then blur

 Only needs to be approximate  Very fill-rate intensive

 Can render at lower resolution

slide-46
SLIDE 46

Volume Thickness

slide-47
SLIDE 47

Volum etric Absorption

d I = exp( -kd) I = 1

 Beer's Law  Light decays exponentially with distance  Use different constant k for each color

channel

slide-48
SLIDE 48

Color due to Absorption

slide-49
SLIDE 49

Background Image Refracted in 2D tex2D(bgSampler, texcoord+ N.xy* thickness)

slide-50
SLIDE 50

Transparency (based on thickness)

slide-51
SLIDE 51

Final Shaded Translucent Surface

slide-52
SLIDE 52

Shadow s

 Since fluid is translucent, we

expect it to cast coloured shadows

 Solution - render fluid surface

again (using same technique), but from light’s point of view

 Generate depth (shadow) map and

color map (thickness)

 Project onto receivers (surface and

ground plane)

slide-53
SLIDE 53
slide-54
SLIDE 54

Surface Without Shadows No Shadows

slide-55
SLIDE 55

Surface Without Shadows Shadow Map

slide-56
SLIDE 56

With Shadows

slide-57
SLIDE 57

Problem s

 Only generates surface closest to

camera

 Hidden somewhat by thickness

shading

 Could be correctly rendered using

ray tracing

 Multiple refractions, reflections

 Possible to ray trace using the

same uniform grid acceleration structure used for simulation

 But still quite slow today

slide-58
SLIDE 58

Artifact – can’t see further surfaces through volume

slide-59
SLIDE 59

Caustics

 Refractive caustics are generated

when light shines through a transparent and refractive material

 Light is focused into distinctive

patterns

slide-60
SLIDE 60

Caustics

Image by Rob Ireton

slide-61
SLIDE 61

Caustics Algorithm

 We use a simple image-space

technique

 Similar to Wyman et al (see refs.)

 For each point in light view,

calculate ray refracted through surface from light

 uses surface position and normal

 Intersect ray with ground plane  Render point splats (“photons”)

with additive blending

slide-62
SLIDE 62

Caustics Diagram

surface receiver light image plane

slide-63
SLIDE 63

W ithout Caustics

slide-64
SLIDE 64

W ith Caustics

slide-65
SLIDE 65

Caustics

 Note - caustics are only cast on

ground plane, not on fluid surface!

 Can perform multiple times with

different indices of refraction to simulate refractive dispersion (R, G, B)

 Quite expensive – requires

rendering e.g. 512* 512 = 256K points

slide-66
SLIDE 66

Adding Surface Detail

 Surface can be too smooth

 Doesn’t show flow well

 Solution: add noise  Render spheres again, using 3D

noise texture in object-space

 Moves with fluid

 Store in noise render target

 Can be used during surface shading to

perturb normal

slide-67
SLIDE 67
slide-68
SLIDE 68
slide-69
SLIDE 69
slide-70
SLIDE 70

DEMO

slide-71
SLIDE 71

Sum m ary

 Particle-based fluids are practical

for use in games using today’s hardware

 Rendering particle-based fluids can

be simple and fast

slide-72
SLIDE 72

Future W ork

 Use Compute Shader for more

efficient bilateral blur

 Similar to diffusion DOF

 Polygon mesh collisions using BVH  Add spray / foam  Wet maps  Direct3D 11 sample to be released

in SDK soon

slide-73
SLIDE 73

Questions?

slide-74
SLIDE 74

Thanks

 Wladimir J. van der Laan, Rouslan

Dimitrov, Miguel Sainz

slide-75
SLIDE 75

References

 Robert Bridson, “Fluid Simulation for Computer

Graphics”, A K Peters, 2008

 M. Müller, S. Schirm, S. Duthaler, ”Screen

Space Meshes”, in Proceedings of ACM SIGGRAPH / EUROGRAPHICS Symposium on Computer Animation (SCA), 2007

 CORDS, H., AND STAADT, O. 2008. “I nstant

Liquids”. In Poster Proceedings of ACM Siggraph/ Eurographics Symposium on Computer Animation

 Wladimir J. van der Laan, Simon Green, Miguel

Sainz, “Screen space fluid rendering w ith curvature flow ”, Proceedings of the 2009 symposium on Interactive 3D graphics and games

 Chris Wyman and Scott Davis. "I nteractive

I m age-Space Techniques for Approxim ating Caustics." ACM Symposium on Interactive 3D Graphics and Games, 153-160. (March 2006)