Challenges arise when attempting to modify an ArrowHelper without creating a new instance

In my project using three.js, I am facing a challenge with updating the components of 3 ArrowHelper. Currently, I am able to update these 3 ArrowHelper in my animation by creating new instances each time the drawing function is called.

For instance, in my main function, I initialize the 3 ArrowHelper like this:

// Parameters for vector
var directionMainVectorX = new THREE.Vector3(1, 0, 0);
var directionMainVectorY = new THREE.Vector3(0, 1, 0);
var directionMainVectorZ = new THREE.Vector3(0, 0, 1);

var originMainVector = new THREE.Vector3(0, 0, 0);
var lengthVector = 20;
var widthVector = 3;
var headLengthVector = 1.5;
var headWidthVector = 1.5;
var hexVector = 0x11ff00;

mainVectorX = new THREE.ArrowHelper(directionMainVectorX, originMainVector, lengthVector, hexVector, headLengthVector, headWidthVector);
mainVectorY = new THREE.ArrowHelper(directionMainVectorY, originMainVector, lengthVector, hexVector, headLengthVector, headWidthVector);
mainVectorZ = new THREE.ArrowHelper(directionMainVectorZ, originMainVector, lengthVector, hexVector, headLengthVector, headWidthVector);

Subsequently, in the drawVector() function, I have the following logic:

function drawVector() {

  // Parameter values for ArrowHelper
  var headLengthVector = 1.5;
  var headWidthVector = 1.5;
  var hexVector = 0x11ff00;
  var widthVector = 3;

  // Solution: Creating new vectors every time
  mainVectorX = new THREE.ArrowHelper(directionMainVectorX.normalize(), originMainVector, lengthVector, hexVector, headLengthVector, headWidthVector);
  mainVectorY = new THREE.ArrowHelper(directionMainVectorY.normalize(), originMainVector, lengthVector, hexVector, headLengthVector, headWidthVector);
  mainVectorZ = new THREE.ArrowHelper(directionMainVectorZ.normalize(), originMainVector, lengthVector, hexVector, headLengthVector, headWidthVector);
            
  // Set line width for mainVector
  mainVectorX.line.material.linewidth = widthVector;    
  mainVectorY.line.material.linewidth = widthVector;    
  mainVectorZ.line.material.linewidth = widthVector;    
    
  // Add arrows to scene reference
  scene.add(mainVectorX);
  scene.add(mainVectorY);
  scene.add(mainVectorZ);    

}

Lastly, the render function looks like this:

function render() {

  rotateCamera();
  
  drawVector();

  controls.update();

  renderer.render(scene, camera);
  
  scene.remove(mainVectorX);
  scene.remove(mainVectorY);
  scene.remove(mainVectorZ);

}   

The setup above works smoothly. However, I aim to avoid the repetitive allocation process inside the drawVector() function.

To achieve this optimization, I initially allocate the 3 ArrowHelper instances just once at the beginning of the main code block and then proceed to update their properties within the drawVector() function as shown below:

function drawVector() {

  // Parameter values for ArrowHelper
  var headLengthVector = 1.5;
  var headWidthVector = 1.5;
  var hexVector = 0x11ff00;
  var widthVector = 3;

  // Calculate coordinates for the 3 vectors

  // Compute directions
  directionMainVectorX.set(1, 0, 0);
  directionMainVectorY.set(0, 1, 0);  
  directionMainVectorZ.set(0, 0, 1);    
  
  // Update mainVector
  
  // Solution: Update the vector parameters
  mainVectorX.position.set(originMainVector);
  mainVectorY.position.set(originMainVector);
  mainVectorZ.position.set(originMainVector);    

  mainVectorX.setDirection(directionMainVectorX.normalize());
  mainVectorY.setDirection(directionMainVectorY.normalize());  
  mainVectorZ.setDirection(directionMainVectorZ.normalize());  

  mainVectorX.setLength(lengthVector, headLengthVector, headWidthVector);
  mainVectorX.setColor(hexVector)
  
  mainVectorY.setLength(lengthVector, headLengthVector, headWidthVector);
  mainVectorY.setColor(hexVector)
  
  mainVectorZ.setLength(lengthVector, headLengthVector, headWidthVector);
  mainVectorZ.setColor(hexVector)    

  // Add arrows to scene reference
  scene.add(mainVectorX);
  scene.add(mainVectorY);
  scene.add(mainVectorZ);    
}

I came across this technique from a previous discussion on updating ArrowHelper.

Despite implementing this approach, nothing seems to appear. Am I overlooking something?

Answer №1

Vector3.set requires 3 parameters - x, y, z, not another vector. So make sure to use

mainVectorX.position.set(originMainVector.x,originMainVector.y,originMainVector.z);
or
mainVectorX.position.copy(originMainVector);

Rework your drawVector (you can now call it updateVector) as

function drawVector() {

  // ArrowHelper parameters
  var headLengthVector = 1.5;
  var headWidthVector = 1.5;
  var hexVector = 0x11ff00;
  var widthVector = 3;

  if(!mainVectorX.parent){    
    // Add arrow to scene reference
    scene.add(mainVectorX);
    scene.add(mainVectorY);
    scene.add(mainVectorZ);    
  }
  mainVectorX.position.copy(originMainVector);
  mainVectorY.position.copy(originMainVector);      
  mainVectorZ.position.copy(originMainVector);

  mainVectorX.setDirection(directionMainVectorX.normalize());
  mainVectorY.setDirection(directionMainVectorY.normalize());  
  mainVectorZ.setDirection(directionMainVectorZ.normalize());  

  mainVectorX.setLength(lengthVector, headLengthVector, headWidthVector);
  mainVectorX.setColor(hexVector);

  mainVectorY.setLength(lengthVector, headLengthVector, headWidthVector);
  mainVectorY.setColor(hexVector);

  mainVectorY.setLength(lengthVector, headLengthVector, headWidthVector);
  mainVectorY.setColor(hexVector);
}

