How to capture an image of THREE.Scene uning THREE.PerspectiveCamera? - javascript

I was looking online, but I didn’t found much. I’m basically looking for the camera function that renders an image when a button is pressed.
Notice that I have 2 THREE.PerspectiveCamera (main and 2nd). The main camera is the one I use for the OrbitControls. and the 2nd is for capturing the image.
This is how I declare the Three.PerspectiveCamera:
camera_RT = new THREE.PerspectiveCamera(20, window.innerWidth / window.innerHeight, -100, -1000);
camera_RT.position.set( //Set 2nd camera's position according to its parent
camera_RT.position.x,
camera_RT.position.y,
camera_RT_Holder.position.z
);
camera_RT.updateMatrixWorld(); //Update camera's Matrix Wolrd (location in the scene)
//Add the Camera to the camera Holder (parent objects)
camera_RT_Holder.add(camera_RT);
//Create 2nd Camera Helper (View volume)
camera_RT_Helper = new THREE.CameraHelper( camera_RT ); //Create camera helper
scene_Main.add( camera_RT_Helper );//Add the camera helper to the scene
UPDATE:
function saveImage(){
let imgData;
var final_Image;
try {
noLoop(); //STOP p5JS ITERATION - draw() function is not called
camera_RT.imageData = renderer_Main.domElement.toDataURL();
imgData = camera_RT.imageData;
console.log(imgData); //CONSOLE PRINTS : data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAABD0AAAJFCAYAAADeVYzuAAAgAElEQVR4Xu3YwQ0AMAwCMbr/0K3UMU7OBjG8ONvuHAECBAgQIECAAAECBAgQIEAgJnCMHrFEvUOAAAECBAgQIECAAAECBAh8AaOHIhAgQIAAAQIECBAgQIAAAQJJAaNHMlZPESBAgAABAgQIECBAgAABAkYPHSBAgAABAgQIECBAgAABAgSSAkaPZKyeIkCAAAECBAgQIECAAAECBIweOkCAAAECBAgQIECAAAECBAgkBYweyVg9RYAAAQIECBAgQIAAAQIECBg9dIAAAQIECBAgQIAAAQIECBBIChg9krF6igABAgQIECBAgAABAgQIEDB66AABAgQIECBAgAABAgQIECCQFDB6JGP1FAECBAgQIECAAAECBAgQIGD00AECBAgQIECAAAECBAgQIEAgKWD0SMbqKQIECBAgQIAAAQIECBAgQMDooQMECBAgQIAAAQIECBAgQIBAUsDokYzVUwQIECBAgAABAgQIECBAgIDRQwcIECBAgAABAgQIECBAgACBpIDRIxmrpwgQIECAAAECBAgQIECAAAGjhw4QIECAAAECBAgQIECAAAECSQGjRzJWTxEgQIAAAQIECBAgQIAAAQJGDx0gQIAAAQIECBAgQIAAAQIEkgJGj2SsniJAgAABAgQIECBAgAABAgSMHjpAgAABAgQIECBAgAABAgQIJAWMHslYPUWAAAECBAgQIECAAAECBAgYPXSAAAECBAgQIECAAAECBAgQSAoYPZKxeooAAQIECBAgQIAAAQIECBAweugAAQIECBAgQIAAAQIECBAgkBQweiRj9RQBAgQIECBAgAABAgQIECBg9NABAgQIECBAgAABAgQIECBAIClg9EjG6ikCBAgQIECAAAECBAgQIEDA6KEDBAgQIECAAAECBAgQIECAQFLA6JGM1VMECBAgQIAAAQIECBAgQICA0UMHCBAgQIAAAQIECBAgQIAAgaSA0SMZq6cIECBAgAABAgQIECBAgAABo4cOECBAgAABAgQIECBAgAABAkkBo0cyVk8RIECAAAECBAgQIECAAAECRg8dIECAAAECBAgQIECAAAECBJICRo9krJ4iQIAAAQIECBAgQIAAAQIEjB46QIAAAQIECBAgQIAAAQIECCQFjB7JWD1FgAABAgQIECBAgAABAgQIGD10gAABAgQIECBAgAABAgQIEEgKGD2SsXqKAAECBAgQIECAAAECBAgQMHroAAECBAgQIECAAAECBAgQIJAUMHokY/UUAQIECBAgQIAAAQIECBAgYPTQAQIECBAgQIAAAQIECBAgQCApYPRIxuopAgQIECBAgAABAgQIECBAwOihAwQIECBAgAABAgQIECBAgEBSwOiRjNVTBAgQIECAAAECBAgQIECAgNFDBwgQIECAAAECBAgQIECAAIGkgNEjGaunCBAgQIAAAQIECBAgQIAAAaOHDhAgQIAAAQIECBAgQIAAAQJJAaNHMlZPESBAgAABAgQIECBAgAABAkYPHSBAgAABAgQIECBAgAABAgSSAkaPZKyeIkCAAAECBAgQIECAAAECBIweOkCAAAECBAgQIECAAAECBAgkBYweyVg9RYAAAQIECBAgQIAAAQIECBg9dIAAAQIECBAgQIAAAQIECBBIChg9krF6igABAgQIECBAgAABAgQIEDB66AABAgQIECBAgAABAgQIECCQFDB6JGP1FAECBAgQIECAAAECBAgQIGD00AECBAgQIECAAAECBAgQIEAgKWD0SMbqKQIECBAgQIAAAQIECBAgQMDooQMECBAgQIAAAQIECBAgQIBAUsDokYzVUwQIECBAgAABAgQIECBAgIDRQwcIECBAgAABAgQIECBAgACBpIDRIxmrpwgQIECAAAECBAgQIECAAAGjhw4QIECAAAECBAgQIECAAAECSQGjRzJWTxEgQIAAAQIECBAgQIAAAQJGDx0gQIAAAQIECBAgQIAAAQIEkgJGj2SsniJAgAABAgQIECBAgAABAgSMHjpAgAABAgQIECBAgAABAgQIJAWMHslYPUWAAAECBAgQIECAAAECBAgYPXSAAAECBAgQIECAAAECBAgQSAoYPZKxeooAAQIECBAgQIAAAQIECBAweugAAQIECBAgQIAAAQIECBAgkBQweiRj9RQBAgQIECBAgAABAgQIECBg9NABAgQIECBAgAABAgQIECBAIClg9EjG6ikCBAgQIECAAAECBAgQIEDA6KEDBAgQIECAAAECBAgQIECAQFLA6JGM1VMECBAgQIAAAQIECBAgQICA0UMHCBAgQIAAAQIECBAgQIAAgaSA0SMZq6cIECBAgAABAgQIECBAgAABo4cOECBAgAABAgQIECBAgAABAkkBo0cyVk8RIECAAAECBAgQIECAAAECRg8dIECAAAECBAgQIECAAAECBJICRo9krJ4iQIAAAQIECBAgQIAAAQIEjB46QIAAAQIECBAgQIAAAQIECCQFjB7JWD1FgAABAgQIECBAgAABAgQIGD10gAABAgQIECBAgAABAgQIEEgKGD2SsXqKAAECBAgQIECAAAECBAgQMHroAAECBAgQIECAAAECBAgQIJAUMHokY/UUAQIECBAgQIAAAQIECBAgYPTQAQIECBAgQIAAAQIECBAgQCApYPRIxuopAgQIECBAgAABAgQIECBAwOihAwQIECBAgAABAgQIECBAgEBSwOiRjNVTBAgQIECAAAECBAgQIECAgNFDBwgQIECAAAECBAgQIECAAIGkgNEjGaunCBAgQIAAAQIECBAgQIAAAaOHDhAgQIAAAQIECBAgQIAAAQJJAaNHMlZPESBAgAABAgQIECBAgAABAkYPHSBAgAABAgQIECBAgAABAgSSAkaPZKyeIkCAAAECBAgQIECAAAECBIweOkCAAAECBAgQIECAAAECBAgkBYweyVg9RYAAAQIECBAgQIAAAQIECBg9dIAAAQIECBAgQIAAAQIECBBIChg9krF6igABAgQIECBAgAABAgQIEDB66AABAgQIECBAgAABAgQIECCQFDB6JGP1FAECBAgQIECAAAECBAgQIGD00AECBAgQIECAAAECBAgQIEAgKWD0SMbqKQIECBAgQIAAAQIECBAgQMDooQMECBAgQIAAAQIECBAgQIBAUsDokYzVUwQIECBAgAABAgQIECBAgIDRQwcIECBAgAABAgQIECBAgACBpIDRIxmrpwgQIECAAAECBAgQIECAAAGjhw4QIECAAAECBAgQIECAAAECSQGjRzJWTxEgQIAAAQIECBAgQIAAAQJGDx0gQIAAAQIECBAgQIAAAQIEkgJGj2SsniJAgAABAgQIECBAgAABAgSMHjpAgAABAgQIECBAgAABAgQIJAWMHslYPUWAAAECBAgQIECAAAECBAgYPXSAAAECBAgQIECAAAECBAgQSAoYPZKxeooAAQIECBAgQIAAAQIECBAweugAAQIECBAgQIAAAQIECBAgkBQweiRj9RQBAgQIECBAgAABAgQIECBg9NABAgQIECBAgAABAgQIECBAIClg9EjG6ikCBAgQIECAAAECBAgQIEDA6KEDBAgQIECAAAECBAgQIECAQFLA6JGM1VMECBAgQIAAAQIECBAgQICA0UMHCBAgQIAAAQIECBAgQIAAgaSA0SMZq6cIECBAgAABAgQIECBAgAABo4cOECBAgAABAgQIECBAgAABAkkBo0cyVk8RIECAAAECBAgQIECAAAECRg8dIECAAAECBAgQIECAAAECBJICRo9krJ4iQIAAAQIECBAgQIAAAQIEjB46QIAAAQIECBAgQIAAAQIECCQFjB7JWD1FgAABAgQIECBAgAABAgQIGD10gAABAgQIECBAgAABAgQIEEgKGD2SsXqKAAECBAgQIECAAAECBAgQMHroAAECBAgQIECAAAECBAgQIJAUMHokY/UUAQIECBAgQIAAAQIECBAgYPTQAQIECBAgQIAAAQIECBAgQCApYPRIxuopAgQIECBAgAABAgQIECBAwOihAwQIECBAgAABAgQIECBAgEBSwOiRjNVTBAgQIECAAAECBAgQIECAgNFDBwgQIECAAAECBAgQIECAAIGkgNEjGaunCBAgQIAAAQIECBAgQIAAAaOHDhAgQIAAAQIECBAgQIAAAQJJAaNHMlZPESBAgAABAgQIECBAgAABAkYPHSBAgAABAgQIECBAgAABAgSSAkaPZKyeIkCAAAECBAgQIECAAAECBIweOkCAAAECBAgQIECAAAECBAgkBYweyVg9RYAAAQIECBAgQIAAAQIECBg9dIAAAQIECBAgQIAAAQIECBBIChg9krF6igABAgQIECBAgAABAgQIEDB66AABAgQIECBAgAABAgQIECCQFDB6JGP1FAECBAgQIECAAAECBAgQIGD00AECBAgQIECAAAECBAgQIEAgKWD0SMbqKQIECBAgQIAAAQIECBAgQMDooQMECBAgQIAAAQIECBAgQIBAUsDokYzVUwQIECBAgAABAgQIECBAgIDRQwcIECBAgAABAgQIECBAgACBpIDRIxmrpwgQIECAAAECBAgQIECAAAGjhw4QIECAAAECBAgQIECAAAECSQGjRzJWTxEgQIAAAQIECBAgQIAAAQJGDx0gQIAAAQIECBAgQIAAAQIEkgJGj2SsniJAgAABAgQIECBAgAABAgSMHjpAgAABAgQIECBAgAABAgQIJAWMHslYPUWAAAECBAgQIECAAAECBAgYPXSAAAECBAgQIECAAAECBAgQSAoYPZKxeooAAQIECBAgQIAAAQIECBAweugAAQIECBAgQIAAAQIECBAgkBQweiRj9RQBAgQIECBAgAABAgQIECBg9NABAgQIECBAgAABAgQIECBAIClg9EjG6ikCBAgQIECAAAECBAgQIEDA6KEDBAgQIECAAAECBAgQIECAQFLA6JGM1VMECBAgQIAAAQIECBAgQICA0UMHCBAgQIAAAQIECBAgQIAAgaSA0SMZq6cIECBAgAABAgQIECBAgAABo4cOECBAgAABAgQIECBAgAABAkkBo0cyVk8RIECAAAECBAgQIECAAAECRg8dIECAAAECBAgQIECAAAECBJICRo9krJ4iQIAAAQIECBAgQIAAAQIEjB46QIAAAQIECBAgQIAAAQIECCQFjB7JWD1FgAABAgQIECBAgAABAgQIGD10gAABAgQIECBAgAABAgQIEEgKGD2SsXqKAAECBAgQIECAAAECBAgQMHroAAECBAgQIECAAAECBAgQIJAUMHokY/UUAQIECBAgQIAAAQIECBAgYPTQAQIECBAgQIAAAQIECBAgQCApYPRIxuopAgQIECBAgAABAgQIECBAwOihAwQIECBAgAABAgQIECBAgEBSwOiRjNVTBAgQIECAAAECBAgQIECAgNFDBwgQIECAAAECBAgQIECAAIGkgNEjGaunCBAgQIAAAQIECBAgQIAAAaOHDhAgQIAAAQIECBAgQIAAAQJJAaNHMlZPESBAgAABAgQIECBAgAABAkYPHSBAgAABAgQIECBAgAABAgSSAkaPZKyeIkCAAAECBAgQIECAAAECBIweOkCAAAECBAgQIECAAAECBAgkBYweyVg9RYAAAQIECBAgQIAAAQIECBg9dIAAAQIECBAgQIAAAQIECBBIChg9krF6igABAgQIECBAgAABAgQIEDB66AABAgQIECBAgAABAgQIECCQFDB6JGP1FAECBAgQIECAAAECBAgQIGD00AECBAgQIECAAAECBAgQIEAgKWD0SMbqKQIECBAgQIAAAQIECBAgQMDooQMECBAgQIAAAQIECBAgQIBAUsDokYzVUwQIECBAgAABAgQIECBAgIDRQwcIECBAgAABAgQIECBAgACBpIDRIxmrpwgQIECAAAECBAgQIECAAAGjhw4QIECAAAECBAgQIECAAAECSQGjRzJWTxEgQIAAAQIECBAgQIAAAQJGDx0gQIAAAQIECBAgQIAAAQIEkgJGj2SsniJAgAABAgQIECBAgAABAgSMHjpAgAABAgQIECBAgAABAgQIJAWMHslYPUWAAAECBAgQIECAAAECBAgYPXSAAAECBAgQIECAAAECBAgQSAoYPZKxeooAAQIECBAgQIAAAQIECBAweugAAQIECBAgQIAAAQIECBAgkBQweiRj9RQBAgQIECBAgAABAgQIECBg9NABAgQIECBAgAABAgQIECBAIClg9EjG6ikCBAgQIECAAAECBAgQIEDA6KEDBAgQIECAAAECBAgQIECAQFLA6JGM1VMECBAgQIAAAQIECBAgQICA0UMHCBAgQIAAAQIECBAgQIAAgaSA0SMZq6cIECBAgAABAgQIECBAgAABo4cOECBAgAABAgQIECBAgAABAkkBo0cyVk8RIECAAAECBAgQIECAAAECRg8dIECAAAECBAgQIECAAAECBJICRo9krJ4iQIAAAQIECBAgQIAAAQIEjB46QIAAAQIECBAgQIAAAQIECCQFjB7JWD1FgAABAgQIECBAgAABAgQIGD10gAABAgQIECBAgAABAgQIEEgKGD2SsXqKAAECBAgQIECAAAECBAgQMHroAAECBAgQIECAAAECBAgQIJAUMHokY/UUAQIECBAgQIAAAQIECBAgYPTQAQIECBAgQIAAAQIECBAgQCApYPRIxuopAgQIECBAgAABAgQIECBAwOihAwQIECBAgAABAgQIECBAgEBSwOiRjNVTBAgQIECAAAECBAgQIECAgNFDBwgQIECAAAECBAgQIECAAIGkgNEjGaunCBAgQIAAAQIECBAgQIAAAaOHDhAgQIAAAQIECBAgQIAAAQJJAaNHMlZPESBAgAABAgQIECBAgAABAkYPHSBAgAABAgQIECBAgAABAgSSAkaPZKyeIkCAAAECBAgQIECAAAECBIweOkCAAAECBAgQIECAAAECBAgkBYweyVg9RYAAAQIECBAgQIAAAQIECBg9dIAAAQIECBAgQIAAAQIECBBIChg9krF6igABAgQIECBAgAABAgQIEDB66AABAgQIECBAgAABAgQIECCQFDB6JGP1FAECBAgQIECAAAECBAgQIGD00AECBAgQIECAAAECBAgQIEAgKWD0SMbqKQIECBAgQIAAAQIECBAgQMDooQMECBAgQIAAAQIECBAgQIBAUsDokYzVUwQIECBAgAABAgQIECBAgIDRQwcIECBAgAABAgQIECBAgACBpIDRIxmrpwgQIECAAAECBAgQIECAAAGjhw4QIECAAAECBAgQIECAAAECSQGjRzJWTxEgQIAAAQIECBAgQIAAAQJGDx0gQIAAAQIECBAgQIAAAQIEkgJGj2SsniJAgAABAgQIECBAgAABAgSMHjpAgAABAgQIECBAgAABAgQIJAWMHslYPUWAAAECBAgQIECAAAECBAgYPXSAAAECBAgQIECAAAECBAgQSAoYPZKxeooAAQIECBAgQIAAAQIECBAweugAAQIECBAgQIAAAQIECBAgkBQweiRj9RQBAgQIECBAgAABAgQIECBg9NABAgQIECBAgAABAgQIECBAIClg9EjG6ikCBAgQIECAAAECBAgQIEDA6KEDBAgQIECAAAECBAgQIECAQFLA6JGM1VMECBAgQIAAAQIECBAgQICA0UMHCBAgQIAAAQIECBAgQIAAgaSA0SMZq6cIECBAgAABAgQIECBAgAABo4cOECBAgAABAgQIECBAgAABAkkBo0cyVk8RIECAAAECBAgQIECAAAECRg8dIECAAAECBAgQIECAAAECBJICRo9krJ4iQIAAAQIECBAgQIAAAQIEjB46QIAAAQIECBAgQIAAAQIECCQFjB7JWD1FgAABAgQIECBAgAABAgQIGD10gAABAgQIECBAgAABAgQIEEgKGD2SsXqKAAECBAgQIECAAAECBAgQMHroAAECBAgQIECAAAECBAgQIJAUMHokY/UUAQIECBAgQIAAAQIECBAgYPTQAQIECBAgQIAAAQIECBAgQCApYPRIxuopAgQIECBAgAABAgQIECBAwOihAwQIECBAgAABAgQIECBAgEBSwOiRjNVTBAgQIECAAAECBAgQIECAgNFDBwgQIECAAAECBAgQIECAAIGkgNEjGaunCBAgQIAAAQIECBAgQIAAAaOHDhAgQIAAAQIECBAgQIAAAQJJAaNHMlZPESBAgAABAgQIECBAgAABAkYPHSBAgAABAgQIECBAgAABAgSSAkaPZKyeIkCAAAECBAgQIECAAAECBIweOkCAAAECBAgQIECAAAECBAgkBYweyVg9RYAAAQIECBAgQIAAAQIECBg9dIAAAQIECBAgQIAAAQIECBBIChg9krF6igABAgQIECBAgAABAgQIEDB66AABAgQIECBAgAABAgQIECCQFDB6JGP1FAECBAgQIECAAAECBAgQIGD00AECBAgQIECAAAECBAgQIEAgKWD0SMbqKQIECBAgQIAAAQIECBAgQMDooQMECBAgQIAAAQIECBAgQIBAUsDokYzVUwQIECBAgAABAgQIECBAgIDRQwcIECBAgAABAgQIECBAgACBpIDRIxmrpwgQIECAAAECBAgQIECAAAGjhw4QIECAAAECBAgQIECAAAECSQGjRzJWTxEgQIAAAQIECBAgQIAAAQJGDx0gQIAAAQIECBAgQIAAAQIEkgJGj2SsniJAgAABAgQIECBAgAABAgSMHjpAgAABAgQIECBAgAABAgQIJAWMHslYPUWAAAECBAgQIECAAAECBAgYPXSAAAECBAgQIECAAAECBAgQSAoYPZKxeooAAQIECBAgQIAAAQIECBAweugAAQIECBAgQIAAAQIECBAgkBQweiRj9RQBAgQIECBAgAABAgQIECBg9NABAgQIECBAgAABAgQIECBAIClg9EjG6ikCBAgQIECAAAECBAgQIEDA6KEDBAgQIECAAAECBAgQIECAQFLA6JGM1VMECBAgQIAAAQIECBAgQICA0UMHCBAgQIAAAQIECBAgQIAAgaSA0SMZq6cIECBAgAABAgQIECBAgAABo4cOECBAgAABAgQIECBAgAABAkkBo0cyVk8RIECAAAECBAgQIECAAAECRg8dIECAAAECBAgQIECAAAECBJICRo9krJ4iQIAAAQIECBAgQIAAAQIEjB46QIAAAQIECBAgQIAAAQIECCQFjB7JWD1FgAABAgQIECBAgAABAgQIGD10gAABAgQIECBAgAABAgQIEEgKGD2SsXqKAAECBAgQIECAAAECBAgQMHroAAECBAgQIECAAAECBAgQIJAUMHokY/UUAQIECBAgQIAAAQIECBAgYPTQAQIECBAgQIAAAQIECBAgQCApYPRIxuopAgQIECBAgAABAgQIECBAwOihAwQIECBAgAABAgQIECBAgEBSwOiRjNVTBAgQIECAAAECBAgQIECAgNFDBwgQIECAAAECBAgQIECAAIGkgNEjGaunCBAgQIAAAQIECBAgQIAAAaOHDhAgQIAAAQIECBAgQIAAAQJJAaNHMlZPESBAgAABAgQIECBAgAABAkYPHSBAgAABAgQIECBAgAABAgSSAkaPZKyeIkCAAAECBAgQIECAAAECBIweOkCAAAECBAgQIECAAAECBAgkBYweyVg9RYAAAQIECBAgQIAAAQIECBg9dIAAAQIECBAgQIAAAQIECBBIChg9krF6igABAgQIECBAgAABAgQIEDB66AABAgQIECBAgAABAgQIECCQFDB6JGP1FAECBAgQIECAAAECBAgQIGD00AECBAgQIECAAAECBAgQIEAgKWD0SMbqKQIECBAgQIAAAQIECBAgQMDooQMECBAgQIAAAQIECBAgQIBAUsDokYzVUwQIECBAgAABAgQIECBAgIDRQwcIECBAgAABAgQIECBAgACBpIDRIxmrpwgQIECAAAECBAgQIECAAAGjhw4QIECAAAECBAgQIECAAAECSQGjRzJWTxEgQIAAAQIECBAgQIAAAQJGDx0gQIAAAQIECBAgQIAAAQIEkgJGj2SsniJAgAABAgQIECBAgAABAgSMHjpAgAABAgQIECBAgAABAgQIJAWMHslYPUWAAAECBAgQIECAAAECBAgYPXSAAAECBAgQIECAAAECBAgQSAoYPZKxeooAAQIECBAgQIAAAQIECBAweugAAQIECBAgQIAAAQIECBAgkBQweiRj9RQBAgQIECBAgAABAgQIECBg9NABAgQIECBAgAABAgQIECBAIClg9EjG6ikCBAgQIECAAAECBAgQIEDA6KEDBAgQIECAAAECBAgQIECAQFLA6JGM1VMECBAgQIAAAQIECBAgQICA0UMHCBAgQIAAAQIECBAgQIAAgaSA0SMZq6cIECBAgAABAgQIECBAgAABo4cOECBAgAABAgQIECBAgAABAkkBo0cyVk8RIECAAAECBAgQIECAAAECRg8dIECAAAECBAgQIECAAAECBJICRo9krJ4iQIAAAQIECBAgQIAAAQIEjB46QIAAAQIECBAgQIAAAQIECCQFjB7JWD1FgAABAgQIECBAgAABAgQIGD10gAABAgQIECBAgAABAgQIEEgKGD2SsXqKAAECBAgQIECAAAECBAgQMHroAAECBAgQIECAAAECBAgQIJAUMHokY/UUAQIECBAgQIAAAQIECBAgYPTQAQIECBAgQIAAAQIECBAgQCApYPRIxuopAgQIECBAgAABAgQIECBAwOihAwQIECBAgAABAgQIECBAgEBSwOiRjNVTBAgQIECAAAECBAgQIECAgNFDBwgQIECAAAECBAgQIECAAIGkgNEjGaunCBAgQIAAAQIECBAgQIAAAaOHDhAgQIAAAQIECBAgQIAAAQJJAaNHMlZPESBAgAABAgQIEAWdyI4AAA/sSURBVCBAgAABAkYPHSBAgAABAgQIECBAgAABAgSSAkaPZKyeIkCAAAECBAgQIECAAAECBIweOkCAAAECBAgQIECAAAECBAgkBYweyVg9RYAAAQIECBAgQIAAAQIECBg9dIAAAQIECBAgQIAAAQIECBBIChg9krF6igABAgQIECBAgAABAgQIEDB66AABAgQIECBAgAABAgQIECCQFDB6JGP1FAECBAgQIECAAAECBAgQIGD00AECBAgQIECAAAECBAgQIEAgKWD0SMbqKQIECBAgQIAAAQIECBAgQMDooQMECBAgQIAAAQIECBAgQIBAUsDokYzVUwQIECBAgAABAgQIECBAgIDRQwcIECBAgAABAgQIECBAgACBpIDRIxmrpwgQIECAAAECBAgQIECAAAGjhw4QIECAAAECBAgQIECAAAECSQGjRzJWTxEgQIAAAQIECBAgQIAAAQJGDx0gQIAAAQIECBAgQIAAAQIEkgJGj2SsniJAgAABAgQIECBAgAABAgSMHjpAgAABAgQIECBAgAABAgQIJAWMHslYPUWAAAECBAgQIECAAAECBAgYPXSAAAECBAgQIECAAAECBAgQSAoYPZKxeooAAQIECBAgQIAAAQIECBAweugAAQIECBAgQIAAAQIECBAgkBQweiRj9RQBAgQIECBAgAABAgQIECBg9NABAgQIECBAgAABAgQIECBAIClg9EjG6ikCBAgQIECAAAECBAgQIEDA6KEDBAgQIECAAAECBAgQIECAQFLA6JGM1VMECBAgQIAAAQIECBAgQICA0UMHCBAgQIAAAQIECBAgQIAAgaSA0SMZq6cIECBAgAABAgQIECBAgAABo4cOECBAgAABAgQIECBAgAABAkkBo0cyVk8RIECAAAECBAgQIECAAAECRg8dIECAAAECBAgQIECAAAECBJICRo9krJ4iQIAAAQIECBAgQIAAAQIEjB46QIAAAQIECBAgQIAAAQIECCQFjB7JWD1FgAABAgQIECBAgAABAgQIGD10gAABAgQIECBAgAABAgQIEEgKGD2SsXqKAAECBAgQIECAAAECBAgQMHroAAECBAgQIECAAAECBAgQIJAUMHokY/UUAQIECBAgQIAAAQIECBAgYPTQAQIECBAgQIAAAQIECBAgQCApYPRIxuopAgQIECBAgAABAgQIECBAwOihAwQIECBAgAABAgQIECBAgEBSwOiRjNVTBAgQIECAAAECBAgQIECAgNFDBwgQIECAAAECBAgQIECAAIGkgNEjGaunCBAgQIAAAQIECBAgQIAAAaOHDhAgQIAAAQIECBAgQIAAAQJJAaNHMlZPESBAgAABAgQIECBAgAABAkYPHSBAgAABAgQIECBAgAABAgSSAkaPZKyeIkCAAAECBAgQIECAAAECBIweOkCAAAECBAgQIECAAAECBAgkBYweyVg9RYAAAQIECBAgQIAAAQIECBg9dIAAAQIECBAgQIAAAQIECBBIChg9krF6igABAgQIECBAgAABAgQIEDB66AABAgQIECBAgAABAgQIECCQFDB6JGP1FAECBAgQIECAAAECBAgQIGD00AECBAgQIECAAAECBAgQIEAgKWD0SMbqKQIECBAgQIAAAQIECBAgQMDooQMECBAgQIAAAQIECBAgQIBAUsDokYzVUwQIECBAgAABAgQIECBAgIDRQwcIECBAgAABAgQIECBAgACBpIDRIxmrpwgQIECAAAECBAgQIECAAAGjhw4QIECAAAECBAgQIECAAAECSQGjRzJWTxEgQIAAAQIECBAgQIAAAQJGDx0gQIAAAQIECBAgQIAAAQIEkgJGj2SsniJAgAABAgQIECBAgAABAgSMHjpAgAABAgQIECBAgAABAgQIJAWMHslYPUWAAAECBAgQIECAAAECBAgYPXSAAAECBAgQIECAAAECBAgQSAoYPZKxeooAAQIECBAgQIAAAQIECBAweugAAQIECBAgQIAAAQIECBAgkBQweiRj9RQBAgQIECBAgAABAgQIECBg9NABAgQIECBAgAABAgQIECBAIClg9EjG6ikCBAgQIECAAAECBAgQIEDA6KEDBAgQIECAAAECBAgQIECAQFLA6JGM1VMECBAgQIAAAQIECBAgQICA0UMHCBAgQIAAAQIECBAgQIAAgaSA0SMZq6cIECBAgAABAgQIECBAgAABo4cOECBAgAABAgQIECBAgAABAkkBo0cyVk8RIECAAAECBAgQIECAAAECRg8dIECAAAECBAgQIECAAAECBJICRo9krJ4iQIAAAQIECBAgQIAAAQIEjB46QIAAAQIECBAgQIAAAQIECCQFjB7JWD1FgAABAgQIECBAgAABAgQIGD10gAABAgQIECBAgAABAgQIEEgKGD2SsXqKAAECBAgQIECAAAECBAgQMHroAAECBAgQIECAAAECBAgQIJAUMHokY/UUAQIECBAgQIAAAQIECBAgYPTQAQIECBAgQIAAAQIECBAgQCApYPRIxuopAgQIECBAgAABAgQIECBAwOihAwQIECBAgAABAgQIECBAgEBSwOiRjNVTBAgQIECAAAECBAgQIECAgNFDBwgQIECAAAECBAgQIECAAIGkgNEjGaunCBAgQIAAAQIECBAgQIAAAaOHDhAgQIAAAQIECBAgQIAAAQJJAaNHMlZPESBAgAABAgQIECBAgAABAkYPHSBAgAABAgQIECBAgAABAgSSAkaPZKyeIkCAAAECBAgQIECAAAECBIweOkCAAAECBAgQIECAAAECBAgkBYweyVg9RYAAAQIECBAgQIAAAQIECBg9dIAAAQIECBAgQIAAAQIECBBIChg9krF6igABAgQIECBAgAABAgQIEDB66AABAgQIECBAgAABAgQIECCQFDB6JGP1FAECBAgQIECAAAECBAgQIGD00AECBAgQIECAAAECBAgQIEAgKWD0SMbqKQIECBAgQIAAAQIECBAgQMDooQMECBAgQIAAAQIECBAgQIBAUsDokYzVUwQIECBAgAABAgQIECBAgIDRQwcIECBAgAABAgQIECBAgACBpIDRIxmrpwgQIECAAAECBAgQIECAAAGjhw4QIECAAAECBAgQIECAAAECSQGjRzJWTxEgQIAAAQIECBAgQIAAAQJGDx0gQIAAAQIECBAgQIAAAQIEkgJGj2SsniJAgAABAgQIECBAgAABAgSMHjpAgAABAgQIECBAgAABAgQIJAWMHslYPUWAAAECBAgQIECAAAECBAgYPXSAAAECBAgQIECAAAECBAgQSAoYPZKxeooAAQIECBAgQIAAAQIECBAweugAAQIECBAgQIAAAQIECBAgkBQweiRj9RQBAgQIECBAgAABAgQIECBg9NABAgQIECBAgAABAgQIECBAIClg9EjG6ikCBAgQIECAAAECBAgQIEDA6KEDBAgQIECAAAECBAgQIECAQFLA6JGM1VMECBAgQIAAAQIECBAgQICA0UMHCBAgQIAAAQIECBAgQIAAgaSA0SMZq6cIECBAgAABAgQIECBAgAABo4cOECBAgAABAgQIECBAgAABAkkBo0cyVk8RIECAAAECBAgQIECAAAECRg8dIECAAAECBAgQIECAAAECBJICRo9krJ4iQIAAAQIECBAgQIAAAQIEjB46QIAAAQIECBAgQIAAAQIECCQFjB7JWD1FgAABAgQIECBAgAABAgQIGD10gAABAgQIECBAgAABAgQIEEgKGD2SsXqKAAECBAgQIECAAAECBAgQMHroAAECBAgQIECAAAECBAgQIJAUMHokY/UUAQIECBAgQIAAAQIECBAgYPTQAQIECBAgQIAAAQIECBAgQCApYPRIxuopAgQIECBAgAABAgQIECBAwOihAwQIECBAgAABAgQIECBAgEBSwOiRjNVTBAgQIECAAAECBAgQIECAgNFDBwgQIECAAAECBAgQIECAAIGkgNEjGaunCBAgQIAAAQIECBAgQIAAAaOHDhAgQIAAAQIECBAgQIAAAQJJAaNHMlZPESBAgAABAgQIECBAgAABAkYPHSBAgAABAgQIECBAgAABAgSSAkaPZKyeIkCAAAECBAgQIECAAAECBIweOkCAAAECBAgQIECAAAECBAgkBYweyVg9RYAAAQIECBAgQIAAAQIECBg9dIAAAQIECBAgQIAAAQIECBBIChg9krF6igABAgQIECBAgAABAgQIEDB66AABAgQIECBAgAABAgQIECCQFDB6JGP1FAECBAgQIECAAAECBAgQIGD00AECBAgQIECAAAECBAgQIEAgKWD0SMbqKQIECBAgQIAAAQIECBAgQMDooQMECBAgQIAAAQIECBAgQIBAUsDokYzVUwQIECBAgAABAgQIECBAgIDRQwcIECBAgAABAgQIECBAgACBpIDRIxmrpwgQIECAAAECBAgQIECAAAGjhw4QIECAAAECBAgQIECAAAECSQGjRzJWTxEgQIAAAQIECBAgQIAAAQJGDx0gQIAAAQIECBAgQIAAAQIEkgJGj2SsniJAgAABAgQIECBAgAABAgSMHjpAgAABAgQIECBAgAABAgQIJAWMHslYPUWAAAECBAgQIECAAAECBAgYPXSAAAECBAgQIECAAAECBAgQSAoYPZKxeooAAQIECBAgQIAAAQIECBAweugAAQIECBAgQIAAAQIECBAgkBQweiRj9RQBAgQIECBAgAABAgQIECBg9NABAgQIECBAgAABAgQIECBAIClg9EjG6ikCBAgQIECAAAECBAgQIEDA6KEDBAgQIECAAAECBAgQIECAQFLA6JGM1VMECBAgQIAAAQIECBAgQICA0UMHCBAgQIAAAQIECBAgQIAAgaSA0SMZq6cIECBAgAABAgQIECBAgAABo4cOECBAgAABAgQIECBAgAABAkkBo0cyVk8RIECAAAECBAgQIECAAAECRg8dIECAAAECBAgQIECAAAECBJICRo9krJ4iQIAAAQIECBAgQIAAAQIEjB46QIAAAQIECBAgQIAAAQIECCQFjB7JWD1FgAABAgQIECBAgAABAgQIGD10gAABAgQIECBAgAABAgQIEEgKGD2SsXqKAAECBAgQIECAAAECBAgQMHroAAECBAgQIECAAAECBAgQIJAUMHokY/UUAQIECBAgQIAAAQIECBAgYPTQAQIECBAgQIAAAQIECBAgQCApYPRIxuopAgQIECBAgAABAgQIECBAwOihAwQIECBAgAABAgQIECBAgEBSwOiRjNVTBAgQIECAAAECBAgQIECAgNFDBwgQIECAAAECBAgQIECAAIGkgNEjGaunCBAgQIAAAQIECBAgQIAAAaOHDhAgQIAAAQIECBAgQIAAAQJJAaNHMlZPESBAgAABAgQIECBAgAABAkYPHSBAgAABAgQIECBAgAABAgSSAg8FMUUfXYyMwAAAAABJRU5ErkJggg==
final_Image = createImg(imgData);
final_Image.attribute("id", "finalImage");
final_Image.attribute("style", "display:block; margin:20px auto;");
final_Image.parent(canvas_Parent);
//Hide Scene_Main and disable slider
document.getElementById("canvas_3d_element").style.display = "none";
document.getElementById("slider-element").disabled = true;
stage_9_declared = true;
saveImageRequest = true;
} catch (e) {
console.log(e);
return;
}
}
function animate() {
...
if(!stage_9_declared && !saveImageRequest){
console.log(saveImageRequest);
this.render(); //Render Scene and Camera every frame
}
}
function render() {
//Render only if the camera_Main is declared
if(camera_Main_declared){
renderer_Main.render(scene_Main, camera_Main);
}
}

