What is the best way to animate a particular item from my JSON data to spin around?

I have successfully created a scene in three.js/editor and exported it as a JSON file that I am currently using. While I have been able to program the camera and orbit controls, I am unsure of how to specifically select an object within my JSON structure. Is there a method for accomplishing this? If so, how can I "target" a specific object? Could utilizing a unique identifier like a "uuid" be a solution? Here is an excerpt from my JSON code:

{
"metadata": {
    "version": 4.4,
    "type": "Object",
    "generator": "Object3D.toJSON"
},
"geometries": [
    {
        "uuid": "DEB90436-B316-4E49-83A6-323712AA3A78",
        "type": "TorusGeometry",
        "radius": 1,
        "tube": 0.34,
        "radialSegments": 42,
        "tubularSegments": 44,
        "arc": 6.32,
    },
    {
        "uuid": "0F8E3492-4B1B-436A-973C-7F8433AA7582",
        "type": "PlaneGeometry",
        "width": 2,
        "height": 2
    }                    /...and so forth...
]

Below is my JavaScript implementation:

var scene, camera, renderer;

init();
animate();

function init() {

    // Setting up the scene with appropriate dimensions.
    scene = new THREE.Scene();
    var WIDTH = window.innerWidth,
        HEIGHT = window.innerHeight;

    // Creating a renderer and appending it to the DOM.
    renderer = new THREE.WebGLRenderer({antialias:true});
    renderer.setSize(WIDTH, HEIGHT);
    document.body.appendChild(renderer.domElement);

    // Establishing a camera's position relative to the model, then adding it to the scene.
        camera = new THREE.OrthographicCamera( window.innerWidth / - 2, window.innerWidth / 2, window.innerHeight / 2, window.innerHeight / - 2, - 2000, 1000 );
        camera.position.x = 200;
        camera.position.y = 100;
        camera.position.z = 200;
     scene.add(camera);



 // Loading the mesh and integrating it into the scene. The JSON structure is imported here...
 var loader = new THREE.ObjectLoader();
 loader.load("scene.json",function ( obj ) {
 scene.add( obj );
 });

 // Implementing OrbitControls
controls = new THREE.OrbitControls(camera, renderer.domElement);
controls.enablePan = false;
            controls.minDistance = 6; // determining max zoom in distance
controls.maxDistance = 71; // defining maximum zoom out allowance

  }

function animate() {


     // Render the scene.
     renderer.render(scene, camera);
     controls.update();

   }

Is there a viable approach?

Answer №1

The Object3D class includes a method known as getObjectByName. Here is an example of how to use it:

loader.load("scene.json",function ( obj ) {
    scene.add( obj );
    var targetObject = obj.getObjectByName('name of object from scene.json');
    // You can now manipulate the targetObject...
});

To locate the object names, simply refer to the scene.json file and search for the "object": ... section.

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

Discovering the volume, weight, and density with three.js

While Three.js offers examples for previewing STL objects in ASCII and Binary formats, I am interested in how to extract the volume, weight, and density of the object itself. Are there any hints or does the three.js API provide this functionality? Your he ...

Accessing WCF service through various domain clients

Utilizing a WCF service, I have successfully accessed it from Javascript using AJAX and JSON. Both the client and service are located on the same domain, which has been working well. Now, I am faced with the challenge of calling the same service from mult ...

Data fetch error: Mount data could not be retrieved due to an undefined

While working with my database, I encountered an issue where the data needs to be accessed before the component mounts in order for the page to display properly using the componentDidMount() lifecycle method. Upon testing, it was confirmed that if I remo ...

Searching for the location of a specific item within an array using

Trying to grasp the fundamental components of Javascript has led me to stumble upon a specific line of code: if (varX.indexOf(String(varY),0) < 0) In this scenario, varX represents an array of Strings and varY is one of the strings contained within th ...

working with received data from a JavaScript object

Looking for code assistance, I have a data object serving as my data source. var data = [ { "label":"May 7", "value":25736.6, "proID":"ISB" }, // more data objects... ]; I'm manipulating this data to generate another ...

Understanding how to use the `e.persist` method in TypeScript can greatly improve

My form validation process using formik includes the code snippet below: {props => { const { values: { email, password }, errors, touched, handleChange, isValid, setFieldTouched, } = props; const change = (name: string, e: ...

A step-by-step guide on incorporating a session variable from a for loop into an Onclick Javascript event and sending it via ajax

Apologies for the unclear title, my situation is a bit complex and I'm struggling to explain it correctly. The scenario is illustrated with an image. 1.) I have a while loop processing JSON data from PHP (which includes multiple user IDs). 2.) Ajax ...

One of the parameters is converging faster than the other with Gradient Descent

My introduction to univariate linear regression using gradient descent was a hands-on experience in JavaScript. const LEARNING_RATE = 0.000001; let m = 0; let b = 0; const hypothesis = x => m * x + b; const learn = (alpha) => { if (x.length < ...

The API is now configured to provide a single result rather than returning multiple results in response to an 11ty/elevent

In my 11ty project (static site generator), I am fetching property data and then using it in a Promise.all call to: fetch all property images fetch tenancy application URLs After that, I combine these into one array named allData for my pages. The image ...

Tips for creating a custom notification container using CSS

I have been tasked with creating a notification display for a web application. The notifications should appear in the bottom right corner of the window and disappear after a certain period. Alternatively, users can click on a notification to dismiss it. Wh ...

What is the best way to handle error responses in a Rails API when returning JSON data?

Suppose... module Api module V1 class SessionsController < ApplicationController respond_to :json skip_before_filter :verify_authenticity_token def create @user = User.find_by(email: para ...

Separate the divs in a template into individual templates, or alternatively, utilize ng-if to display different divs within a single template

As a newcomer to AngularJS, I am seeking advice on the most efficient way to code for the given scenario: Scenario: I have a single HTML template containing multiple divs, and I need to display only one div at a time based on specific conditions. Two app ...

Storing data with ElectronJS

I am currently working on an Electron JS and React JS offline application. One of the initial steps in my launch process involves loading a large file that is over 1 GB in size and cannot be divided. This causes a delay of approximately 50-60 seconds whi ...

JavaScript tip: Improve the way you highlight the current navigation page while scrolling by finding alternative methods to using "scrollY > x"

Currently, my webpage layout is divided into 4 sections - HOME, ABOUT, SKILLS, and CONTACT. Below is the JavaScript code I am using to highlight a specific section on the navigation bar based on the scroll position: let home = document.querySelector(" ...

What is the best way to implement a series of delayed animations in jQuery that are connected

Imagine you have the following items: <div id="d1"><span>This is div1</span></div> <div id="d2"><span>This is div2</span></div> <div id="d3"><span>This is div3</sp ...

Excessive alerts being produced within the loop

I am trying to remove a wine from a JSON wine list and I want to display an alert if the wine doesn't exist in the JSON file. However, the alert is popping up for every entry in the list. I am struggling to find a way to use an if statement before pro ...

Retrieve data from a JSON file and assign the objects to a map

I have this extensive JSON file that I need to extract data from. Here's a snippet of the JSON format: { "enabled":true, "contentMetadataPartial": { "customTags": { "tag1":"value1" } }, "simulatedChanges": [ 30 ...

Res.end isn't halting the script's execution process

I'm currently facing an issue while building an API around a third-party API in my Express route. The problem is that the script keeps executing even after encountering a 406 error. Below is the snippet of my code: app.get('/submit/:imei', a ...

What is the process for activating an event before another event on the same element with vanilla JavaScript?

Having an issue with the eventListener function. I am using the external library jquery.jstree-pre1.0fix2, which triggers a blur event on an input in my case. However, I need to trigger my event before the one from the library. I have come across some sol ...

Content is pushed by a scrolling drop down menu

I had a drop-down menu that was functioning well. However, when I made changes to allow scrolling for too many items, it started behaving differently. The drop-down without scrolling doesn't move down, but the one with scrolling does. JS var maxHei ...