Home › Forums › Programming › Updating object scale to appear consistent regardless of distance from camera
- This topic has 5 replies, 3 voices, and was last updated 10 months, 1 week ago by masj.
-
AuthorPosts
-
2024-01-08 at 7:56 pm #69540masjParticipant
Hello!
Linear algebra is def not my strength as a creative-type first. Hoping a kind technician in the V3D community can help me with something I’m trying to build.
The goal is to update the size of a given Object3D(s) to remain visually consistent, regardless of its distance from the app’s camera. Think I’m getting the distance between the object(s) and the camera correctly – was able to get this far:
function setObjectConsistentScale(objs) {
var [_vec1, _vec2] = [new v3d.Vector3(), new v3d.Vector3()];
objs.forEach(obj => {
var objDist = obj.getWorldPosition(_vec1).distanceTo(app.camera.getWorldPosition(_vec2));
console.log(objDist);
// obj.scale = (?, ?, ?);
});
}What to do next is…where I need help, especially when we start potentially factoring in camera zoom, FOV, etc. Any guidance on the matter would be HUGELY appreciated. Thanks!
2024-01-08 at 8:45 pm #69541xeonCustomerAny chance you can describe the use case?
Xeon
Route 66 Digital
Interactive Solutions - https://www.r66d.com
Tutorials - https://www.xeons3dlab.com2024-01-08 at 9:18 pm #69542kdvParticipantfunction setObjectScale(mesh, factor) { if (app.camera.isOrthographicCamera) factor = ((app.camera.top - app.camera.bottom) / app.camera.zoom) / factor; else factor = (mesh.position.distanceTo(app.camera.position) * Math.tan(Math.PI * app.camera.fov / 360) * 2) / factor; mesh.scale.set(1, 1, 1).multiplyScalar(factor); }
Puzzles and JS coding. Fast and expensive.
If you don’t see the meaning in something it primarily means that you just don’t see it but not the absence of the meaning at all.
2024-01-08 at 11:00 pm #69544masjParticipant@kdv, you are brilliant! Thank you once again for sharing your insights. Really appreciate your help.
Can I ask a follow up question re: the ‘factor’ arg?
I think I found a way manually to set ‘factor’ on a per-object basis via its bounding box & Math.cbrt to keep it at the same scale as inside the 3D model file itself, but was wondering if you knew of a cleaner/simpler way to do this via maths inside the function call, when an object’s bounding box will have grown/shrunk with the object’s scale (hope that make sense).
@xeon, for sure! I was envisioning something almost like fancy 3D UI annotation-like objects on the surface of model(s) in an app with lots of leeway for zooming, kind of like the feel of zooming in to a 2D map in an app, but w/ some proper 3D fanciness.2024-01-08 at 11:10 pm #69545kdvParticipantCan I ask a follow up question re: the ‘factor’ arg?
using this argument you can adjust the desired visual scale. Higher values make objects smaller. You may start with 5 and then adjust scales as needed.
The way to apply scaling per object:
object.onBeforeRender = function() { setObjectScale(this, 5); };
Puzzles and JS coding. Fast and expensive.
If you don’t see the meaning in something it primarily means that you just don’t see it but not the absence of the meaning at all.
2024-01-09 at 12:46 am #69546masjParticipantThanks for the breakdown.
Again, you’ve been amazing, and thanks for the help! -
AuthorPosts
- You must be logged in to reply to this topic.