Here is an example of taking a screenshot.
To change which camera the screenshot is taken from, you would just change which camera you send to the renderer.render() function.
In this example I only have one camera though.
UPDATE: The screenshot button in this example code may not actually work in the demo as it is in an iframe on stackoverflow (in Chrome, and maybe other browsers) because of this.
But here is a jsfiddle demo
var camera, scene, renderer, mesh, material;
init();
animate();
function init() {
// Renderer.
renderer = new THREE.WebGLRenderer({
antialias: true,
//preserveDrawingBuffer: true
});
//renderer.setPixelRatio(window.devicePixelRatio);
renderer.setSize(window.innerWidth, window.innerHeight);
// Add renderer to page
document.body.appendChild(renderer.domElement);
// add Screenshot listener
document.getElementById("shot").addEventListener('click', takeScreenshot);
// Create camera.
camera = new THREE.PerspectiveCamera(70, window.innerWidth / window.innerHeight, 1, 1000);
camera.position.z = 400;
// Create scene.
scene = new THREE.Scene();
// Create material
material = new THREE.MeshPhongMaterial();
// Create cube and add to scene.
var geometry = new THREE.BoxGeometry(200, 200, 200);
mesh = new THREE.Mesh(geometry, material);
scene.add(mesh);
// Create ambient light and add to scene.
var light = new THREE.AmbientLight(0x404040); // soft white light
scene.add(light);
// Create directional light and add to scene.
var directionalLight = new THREE.DirectionalLight(0xffffff);
directionalLight.position.set(1, 1, 1).normalize();
scene.add(directionalLight);
// Add listener for window resize.
window.addEventListener('resize', onWindowResize, false);
}
function takeScreenshot() {
// open in new window like this
//
/*
var w = window.open('', '');
w.document.title = "Screenshot";
//w.document.body.style.backgroundColor = "red";
var img = new Image();
// Without 'preserveDrawingBuffer' set to true, we must render now
renderer.render(scene, camera);
img.src = renderer.domElement.toDataURL();
w.document.body.appendChild(img);
*/
/*
// download file like this.
//
var a = document.createElement('a');
// Without 'preserveDrawingBuffer' set to true, we must render now
renderer.render(scene, camera);
a.href = renderer.domElement.toDataURL().replace("image/png", "image/octet-stream");
a.download = 'canvas.png'
a.click();
*/
// New version of file download using toBlob.
// toBlob should be faster than toDataUrl.
// But maybe not because also calling createOjectURL.
// I dunno....
//
renderer.render(scene, camera);
renderer.domElement.toBlob(function(blob){
var a = document.createElement('a');
var url = URL.createObjectURL(blob);
a.href = url;
a.download = 'canvas.png';
a.click();
}, 'image/png', 1.0);
}
function animate() {
requestAnimationFrame(animate);
mesh.rotation.x += 0.005;
mesh.rotation.y += 0.01;
renderer.render(scene, camera);
}
function onWindowResize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
}
body {
padding: 0;
margin: 0;
}
canvas {
display: block;
}
#shot {
position: absolute;
top: 0px;
right: 0px;
margin: 5px;
}
<script src="https://rawgit.com/mrdoob/three.js/r102/build/three.min.js"></script>
<button id="shot">Take Screenshot</button>

