Cg shaders with Unity3D

16,284 views

Published on

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

Published in: Technology

Cg shaders with Unity3D

  1. 1. Cg Shaders with Unity By Michael Ivanov 3D Graphics Geek
  2. 2. What are Shaders.
  3. 3. Geometry Deformation.Pixels processing.Math intensive calculations.GPGPU.
  4. 4. • D3D / OpenGL Render Pipeline Stages Hull Tessellator Stage Domain Tessellation Geometry Shader Vertex Primitive Vertex Shader RasterizationAssembly Assembly Raster Screen Pixels Fragment Shader Operations
  5. 5. Vertex Shader
  6. 6. • 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.
  7. 7. Fragment (Pixel) Shader
  8. 8. • 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/
  9. 9. What is Cg language?
  10. 10. 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;….…………………..
  11. 11. • “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.
  12. 12. • Cg and Graphics APIs interop scheme:
  13. 13. How it works in Unity
  14. 14. 3 Ways of writing shaders in Unity:1. Surface shaders.2. Vertex and fragment shaders.3. Fixed function shaders.
  15. 15. Simple Custom shader:
  16. 16. • 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) Outputs1. Varying outputs – interpolated values sent from Vertex to Fragment shader.2. Binding Semantics for fragment program output: COLOR semantic is must to have!
  17. 17. Per Frame varying data from CPU: Uniform Uniform variables variables Variables Variables Variables Variables Output Output Input Vertex InputAttributes Vertex Shader Fragment Shader Frame Buffer / Texture Attachment
  18. 18. 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:
  19. 19. 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 themembers:• 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
  20. 20. Accessing uniforms:Very simple:Declare uniforms in the shader header with exactly the same names and data types as they havein Shader Lab prop block.
  21. 21. Some demos.
  22. 22. Post-process shaders
  23. 23. • DOF ,SSAO, Motion Blur , Gaussian Blur ,Glow ,Blum etc.• Done via accessing Frame Buffer texture.
  24. 24. Some more demos.
  25. 25. Advanced stuff: multi-pass shading.
  26. 26. • 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:}
  27. 27. Even more demos.
  28. 28. Thank you.il.linkedin.com/pub/michael-ivanov/16/570/a0a/ explomaster@gmail.com blog.alladvanced.net
  29. 29. 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

×