Flat Surface Shader for rendering illuminated triangles
Flat Surface Shader for rendering illuminated triangles
Simple, lightweight Flat Surface Shader written in JavaScript for rendering lit Triangles to a number of contexts. Currently there is support for WebGL, Canvas 2D and SVG. Check out this demo to see it in action.
Simply put, FSS uses the Lambertian Reflectance model to calculate the color of a Triangle based on an array of Light sources within a Scene.
A Light is composed of a 3D position Vector and 2 Color objects defining its ambient & diffuse emissions. These color channels interact with the Material of a Mesh to calculate the color of a Triangle.
A Triangle is constructed from 3 Vertices which each define the x, y and z coordinates of a corner. Based on these 3 Vertices, a forth 3D Vector is automatically derived at the center of the Triangle – this is known as its Centroid. Alongside the Centroid, a fifth unit Vector is automatically calculated which defines the surface Normal. The Normal describes the direction that the Triangle is facing.
Geometry is simply a collection of triangles – nothing more.
A Material is composed of 2 Color objects which define the ambient & diffuse properties of a surface.
A Mesh is constructed from a Geometry object and a Material object. All the Triangles within the Geometry are rendered using the properties of the Material.
A Scene sits at the very top of the stack. It simply manages arrays of Mesh & Light objects.
The Renderer takes all the information in a Scene and renders it to a context. Currently FSS supports WebGL, Canvas 2D and SVG.
For every Triangle in a Scene the following calculation is performed:
{ R:1, G:1, B:1 } and the diffuse color of the Material is #FF0000 { R:1, G:0, B:0 }, the combined color would be #FF0000 { R:1*1=1, G:1*0=0, B:1*0=0 }. If the coincidence angle was 0.5, the final color of the Triangle would be #800000 { R:1*0.5=0.5, G:0*0.5=0, B:0*0.5=0 }.NOTE: All objects exist within the FSS namespace.
…Install Dependancies:
npm install [email protected]Build:
node build.jsPlease also checkout the case study on Behance created by my dear friend Tobias van Schneider – @schneidertobias.
The architecture of this project was heavily influenced by three.js and the implementation of the Vector calculations was taken from glMatrix.
Matthew Wagerfield: @mwagerfield
Licensed under MIT. Enjoy.
No open issues yet, or sync has not completed.