On button click use the method toDataURL() to capture the image from the camera.
imageData = renderer.domElement.toDataURL();
The DOM element of renderer is a canvas. Refer: https://developer.mozilla.org/en-US/docs/Web/API/HTMLCanvasElement/toDataURL for more details

Related

Render Screen is Dificult

So I'm trying to learn how to use THREE.js to make browser games, however when I try to use the code to import a model the screen renders black. I'm using Brackets to code and run it and I haven't had an issue until now.
Heres the code
var scene = new THREE.Scene();
var camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
scene.background = new THREE.Color(0xdddddd);
var renderer = new THREE.WebGLRenderer();
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);
var loader = new THREE.GLTFLoader();
loader.load('CabbageBase.glb', function(gltf) {
scene.add(gltf.scene);
}, undefined, function(error) {
console.error(error);
});
camera.position.z = 5;
var animate = function() {
requestAnimationFrame(animate);
/*cube.rotation.x += 0.01;
cube.rotation.y += 0.01;*/
renderer.render(scene, camera);
};
animate();
body {
background: linear-gradient(#e4e0ba, #f7d9aa);
margin: 0px;
overflow: hidden;
}
canvas {
display: block;
}
<script src="//cdn.rawgit.com/mrdoob/three.js/master/build/three.min.js"></script>
<script src="GLTFLoader.js"></script>
I don't see anything wrong with the code except for the URLs.
You need to use a specific version of three.js by either downloading it or using a versioned CDN. All of the loaders, controls, and the examples require a specific version. Never link to threejs.org, always either download your own version or use a CDN. For example jsdeliver.
https://www.jsdelivr.com/package/npm/three
So for the three.js library the URL is
https://cdn.jsdelivr.net/npm/three#0.113.2/build/three.min.js
and for the version compatible GLTFLoader the URL is
https://cdn.jsdelivr.net/npm/three#0.113.2/examples/js/loaders/GLTFLoader.js
After that you need a model. I pointed to one on the three.js site (which is also bad since there is no guarantee it will stay their or stay compatible but for models we have less choices, normally you'd use your own model)
And with that the code works as is.
Note: For three.js you also generally need to run a local server which is covered in the docs
var scene = new THREE.Scene();
var camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
scene.background = new THREE.Color(0xdddddd);
var renderer = new THREE.WebGLRenderer();
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);
var loader = new THREE.GLTFLoader();
loader.load('https://threejs.org/examples/models/gltf/Horse.glb', function(gltf) {
scene.add(gltf.scene);
}, undefined, function(error) {
console.error(error);
});
camera.position.z = 5;
var animate = function() {
requestAnimationFrame(animate);
/*cube.rotation.x += 0.01;
cube.rotation.y += 0.01;*/
renderer.render(scene, camera);
};
animate();
body {
background: linear-gradient(#e4e0ba, #f7d9aa);
margin: 0px;
overflow: hidden;
}
canvas {
display: block;
}
<script src="https://cdn.jsdelivr.net/npm/three#0.113.2/build/three.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/three#0.113.2/examples/js/loaders/GLTFLoader.js"></script>

How to load all models with the same size and the same camera position?

I'm setting up a website that loads 3d models in ply format, and I'm using Three.js with his PLYLOADER to do that.
My problem is that every model is loading in a different position. I've managed to make them load in a perpendicular way with the ground, but some of them are looking, some are looking far away from the camera and others look like they turn around a Y axis (not their center).
This is my HTML div:
<div id="WebGL-output"></div>
This is my JS code:
<script type="text/javascript">
var lesson7 = {
scene: null,
camera: null,
renderer: null,
container: null,
controls: null,
clock: null,
stats: null,
init: function() { // Initialization
// create main scene
this.scene = new THREE.Scene();
this.scene.fog = new THREE.FogExp2(0xcce0ff, 0.0003);
var SCREEN_WIDTH = '800',
SCREEN_HEIGHT = '600';
// prepare camera
var VIEW_ANGLE = 60, ASPECT = SCREEN_WIDTH / SCREEN_HEIGHT, NEAR = 0.1, FAR = 20000;
this.camera = new THREE.PerspectiveCamera( VIEW_ANGLE, ASPECT, NEAR, FAR);
this.scene.add(this.camera);
this.camera.position.set(0, 0, 300);
this.camera.lookAt(new THREE.Vector3(0,0,0));
// prepare renderer
this.renderer = new THREE.WebGLRenderer({ antialias:true });
this.renderer.setSize(SCREEN_WIDTH, SCREEN_HEIGHT);
this.renderer.setClearColor(this.scene.fog.color);
this.renderer.shadowMapEnabled = true;
this.renderer.shadowMapSoft = true;
// prepare container
this.container = document.getElementById('WebGL-output');
this.container.appendChild(this.renderer.domElement);
// events
THREEx.WindowResize(this.renderer, this.camera);
// prepare controls (OrbitControls)
this.controls = new THREE.OrbitControls(this.camera, this.renderer.domElement);
this.controls.target = new THREE.Vector3(0, 0, 0);
this.controls.maxDistance = 20000;
// prepare clock
this.clock = new THREE.Clock();
this.scene.add( new THREE.AmbientLight(0xFFFFFF) );
// light
var dirLight = new THREE.DirectionalLight(0x606060);
dirLight.position.set(200, 200, 1000).normalize();
this.camera.add(dirLight);
this.camera.add(dirLight.target);
// load models
this.loadModels();
},
loadModels: function() {
// prepare PLY loader and load the model
var scene = new THREE.Scene();
var oPlyLoader = new THREE.PLYLoader();
oPlyLoader.load("../models/{{ $analysis->three_d_model }}", function(geometry) {
var material = new THREE.MeshPhongMaterial( { color: 0xAAAAAA, specular: 0x111111, shininess: 0 } );
var meshMaterial = material;
// if (geometry.hasColors) { // to fix when models will have colors
meshMaterial = new THREE.MeshPhongMaterial({ opacity: 1, vertexColors: THREE.VertexColors });
// }
var mesh = new THREE.Mesh(geometry, meshMaterial);
// Solution: center mesh to world origin
mesh.geometry.computeBoundingBox();
var bbox = mesh.geometry.boundingBox;
var centerVec = new THREE.Vector3();
bbox.getCenter(centerVec); // get center of bbox into centerVec
// move mesh so that center of bbox is placed to world origin
mesh.applyMatrix(new THREE.Matrix4().makeTranslation(-centerVec.x, -centerVec.y, -centerVec.z));
// proceed doing stuff
// mesh.position.set(0, -6, 0);
// mesh.scale.set(5, 5, 5);
lesson7.scene.add(mesh);
});
}
};
// Animate the scene
function animate() {
requestAnimationFrame(animate);
render();
update();
}
// Update controls and stats
function update() {
lesson7.controls.update(lesson7.clock.getDelta());
// lesson7.stats.update();
}
// Render the scene
function render() {
if (lesson7.renderer) {
lesson7.renderer.render(lesson7.scene, lesson7.camera);
}
}
// Initialize lesson on page load
function initializeLesson() {
lesson7.init();
animate();
}
if (window.addEventListener)
window.addEventListener('load', initializeLesson, false);
else if (window.attachEvent)
window.attachEvent('onload', initializeLesson);
else window.onload = initializeLesson;
</script>
I need help to make them all load with the same camera position, looking centred and in the same size.
PS: If i can have better results loading in different extension, I'm up for it.
Thanks in advance
EDIT:
I updated the code with the help of #alex. Now my models are very well centered. The only thing missing now si a fixed camera position however the model was saved
Currently see two solutions here ( if i understand correctly )
You objects are not centered correctly. The solution would be to open the model files in a 3d editor and set them all to the same size and make their center points consistent.
Once you load the model add it to an Object3D and find its center.
Docs about that here: https://threejs.org/docs/#api/en/core/Object3D
the below code this.objectAll is the Object3D. Which is put into a box and we make the controls look at the center of that box. If all model are consistent then this should work.
this.boundingBox = new THREE.Box3();
this.boundingBox.setFromObject(this.objectAll);
this.boundingBox.center(this.controls.target);
Best of luck!

In threejs, the value for setClearColor is white But it render black, when i call external html file?

In Three.js, the value for setClearColor is white But it render black, when i call external html file:
enter image description here
External file codes:
<div id="3d-modal"></div>
<script src="juicer/js/three.js"></script>
3D-modal scripts:
<script>
var scene = new THREE.Scene();
var camera = new THREE.PerspectiveCamera(75,
window.innerWidth/window.innerHeight, 0.1, 1000);
var renderer = new THREE.WebGLRenderer({antialias : true});
// renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setClearColor(0xffffff);
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
// Appending Webgl to the "container" Div...
// document.body.appendChild( renderer.domElement );
var container = document.getElementById("3d-modal");
var canvas_width = 290;
var canvas_height = 165;
var renderer = new THREE.WebGLRenderer();
renderer.setSize(canvas_width , canvas_height);
container.appendChild(renderer.domElement);
var animate = function () {
requestAnimationFrame( animate );
renderer.render(scene, camera);
};
animate();
</script>
How can i solve this?
I came across this when I started using three.js as well. It's actually a javascript issue:
Update: Thanks to HdN8 for the updated solution:
renderer.setClearColor( 0xffffff, 0);
(note: syntax => setClearColor ( color, alpha ))
Update #2: As pointed out by WestLangley in another similar question - you must now use the below code when creating a new WebGLRenderer instance in conjunction with the setClearColor() function:
var renderer = new THREE.WebGLRenderer({ alpha: true });
Update #3: Mr.doob points out that since r78 you can alternatively use the code below to set your scene's background colour:
var scene = new THREE.Scene(); // initialising the scene
scene.background = new THREE.Color( 0xff0000 );
Update #4: G Dog
If the background color doesn't change, try changing alpha to 1 from 0.

Three.js fast text engraving

I'm trying to engrave some text on a surface using Three.js. I've achieved it using csg.js and ThreeCSG and it works perfect, the result is really good but the problem is it takes a lot of time. On my PC it takes about 30 seconds to engrave the word Hello.
Searching for other solution I found this site. They make custom jewelry and you can engrave text on it and the time it takes to engrave the text is really short! So I assume they are not using csg.js. What other technique can be used to achieve this result?
I though about using bump maps, I should generate a bump map for each letter but I don't know if that's the correct approach.
I can see by looking at the shaders that the site your linked to uses bump maps.
I don't think you would create a bump map for each letter, you would just do all the drawing (text) on a single canvas and apply that as a bump map.
Click on "Run Code Snippet" below for a demo of canvas bump maps (click and drag in the white box).
I hope this helps.
var camera, scene, renderer, mesh, material, stats;
var drawStartPos = {x:0, y:0};
init();
setupCanvasDrawing();
animate();
function init() {
// Renderer.
renderer = new THREE.WebGLRenderer();
//renderer.setPixelRatio(window.devicePixelRatio);
renderer.setSize(window.innerWidth, window.innerHeight);
// Add renderer to page
document.getElementById('threejs-container').appendChild(renderer.domElement);
// Create camera.
camera = new THREE.PerspectiveCamera(70, window.innerWidth / window.innerHeight, 1, 1000);
camera.position.z = 400;
// Create scene.
scene = new THREE.Scene();
// Create material
material = new THREE.MeshPhongMaterial();
// Create cube and add to scene.
var geometry = new THREE.BoxGeometry(200, 200, 200);
mesh = new THREE.Mesh(geometry, material);
scene.add(mesh);
// Create ambient light and add to scene.
var light = new THREE.AmbientLight(0x404040); // soft white light
scene.add(light);
// Create directional light and add to scene.
var directionalLight = new THREE.DirectionalLight(0xffffff);
directionalLight.position.set(1, 1, 1).normalize();
scene.add(directionalLight);
// Add listener for window resize.
window.addEventListener('resize', onWindowResize, false);
// Add stats to page.
stats = new Stats();
document.body.appendChild( stats.dom );
}
function setupCanvasDrawing() {
// get canvas and context
var drawingCanvas = document.getElementById('drawing-canvas');
var drawingContext = drawingCanvas.getContext('2d');
// draw white background
drawingContext.fillStyle = "#FFFFFF";
drawingContext.fillRect(0,0,128,128);
// set canvas as bumpmap
material.bumpMap = new THREE.Texture(drawingCanvas);
// set the variable to keep track of when to draw
var paint = false;
// add canvas event listeners
drawingCanvas.addEventListener('mousedown', function(e){
paint = true
drawStartPos = {x:e.offsetX, y:e.offsetY};
});
drawingCanvas.addEventListener('mousemove', function(e){
if(paint){
draw(drawingContext, e.offsetX, e.offsetY);
}
});
drawingCanvas.addEventListener('mouseup', function(e){
paint = false;
});
drawingCanvas.addEventListener('mouseleave', function(e){
paint = false;
});
}
// Draw function
function draw(drawContext, x, y) {
drawContext.moveTo(drawStartPos.x, drawStartPos.y);
drawContext.lineTo(x,y);
drawContext.stroke();
drawStartPos = {x:x, y:y};
material.bumpMap.needsUpdate = true;
}
function animate() {
requestAnimationFrame(animate);
mesh.rotation.x += 0.005;
mesh.rotation.y += 0.01;
renderer.render(scene, camera);
stats.update();
}
function onWindowResize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
}
body {
padding: 0;
margin: 0;
}
#drawing-canvas {
position: absolute;
background-color: #000;
top: 0px;
right: 0px;
z-index: 3;
}
#threejs-container {
position: absolute;
left: 0px;
top: 0px;
width: 100%;
height: 100%;
z-index: 1;
}
<script src="https://rawgit.com/mrdoob/three.js/r83/build/three.min.js"></script>
<script src="https://cdn.rawgit.com/mrdoob/stats.js/r17/build/stats.min.js"></script>
<canvas id="drawing-canvas" height="128" width="128"></canvas>
<div id="threejs-container"></div>

