Threejs - Materials 101

by black strings

HTML

<script src="https://rawgit.com/mrdoob/three.js/dev/build/three.js"></script>
<script src="https://rawgit.com/mrdoob/three.js/dev/examples/js/controls/OrbitControls.js"></script>

CSS

/* fix mouse click offset errors when doing drags */
body {
  margin: 0;
}

JavaScript

class Main {
	constructor(){
    this.objects = []; 
    this.scene = new THREE.Scene();
    
    var width = window.innerWidth;
    var height = window.innerHeight * .75;
    this.camera = new THREE.PerspectiveCamera( 
    	35, width/height, 0.1, 1000 
    );
    
    this.renderer = new THREE.WebGLRenderer();
    
    
    this.renderer.setSize( width, height );
    this.renderer.setClearColor( 0xcccccc, 1 ); 
    
    // controls should set 2nd param or mouse move will be all over the main dom element
    // null on 2nd param, threejs will use the parent as the mouse click and prevent default
    this.controls = new THREE.OrbitControls(this.camera, this.renderer.domElement);
    
    // will turn off zooming while panning with right mouse click, false by default
    this.controls.screenSpacePanning = true;
    
    document.body.appendChild( this.renderer.domElement );
    this.scene.add(this.camera);

    
    this.setToFullOrbit(this.controls);
    this.camera.position.copy(new THREE.Vector3(10,10,10));
    // fix first frame render issue going invisible
    this.camera.lookAt(new THREE.Vector3(0,0,0));

    var axisHelper = new THREE.AxisHelper();
    this.scene.add(axisHelper);

		this.createScene();

  }
  
  // playground
  createScene(){
  
  	var geo = new THREE.BoxGeometry(3,3,3);
    var mat = new THREE.MeshBasicMaterial({color:0x0000ff});
    var mesh = new THREE.Mesh(geo, mat);
    this.scene.add(mesh);
    
    var sGeo = new THREE.SphereGeometry(3,10,10);
    var sMesh = new THREE.Mesh(sGeo, mat);
    this.scene.add(sMesh);
    sMesh.position.x += 5;
    
  }
  
  render() {
    
    //camera.lookAt( scene.position );
    this.renderer.render( this.scene, this.camera );
    
    
    requestAnimationFrame( () => this.render() );
  }
  setToFullOrbit(orbitControl) {
    // how far you can orbit vertically
    orbitControl.minPolarAngle = 0;
    orbitControl.maxPolarAngle = Math.PI;

    // How far you can dolly in and out ( PerspectiveCamera only...