Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Shaders in Unity by Zoel


Published on

  • Be the first to comment

Shaders in Unity by Zoel

  1. 1. @agatestudio Shaders in Unity Zoel Knight Agate Studio
  2. 2. Geometry Deformation. Pixels processing. Math intensive calculations. GPGPU.
  3. 3. • D3D / OpenGL Render Pipeline Stages Vertex Shader Fragment Shader Geometry ShaderTessellation Primitive Assembly Rasterization Raster Operations Pixels Vertex Assembly Screen Hull DomainTessellator Stage
  4. 4. • 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.
  5. 5. • 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
  6. 6. Shaders before Cg/HLSL/GLSL: TEX H0, f[TEX0], TEX4, 2D; TEX H1, f[TEX2], TEX5, CUBE; DP3X, 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, H2.xzyw, H0; MULX, H0, H1.w; TEX H1, f[TEX0], TEX1, 2D; TEX H3, f[TEX0], TEX3, 2D; MULX, H0, H3; Bla… Bla… Bla…
  7. 7. CG Shader • “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.
  8. 8. Cg and Graphics APIs interop scheme:
  9. 9. 3 Ways of writing shaders in Unity: Surface shaders. Vertex and fragment shaders. Fixed function shaders.
  10. 10. Simple Custom shader:
  11. 11. Passing vertex attributes and uniforms • Programmable setup (via scripts): - SetFloat() - SetVector() - SetMatrix() - SetColor() - SetBuffer() - SetTexture() • Shader lab properties: [UniformName+(*“Uniform property panel name“+, *Data Type+) = [Default value]
  12. 12. 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.
  13. 13. Thank you!!!