Commented and updated

This commit is contained in:
Paul Graffam 2016-10-07 04:42:23 -04:00
parent ac06ae8cbd
commit 3737462bd5
9 changed files with 49 additions and 36 deletions

View File

@ -2,7 +2,7 @@ import Config from './data/config';
import Detector from './utils/detector'; import Detector from './utils/detector';
import Main from './app/main'; import Main from './app/main';
// verify environment. // Check environment and set the Config helper
if(__ENV__ == 'dev') { if(__ENV__ == 'dev') {
console.log('----- RUNNING IN DEV ENVIRONMENT! -----'); console.log('----- RUNNING IN DEV ENVIRONMENT! -----');
@ -10,6 +10,7 @@ if(__ENV__ == 'dev') {
} }
function init() { function init() {
// Check for webGL capabilities
if(!Detector.webgl) { if(!Detector.webgl) {
Detector.addGetWebGLMessage(); Detector.addGetWebGLMessage();
} else { } else {
@ -18,4 +19,4 @@ function init() {
} }
} }
window.onload = init; init();

View File

@ -12,11 +12,12 @@ export default class Camera {
this.updateSize(renderer); this.updateSize(renderer);
// listeners // Listeners
window.addEventListener('resize', () => this.updateSize(renderer), false); window.addEventListener('resize', () => this.updateSize(renderer), false);
} }
updateSize(renderer) { updateSize(renderer) {
// Multiply by dpr in case it is retina device
this.threeCamera.aspect = renderer.domElement.width * Config.dpr / renderer.domElement.height * Config.dpr; this.threeCamera.aspect = renderer.domElement.width * Config.dpr / renderer.domElement.height * Config.dpr;
this.threeCamera.updateProjectionMatrix(); this.threeCamera.updateProjectionMatrix();
} }

View File

@ -5,6 +5,7 @@ import Config from '../../data/config';
export default class Controls { export default class Controls {
constructor(camera, container) { constructor(camera, container) {
// Orbit controls first needs to pass in THREE to constructor
const orbitControls = new OrbitControls(THREE); const orbitControls = new OrbitControls(THREE);
this.threeControls = new orbitControls(camera, container); this.threeControls = new orbitControls(camera, container);

View File

@ -4,29 +4,27 @@ import Config from '../../data/config';
export default class Renderer { export default class Renderer {
constructor(scene, container) { constructor(scene, container) {
// Properties
this.scene = scene; this.scene = scene;
this.container = container; this.container = container;
// Create WebGL renderer and set its antialias
this.threeRenderer = new THREE.WebGLRenderer({antialias: true}); this.threeRenderer = new THREE.WebGLRenderer({antialias: true});
//this.renderer.setClearColor(0x000000, 0); // Set clear color to fog to enable fog or to color
this.threeRenderer.setClearColor(scene.fog.color); this.threeRenderer.setClearColor(scene.fog.color);
this.threeRenderer.setPixelRatio(window.devicePixelRatio); this.threeRenderer.setPixelRatio(window.devicePixelRatio); // For retina
container.appendChild(this.threeRenderer.domElement); container.appendChild(this.threeRenderer.domElement);
this.threeRenderer.gammaInput = true; // Shadow map options
this.threeRenderer.gammaOutput = true;
// shadow
this.threeRenderer.shadowMap.enabled = true; this.threeRenderer.shadowMap.enabled = true;
this.threeRenderer.shadowMap.type = THREE.PCFSoftShadowMap; this.threeRenderer.shadowMap.type = THREE.PCFSoftShadowMap;
this.threeRenderer.shadowMapSoft = true;
this.threeRenderer.autoClear = false;
// Get anisotropy for textures
Config.maxAnisotropy = this.threeRenderer.getMaxAnisotropy(); Config.maxAnisotropy = this.threeRenderer.getMaxAnisotropy();
// Initial size update set to canvas container
this.updateSize(); this.updateSize();
// Listeners // Listeners
@ -39,6 +37,7 @@ export default class Renderer {
} }
render(scene, camera) { render(scene, camera) {
// Renders scene to canvas target
this.threeRenderer.render(scene, camera); this.threeRenderer.render(scene, camera);
} }
} }

View File

@ -2,6 +2,7 @@ import * as THREE from 'three';
import Config from '../../data/config'; import Config from '../../data/config';
// This helper class can be used to create and then place geometry in the scene
export default class Geometry { export default class Geometry {
constructor(scene) { constructor(scene) {
this.scene = scene; this.scene = scene;
@ -20,12 +21,12 @@ export default class Geometry {
const material = new THREE.MeshStandardMaterial({ color: 0xCCCCCC, side: THREE.DoubleSide }); const material = new THREE.MeshStandardMaterial({ color: 0xCCCCCC, side: THREE.DoubleSide });
const mesh = new THREE.Mesh(this.geo, material); const mesh = new THREE.Mesh(this.geo, material);
// Use ES6 spread to set position and rotation from passed in array
mesh.position.set(...position); mesh.position.set(...position);
mesh.rotation.set(...rotation); mesh.rotation.set(...rotation);
if(Config.shadow.enabled) { if(Config.shadow.enabled) {
mesh.receiveShadow = true; mesh.receiveShadow = true;
mesh.castShadow = true;
} }
this.scene.add(mesh); this.scene.add(mesh);

View File

@ -2,20 +2,23 @@ import * as THREE from 'three';
import Config from '../../data/config'; import Config from '../../data/config';
// USe this class as a Helper to set up some default materials
export default class Material { export default class Material {
constructor() { constructor() {
this.emissive = new THREE.MeshBasicMaterial({ this.emissive = new THREE.MeshBasicMaterial({
color: 0xeeeeee, color: 0xeeeeee,
side: THREE.DoubleSide, side: THREE.DoubleSide
fog: false
}); });
this.standard = new THREE.MeshStandardMaterial({ this.standard = new THREE.MeshStandardMaterial({
shading: THREE.FlatShading, shading: THREE.FlatShading,
roughness: 1, roughness: 1,
metalness: 0, metalness: 0,
side: THREE.DoubleSide, side: THREE.DoubleSide
fog: false });
this.phong = new THREE.MeshPhongMaterial({
}); });
this.wire = new THREE.MeshBasicMaterial({wireframe: true}); this.wire = new THREE.MeshBasicMaterial({wireframe: true});

View File

@ -22,13 +22,13 @@ import DatGUI from './managers/datGUI';
// data // data
import Config from './../data/config'; import Config from './../data/config';
// -- End imports // -- End of imports
// Local vars for rStats // Local vars for rStats
let rS, bS, glS, tS; let rS, bS, glS, tS;
// This is the main class that ties all of the components together and renders the loop // This class instantiates and ties all of the components together, starts the loading process and renders the main loop
export default class Main { export default class Main {
constructor(container) { constructor(container) {
// Set container property to container element // Set container property to container element
@ -41,7 +41,7 @@ export default class Main {
this.scene = new THREE.Scene(); this.scene = new THREE.Scene();
this.scene.fog = new THREE.FogExp2(Config.fog.color, Config.fog.near); this.scene.fog = new THREE.FogExp2(Config.fog.color, Config.fog.near);
// Get Device Pixel Ratio first // Get Device Pixel Ratio first for retina
if(window.devicePixelRatio) { if(window.devicePixelRatio) {
Config.dpr = window.devicePixelRatio; Config.dpr = window.devicePixelRatio;
} }
@ -62,7 +62,7 @@ export default class Main {
// Create and place geo in scene // Create and place geo in scene
this.geometry = new Geometry(this.scene); this.geometry = new Geometry(this.scene);
this.geometry.make('plane')(100, 100, 10, 10); this.geometry.make('plane')(150, 150, 10, 10);
this.geometry.place([0, -20, 0], [Math.PI/2, 0, 0]); this.geometry.place([0, -20, 0], [Math.PI/2, 0, 0]);
// Set up rStats if dev environment // Set up rStats if dev environment
@ -72,7 +72,7 @@ export default class Main {
tS = new threeStats(this.renderer.threeRenderer); tS = new threeStats(this.renderer.threeRenderer);
rS = new rStats({ rS = new rStats({
CSSPath: '/assets/css/', CSSPath: './assets/css/',
userTimingAPI: true, userTimingAPI: true,
values: { values: {
frame: { caption: 'Total frame time (ms)', over: 16, average: true, avgMs: 100 }, frame: { caption: 'Total frame time (ms)', over: 16, average: true, avgMs: 100 },
@ -96,7 +96,7 @@ export default class Main {
// Instantiate texture class // Instantiate texture class
this.texture = new Texture(); this.texture = new Texture();
// Start loading the textures // Start loading the textures and then go on to load the model after the texture Promises have resolved
this.texture.load().then(() => { this.texture.load().then(() => {
this.manager = new THREE.LoadingManager(); this.manager = new THREE.LoadingManager();
@ -104,23 +104,24 @@ export default class Main {
this.model = new Model(this.scene, this.manager, this.texture.textures); this.model = new Model(this.scene, this.manager, this.texture.textures);
this.model.load(); this.model.load();
// onProgress // onProgress callback
this.manager.onProgress = (item, loaded, total) => { this.manager.onProgress = (item, loaded, total) => {
console.log(`${item}: ${loaded} ${total}`); console.log(`${item}: ${loaded} ${total}`);
}; };
// All loaders done // All loaders done now
this.manager.onLoad = () => { this.manager.onLoad = () => {
// Set up interaction manager with app now that the model is finished loading // Set up interaction manager with the app now that the model is finished loading
new Interaction(this.renderer.threeRenderer, this.scene, this.camera.threeCamera, this.controls.threeControls); new Interaction(this.renderer.threeRenderer, this.scene, this.camera.threeCamera, this.controls.threeControls);
// Add dat.GUI controls if dev as helper // Add dat.GUI controls if dev
if(Config.isDev) { if(Config.isDev) {
new DatGUI(this, this.model.obj); new DatGUI(this, this.model.obj);
} }
// Fully loaded // Everything is now fully loaded
Config.isLoaded = true; Config.isLoaded = true;
this.container.querySelector('#loading').style.display = 'none';
}; };
}); });
@ -129,8 +130,6 @@ export default class Main {
} }
render() { render() {
const delta = this.clock.getDelta();
// Render rStats if Dev // Render rStats if Dev
if(Config.isDev) { if(Config.isDev) {
rS('frame').start(); rS('frame').start();
@ -142,20 +141,24 @@ export default class Main {
rS('render').start(); rS('render').start();
} }
// Clear renderer and re-render // Call render function and pass in created scene and camera
this.renderer.threeRenderer.clear();
this.renderer.render(this.scene, this.camera.threeCamera); this.renderer.render(this.scene, this.camera.threeCamera);
// rStats has finished determining render call now
if(Config.isDev) { if(Config.isDev) {
rS('render').end(); // render finished rS('render').end(); // render finished
rS('frame').end(); // frame finished rS('frame').end(); // frame finished
// Local rStats update
rS('rStats').start(); rS('rStats').start();
rS().update(); rS().update();
rS('rStats').end(); rS('rStats').end();
} }
// Updates section // Delta time is sometimes needed for certain updates
//const delta = this.clock.getDelta();
// Call any vendor or module updates here
TWEEN.update(); TWEEN.update();
this.controls.threeControls.update(); this.controls.threeControls.update();

View File

@ -144,7 +144,7 @@ export default class DatGUI {
shadowFolder.add(Config.shadow, 'helperEnabled').name('Helper Enabled').onChange((value) => { shadowFolder.add(Config.shadow, 'helperEnabled').name('Helper Enabled').onChange((value) => {
this.light.directionalLightHelper.visible = value; this.light.directionalLightHelper.visible = value;
}); });
const shadowNearGui = shadowFolder.add(Config.shadow, 'near', 0, 100).name('Near'); const shadowNearGui = shadowFolder.add(Config.shadow, 'near', 0, 400).name('Near');
shadowNearGui.onChange((value) => { shadowNearGui.onChange((value) => {
this.controls.enableRotate = false; this.controls.enableRotate = false;

View File

@ -10,18 +10,20 @@ export default class Interaction {
this.camera = camera; this.camera = camera;
this.controls = controls; this.controls = controls;
this.timeout = null;
// Instantiate keyboard helper // Instantiate keyboard helper
this.keyboard = new Keyboard(); this.keyboard = new Keyboard();
// Listeners // Listeners
// Mouse events // Mouse events
this.renderer.domElement.addEventListener('mouseup', (event) => this.onMouseUp(event), false);
this.renderer.domElement.addEventListener('mousemove', (event) => Helpers.throttle(this.onMouseMove(event), 250), false); this.renderer.domElement.addEventListener('mousemove', (event) => Helpers.throttle(this.onMouseMove(event), 250), false);
this.renderer.domElement.addEventListener('mouseleave', (event) => this.onMouseLeave(event), false); this.renderer.domElement.addEventListener('mouseleave', (event) => this.onMouseLeave(event), false);
this.renderer.domElement.addEventListener('mouseover', (event) => this.onMouseOver(event), false); this.renderer.domElement.addEventListener('mouseover', (event) => this.onMouseOver(event), false);
// Keyboard events // Keyboard events
this.keyboard.domElement.addEventListener('keydown', (event) => { this.keyboard.domElement.addEventListener('keydown', (event) => {
// Only once
if(event.repeat) { if(event.repeat) {
return; return;
} }
@ -47,7 +49,9 @@ export default class Interaction {
onMouseMove(event) { onMouseMove(event) {
event.preventDefault(); event.preventDefault();
setTimeout(function() { clearTimeout(this.timeout);
this.timeout = setTimeout(function() {
Config.isMouseMoving = false; Config.isMouseMoving = false;
}, 200); }, 200);