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.
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:
- Reflection: Accomplished by drawing rays from the viewer to the reflective surface and calculating where it bounces off = a reverse calculation to lighting.
- Diffusion: examines how light and colour are distributed across the surface by considering what light is absorbed and what light is reflected and how.
- Translucency & transparency: Examines how light can move through objects and render them fully or partly see through.
- 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.
- 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.
- 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.
- 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.
Cooking Shaders Book for Unity https://www.packtpub.com/game-development/unity-5x-shaders-and-effects-cookbook
Shader Graph: https://blogs.unity3d.com/2018/02/27/introduction-to-shader-graph-build-your-shaders-with-a-visual-editor/
Mathematical Formulae for Plasma: https://www.bidouille.org/prog/plasma
Textures and Normal Maps: http://www.textures.com
Open Source Shader Plugin For Unity - LUX: https://assetstore.unity.com/packages/vfx/shaders/lux-physically-based-shader-framework-16000
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.