As I delved into the world of THREE.js
, I experimented with various geometries. However, manually writing out each geometry became tedious due to the vast array of options available.
For example, creating a simple cube required these lines of code:
var material = new THREE.MeshNormalMaterial();
var boxGeometry = new THREE.BoxGeometry( 20, 20, 20 );
var box = new THREE.Mesh( boxGeometry, material );
box.position.set(-10, -10, 0);
scene.add( box );
If I wanted to add another shape like a cone, I would have to mindlessly copy and paste the above lines, only replacing Box
with Cone
. This monotonous task seemed more suited for a computer than a human.
This led me to consider creating a generic class
that would simplify this process:
var cube = new Shape('Cube', 20, 20, 20);
// or
var cone = new Shape('Cone', 20, 30);
I envisioned a scenario where this class could automate most of the work for me. Although I could extract the arguments from the function, I pondered on how to convert them from strings to logical statements efficiently.