SlideShare a Scribd company logo
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

More Related Content

What's hot

Real-time Shadowing Techniques: Shadow Volumes
Real-time Shadowing Techniques: Shadow VolumesReal-time Shadowing Techniques: Shadow Volumes
Real-time Shadowing Techniques: Shadow Volumes
Mark Kilgard
 
OpenGL Shading Language
OpenGL Shading LanguageOpenGL Shading Language
OpenGL Shading Language
Jungsoo Nam
 
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
Electronic Arts / DICE
 
Substanceshanghaippt repacked
Substanceshanghaippt repackedSubstanceshanghaippt repacked
Substanceshanghaippt repacked
Lee Jungpyo
 
CS 354 Programmable Shading
CS 354 Programmable ShadingCS 354 Programmable Shading
CS 354 Programmable Shading
Mark Kilgard
 
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 LightingSteven Tovey
 
Uncharted3 effect technique
Uncharted3 effect techniqueUncharted3 effect technique
Uncharted3 effect techniqueMinGeun Park
 
NVIDIA's OpenGL Functionality
NVIDIA's OpenGL FunctionalityNVIDIA's OpenGL Functionality
NVIDIA's OpenGL Functionality
Mark Kilgard
 
GTC 2012: GPU-Accelerated Path Rendering
GTC 2012: GPU-Accelerated Path RenderingGTC 2012: GPU-Accelerated Path Rendering
GTC 2012: GPU-Accelerated Path Rendering
Mark Kilgard
 
CS 354 Vector Graphics & Path Rendering
CS 354 Vector Graphics & Path RenderingCS 354 Vector Graphics & Path Rendering
CS 354 Vector Graphics & Path Rendering
Mark Kilgard
 
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
Electronic Arts / DICE
 
GTC 2012: NVIDIA OpenGL in 2012
GTC 2012: NVIDIA OpenGL in 2012GTC 2012: NVIDIA OpenGL in 2012
GTC 2012: NVIDIA OpenGL in 2012
Mark Kilgard
 
CS 354 Introduction
CS 354 IntroductionCS 354 Introduction
CS 354 Introduction
Mark Kilgard
 
565 Alpha Chun-Fu Chao
565 Alpha Chun-Fu Chao565 Alpha Chun-Fu Chao
565 Alpha Chun-Fu ChaoFrank Chao
 
Clean architecture for shaders unite2019
Clean architecture for shaders unite2019Clean architecture for shaders unite2019
Clean architecture for shaders unite2019
Abhilash Majumder
 
Z Buffer Optimizations
Z Buffer OptimizationsZ Buffer Optimizations
Z Buffer Optimizationspjcozzi
 
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
Electronic Arts / DICE
 
Khronos Munich 2018 - Halcyon and Vulkan
Khronos Munich 2018 - Halcyon and VulkanKhronos Munich 2018 - Halcyon and Vulkan
Khronos Munich 2018 - Halcyon and Vulkan
Electronic Arts / DICE
 
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
Ki Hyunwoo
 
Epic_GDC2011_Samaritan
Epic_GDC2011_SamaritanEpic_GDC2011_Samaritan
Epic_GDC2011_SamaritanMinGeun Park
 

What's hot (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
 

Viewers also liked

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!
DevGAMM Conference
 
Unity Surface Shader for Artist 03
Unity Surface Shader for Artist 03Unity Surface Shader for Artist 03
Unity Surface Shader for Artist 03
SangYun Yi
 
Unity Surface Shader for Artist 02
Unity Surface Shader for Artist 02Unity Surface Shader for Artist 02
Unity Surface Shader for Artist 02
SangYun Yi
 
Unity Surface Shader for Artist 01
Unity Surface Shader for Artist 01Unity Surface Shader for Artist 01
Unity Surface Shader for Artist 01
SangYun Yi
 
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
Renaldas Zioma
 
Shaders in Unity by Zoel
Shaders in Unity by ZoelShaders in Unity by Zoel
Shaders in Unity by ZoelAgate Studio
 
