Calculating combined scaling ratios for a child mesh in Three.js

Whenever I select a target mesh object, my goal is to connect a standard size sphere mesh object to it. In case the target mesh has been scaled differently than (1,1,1), the code below will make sure that the scaling of the attached marker sphere adjusts accordingly to maintain a consistent radius.

//...attach marker sphere to clicked object

        intersected_object.add(marker_sphere);

        var positionV3 = new THREE.Vector3(); 
        positionV3 = intersected_object.worldToLocal(intersects[0].point);
        xxx = F_Position_Copy_from_vector3_to_Object3D(positionV3, marker_sphere);

        marker_sphere.scale.x = 1/intersected_object.scale.x;
        marker_sphere.scale.y = 1/intersected_object.scale.y;
        marker_sphere.scale.z = 1/intersected_object.scale.z;

However, if the selected target mesh is a child of a parent mesh (or an Object3D) with non-uniform scaling, then we need to consider the parent's scaling when adjusting the marker sphere's scale. This becomes even more complex if the target is a grandchild.

One alternative approach would be to identify the most senior object within the target's ancestry hierarchy (excluding Scene) and base the positioning and scaling of the attached marker sphere on the values of this senior object.

INQUIRY

Is there an efficient and reliable method to determine the "senior" object in the ancestry hierarchy of a mesh object?

Answer №1

This method appears to be effective when dealing with a variety of different objects.

//... locating senior parent object that is not a Scene
var currentObj = intersected_object; //...default
var finished = false;
while ( !finished )
{
    if ( currentObj.parent != "" ) //... not undefined
    { 
        if ( currentObj.parent.type == "Scene" ) //... reached the top
        {
            finished = true;
            var seniorObject = currentObj;
        }
        else
        {
            currentObj = currentObj.parent;
        }
    }
    else  //... no parent object
    {
     alert ('Hmm???');
     finished = true;
    }
}//... end while

//... adding marker sphere to the clicked object
seniorObject.add(marker_sphere);

//... positioning marker sphere in relation to clicked object
var positionVector3 = new THREE.Vector3(); 
positionVector3 = seniorObject.worldToLocal(intersects[0].point);
xxx = SOW_F_Position_Copy_from_vector3_to_Object3D(positionVector3, marker_sphere);

marker_sphere.scale.x = 1 / seniorObject.scale.x;
marker_sphere.scale.y = 1 / seniorObject.scale.y;
marker_sphere.scale.z = 1 / seniorObject.scale.z;

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Changing the height of one Div based on another Div's height

I currently have a display of four divs positioned side by side. I am seeking a solution to ensure that the height of each div remains consistent, and should adjust accordingly if one of them increases in size due to added text content. For reference, ple ...

What is the best way to organize and group messages in JavaScript in order to push them to a subarray?

Having trouble coming up with a name for this question, but here's the issue I'm facing: I currently have an array in PHP containing several entries. Array ( [0] => stdClass Object ( [sender_id] => 0 [me ...

The mistake in npm install is when the console logs a bug that is notorious for causing npm to malfunction

After reading this article, I successfully installed Node.JS version 9.4.0. $brew install node $node -v $v0.12.7 Next, I ran npm install -g grunt-cli for testing. H ...

Having trouble with importing SendInBlue into my NodeJS application?

Currently in the process of updating my Node app to utilize ES6 import modules over requiring files. Encountering difficulties while trying to integrate this change with SendInBlue for email functionality, resulting in the following error message: TypeEr ...

Error with the mongo (1.4.4) query

After running the query like this : collection.find({ "position": { $in: [ 1, 2 ] } }).toArray().... The correct result is returned. However, when I try using $and or $or, such as: collection.find({ $or: [ { "position": 1 }, { "position": 2 } ] }).toAr ...

transferring data from JavaScript to PHP variables

Currently, I am working on a project that involves using ajax to access the database asynchronously. The value retrieved is stored in a JavaScript variable like this: var content=xmlhttp.responseText; My next step is to pass this value to the PHP module ...

Is there a way to eliminate a div and all its contents from the DOM?

As I work on my web application, I am trying to implement a system where error messages can be returned via ajax upon success or failure. The ajax script is functioning correctly in terms of returning errors or successes. However, my challenge lies in com ...

At what point should I be invoking compileComponents, and why am I managing to avoid doing so without consequence?

The documentation for compileComponents provides limited information, stating: Compile components with a templateUrl for the test's NgModule. It is necessary to call this function as fetching urls is asynchronous. Despite this explanation, it rema ...

Testing with Jest in JavaScript shows the failure of a previously passing test when an object is moved to an external helper function

During my test setup, I initialize a player object in the beforeEach block. Within my test logic, every time I call jest.advanceTimersByTime(2001), the player.seekable.mock.calls count increases. As such, given that I make two calls to jest.advanceTimersBy ...

Generate a graphical representation with react-d3

In an attempt to create a histogram using react-d3-components, I have the following code: import React, { Component } from 'react'; import * as d3 from 'd3'; import * as ReactD3 from 'react-d3-components'; import propTypes fr ...

JavaScript code to enforce a 100% page zoom setting

After developing a small game in Canvas, I encountered an issue. Some users with their default zoom level set to something other than 100% are unable to view the entire game page. I attempted to resolve this by using the following CSS: zoom: 100%; This ...

Increasing the width of a column in CSS

Looking to set a maximum width for my table using the size attribute in the <table> tag. However, the table keeps stretching horizontally and causing a bottom scroll bar to appear. Even after refreshing the page or making changes in the console, noth ...

When the parent component in React JS rerenders, the props are not automatically passed down to the child

I have come across similar questions in the past, but I have not found any answers that directly address the issue I am facing in my scenario. In my case, there is a parent component that passes a property to a child component's state. Depending on t ...

Having trouble adding HTML content to a parent div using jQuery and BackboneJS?

Utilizing Backbone Marionette to create a series of views, I am now faced with the task of making an AJAX call to my backend. In order to provide visual feedback to the user indicating that an action is being performed, I decided to integrate a spinner int ...

Encountering a problem while attempting to display an id dynamically using Vue.js?

AppTabs.vue <template> <div :class="{ 'flex space-x-4': variant === 'horizontal', }" > <ul class=" list-none bg-opacity-30 p-1.5 rounded-lg text-cente ...

Transitioning to TypeScript: Why won't my function get identified?

I am in the process of transitioning a functional JavaScript project to TypeScript. The project incorporates nightwatch.js Below is my primary test class: declare function require(path: string): any; import * as dotenv from "dotenv"; import signinPage = ...

eslint is designed to not handle multiple package.json files

There are two package.json files in my project root folder └ app ---- /public └ /styles └ /src └ package.json └ eslintrc.json └ webpack.config.js └ server - /something └ /something ...

Modifying the user interface (UI) through the storage of data in a class variable has proven to be

If I need to update my UI, I can directly pass the data like this: Using HTML Template <li *ngFor="let post of posts; let i = index;"> {{i+1}}) {{post.name}} <button (click)="editCategory(post)" class="btn btn-danger btn-sm">Edit</butto ...

Clicking on a different texture/image allows for the texture to change in Three.js

I am working on creating a 360 image view using Threejs. I have a total of 4 images, with one linked to Threejs and the other 3 displayed as thumbnails at the bottom of the page. When a thumbnail is clicked, the 360 image view should change accordingly. Y ...

Extend the current main scene by overlaying a similar scene on the side

In my game scene, I have a group of robots that are currently active. What I'm trying to achieve is switching the camera perspective to one of the robots' first person view and displaying it in a small window on the side above the current scene. ...