Adjust the placement of a gltf-model within an a-frame scene by leveraging three.js

I have a gltf-model loaded into an a-frame and I am trying to move it along the y-axis by 20 units. The code I am currently using is:

However, when I run the scene in a-frame, the object appears to be moved upwards (you can check this by looking at shadows and inspecting the scene in the "AFTER" images), but it still remains displayed in its original position. It seems like the scene needs some sort of refresh.

So my question is, how can I properly move the object using three.js code?
BEFORE: BEFORE1 BEFORE2


AFTER: AFTER1 AFTER2

Here is the code snippet:

<html>
<a-scene>
  <a-sky color="#f9f2cf"></a-sky>

  <!-- Additional LIGHT attributes that are not required from blender -->
  <a-light color="#fff" position="-8 5 0" intensity="3.5" light="intensity:2"></a-light>
  <a-light color="#fff" position="0 5 -14.163" intensity="3.5" light="intensity:2"></a-light>
  <a-light color="#fff" position="0 5 14.192" intensity="3.5" light="intensity:2"></a-light>
  <a-light color="#fff" position="0 -9.574 -2.443" intensity="3.5" light="intensity:2"></a-light>
  <a-light color="#fff" position="8.5 5 0" intensity="3.5" light="intensity:2"></a-light>

  <!-- CAMERA with WASD controls and circle cursor -->
  <a-camera fly look-controls wasd-controls position="17.020 16.700 7.958" rotation="-34.149 89.382 0.000">  
    <a-entity 
      cursor="fuse: true; fuseTimeout: 500"
      position="0 0 -1"
      geometry="primitive: ring; radiusInner: 0.02; radiusOuter: 0.03"
      material="color: black; shader: flat">
    </a-entity>
  </a-camera>

  <a-assets>
    <!-- Sample GLTF animation assets -->
    <a-asset-item id="afb_animation" src="models/afb_animation.gltf"></a-asset-item>
  </a-assets>

  <a-entity id="_afb_animation" position="0 0 0"  gltf-model="#afb_animation" ></a-entity>
</a-scene>

<!-- JavaScript to change model position -->
<script>

  $( document ).ready(function() {

    var yAxis = new THREE.Vector3(0,1,0);

    setTimeout(function(){
      console.log("rotation complete");

      document.querySelector('#_afb_animation').sceneEl.object3D.translateY(20);

    }, 3000);

  });
</script>

Answer №1

If you're looking for a solution to your issue, one approach is to utilize the setAttribute method on the position component.

For instance:

entity.setAttribute('position', { x: 1, y: 2, z: 3 });

In this code snippet, the entity can be any A-Frame entity.

Example 1

Here's an example where I created a custom A-Frame component named move-my-model. This component lets me move my entity (in this case, a <a-box> entity) by adding 10 to its y-position after a delay of 3000ms using setTimeout:

The provided code registers the component and applies the necessary transformations.

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

Activate a clever feature by selecting a checkbox

Experimenting with smarty and jquery has been quite an interesting journey for me... I am currently working on a project that requires some enhancements in terms of source code management. My HTML page (.tpl) contains various content, including a checkbo ...

Is it feasible to impersonate a session in PHP by manipulating cookies on the client-side with JavaScript?

Is it possible for an unauthorized visitor to view, delete, or edit session cookies in a PHP web app if HttpOnly cookies are not being used? What happens when a user on a non-session page of a web app sets a cookie with the same name as a session page coo ...

Issue with Highcharts React: The menu button fails to switch from displaying "view full screen" to "exit full screen" when full screen mode is activated

We've encountered some difficulties while using highcharts in React. Specifically, when switching to full screen mode with the menu button, the option for "Exit from full screen" is not visible. Instead, it still reads "View in full screen." Oddly eno ...

How to update icon for fa-play using Javascript in HTML5

I recently added an autoplay audio feature to my website. I would like to implement the functionality to pause and play the music, while also toggling the icon to fa-play at the same time. This is the HTML code I am using: <script type="text/javascri ...

Is there a way for me to transition a grid from 4x3 to 3x4 at a specific breakpoint in the media query?