Shadow Volumes on Programmable Graphics Hardware
Shadow Volumes on Programmable Graphics HardwareShadow Volumes on Programmable Graphics Hardware
Shadow Volumes on Programmable Graphics Hardware
stefan_b
 
NVIDIA Graphics, Cg, and Transparency
NVIDIA Graphics, Cg, and TransparencyNVIDIA Graphics, Cg, and Transparency
NVIDIA Graphics, Cg, and Transparency
Mark Kilgard
 
Unity advanced computer graphics week 02
Unity advanced computer graphics week 02Unity advanced computer graphics week 02
Unity advanced computer graphics week 02
Tri Thanh
 
шейдеры
шейдерышейдеры
шейдерыasmorkalov
 
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
fungfung Chen
 
Beginning direct3d gameprogramming09_shaderprogramming_20160505_jintaeks
Beginning direct3d gameprogramming09_shaderprogramming_20160505_jintaeksBeginning direct3d gameprogramming09_shaderprogramming_20160505_jintaeks
Beginning direct3d gameprogramming09_shaderprogramming_20160505_jintaeks
JinTaek Seo
 
2D Game Development with Starling
2D Game Development with Starling2D Game Development with Starling
2D Game Development with Starling
Juwal Bose
 
Unity 2d sharp
Unity 2d sharpUnity 2d sharp
Unity 2d sharp
Jerel Hass
 
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
Valentin Simonov
 
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
Arron96
 
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
Pisal Setthawong
 
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
Murat Gürel
 
Unity technologies on BarCamp Vilnius
Unity technologies on BarCamp VilniusUnity technologies on BarCamp Vilnius
Unity technologies on BarCamp Vilnius
BarCamp Lithuania
 
Unity in community powerpoint
Unity in community powerpointUnity in community powerpoint
Unity in community powerpoint
thinkzalot
 

