Tween object with respect to bounds of the screen / camera?


I have an object that randomly tweens its position forever, so dealing with X Y Z.

I arbitrarily have each of those values randomized from -5 to 5, this works great for the test window that I am working on. But I would now like this to know where the bounds of the screen / camera are, and instead of passing in the arbitrary -5 and 5, have those values basically be related to the screen / camera - so the object is always in the game view.

I did some poking around and found a function that converts the game objects position to screen coordinates, but I kind of need the opposite I guess? I need to get the screen size and have that converted to the values to pass to the tween I think?

Thanks guys!

I think you are saying that you want to tween the object to a position within the camera frustum.
This sample code, creates a random vecor3 with x,y,z between -5 and 5 and logs if the vector is within the camera frustum.
maybe tween to that point if its within.

const v = new THREE.Vector3(
    Math.random() * 10 - 5,
    Math.random() * 10 - 5,
    Math.random() * 10 - 5
const frustum = new THREE.Frustum()
    new THREE.Matrix4().multiplyMatrices(camera.projectionMatrix, camera.matrixWorldInverse)
1 Like

Thank you for this! I will be experimenting with it shortly!