cg shaders with unity3d

Post on 06-May-2015

11.255 Views

Category:

Technology

7 Downloads

Preview:

Click to see full reader

DESCRIPTION

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

TRANSCRIPT

Cg Shaders with Unity

By Michael Ivanov3D Graphics Geek

What are Shaders.

Geometry Deformation.

Pixels processing.

Math intensive calculations.

GPGPU.

• D3D / OpenGL Render Pipeline Stages

Vertex Shader

Fragment Shader

Geometry ShaderTessellation

Primitive Assembly Rasterization

Raster OperationsPixels

Vertex Assembly

Screen

Hull DomainTessellator Stage

Vertex Shader

• 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.

Fragment (Pixel) Shader

• 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/

What is Cg language?

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; …. ……… …………..

• “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.

• Cg and Graphics APIs interop scheme:

How it works in Unity

3 Ways of writing shaders in Unity:

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

Simple Custom shader:

• 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!

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

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:

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

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.

Some demos.

Post-process shaders

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

Some more demos.

Advanced stuff: multi-pass shading.

• 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:}

Even more demos.

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

explomaster@gmail.comblog.alladvanced.net

top related