Attempting to display a cube
generated in ThreeJS on an HTML canvas
at a specific location has been quite challenging.
The location is defined in pixels, for example (100,200). Unfortunately, the process of converting these pixel values into ThreeJS coordinates remains elusive.
// Dimensions of my canvas are 640 X 480
const CANVAS_WIDTH = 640;
const CANVAS_HEIGHT = 480;
// Create a cube and add it to the scene
let geometry = new THREE.CubeGeometry(1, 1, 1);
let materials = createMaterials();
this.cube = new THREE.Mesh(geometry, materials);
this.cube.doubleSided = true;
this.scene.add(this.cube);
// Set up the camera
this.camera = new THREE.PerspectiveCamera(45, CANVAS_WIDTH / CANVAS_HEIGHT, 1, 5000);
this.camera.position.z = CANVAS_WIDTH / 2;
this.scene.add(this.camera);
An interesting discovery was made that the origin (0,0) in ThreeJS corresponds to the center of the screen.
If anyone has insights on how to translate 2D coordinates from the canvas into 3D coordinates compatible with ThreeJS, kindly share your knowledge. Any assistance would be greatly appreciated.