Cg shaders with Unity3D

29
Cg Shaders with Unity By Michael Ivanov 3D Graphics Geek

description

Introduction into shaders development using Cg language in Unity.Israeli Unity "Unite" even.

Transcript of Cg shaders with Unity3D

Page 1: Cg shaders with Unity3D

Cg Shaders with Unity

By Michael Ivanov3D Graphics Geek

Page 2: Cg shaders with Unity3D

What are Shaders.

Page 3: Cg shaders with Unity3D

Geometry Deformation.

Pixels processing.

Math intensive calculations.

GPGPU.

Page 4: Cg shaders with Unity3D

• D3D / OpenGL Render Pipeline Stages

Vertex Shader

Fragment Shader

Geometry ShaderTessellation

Primitive Assembly Rasterization

Raster OperationsPixels

Vertex Assembly

Screen

Hull DomainTessellator Stage

Page 5: Cg shaders with Unity3D

Vertex Shader

Page 6: Cg shaders with Unity3D

• Runs per input vertex.• Transform 3D into 2D (screen space) position.• Can perform vertex position , color , UV (texture coordinates)

manipulation.• Cannot create new vertices.• Cannot “see” other vertices.

Page 7: Cg shaders with Unity3D

Fragment (Pixel) Shader

Page 8: Cg shaders with Unity3D

• Runs per fragment.• Computes fragment’s color.• With fragment shader you can create: bump mapping,

shadows , lights , post processing effects and other cool shit.• See http://glsl.heroku.com/

Page 9: Cg shaders with Unity3D

What is Cg language?

Page 10: Cg shaders with Unity3D

Shaders before Cg/HLSL/GLSL:

TEX H0, f[TEX0], TEX4, 2D; TEX H1, f[TEX2], TEX5, CUBE; DP3X H1.xyz, H1, LUMINANCE; MULX H0.w, H0.w, LUMINANCE.w; MULX H1.w, H1.x, H1.x; MOVH H2, f[TEX3].wxyz; MULX H1.w, H1.x, H1.w; DP3X H0.xyz, H2.xzyw, H0; MULX H0.xyz, H0, H1.w; TEX H1, f[TEX0], TEX1, 2D; TEX H3, f[TEX0], TEX3, 2D; MULX H0.xyz, H0, H3; …. ……… …………..

Page 11: Cg shaders with Unity3D

• “C for graphics”• High Level language.

Cg != C

• No classes ,pointers ,malloc ,IO etc.• Cg has loops , conditionals , functions/overloads.• Member variables ,local (temporary) variables constants.• Data types: numeric primitives ,vectors , matrices ,arrays,

structs and interfaces.• Built-in trig and other math methods.• Static & Dynamic compilation.

Page 12: Cg shaders with Unity3D

• Cg and Graphics APIs interop scheme:

Page 13: Cg shaders with Unity3D

How it works in Unity

Page 14: Cg shaders with Unity3D

3 Ways of writing shaders in Unity:

1. Surface shaders.2. Vertex and fragment shaders.3. Fixed function shaders.

Page 15: Cg shaders with Unity3D

Simple Custom shader:

Page 16: Cg shaders with Unity3D

• CG Program inputs / outputs(Show code example):

Inputs1. Varying inputs(Attributes) –streamed data VARYING per

processed element.(Vertex arrays ,UV arrays,normal arrays)2. Uniform inputs(Uniforms)-separate from the main stream

values which don’t change each stream element.(Matrices , vectors)

Outputs3. Varying outputs – interpolated values sent from Vertex to

Fragment shader.4. Binding Semantics for fragment program output: COLOR

semantic is must to have!

Page 17: Cg shaders with Unity3D

Per Frame varying data from CPU:

VertexAttributes

Vertex Shader Fragment Shader

InputVariables

Output

Variables

Uniform variables

Uniform variables

InputVariables

Output

Variables

Frame Buffer / Texture Attachment

Page 18: Cg shaders with Unity3D

Passing vertex attributes and uniforms.• Programmable setup (via scripts): (demo)SetFloat(),SetVector(),SetMatrix(),SetColor(),SetBuffer(),SetTexture().• Shader lab properties:[UniformName]([“Uniform property panel name“], [Data Type]) = [Default value]

• Attributes definitions are hooked to Mesh object directly:

Page 19: Cg shaders with Unity3D

Accessing vertex attributes

Vertex attributes structs are defined in UnityCG.cginc include file

• appdata_base: vertex consists of position, normal and one texture coordinate.• appdata_tan: vertex consists of position, tangent, normal and one texture coordinate.• appdata_full: vertex consists of position, tangent, normal, two texture coordinates and color.

We can define our own vertex attribute structs but must use the following names for the members:• float4 vertex is the vertex position• float3 normal is the vertex normal• float4 texcoord is the first UV coordinate• float4 texcoord1 is the second UV coordinate• float4 tangent is the tangent vector (used for normal mapping)• float4 color is per-vertex color

Page 20: Cg shaders with Unity3D

Accessing uniforms:Very simple:Declare uniforms in the shader header with exactly the same names and data types as they have in Shader Lab prop block.

Page 21: Cg shaders with Unity3D

Some demos.

Page 22: Cg shaders with Unity3D

Post-process shaders

Page 23: Cg shaders with Unity3D

• DOF ,SSAO, Motion Blur , Gaussian Blur ,Glow ,Blum etc.• Done via accessing Frame Buffer texture.

Page 24: Cg shaders with Unity3D

Some more demos.

Page 25: Cg shaders with Unity3D

Advanced stuff: multi-pass shading.

Page 26: Cg shaders with Unity3D

• Used for advanced effects.• Done using “pass” blocks.

//first passpass { … shader program code here:

} GrabPass{} //pass first pass output into second pass//second pass// pass{ … shader program code here:}

Page 27: Cg shaders with Unity3D

Even more demos.

Page 28: Cg shaders with Unity3D

Thank you.il.linkedin.com/pub/michael-ivanov/16/570/a0a/

[email protected]