Skip to content

Instantly share code, notes, and snippets.

@Simbaclaws
Last active March 14, 2023 12:25
Show Gist options
  • Save Simbaclaws/8a01666f0770c1acd8ab1836a0508d2b to your computer and use it in GitHub Desktop.
Save Simbaclaws/8a01666f0770c1acd8ab1836a0508d2b to your computer and use it in GitHub Desktop.
class GLRendering {
constructor() {
this.scene = new THREE.Scene( );
this.camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 0.1, 1000 );
this.renderer = new THREE.WebGLRenderer( );
this.renderer.setSize( window.innerWidth, window.innerHeight );
document.body.appendChild( this.renderer.domElement );
window.addEventListener('resize', () => {
const width = window.innerWidth;
const height = window.innerHeight;
this.renderer.setSize( width, height );
this.camera.aspect = width / height;
this.camera.updateProjectionMatrix();
});
this.addToScene = this.addToScene.bind(this);
this.setCameraZPosition = this.setCameraZPosition.bind(this);
this.loop = this.loop.bind(this);
this.update = this.update.bind(this);
this.render = this.render.bind(this);
this.loop();
}
//Logic
update(callback) {
callback();
}
//Draws the scene
render() {
this.renderer.render( this.scene, this.camera );
}
//run loop (update, render, repeat)
loop() {
requestAnimationFrame( this.loop );
this.update( );
this.render( );
}
addToUpdate(callback) {
}
addToScene(object) {
this.scene.add(object);
}
setCameraZPosition(z) {
this.camera.position.z = z;
}
}
class RotatingCube extends GLRendering {
constructor() {
super();
this.geometry = new THREE.BoxGeometry( 1, 1, 1 );
this.material = new THREE.MeshBasicMaterial( { color: 0x00ff00, wireframe: true } );
this.cube = new THREE.Mesh( this.geometry, this.material );
this.addToScene( this.cube );
this.setCameraZPosition( 3 );
this.update = this.update.bind(this);
}
//Logic
update() {
console.log(JSON.stringify(this));
super.update(() => {
this.cube.rotation.x += 0.01;
this.cube.rotation.y += 0.005;
});
}
}
const waves = new RotatingCube();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment