three.js - OrbitControls

Rotate the camera around an object with three.js r86

by Thanos Saringelos

HTML

<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/86/three.js"></script>
<script data-name="orbitControls.js">
  /**
   * @author qiao / https://github.com/qiao
   * @author mrdoob / http://mrdoob.com
   * @author alteredq / http://alteredqualia.com/
   * @author WestLangley / http://github.com/WestLangley
   * @author erich666 / http://erichaines.com
   */

  // This set of controls performs orbiting, dollying (zooming), and panning.
  // Unlike TrackballControls, it maintains the "up" direction object.up (+Y by default).
  //
  //    Orbit - left mouse / touch: one finger move
  //    Zoom - middle mouse, or mousewheel / touch: two finger spread or squish
  //    Pan - right mouse, or arrow keys / touch: three finger swipe

  THREE.OrbitControls = function(object, domElement) {

    this.object = object;

    this.domElement = (domElement !== undefined) ? domElement : document;

    // Set to false to disable this control
    this.enabled = true;

    // "target" sets the location of focus, where the object orbits around
    this.target = new THREE.Vector3();

    // How far you can dolly in and out ( PerspectiveCamera only )
    this.minDistance = 0;
    this.maxDistance = Infinity;

    // How far you can zoom in and out ( OrthographicCamera only )
    this.minZoom = 0;
    this.maxZoom = Infinity;

    // How far you can orbit vertically, upper and lower limits.
    // Range is 0 to Math.PI radians.
    this.minPolarAngle = 0; // radians
    this.maxPolarAngle = Math.PI; // radians

    // How far you can orbit horizontally, upper and lower limits.
    // If set, must be a sub-interval of the interval [ - Math.PI, Math.PI ].
    this.minAzimuthAngle = -Infinity; // radians
    this.maxAzimuthAngle = Infinity; // radians

    // Set to true to enable damping (inertia)
    // If damping is enabled, you must call controls.update() in your animation loop
    this.enableDamping = false;
    this.dampingFactor = 0.25;

    // This option actually...

CSS

body,
canvas {
  margin: 0;
  padding: 0;
}

body {
  overflow: hidden
}

JavaScript

'use strict';

var camera, scene, renderer;
var cube, cube_geometry, cube_material;
var controls;

init();
render();

function init() {

  scene = new THREE.Scene();

  // renderer
  var xAxis = new THREE.Geometry();
  xAxis.vertices.push(
    new THREE.Vector3(1000, 0, 0),
    new THREE.Vector3(0, 0, 0)
  );

  window.mesh = new THREE.Mesh(
    new THREE.BoxGeometry(13, 0.3, 15),
     new THREE.MeshStandardMaterial({ 
     roughness: 0.1,
    color: 0xffffff,
    metalness: 0.1 
  }));
		window.mesh.castShadow = true;
		window.mesh.receiveShadow = true;

   scene.add(mesh);
  
  window.mesh2 = new THREE.Mesh(
    new THREE.BoxGeometry(6, 0.2,7),
    [new THREE.MeshStandardMaterial({ 
    emissiveIntensity: 1,
    //side: THREE.DoubleSide,
    color: 0xffffbe,
    roughness: 0.3,
    metalness: 0.2 
  })]);
		window.mesh2.receiveShadow = true;
    		window.mesh2.castShadow = true;

    scene.add(mesh2)  ;

  var yAxis = new THREE.Geometry();
  yAxis.vertices.push(
    new THREE.Vector3(0, 1000, 0),
    new THREE.Vector3(0, 0, 0)
  );

  var zAxis = new THREE.Geometry();
  zAxis.vertices.push(
    new THREE.Vector3(0, 0, 1000),
    new THREE.Vector3(0, 0, 0)
  );

  var xAxisLine = new THREE.Line(xAxis, new THREE.LineBasicMaterial({
    color: "#6F6222"
  }));
  var yAxisLine = new THREE.Line(yAxis, new THREE.LineBasicMaterial({
    color: "#FF0000"
  }));
  var zAxisLine = new THREE.Line(zAxis, new THREE.LineBasicMaterial({
    color: "#6F6299"
  }));



  renderer = new THREE.WebGLRenderer({
    alpha: true
  });
  renderer = new THREE.WebGLRenderer({ antialias: true });
  renderer.shadowMap.enabled = true;
  renderer.shadowMap.type = THREE.PCFSoftShadowMap;
  renderer.shadowMap.renderSingleSided = false;
  renderer.setSize(window.innerWidth, window.innerHeight);
  renderer.setClearColor(0xffffff, 1); 
  renderer.setSize(window.innerWidth, window.innerHeight);
  document.body.appendChild(renderer.domElement);

  // camera

  camera = new...