Been stuck on this problem for the past 3 days so would appreciate any help!
PROBLEM:
I’m trying to seamlessly transition between a Three.js particle animation that forms my logo in its final position and a 2D React image of the same logo. The goal is to have the React image consistently match the size of the particle logo, even when the browser window is resized.
CONTEXT:
- I have a three.js animation that ends on particles in the arrangement / position, of my logo.
- My camera is positioned at: 0,0,80 while the bounding box (and also the particle logo) is at 0,0,0.
- The 2D React image of the logo needs to dynamically scale to match the size of this particle logo.
CURRENT APPROACH:
- Computing a bounding box for the particle logo at the same coordinates (0,0,0) and then getting the bounding box size [logoBoundingBox.getSize(new THREE.Vector3());] in world units.
- Attempt to convert the world units to pixels using the following calculations:
const vFov = (camera.fov * Math.PI) / 180;
const height = 2 * Math.tan(vFov / 2) * distance;
const aspect = window.innerWidth / window.innerHeight;
const width = height * aspect;
// Calculate the size of the bounding box in pixels
const widthInPixels = window.innerWidth * ((1 / width) * size.x);
const heightInPixels = window.innerHeight * ((1 / height ) * size.y);
resolveSizePromise({ widthInPixels, heightInPixels });
This outputs a larger react image.
Alternatively, I tried this to get the proportion of the visible width the bounding box occupies and then using that factor to get the same proportion of the window’ inner width to size the react image:
var VFov = camera.fov * Math.PI / 180;
var Height = 2 * Math.tan(sceneVFov / 2) * (camera.position.z - center.z);
var Aspect = window.innerWidth / window.innerHeight;
var Width = Height * Aspect;
const scaleFactor = size.x / Width;
// in the react file i then multiplied the window's inner width by the scaleFactor to try and get the react image to match the bounding box in size as such...
logoWidth: window.innerWidth * scaleFactor
However this outputs a react image larger than the boundingBox.