I am currently working on creating a responsive grid layout. Initially, the grid is set up as a 4x3 configuration, but once the screen width reaches 720px, I need it to convert into a 3x4 grid. The challenge is that I must accomplish this using only HTML a ...

Exploring Specific Locations with AmCharts 5 Zoom Feature

Just starting to use AmCharts 5 and I'm struggling to trigger a simple zoom into a location on a map. No matter which tutorial I follow, I can't seem to replicate the results others are getting. If you want to take a look at my Codepen setup to ...

Error: Unable to iterate over tasks using the map function

My friends and I are collaborating on an application, where he is sending me tasks through a rest API. I encountered an error: TypeError: tasks.map is not a function. Can someone please assist me? :) const [tasks, changeTasks] = useState ...

Updating textures in Three.js

Is there a way to access and modify the data in an exported JSON file using JavaScript? For instance, if I have a material with specific properties like mapDiffuse value that I want to change, how can I achieve this through a JavaScript function? "materia ...

Calling Number() on a string will result in returning a value of NaN

Currently, I am working on the following code snippet: app.put("/transaction/:value/:id1/:id2", async(req,res) => { try { const {value,id1,id2} = req.params; const bal1 = await pool.query("Select balance from balance where id=$1",[i ...

Trigger a Vue method using jQuery when a click event occurs

I'm attempting to attach a click event to an existing DOM element. <div class="logMe" data-log-id="{{ data.log }}"></div> ... <div id="events"></div> Struggling to access external Vue methods within my jQuery click handler. A ...

How odd! Using window.location or location.replace redirects to the page and then reverses back!

While in debug mode, I am able to track which page is being accessed. Interestingly, whenever I use window.location or window.location.replace(..), the browser redirects to the specified page but then quickly returns to the original one! This strange beh ...

unable to display items in the navigation bar according to the requirements

Currently, I am in the process of developing an application using reactjs and material-ui. This particular project involves creating a dashboard. In the past, I utilized react-mdl for components and found it to work well, especially with the navbar compone ...

Encountering issues while establishing a connection to SQL Server through Node.js

I've developed a Node.js application to interact with SQL Server. Here's the code snippet: app.get('/SalesStatistics', function (req, res) { var Connection = require('tedious').Connection; // configuration for the databas ...

Customizing Column Visibility in AgGrid React

In my React application, I have a backend (BE) that returns an Array of objects representing columns. Each object has the structure {headerName: 'string', field: 'string', visible: boolean}. However, the 'visible' parameter se ...

Is there a way to substitute the HOC with a single call and solely modify the prop?

One issue I've encountered in my project is the repetitive use of a Higher Order Component (HOC) for the header. Each time it's used, the props are set to determine whether header links should be displayed or not. My objective is to streamline th ...

Determine the size of an array by utilizing the map function in ES6

Attempting to determine the length of an array using ES6 with the following code, but encountering issues. a=[[1,2,3,4],[4,5,6]] result = a.map(d=>({d[0]:d.length})) console.log(result) The correct method is: a=[[1,2,3,4],[4,5,6]] result = a.map(d=&g ...

Loop through the keys of an object to retrieve only the valid values

Task Description: Develop a function named displayOptions that takes an object called "displayDevice" as input and returns an array. The properties of the input object will be various "video output" options, each with either a true or false value. Your tas ...

Layer a div on top of another when a button is clicked

I am currently working on a project that involves two visualizations placed in different divs. My goal is to overlay one div over another when a button is clicked so that they can be compared, and then separate them again with another button click. Does ...

Having trouble getting smooth scrolling with easing to function properly

I'm facing an issue with a JQuery function that is supposed to enable smooth scrolling using JQuery easing, but for some reason, it's not functioning correctly and I'm unable to pinpoint the error. Here is the code for the function: $(func ...

Enhancing Terrain Collision Accuracy with Three.js

I have been developing a game engine utilizing the WebGL library Three.js and have successfully implemented basic terrain collision detection. However, I am facing an issue in determining the exact Y position of an object relative to the terrain face it is ...