three.js
three.js
Tips for implementing three.js
Introduction
three.js is JavaScript library creating and displaying animated 3D
this page http://blog.teamtreehouse.com/the-beginners-guide-to-three-js is best recommended
to load json 3d model file and see the 3D model --> for to https://threejs.org/editor/
Tip 1: JS in Body not in header
- Place JavaScript in html file in the body - not in header
<!DOCTYPE html> <html> <head> <meta charset=utf-8> <title>My first three.js app</title> <style> body { margin: 0; } canvas { width: 100%; height: 100% } </style> </head> <body> <script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r73/three.js"></script> <script> var scene = new THREE.Scene(); var camera = new THREE.PerspectiveCamera( 35, // Field of view window.innerWidth / window.innerHeight, // Aspect ratio 0.1, // Near 10000 // Far ); var renderer = new THREE.WebGLRenderer(); renderer.setSize( window.innerWidth, window.innerHeight ); document.body.appendChild( renderer.domElement ); var geometry = new THREE.BoxGeometry( 1, 1, 1 ); var material = new THREE.MeshBasicMaterial( { color: 0x00ff00 } ); var cube = new THREE.Mesh( geometry, material ); scene.add( cube ); camera.position.z = 5; var animate = function () { requestAnimationFrame( animate ); cube.rotation.x += 0.01; cube.rotation.y += 0.01; renderer.render(scene, camera); }; animate(); </script> </body> </html>
Tip 2: Replacing a model - only with the same type
A. Replace model.js file with geometry type
- enter http://blog.teamtreehouse.com/the-beginners-guide-to-three-js, download the code, Extract the zip file to your computer
- now you see this
-

- you can see this module file here --> modules/treehouse_logo.js
-
- Get the new model file and place it in your main three.js directory
- Enter https://threejs.org/ and download three.js-master zip file, then extract it to your directory
- Find the new model file here .../three.js-master/examples/models/animated/sittingBox.js
- place sittingBox.js at modules directory
- Change the code
- at index.html replace this line
- loader.load( "models/treehouse_logo.js", function(geometry){
- with this line
- loader.load( "models/sittingBox.js", function(geometry){
- pay attention that both models are "geometry" type
- at index.html replace this line
- Now you will see

- you can see this module file here --> modules/sitting.js
B. Replace model.obj file with obj type
- Enter https://threejs.org/ and download three.js-master zip file, then extract it to your directory
- Enter ../three.js-master/examples/webgl_loader_obj2.html
- you will see this

- and the model file --> models/obj/female02/female02.obj
- Get any file.obj, I received lamp.obj file from Hagai from Brightway https://drive.google.com/drive/folders/162PuzEitVvS57DkVjPrpHOMgyGRzshib
- Place the file.obj at the model directory
- replace two lines in webgl_loader_obj2.html code
- this line
- objLoader.load( 'models/obj/female02/female02.obj', callbackOnLoad, null, null, null, false );
- with this line
- objLoader.load( 'models/obj/lamp.obj', callbackOnLoad, null, null, null, false );
- and also this line
- objLoader.loadMtl( 'models/obj/female02/female02.obj', null, onLoadMtl );
- with this line
- objLoader.loadMtl( 'models/lamp.obj', null, onLoadMtl );
- this line
- now you will see this

- and the model file --> models/lamp.obj
Tip 3: Adjust mouse speed animation
A. The code change
- Go to OrbitControls.js at https://s.cdpn.io/25480/OrbitControls.js - see below the section of mouse speed adjustment
- chAnge zoomSpeed from 1.0 to 2.0 and play with your mouse scroll wheel to change zoom
- see more about it here https://threejs.org/docs/#examples/controls/OrbitControls
// This option actually enables dollying in and out; left as "zoom" for
// backwards compatibility
this.noZoom = false;
this.zoomSpeed = 1.0;
// Limits to how far you can dolly in and out
this.minDistance = 0;
this.maxDistance = Infinity;
// Set to true to disable this control
this.noRotate = false;
this.rotateSpeed = 1.0;
// Set to true to disable this control
this.noPan = false;
this.keyPanSpeed = 7.0; // pixels moved per arrow key push
// Set to true to automatically rotate around the target
this.autoRotate = false;
this.autoRotateSpeed = 2.0; // 30 seconds per round when fps is 60
B. The actual testing
For actually change in code mouse speed and then test it there is a need to do the following
- enter http://blog.teamtreehouse.com/the-beginners-guide-to-three-js and download the code
- Extract the zip file to your computer and open index.html file --> you should see the animation in you browser - if not - see tip 4 below
- adust mouse speed at OrbitControls.js and test it by your self
Tip 4: to see three.js animation in chrome
- open your chrome browser by this "chrome.exe --allow-file-access-from-files" command



