SlideShare uma empresa Scribd logo
1 de 29
Cg Shaders with Unity




     By Michael Ivanov
     3D Graphics Geek
What are Shaders.
Geometry Deformation.
Pixels processing.

Math intensive calculations.

GPGPU.
•    D3D / OpenGL Render Pipeline Stages


                Hull           Tessellator Stage         Domain

                                 Tessellation                          Geometry Shader




 Vertex                                            Primitive
                       Vertex Shader                                     Rasterization
Assembly                                           Assembly




                                                            Raster
 Screen                                Pixels                          Fragment Shader
                                                          Operations
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):

                             Inputs
1. 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)

                             Outputs
1. Varying outputs – interpolated values sent from Vertex to
   Fragment shader.
2. Binding Semantics for fragment program output: COLOR
   semantic is must to have!
Per Frame varying data from CPU:




                         Uniform                                  Uniform
                         variables                                variables
             Variables




                                     Variables




                                                      Variables




                                                                              Variables
                                      Output




                                                                               Output
               Input



  Vertex




                                                        Input
Attributes




                   Vertex Shader                            Fragment Shader


                  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 pass
pass {
 … 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.com
                blog.alladvanced.net
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

Mais conteúdo relacionado

Mais procurados

A Bizarre Way to do Real-Time Lighting
A Bizarre Way to do Real-Time LightingA Bizarre Way to do Real-Time Lighting
A Bizarre Way to do Real-Time Lighting
Steven Tovey
 
Uncharted3 effect technique
Uncharted3 effect techniqueUncharted3 effect technique
Uncharted3 effect technique
MinGeun Park
 
565 Alpha Chun-Fu Chao
565 Alpha Chun-Fu Chao565 Alpha Chun-Fu Chao
565 Alpha Chun-Fu Chao
Frank Chao
 
Z Buffer Optimizations
Z Buffer OptimizationsZ Buffer Optimizations
Z Buffer Optimizations
pjcozzi
 
Epic_GDC2011_Samaritan
Epic_GDC2011_SamaritanEpic_GDC2011_Samaritan
Epic_GDC2011_Samaritan
MinGeun Park
 

Mais procurados (20)

Real-time Shadowing Techniques: Shadow Volumes
Real-time Shadowing Techniques: Shadow VolumesReal-time Shadowing Techniques: Shadow Volumes
Real-time Shadowing Techniques: Shadow Volumes
 
OpenGL Shading Language
OpenGL Shading LanguageOpenGL Shading Language
OpenGL Shading Language
 
The Unique Lighting of Mirror's Edge
The Unique Lighting of Mirror's EdgeThe Unique Lighting of Mirror's Edge
The Unique Lighting of Mirror's Edge
 
Substanceshanghaippt repacked
Substanceshanghaippt repackedSubstanceshanghaippt repacked
Substanceshanghaippt repacked
 
CS 354 Programmable Shading
CS 354 Programmable ShadingCS 354 Programmable Shading
CS 354 Programmable Shading
 
A Bizarre Way to do Real-Time Lighting
A Bizarre Way to do Real-Time LightingA Bizarre Way to do Real-Time Lighting
A Bizarre Way to do Real-Time Lighting
 
Uncharted3 effect technique
Uncharted3 effect techniqueUncharted3 effect technique
Uncharted3 effect technique
 
NVIDIA's OpenGL Functionality
NVIDIA's OpenGL FunctionalityNVIDIA's OpenGL Functionality
NVIDIA's OpenGL Functionality
 
GTC 2012: GPU-Accelerated Path Rendering
GTC 2012: GPU-Accelerated Path RenderingGTC 2012: GPU-Accelerated Path Rendering
GTC 2012: GPU-Accelerated Path Rendering
 
CS 354 Vector Graphics & Path Rendering
CS 354 Vector Graphics & Path RenderingCS 354 Vector Graphics & Path Rendering
CS 354 Vector Graphics & Path Rendering
 
HPG 2018 - Game Ray Tracing: State-of-the-Art and Open Problems
HPG 2018 - Game Ray Tracing: State-of-the-Art and Open ProblemsHPG 2018 - Game Ray Tracing: State-of-the-Art and Open Problems
HPG 2018 - Game Ray Tracing: State-of-the-Art and Open Problems
 
GTC 2012: NVIDIA OpenGL in 2012
GTC 2012: NVIDIA OpenGL in 2012GTC 2012: NVIDIA OpenGL in 2012
GTC 2012: NVIDIA OpenGL in 2012
 
CS 354 Introduction
CS 354 IntroductionCS 354 Introduction
CS 354 Introduction
 
565 Alpha Chun-Fu Chao
565 Alpha Chun-Fu Chao565 Alpha Chun-Fu Chao
565 Alpha Chun-Fu Chao
 
Clean architecture for shaders unite2019
Clean architecture for shaders unite2019Clean architecture for shaders unite2019
Clean architecture for shaders unite2019
 
Z Buffer Optimizations
Z Buffer OptimizationsZ Buffer Optimizations
Z Buffer Optimizations
 
SIGGRAPH 2010 - Style and Gameplay in the Mirror's Edge
SIGGRAPH 2010 - Style and Gameplay in the Mirror's EdgeSIGGRAPH 2010 - Style and Gameplay in the Mirror's Edge
SIGGRAPH 2010 - Style and Gameplay in the Mirror's Edge
 
Khronos Munich 2018 - Halcyon and Vulkan
Khronos Munich 2018 - Halcyon and VulkanKhronos Munich 2018 - Halcyon and Vulkan
Khronos Munich 2018 - Halcyon and Vulkan
 
Rendering AAA-Quality Characters of Project A1
Rendering AAA-Quality Characters of Project A1Rendering AAA-Quality Characters of Project A1
Rendering AAA-Quality Characters of Project A1
 
Epic_GDC2011_Samaritan
Epic_GDC2011_SamaritanEpic_GDC2011_Samaritan
Epic_GDC2011_Samaritan
 

Destaque

Shaders in Unity by Zoel
Shaders in Unity by ZoelShaders in Unity by Zoel
Shaders in Unity by Zoel
Agate Studio
 
шейдеры
шейдерышейдеры
шейдеры
asmorkalov
 

Destaque (20)

An Introduction to Writing Custom Unity Shaders!
An Introduction to Writing  Custom Unity Shaders!An Introduction to Writing  Custom Unity Shaders!
An Introduction to Writing Custom Unity Shaders!
 
Unity Surface Shader for Artist 03
Unity Surface Shader for Artist 03Unity Surface Shader for Artist 03
Unity Surface Shader for Artist 03
 
Unity Surface Shader for Artist 02
Unity Surface Shader for Artist 02Unity Surface Shader for Artist 02
Unity Surface Shader for Artist 02
 
Unity Surface Shader for Artist 01
Unity Surface Shader for Artist 01Unity Surface Shader for Artist 01
Unity Surface Shader for Artist 01
 
Unite2014: Mastering Physically Based Shading in Unity 5
Unite2014: Mastering Physically Based Shading in Unity 5Unite2014: Mastering Physically Based Shading in Unity 5
Unite2014: Mastering Physically Based Shading in Unity 5
 
Shaders in Unity by Zoel
Shaders in Unity by ZoelShaders in Unity by Zoel
Shaders in Unity by Zoel
 
Shadow Volumes on Programmable Graphics Hardware
Shadow Volumes on Programmable Graphics HardwareShadow Volumes on Programmable Graphics Hardware
Shadow Volumes on Programmable Graphics Hardware
 
NVIDIA Graphics, Cg, and Transparency
NVIDIA Graphics, Cg, and TransparencyNVIDIA Graphics, Cg, and Transparency
NVIDIA Graphics, Cg, and Transparency
 
Unity advanced computer graphics week 02
Unity advanced computer graphics week 02Unity advanced computer graphics week 02
Unity advanced computer graphics week 02
 
шейдеры
шейдерышейдеры
шейдеры
 
CG OpenGL Shadows + Light + Texture -course 10
CG OpenGL Shadows + Light + Texture -course 10CG OpenGL Shadows + Light + Texture -course 10
CG OpenGL Shadows + Light + Texture -course 10
 
Beginning direct3d gameprogramming09_shaderprogramming_20160505_jintaeks
Beginning direct3d gameprogramming09_shaderprogramming_20160505_jintaeksBeginning direct3d gameprogramming09_shaderprogramming_20160505_jintaeks
Beginning direct3d gameprogramming09_shaderprogramming_20160505_jintaeks
 
2D Game Development with Starling
2D Game Development with Starling2D Game Development with Starling
2D Game Development with Starling
 
Unity 2d sharp
Unity 2d sharpUnity 2d sharp
Unity 2d sharp
 
Mobile Performance Tuning: Poor Man's Tips And Tricks
Mobile Performance Tuning: Poor Man's Tips And TricksMobile Performance Tuning: Poor Man's Tips And Tricks
Mobile Performance Tuning: Poor Man's Tips And Tricks
 
My Level Design For 2D Scroll Game
My Level Design For 2D Scroll GameMy Level Design For 2D Scroll Game
My Level Design For 2D Scroll Game
 
Developing a 2D Game - A Case Study (Ninja Puzzle) - Presented at CGGA 2013
Developing a 2D Game - A Case Study (Ninja Puzzle) - Presented at CGGA 2013Developing a 2D Game - A Case Study (Ninja Puzzle) - Presented at CGGA 2013
Developing a 2D Game - A Case Study (Ninja Puzzle) - Presented at CGGA 2013
 
2D Endless Runner in Unity for Mobile - GDG DevFest Istanbul 2014
2D Endless Runner in Unity for Mobile - GDG DevFest Istanbul 20142D Endless Runner in Unity for Mobile - GDG DevFest Istanbul 2014
2D Endless Runner in Unity for Mobile - GDG DevFest Istanbul 2014
 
Unity technologies on BarCamp Vilnius
Unity technologies on BarCamp VilniusUnity technologies on BarCamp Vilnius
Unity technologies on BarCamp Vilnius
 
Unity in community powerpoint
Unity in community powerpointUnity in community powerpoint
Unity in community powerpoint
 

Semelhante a Cg shaders with Unity3D

Hardware Shaders
Hardware ShadersHardware Shaders
Hardware Shaders
gueste52f1b
 
Minko stage3d workshop_20130525
Minko stage3d workshop_20130525Minko stage3d workshop_20130525
Minko stage3d workshop_20130525
Minko3D
 
Game development
Game developmentGame development
Game development
Asido_
 
Opengl lec 3
Opengl lec 3Opengl lec 3
Opengl lec 3
elnaqah
 

Semelhante a Cg shaders with Unity3D (20)

Hardware Shaders
Hardware ShadersHardware Shaders
Hardware Shaders
 
Game Programming 12 - Shaders
Game Programming 12 - ShadersGame Programming 12 - Shaders
Game Programming 12 - Shaders
 
Easy edd phd talks 28 oct 2008
Easy edd phd talks 28 oct 2008Easy edd phd talks 28 oct 2008
Easy edd phd talks 28 oct 2008
 
Hpg2011 papers kazakov
Hpg2011 papers kazakovHpg2011 papers kazakov
Hpg2011 papers kazakov
 
iOS Visual F/X Using GLSL
iOS Visual F/X Using GLSLiOS Visual F/X Using GLSL
iOS Visual F/X Using GLSL
 
GFX Part 6 - Introduction to Vertex and Fragment Shaders in OpenGL ES
GFX Part 6 - Introduction to Vertex and Fragment Shaders in OpenGL ESGFX Part 6 - Introduction to Vertex and Fragment Shaders in OpenGL ES
GFX Part 6 - Introduction to Vertex and Fragment Shaders in OpenGL ES
 
Minko stage3d workshop_20130525
Minko stage3d workshop_20130525Minko stage3d workshop_20130525
Minko stage3d workshop_20130525
 
Markus Tessmann, InnoGames
Markus Tessmann, InnoGames	Markus Tessmann, InnoGames
Markus Tessmann, InnoGames
 
Why Graphics Is Fast, and What It Can Teach Us About Parallel Programming
Why Graphics Is Fast, and What It Can Teach Us About Parallel ProgrammingWhy Graphics Is Fast, and What It Can Teach Us About Parallel Programming
Why Graphics Is Fast, and What It Can Teach Us About Parallel Programming
 
Easy edd
Easy eddEasy edd
Easy edd
 
Game development
Game developmentGame development
Game development
 
Chapter-3.pdf
Chapter-3.pdfChapter-3.pdf
Chapter-3.pdf
 
GPU - how can we use it?
GPU - how can we use it?GPU - how can we use it?
GPU - how can we use it?
 
Shaders in Unity
Shaders in UnityShaders in Unity
Shaders in Unity
 
Opengl lec 3
Opengl lec 3Opengl lec 3
Opengl lec 3
 
OpenGL for 2015
OpenGL for 2015OpenGL for 2015
OpenGL for 2015
 
Pixel shaders
Pixel shadersPixel shaders
Pixel shaders
 
Pixel shaders
Pixel shadersPixel shaders
Pixel shaders
 
OpenGL basics
OpenGL basicsOpenGL basics
OpenGL basics
 
Greg Hogan – To Petascale and Beyond- Apache Flink in the Clouds
Greg Hogan – To Petascale and Beyond- Apache Flink in the CloudsGreg Hogan – To Petascale and Beyond- Apache Flink in the Clouds
Greg Hogan – To Petascale and Beyond- Apache Flink in the Clouds
 

Último

+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
vu2urc
 

Último (20)

Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 

Cg shaders with Unity3D

  • 1. Cg Shaders with Unity By Michael Ivanov 3D Graphics Geek
  • 3. Geometry Deformation. Pixels processing. Math intensive calculations. GPGPU.
  • 4. D3D / OpenGL Render Pipeline Stages Hull Tessellator Stage Domain Tessellation Geometry Shader Vertex Primitive Vertex Shader Rasterization Assembly Assembly Raster Screen Pixels Fragment Shader Operations
  • 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.
  • 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. What is Cg language?
  • 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. • “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. Cg and Graphics APIs interop scheme:
  • 13. How it works in Unity
  • 14. 3 Ways of writing shaders in Unity: 1. Surface shaders. 2. Vertex and fragment shaders. 3. Fixed function shaders.
  • 16. • CG Program inputs / outputs(Show code example): Inputs 1. 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) Outputs 1. Varying outputs – interpolated values sent from Vertex to Fragment shader. 2. Binding Semantics for fragment program output: COLOR semantic is must to have!
  • 17. Per Frame varying data from CPU: Uniform Uniform variables variables Variables Variables Variables Variables Output Output Input Vertex Input Attributes Vertex Shader Fragment Shader Frame Buffer / Texture Attachment
  • 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. 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
  • 20. 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.
  • 23. • DOF ,SSAO, Motion Blur , Gaussian Blur ,Glow ,Blum etc. • Done via accessing Frame Buffer texture.
  • 26. • Used for advanced effects. • Done using “pass” blocks. //first pass pass { … shader program code here: } GrabPass{} //pass first pass output into second pass //second pass// pass{ … shader program code here: }
  • 28. Thank you. il.linkedin.com/pub/michael-ivanov/16/570/a0a/ explomaster@gmail.com blog.alladvanced.net
  • 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