cg shaders with unity3d
DESCRIPTION
Introduction into shaders development using Cg language in Unity.Israeli Unity "Unite" even.TRANSCRIPT
![Page 1: Cg shaders with Unity3D](https://reader035.vdocuments.mx/reader035/viewer/2022081422/554a0d80b4c9055c598b47c9/html5/thumbnails/1.jpg)
Cg Shaders with Unity
By Michael Ivanov3D Graphics Geek
![Page 2: Cg shaders with Unity3D](https://reader035.vdocuments.mx/reader035/viewer/2022081422/554a0d80b4c9055c598b47c9/html5/thumbnails/2.jpg)
What are Shaders.
![Page 3: Cg shaders with Unity3D](https://reader035.vdocuments.mx/reader035/viewer/2022081422/554a0d80b4c9055c598b47c9/html5/thumbnails/3.jpg)
Geometry Deformation.
Pixels processing.
Math intensive calculations.
GPGPU.
![Page 4: Cg shaders with Unity3D](https://reader035.vdocuments.mx/reader035/viewer/2022081422/554a0d80b4c9055c598b47c9/html5/thumbnails/4.jpg)
• 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](https://reader035.vdocuments.mx/reader035/viewer/2022081422/554a0d80b4c9055c598b47c9/html5/thumbnails/5.jpg)
Vertex Shader
![Page 6: Cg shaders with Unity3D](https://reader035.vdocuments.mx/reader035/viewer/2022081422/554a0d80b4c9055c598b47c9/html5/thumbnails/6.jpg)
• 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](https://reader035.vdocuments.mx/reader035/viewer/2022081422/554a0d80b4c9055c598b47c9/html5/thumbnails/7.jpg)
Fragment (Pixel) Shader
![Page 8: Cg shaders with Unity3D](https://reader035.vdocuments.mx/reader035/viewer/2022081422/554a0d80b4c9055c598b47c9/html5/thumbnails/8.jpg)
• 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](https://reader035.vdocuments.mx/reader035/viewer/2022081422/554a0d80b4c9055c598b47c9/html5/thumbnails/9.jpg)
What is Cg language?
![Page 10: Cg shaders with Unity3D](https://reader035.vdocuments.mx/reader035/viewer/2022081422/554a0d80b4c9055c598b47c9/html5/thumbnails/10.jpg)
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](https://reader035.vdocuments.mx/reader035/viewer/2022081422/554a0d80b4c9055c598b47c9/html5/thumbnails/11.jpg)
• “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](https://reader035.vdocuments.mx/reader035/viewer/2022081422/554a0d80b4c9055c598b47c9/html5/thumbnails/12.jpg)
• Cg and Graphics APIs interop scheme:
![Page 13: Cg shaders with Unity3D](https://reader035.vdocuments.mx/reader035/viewer/2022081422/554a0d80b4c9055c598b47c9/html5/thumbnails/13.jpg)
How it works in Unity
![Page 14: Cg shaders with Unity3D](https://reader035.vdocuments.mx/reader035/viewer/2022081422/554a0d80b4c9055c598b47c9/html5/thumbnails/14.jpg)
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](https://reader035.vdocuments.mx/reader035/viewer/2022081422/554a0d80b4c9055c598b47c9/html5/thumbnails/15.jpg)
Simple Custom shader:
![Page 16: Cg shaders with Unity3D](https://reader035.vdocuments.mx/reader035/viewer/2022081422/554a0d80b4c9055c598b47c9/html5/thumbnails/16.jpg)
• 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](https://reader035.vdocuments.mx/reader035/viewer/2022081422/554a0d80b4c9055c598b47c9/html5/thumbnails/17.jpg)
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](https://reader035.vdocuments.mx/reader035/viewer/2022081422/554a0d80b4c9055c598b47c9/html5/thumbnails/18.jpg)
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](https://reader035.vdocuments.mx/reader035/viewer/2022081422/554a0d80b4c9055c598b47c9/html5/thumbnails/19.jpg)
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](https://reader035.vdocuments.mx/reader035/viewer/2022081422/554a0d80b4c9055c598b47c9/html5/thumbnails/20.jpg)
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](https://reader035.vdocuments.mx/reader035/viewer/2022081422/554a0d80b4c9055c598b47c9/html5/thumbnails/21.jpg)
Some demos.
![Page 22: Cg shaders with Unity3D](https://reader035.vdocuments.mx/reader035/viewer/2022081422/554a0d80b4c9055c598b47c9/html5/thumbnails/22.jpg)
Post-process shaders
![Page 23: Cg shaders with Unity3D](https://reader035.vdocuments.mx/reader035/viewer/2022081422/554a0d80b4c9055c598b47c9/html5/thumbnails/23.jpg)
• DOF ,SSAO, Motion Blur , Gaussian Blur ,Glow ,Blum etc.• Done via accessing Frame Buffer texture.
![Page 24: Cg shaders with Unity3D](https://reader035.vdocuments.mx/reader035/viewer/2022081422/554a0d80b4c9055c598b47c9/html5/thumbnails/24.jpg)
Some more demos.
![Page 25: Cg shaders with Unity3D](https://reader035.vdocuments.mx/reader035/viewer/2022081422/554a0d80b4c9055c598b47c9/html5/thumbnails/25.jpg)
Advanced stuff: multi-pass shading.
![Page 26: Cg shaders with Unity3D](https://reader035.vdocuments.mx/reader035/viewer/2022081422/554a0d80b4c9055c598b47c9/html5/thumbnails/26.jpg)
• 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](https://reader035.vdocuments.mx/reader035/viewer/2022081422/554a0d80b4c9055c598b47c9/html5/thumbnails/27.jpg)
Even more demos.
![Page 29: Cg shaders with Unity3D](https://reader035.vdocuments.mx/reader035/viewer/2022081422/554a0d80b4c9055c598b47c9/html5/thumbnails/29.jpg)
Resources• http://aras-p.info/blog/• http://docs.unity3d.com/Documentation/Manual/Shaders.html• http://http.developer.nvidia.com/CgTutorial/cg_tutorial_chapter01.html• https://developer.nvidia.com/shader-library • http://wiki.unity3d.com/index.php/Shaders