Three.js update scale object-size via view

i'm new in three.js and have a problem by scaling a object. I already looked at the documentation (https://github.com/mrdoob/three.js/wiki/Updates), but i don't really understand it at all.
My Problem: By changing a HTML-select-element the CubeGeometry should be scaled in the x-direction. That is already working BUT the "old" Cube do not disappear. So i have 2 Cubes. But i want only one Cube with the current size. I hope you can understand my problem ;-)
So here is my Code from the View:
$(document).on('change',".configurator > form select",function(event){
// update 3D-object - is that a nice way???
$.getScript("/javascripts/3D-animation.js.erb", function(){
// here comes the update
OBJECT3D.updateObject();
});
})
And here is my 3D-animation.js.erb:
var drawing_area;
var renderer;
var camera;
var obj;
var scene;
var OBJECT3D = {};
$(function() {
// get drawing_area
drawing_area = document.getElementById("canvas_wrapper");
// initialize renderer
renderer = new THREE.WebGLRenderer();
renderer.setSize(drawing_area.clientWidth, drawing_area.clientHeight);
renderer.setClearColor( 0xffffff, 1);
// add renderer to drawing_area
drawing_area.appendChild(renderer.domElement);
// initialize camera
camera = new THREE.PerspectiveCamera(45, drawing_area.clientWidth/drawing_area.clientHeight, 1, 100000);
camera.position.z = 1000;
camera.position.y = 100;
camera.position.x = 300;//-0.78;
// create texture
var texture = THREE.ImageUtils.loadTexture( "/images/materials/texture_1.jpg" );
texture.wrapS = THREE.RepeatWrapping;
texture.wrapT = THREE.RepeatWrapping;
texture.repeat.set( 1, 1 );
// create object
var obj_form = new THREE.CubeGeometry(250,250,250);
var obj_material = new THREE.MeshLambertMaterial( { map: texture,ambient: 0x999999 } );
OBJECT3D.obj = new THREE.Mesh(obj_form, obj_material);
// so what do i need here?
OBJECT3D.obj.geometry.dynamic = true;
// OBJECT3D.obj.geometry.__dirtyVertices = true;
// OBJECT3D.obj.geometry.__dirtyNormals = true;
// OBJECT3D.obj.geometry.verticesNeedUpdate = true;
// create scene
scene = new THREE.Scene();
scene.add(camera);
scene.add(OBJECT3D.obj);
// create lights
pointLight = new THREE.PointLight(0xFFFFFF);
pointLight.position.x = 400;
pointLight.position.y = 200;
pointLight.position.z = 1300;
scene.add(pointLight);
ambientLight = new THREE.AmbientLight( 0xffffff);
scene.add( ambientLight );
requestAnimationFrame(render);
function render(){
requestAnimationFrame(render);
OBJECT3D.obj.rotation.y += 0.005;
OBJECT3D.obj.rotation.z += 0.005;
renderer.render(scene, camera);
};
// update object
OBJECT3D.updateObject = function () {
console.log("in update");
OBJECT3D.obj.scale.x = 2.5; // SCALE
OBJECT3D.obj.geometry.needsUpdate = true;
//render();
}
});
Sorry, if the Code is not the best one, but i'm really new in this stuff! :) hope you can help me!
Thanks!
I think it is because you are using $.getScript method. This will load your script each time again and create a new instance of OBJECT3D.
I propose that you make sure that your code in 3D-animation.js.erb will be included and called only once upon the load of a page (include it just like any other regular javascript file) and then call your update directly like this:
$(document).on('change',".configurator > form select", function(event) {
OBJECT3D.updateObject();
});
also, I believe you can drop following lines of code:
OBJECT3D.obj.geometry.needsUpdate = true;
and
OBJECT3D.obj.geometry.dynamic = true;

Categories