We use cookies to ensure that we give you the best experience on our website. If you continue to use this site we will assume that you are happy with it.

Updating object scale to appear consistent regardless of distance from camera

Home Forums Programming Updating object scale to appear consistent regardless of distance from camera

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #69540
    masj
    Participant

    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. :wacko: Any guidance on the matter would be HUGELY appreciated. Thanks!

    #69541
    xeon
    Customer

    Any chance you can describe the use case?

    Xeon
    Route 66 Digital
    Interactive Solutions - https://www.r66d.com
    Tutorials - https://www.xeons3dlab.com

    #69542
    kdv
    Participant
      function 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.

    #69544
    masj
    Participant

    @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.

    #69545
    kdv
    Participant

    Can 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.

    #69546
    masj
    Participant

    Thanks for the breakdown.
    Again, you’ve been amazing, and thanks for the help!

Viewing 6 posts - 1 through 6 (of 6 total)
  • You must be logged in to reply to this topic.