Shaders by spiderlili - 11

Shaders & Effects

HLSL(Cg) Shaders for Unity

Unknown VersionUnknown LicenseUpdated 342 days agoCreated on January 9th, 2018
Go to source


Shader code is executed on a per-pixel or per vertex basis, so write the code as if you’re only writing for one pixel. No need to write loops that process all of the pixels that need to appear on screen(the GPU does the rest).

There are some visible differences between shader code and normal code: float(the highest precision is 32 bits), half, fixed and int. They have been designed to be more efficient.

Types of Shaders

Unlit: include both vertx and fragement
Surface: hides them behind a infrastructure optimised for writing lighting calculations, removing some boilerplates
Image Effect: in conjunction with c# at the end of the rendering of the frame
Compute: provides high-speed general purpose computing and takes advantage of the large numbers of parallel processors on the GPU - allows more effective parallel programming methods.

Best Practices

Use floats for texture coordinates, world positions and calculations
Use half for dynamic colour changes, short vectors and directions
Use fixed for regular colours and simple colour operations
Use int for counters and array indices.

Texture Data types

Each of these has a high and low precision version for low end image processing and high end image manilulation.

Sampler2D: regular images
SamplerCUBE: cube maps
Packed arrays: any of these data types can be made into special arrays(RGBA or XYZW)

PBR Pipeline focuses on 7 areas:

  1. Reflection: Accomplished by drawing rays from the viewer to the reflective surface and calculating where it bounces off = a reverse calculation to lighting.
  2. Diffusion: examines how light and colour are distributed across the surface by considering what light is absorbed and what light is reflected and how.
  3. Translucency & transparency: Examines how light can move through objects and render them fully or partly see through.
  4. Conservation of energy: A concept that ensures objects never reflect more light than they receive. unless an object is a perfect mirror finish, it will absorb light depending on the surface. however some lights will always be reflected and available to light other objects.
  5. Metallicity: Considers the interaction of light on shiny surfaces, highlight and colours that are reflected. metals tend to be highly reflective with very little in the way of diffused light.
  6. Fresnel reflectivity: Examines how reflections on a curved surface becomes stronger towards the edges and fading towards the centre. normal reflection reflects the environment as it is, fresnel reflection is how real world reflection works on a curved surface. this varies as reflective surface change. however you will never get the perfect straight line of the horizon in a curved surface as you do with normal reflection.
  7. Microsurface scattering: Similar to bump mapping - suggests that most surfaces are going to contain grooves and cracks which will reflect the light at different angles other than those dictated by a regular surface.

Vertex vs Pixel Lighting

Vertex lit = Gouraud shading in reverse, the incoming light is caluclated at each vertex and average across the surface.

Pixel lit = Phong-like: a light for each pixel is calculated.

Pixel will pick up far more details and specular highlights than vertex, as light is calculated for every point. it provides far more detailed shading but requires more processing. Vertex lit is the way to go for older graphics card and mobile devices(or when you have a lot of things to render but the quality doesn’t matter so much).

Write your own lighting model if all these lighting models doesn’t suit your needs.

Graphics APIs


Useful Resources

PBR Guides by Allegorithmic:
The Book of Shaders:

Cooking Shaders Book for Unity
Shader Graph:
Mathematical Formulae for Plasma:
Textures and Normal Maps:
Open Source Shader Plugin For Unity - LUX:

Unreal Engine

RenderDoc: a free MIT licensed stand-alone graphics debugger that allows quick and easy single-frame capture and detailed introspection of any application using Vulkan, D3D11, OpenGL or D3D12 across Windows 7 - 10, Linux.

Show all projects by spiderlili