A graphics tutorial with Three.js and dat.GUI
This project is a demo of some common shading and reflection models in graphics.
While Three.js provides implementations of many of the materials used, all of the shaders are implemented individually and are accompanied by brief explanations in the sections Shading Models and Environment Mapping.
Jump to Section:
Setup
Organization
Adding Skyboxes
Adding Shaders
To run the demo locally you can use node.js http-server
or something alike.
Your browser must support WebGL. Firefox and Chrome are recommended.
Note: Firefox or Chrome are recommended
There is a known warning in Safari and some older versions of Chrome and Firefox, see here if you encounter this:
WARNING: 0:1: extension 'GL_ARB_gpu_shader5' is not supported
Download the source from Github
npm install http-server -g
Navigate to the root directory of the project
-p
is an optional flag to specify the port, 8000 in this case (default 8080):
http-server -p 8000
Note: Firefox or Chrome is recommended (not tested in IE)
This folder contains all the custom shaders.
This folder contains a separate folder for each set of skybox images.
demo.js
This is the main demo file where the scene is created and rendered.
settings.js
Contains all the settings & default values for the demo. This is the only place you would need to change any values directly. Default settings are inside the defaults
object.
controllers.js
Contains all the code that sets up the dat.GUI controllers.
helpers.js
Helper functions that the dat.GUI controllers call like the reset button, switching the shaders etc. are all located here.
shaders.js
Contains all the code that sets up and loads the custom shaders. Uniforms that are shared by the various shaders are declared at the top.
This contains the models used in the demo.
To add a new skybox the 6 images must be placed in a folder with name mySkyboxName
and named individually with the convention below, such that the folder name matches the starting of the image names:
mySkyboxName_ft.png
mySkyboxName_bk.png
mySkyboxName_up.png
mySkyboxName_dn.png
mySkyboxName_rt.png
mySkyboxName_lf.png
The 6 images should correspond to the front, back, up, down, right, and left sides of the skybox.
Place the folder of images inside the img
folder of the project.
Add a thumbnail inside demo.html where the id is the same as mySkyBoxName
.
demo.html:
<img id="mySkyboxName" onclick="updateSkybox(this.id)" src="img/mySkyboxName/mySkyboxName_lf.png" alt=""/>
The follow naming convention is being used for shader files:
Vertex Shaders - myShaderName.vs.glsl
Fragment Shaders - myShaderName.fs.glsl
Overview of Steps
shaders.js
create the material and load the shader filessettings.js
define a new const
that is used as a label to switch shaderscontrollers.js
settings.js
To add a new shader place the file inside the shaders
folder.
settings.js
define a new const
for the shader. The shader constants are used to index into an array of shaders for switching between them.settings.js:
const REFRACTION = 5;
const MYSHADER = 6;
shaderMenu
object in order to select the new shader in the controllers.const
.
settings.js:
var shaderMenu = {
...
MyShaderMenuName : MYSHADER
}
In shaders.js
follow the format of the code for other shaders to create the material and then load the glsl files. If you need to create any new uniforms declare them in shaders.js
but all hardcoded values should be in settings.js
.
controllers.js
build a dat.GUI controller for the new shader and add the shader to the array of shaders so that it can be switched to by the controllers. controllers.js
var myShader = new shader(myShaderMaterial, myShaderGUI);
shaders[MYSHADER] = myShader;
Note: The initial shader and GUI displayed is Phong. The other GUIs are hidden until they are switched to.