Giter Site home page Giter Site logo

flat-surface-shader's Introduction

Flat Surface Shader [FSS]

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.

Understanding Lighting

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.

Light

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.

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

Geometry is simply a collection of triangles – nothing more.

Material

A Material is composed of 2 Color objects which define the ambient & diffuse properties of a surface.

Mesh

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.

Scene

A Scene sits at the very top of the stack. It simply manages arrays of Mesh & Light objects.

Renderer

The Renderer takes all the information in a Scene and renders it to a context. Currently FSS supports WebGL, Canvas 2D and SVG.

Calculation

For every Triangle in a Scene the following calculation is performed:

  1. A Vector between the Triangle's Centroid and the Light's Position is calculated and normalised. This can be considered as a single Ray travelling from the Light to the center of the Triangle.
  2. The angle beween this Ray and the Normal of the Triangle is then calculated using the dot product. This angle is simply a number ranging from -1 to 1. When the Ray and the Normal are coincident, this value is 0, and when they are perpendicular to one another, this value is 1. This value goes into the negative range when the Light is behind the Triangle.
  3. Firstly, the diffuse color of the Light is multiplied by the diffuse color of the Material associated with the Triangle. This color is then multiplied by the coincidence angle described above. For example, if the diffuse color of the Light is #FFFFFF { 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 }.
  4. In much the same way as above, the ambient color of the Light is multipled by the ambient color of the Material. Since ambient light is a uniform dissipation of scattered light, it is not modified any further.
  5. The final color of the Triangle is simply calculated by adding the diffuse & ambient colors together. Simples.

Example

NOTE: All objects exist within the FSS namespace.

// 1) Create a Renderer for the context you would like to render to.
//    You can use either the WebGLRenderer, CanvasRenderer or SVGRenderer.
var renderer = new FSS.CanvasRenderer();

// 2) Add the Renderer's element to the DOM:
var container = document.getElementById('container');
container.appendChild(renderer.element);

// 3) Create a Scene:
var scene = new FSS.Scene();

// 4) Create some Geometry & a Material, pass them to a Mesh constructor, and add the Mesh to the Scene:
var geometry = new FSS.Plane(200, 100, 4, 2);
var material = new FSS.Material('#444444', '#FFFFFF');
var mesh = new FSS.Mesh(geometry, material);
scene.add(mesh);

// 5) Create and add a Light to the Scene:
var light = new FSS.Light('#FF0000', '#0000FF');
scene.add(light);

// 6) Finally, render the Scene:
renderer.render(scene);

Building

Install Dependancies:

npm install [email protected]

Build:

node build.js

Inspiration

Please also checkout the case study on Behance created by my dear friend Tobias van Schneider@schneidertobias.

Acknowledgments

The architecture of this project was heavily influenced by three.js and the implementation of the Vector calculations was taken from glMatrix.

Author

Matthew Wagerfield: @mwagerfield

License

Licensed under MIT. Enjoy.

flat-surface-shader's People

Contributors

wagerfield avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

flat-surface-shader's Issues

Different shapes

Is there a way to modify it to create different shapes other than triangles?

Or where can I start to look into using different shapes other than triangles?

Thank you in advance.

Safari 8.0.7 rendering via WebGL not working (demo website)

Hi,
on Safari 8 rendering via WebGL doesn't work.

Screenshot with console log

Error reported:
[Warning] WebGL: INVALID_VALUE: bufferData: size == 0 (fss.min.js, line 31, x6)
[Warning] WebGL: INVALID_OPERATION: drawArrays: attempt to access out of bounds arrays (fss.min.js, line 31)
[Warning] WebGL: too many errors, no more errors will be reported to the console for this context. (fss.min.js, line 31)

cursor icon effect

hi, great script, was wondering how to change the mouse icon and have something else instead of the two circles spinning? cant find anything relevant in the css.
thanks

How to make triangles to have a different form shape

Hello, Im discovering your work and I'm trying to figure out how to make randomize shapes like the demo site. Right now I only get squared shapes with static triangles. Could you explain a bit more please ? And thanks agin for this work !

support other geometry?

hi,
I want to user the flat-surface-shader on sphere, the effect just like a low-poly earth. Can u help me with that? thanks :>

Boostrap integration trouble ?

Hi mate, love your work but by some weird experience the background is above all of my class="well" divs. Destroying pretty much all my design... Any idea of why this does happen ?

Demo is down

Hi Matthew -- not sure of the best way to reach you, but the demo page for flat surface shader is down. Perhaps it is a problem with matthew.wagerfield.com?

-Jacob

Alpha Transparency?

Thanks for maintaining this repo --looks great**!**

Any ideas how to implement transparency for the triangles (globally or specifically)?
The README doesn't seem to mention anything relevant and the source, while making using of rgba() appears to ignore the alpha (transparency) values.

I tried modifying the following lines (see http://codepen.io/wouwi/pen/Apvaq) without success:

Line-382:
original: this.opacity = FSS.Utils.isNumber(opacity) ? opacity : 1;
modded: this.opacity = FSS.Utils.isNumber(opacity) ? opacity : 0.5;

Line-405
original: this.hex = '#' + r + g + b;
modded: this.hex = '#00' + r + g + b;

Multiple animations in one page.

Greetings and great work you got there!

I want to use this animation on section headers with a title on it and it looks great.

But couldn't manage to have more than one animation divs per page. Is this possible and what's the most practical way for using it?

Thanks!

react ussage

hey

did anyone try to use this in react? or to turn it into an npm package?

thanks,
Rares

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.