19
@agatestudio Shaders in Unity Zoel Knight Agate Studio

Shaders in Unity by Zoel

Embed Size (px)

Citation preview

Page 1: Shaders in Unity by Zoel

@agatestudio

Shaders in Unity

Zoel

Knight

Agate Studio

Page 2: Shaders in Unity by Zoel
Page 3: Shaders in Unity by Zoel
Page 4: Shaders in Unity by Zoel

Geometry Deformation.

Pixels processing.

Math intensive calculations.

GPGPU.

Page 5: Shaders in Unity by Zoel

• D3D / OpenGL Render Pipeline Stages

Vertex Shader

Fragment Shader

Geometry ShaderTessellation

Primitive Assembly

Rasterization

Raster Operations

Pixels

Vertex Assembly

Screen

Hull DomainTessellator Stage

Page 6: Shaders in Unity by Zoel
Page 7: Shaders in Unity by Zoel

• 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 8: Shaders in Unity by Zoel
Page 9: Shaders in Unity by Zoel

• 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 10: Shaders in Unity by Zoel
Page 11: Shaders in Unity by Zoel

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;Bla… Bla… Bla…

Page 12: Shaders in Unity by Zoel

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.

Page 13: Shaders in Unity by Zoel

Cg and Graphics APIs interop scheme:

Page 14: Shaders in Unity by Zoel
Page 15: Shaders in Unity by Zoel

3 Ways of writing shaders in Unity:

Surface shaders.

Vertex and fragment shaders.

Fixed function shaders.

Page 16: Shaders in Unity by Zoel

Simple Custom shader:

Page 17: Shaders in Unity by Zoel

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]

Page 18: Shaders in Unity by Zoel

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 19: Shaders in Unity by Zoel

Thank you!!!http://aras-p.info/blog/

http://docs.unity3d.com/Documentation/Manual/Shaders.htmlhttp://http.developer.nvidia.com/CgTutorial/cg_tutorial_chapter01.html

https://developer.nvidia.com/shader-library http://wiki.unity3d.com/index.php/Shaders

[email protected]