Remember to remove the

scene.remove(mainVectorX);
scene.remove(mainVectorY);
scene.remove(mainVectorZ);

from the render function

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

React / Next.js Rendering Problem, Data Discrepancy Between Client and Server

A new feature has been added to a webpage where an image background and a city name are displayed on top of it. The data file that generates these image backgrounds and city data consists of a standard array of objects. The challenge lies in dynamically l ...

Submitting an extremely large string to an Express server using JS

How can a large String be efficiently sent to a Node.js Express server? On my webpage, I am using Codemirror to load files from an Express server into the editor. However, what is the most effective method for sending "the file" (which is actually a bi ...

The custom radio button I created is not functioning as expected in JavaScript

The JavaScript I wrote for my custom radio button is not working properly. When I check the console, it shows an error message: Uncaught SyntaxError: Unexpected token } on line 14. Here is the code snippet that I used: $(document).ready(function() { ...

Adding a loader to the specific button that has been clicked can be achieved by following these steps:

I am currently in the process of building an e-commerce platform website and I'm looking to implement a feature where users can add products to their cart with just a click of a button. However, before the product is added to the cart, I want to disp ...

Error with review score in material-ui for react.js

There is an issue with my code where the ratings from different sets are not behaving independently. When a rating in one set is clicked, only the first set of ratings changes, suggesting that there is a connection between all the sets. https://i.sstatic. ...

Tips for delaying a node api's execution until a database query no longer returns null

While it may seem like an unnecessary complication, in my current scenario, it is exactly what I require. I am dealing with an API (API-1) that communicates with a third-party service. Instead of directly providing me with a response that I can send back ...

Catching the Selenium NoSuchElementError in JavaScript is impossible

Update: It's puzzling why this was marked as answered since the linked questions don't address the issue at hand and do not involve Javascript. My objective is to detect this error, rather than prevent it, given that methods like invisibilityOfEl ...

What is the best way to change CSS style for a button when clicked using JavaScript?

Is there a way to switch the background style of a CSS div when clicking the same button? function changeBg() { var divElem = document.getElementById("change-bg"); if (divElem.style.backgroundColor === "yellow") { divElem.style.backgroundColor ...

Encountering an error: "Unhandled promise rejection SyntaxError: Unexpected character in JSON data at line 1 column 1."

When the submit button is clicked, my registration form data is sent using an event function called postData() in React. The user data is sent at the register route, and I have mentioned line numbers in the comments: const postData = async(event) =>{ ...

Tips on transferring a selected value from a dropdown menu to a getJSON URL

I am working on a dropdown menu containing the names of Surahs from the Quran. How can I pass the value of the selected item in the dropdown to a function? $.getJSON("http://api.globalquran.com/surah/2/quran-simple?key=api_key&jsoncallback=?", { ... ...

Update the choices in a select dropdown based on the data selected in another form's select field in real-time

Do you think it can be done? If so, how exactly? I've been looking for an example but haven't found anything yet. (I did come across a case of updating options dynamically based on another select field's data within the same form). ...

Uncover the secrets of HTML with JavaScript

I'm facing an issue with extracting data from a link's data attribute and trying to decode the HTML within it, but unfortunately, it's not functioning as expected. Check out my code on JSFiddle: http://jsfiddle.net/Kd25m/1/ Here's the ...

Passing data from child to parent in Angular using EventEmitter

I have integrated a child grid component into my Angular application's parent component, and I am facing an issue with emitting data from the child to the parent. Despite using event emitter to transmit the value to the parent, the variable containing ...

What strategies can be used to stop elements from reverting to their original state?

Hey there, I'm currently experimenting with animation and trying to create a cool two-step effect: 1. elements falling down and 2. pulsating on click. However, I've run into an issue where after the clicking action, the elements return to their o ...

The art of positioning images and creatively cropping

Seeking advice on allowing users to dynamically position and clip an image on a webpage. I've tried using CSS and JavaScript for clipping and resizing, but it's not working as expected. If PHP could provide a solution, that would be ideal for my ...

Guide to verifying Regular Expressions for text fields in JSP with JavaScript

Need help with validating the firstname using regex. The javascript code provided generates an error if the value length is 0, however, even after entering a correct firstname format based on the regex, it still shows 'First name invalid'. I susp ...

Generate menu options in real-time

I have developed a React single page application using the NASA Picture of the Day API. I am looking to incorporate a drawer feature that displays a history of previously shown images, but only showing their dates. However, I am unsure of how to dynamical ...

Using window.location.replace() for redirection after AJAX call succeeds

Attempting to redirect the page after a successful ajax call, the code below is functional: $.ajax( { type: "POST", url: path, data: response1, contentType: "application/json", success: -> window.lo ...

How well are DOM Mutation Observers supported across different web browsers?

I attempted to search for a solution by Googling, but was unsuccessful in finding an answer. Does anyone know if there is a compatibility matrix available for this feature that works across all browsers? In case someone is interested in the answer, here ...

Highlighting an Element Using Selenium Webdriver at Specific Coordinates

I'm currently in the process of automating a web application built with HTML5 using Selenium Webdriver. I need help figuring out how to highlight the exact coordinates where I have clicked on the Canvas element. For example, if I click on the Canvas a ...