Docsity

Unity Shader - SPECIAL EFFECTS WITH DEPTH, Esercizi di Tecniche E Linguaggi Di Programmazione

Anteprima parziale del testo

Scarica Unity Shader - SPECIAL EFFECTS WITH DEPTH e più Esercizi in PDF di Tecniche E Linguaggi Di Programmazione solo su Docsity!

Kuba Cupisz

Ole Ciliox

SPECIAL EFFECTS WITH DEPTH

OVERVIEW

• Special Effects

• About Buffers

• Examples

The talk is about simple and effective techniques, that can be quickly implemented based on the data (easily) available to Unity developers.

Ok, so here we see a lightmapped scene (running deferred), so let's just define "special effects", for the purpose of this talk, as the result of this image...

... subtracted from this image. This results in...

TOOLS OF TRADE

• Depending on what you need ...

• ... several options in Unity to add stuff to the screen

• (Multi-pass) shaders

  • (^) Surface shaders or oldschool CG vertex/pixel

• Particle systems

• Camera.OnRenderImage()

• Camera.OnPostRender()

• Graphics.DrawMesh(), DrawMeshNow()

  • (^) also, MonoBehaviour.OnWillRenderObject() for visibility determination There are several ways to create effects in unity, all depending on the kind of effect you want to get. Your tools of trade in Unity for making effects are the following: the multi-pass shader system, along with the OnRenderImage and OnPostRender functions to add stuff to the screen. Furthermore, DrawMeshNow() and DrawMesh() are very useful for drawing selected objects a second time (when shader passes won't be enough), also OnWillRenderObject is good to find out if an object is seen by a camera or not.

ABOUT BUFFERS

Let's talk about various buffers that are available in Unity, which are either part of the rendering or can be easily created.

DEPTH

• Distance from the camera

• In [0, 1] range

• Nonlinear distribution

• Precision: 24 or 16 bits

• How to get it?

• Just ask for it:

  • (^) camera.depthTextureMode = DepthTextureMode.Depth;
  • (^) it's just there when running deferred The depth buffer is storing the distance from camera's near clip plane. The values are in the [0;1] range with nonlinear distribution. The precision is usually 24 or 16 bits. How do you get it? Just ask for it by setting camera.depthTextureMode to Depth. Then the depth buffer will be available as _CameraDepthTexture. Unity is hiding the burden of getting the depth buffer from you. We are binding the buffer when possible or rendering the scene with a replacement shader when it’s not.

NONLINEAR DISTRIBUTION

Where does the nonlinearity come from? The input to the vertex shader is the object space position of the vertex (v.vertex, POSITION semantic). The output of the vertex shader should be the position of the vertex in clip space. This is the space where things outside of the viewing frustum get clipped. Typically you multiply v.vertex by the model-view-projection matrix in one go. Multiplying by the model matrix places the object in the world. Multiplying by the view matrix makes the position relative to the camera. Multiplying by the projection matrix projects th vertex on the near clip plane, resulting in a value in clip space.

NORMALS

NORMALS

• Only accessible in deferred lighting

• .xyz, world space normals

• .w, shininess (0.03, 1.0)

• Sampling

• float3 worldNormal = tex2D(_CameraNormalsTexture, uv) * 2.0 - 1.0;

The normals buffer is a natural product of the deferred lighting pipeline. It stores world space normals in .xyz components. In the .w component it stores shininess. Just sample it and move back into the [-1;1] range.

COLOR

COLOR

• How to get it?

• request it via GrabPass and access as _GrabTexture

• OnRenderImage

And of course you have access to the color buffer as well. You can request it via GrabPass and then access the texture as _GrabTexture in the shader. Or you can use camera.OnRenderImage callback. The incoming image is the source render texture. The result should end up in the destination render texture. When there are multiple image filters attached to the camera, they process the image sequentially, by passing first filter's destination as the source to the next filter. You write to the destination render texture by using Graphics.Blit with a specific material. The source image is then accessible in the shader as _MainTex.

EXAMPLES

All of the examples we are showing run in the vanilla Unity 3.4 (probably 3.3 etc as well, no voodoo required)

WHAT WE WON'T TALK ABOUT ...

We are not going to show the traditional depth/normals based effects such as SSAO, depth of field, etc. but talk about more unusual scenarios…