Viewers also liked (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
 

Similar to Cg shaders with Unity3D

Hardware Shaders
Hardware ShadersHardware Shaders
Hardware Shadersgueste52f1b
 
Game Programming 12 - Shaders
Game Programming 12 - ShadersGame Programming 12 - Shaders
Game Programming 12 - Shaders
Nick Pruehs
 
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
Taha Sochi
 
Hpg2011 papers kazakov
Hpg2011 papers kazakovHpg2011 papers kazakov
Hpg2011 papers kazakov
mistercteam
 
iOS Visual F/X Using GLSL
iOS Visual F/X Using GLSLiOS Visual F/X Using GLSL
iOS Visual F/X Using GLSL
Douglass Turner
 
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
Prabindh Sundareson
 
Minko stage3d workshop_20130525
Minko stage3d workshop_20130525Minko stage3d workshop_20130525
Minko stage3d workshop_20130525Minko3D
 
Markus Tessmann, InnoGames
Markus Tessmann, InnoGames	Markus Tessmann, InnoGames
Markus Tessmann, InnoGames
White Nights Conference
 
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
Jonathan Ragan-Kelley
 
Easy edd
Easy eddEasy edd
Easy edd
Taha Sochi
 
Game development
Game developmentGame development
Game developmentAsido_
 
Chapter-3.pdf
Chapter-3.pdfChapter-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?
Bartlomiej Filipek
 
Shaders in Unity
Shaders in UnityShaders in Unity
Shaders in Unity
Matias Lavik
 
Opengl lec 3
Opengl lec 3Opengl lec 3
Opengl lec 3elnaqah
 
OpenGL for 2015
OpenGL for 2015OpenGL for 2015
OpenGL for 2015
Mark Kilgard
 
OpenGL basics
OpenGL basicsOpenGL basics
OpenGL basics
Mohammad Hosein Nemati
 
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
Flink Forward
 

Similar to 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
 

Recently uploaded

FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdfFIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance
 
Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !
KatiaHIMEUR1
 
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
BookNet Canada
 
The Future of Platform Engineering
The Future of Platform EngineeringThe Future of Platform Engineering
The Future of Platform Engineering
Jemma Hussein Allen
 
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdfFIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance
 
Introduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - CybersecurityIntroduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - Cybersecurity
mikeeftimakis1
 
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Ramesh Iyer
 
Free Complete Python - A step towards Data Science
Free Complete Python - A step towards Data ScienceFree Complete Python - A step towards Data Science
Free Complete Python - A step towards Data Science
RinaMondal9
 
Secstrike : Reverse Engineering & Pwnable tools for CTF.pptx
Secstrike : Reverse Engineering & Pwnable tools for CTF.pptxSecstrike : Reverse Engineering & Pwnable tools for CTF.pptx
Secstrike : Reverse Engineering & Pwnable tools for CTF.pptx
nkrafacyberclub
 
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Product School
 
Welocme to ViralQR, your best QR code generator.
Welocme to ViralQR, your best QR code generator.Welocme to ViralQR, your best QR code generator.
Welocme to ViralQR, your best QR code generator.
ViralQR
 
DevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA ConnectDevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA Connect
Kari Kakkonen
 
Elevating Tactical DDD Patterns Through Object Calisthenics
Elevating Tactical DDD Patterns Through Object CalisthenicsElevating Tactical DDD Patterns Through Object Calisthenics
Elevating Tactical DDD Patterns Through Object Calisthenics
Dorra BARTAGUIZ
 
Key Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdfKey Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdf
Cheryl Hung
 
The Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and SalesThe Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and Sales
Laura Byrne
 
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdfObservability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Paige Cruz
 
By Design, not by Accident - Agile Venture Bolzano 2024
By Design, not by Accident - Agile Venture Bolzano 2024By Design, not by Accident - Agile Venture Bolzano 2024
By Design, not by Accident - Agile Venture Bolzano 2024
Pierluigi Pugliese
 
Leading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdfLeading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdf
OnBoard
 
Assure Contact Center Experiences for Your Customers With ThousandEyes
Assure Contact Center Experiences for Your Customers With ThousandEyesAssure Contact Center Experiences for Your Customers With ThousandEyes
Assure Contact Center Experiences for Your Customers With ThousandEyes
ThousandEyes
 
How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...
Product School
 

Recently uploaded (20)

FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdfFIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
 
Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !
 
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
 
The Future of Platform Engineering
The Future of Platform EngineeringThe Future of Platform Engineering
The Future of Platform Engineering
 
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdfFIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
 
Introduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - CybersecurityIntroduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - Cybersecurity
 
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
 
Free Complete Python - A step towards Data Science
Free Complete Python - A step towards Data ScienceFree Complete Python - A step towards Data Science
Free Complete Python - A step towards Data Science
 
Secstrike : Reverse Engineering & Pwnable tools for CTF.pptx
Secstrike : Reverse Engineering & Pwnable tools for CTF.pptxSecstrike : Reverse Engineering & Pwnable tools for CTF.pptx
Secstrike : Reverse Engineering & Pwnable tools for CTF.pptx
 
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
 
Welocme to ViralQR, your best QR code generator.
Welocme to ViralQR, your best QR code generator.Welocme to ViralQR, your best QR code generator.
Welocme to ViralQR, your best QR code generator.
 
DevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA ConnectDevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA Connect
 
Elevating Tactical DDD Patterns Through Object Calisthenics
Elevating Tactical DDD Patterns Through Object CalisthenicsElevating Tactical DDD Patterns Through Object Calisthenics
Elevating Tactical DDD Patterns Through Object Calisthenics
 
Key Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdfKey Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdf
 
The Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and SalesThe Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and Sales
 
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdfObservability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
 
By Design, not by Accident - Agile Venture Bolzano 2024
By Design, not by Accident - Agile Venture Bolzano 2024By Design, not by Accident - Agile Venture Bolzano 2024
By Design, not by Accident - Agile Venture Bolzano 2024
 
Leading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdfLeading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdf
 
Assure Contact Center Experiences for Your Customers With ThousandEyes
Assure Contact Center Experiences for Your Customers With ThousandEyesAssure Contact Center Experiences for Your Customers With ThousandEyes
Assure Contact Center Experiences for Your Customers With ThousandEyes
 
How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...
 

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