Skip to content

Instantly share code, notes, and snippets.

@marioecg
Created March 1, 2021 00:19
Show Gist options
  • Save marioecg/76ec3d4d8c11f5c192cc00c57b739b04 to your computer and use it in GitHub Desktop.
Save marioecg/76ec3d4d8c11f5c192cc00c57b739b04 to your computer and use it in GitHub Desktop.
Threejs renderer setup
import * as THREE from 'three';
import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls';
import { Events } from '../events';
import store from '../store';
export default new class {
constructor() {
this.renderer = new THREE.WebGLRenderer({
antialias: true,
alpha: true,
});
this.renderer.setPixelRatio(Math.min(window.devicePixelRatio, 1.5));
this.renderer.setSize(store.bounds.ww, store.bounds.wh);
this.renderer.setClearColor('white', 0);
this.camera = new THREE.PerspectiveCamera(
45,
store.bounds.ww / store.bounds.wh,
0.1,
1000
);
this.camera.position.set(0, 0, 10);
this.scene = new THREE.Scene();
this.canvas = null;
this.controls = new OrbitControls(this.camera, this.renderer.domElement);
this.controls.enableDamping = true;
this.clock = new THREE.Clock();
this.init();
}
init() {
this.addCanvas();
this.addEvents();
this.addElements();
}
addCanvas() {
this.canvas = this.renderer.domElement;
this.canvas.classList.add('webgl');
document.body.appendChild(this.canvas);
}
addEvents() {
Events.on('tick', this.render.bind(this));
Events.on('resize', this.resize.bind(this));
}
resize() {
let width = store.bounds.ww;
let height = store.bounds.wh;
this.camera.aspect = width / height;
this.renderer.setSize(width, height);
this.camera.updateProjectionMatrix();
}
render() {
this.controls.update();
this.renderer.render(this.scene, this.camera);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment