Animating Plane Geometry with Three.js

I am currently working on a simple 3D "landscape" using three.js for the first time, and I must admit, it's driving me a bit crazy. Despite trying various approaches, I have not been successful so far; in fact, I even managed to crash the browser once. Here is the code snippet I am working with:

$(document).ready(function() {
// Code snippets here
});

Although I made an attempt to use a tweening library for three.js, I couldn't quite figure out how to integrate it into this specific scenario.

Essentially, my goal is to create a "breathing" effect for the landscape by randomly moving the vertices of the plane up and down. Any guidance or suggestions would be greatly appreciated as I seem to be stuck at the moment.

EDIT: Provided an update regarding the last approach I tried which involved utilizing the tween.js library.

EDIT2: After further experimentation, I managed to achieve some movement in the plane vertices, but it seems that there are still shortcomings in achieving the desired effect. The plane now appears to 'shiver' and expand over time, which was not intentional. Seeking assistance to overcome this hurdle.

Answer №1

Hello @Jan, I stumbled upon your query regarding updating/refreshing the scene in Three.js.

It seems you are facing issues while trying to update the scene due to multiple tasks being performed simultaneously. My suggestion would be to simplify by focusing on creating a PlaneGeometry within a canvas window and incorporating a button to refresh the scene by moving just one vertex initially before adding additional complexity.

$(document).ready(function() {
  console.log("BEGIN");

  function render() {
      requestAnimationFrame( render );
      renderer.render( scene, camera );
  }

  function refresh() {
    console.log("Refreshing...");
    plane.geometry.vertices[0].z = 50;
    plane.geometry.verticesNeedUpdate = true;
  }

  var width = 600;
  var height = 400;

  var refreshButton = document.createElement("input");
  refreshButton.type = "button";
  refreshButton.value = "REFRESH SCENE";
  refreshButton.onclick = function(){refresh()};
  document.body.appendChild(refreshButton);

  var scene = new THREE.Scene();
  var camera = new THREE.PerspectiveCamera(45, width / height, 0.1, 1000 );

  var renderer = new THREE.WebGLRenderer();
  renderer.setSize(width, height);
  document.body.appendChild(renderer.domElement);

  var geometry = new THREE.PlaneGeometry(300, 168, 300 , 168);
  var material = new THREE.MeshBasicMaterial({color: 0xffff00, side: THREE.DoubleSide});
  var plane = new THREE.Mesh(geometry, material);
  scene.add(plane);

  camera.position.set(200, 200, 200);
  camera.up = new THREE.Vector3(0, 0, 1);
  camera.lookAt(new THREE.Vector3(50, 0, 0));
  scene.add(camera);

  render();

});

This setup will generate a button and a canvas window displaying a yellow PlaneGeometry. Clicking the button will move plane.geometry.vertices[0] to a z-coordinate of 50 (from its initial position) refreshing the display in the canvas window (located at the far right corner of the PlaneGeometry).

Furthermore, this process can be extended to manipulate all vertices individually using an algorithm to create dynamic transformations rather than random shaking for a more nuanced effect.

Additionally, utilizing:

var ray = new THREE.Ray(camera.position, vector.subSelf(camera.position).normalize()),intersects = ray.intersectObject(plane);

You can interact with a specific vertex on the PlaneGeometry canvas, triggering various reactions among other vertices over time.

I trust this insight proves beneficial! =^)

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

Verify the grid of buttons in my code for the background color

My goal is to make sure that when all the buttons are black, the h2 text in the 'lightsoff' div is displayed. If any buttons are not black, the text will remain hidden. I plan to achieve this by creating a new function that checks the background ...

Printing incorrect value in $.ajax call

I came across this code that I have been working on: var marcas = { nome: '', fipeId: '' }; var marcasVet = []; var select; $.ajax({ dataType: "json", url: 'http://fipeapi.wipsites.co ...

jQuery alert: A TypeError was caught stating that the object being referred to does not have the 'dialog' method available

For a few days now, I have been tirelessly searching for a solution to this issue and it has caused me quite a bit of stress. My problem lies in echoing a JQuery popup script within php: echo '<link rel="stylesheet" href="http://code.jquery.c ...

What is the process for creating and registering custom Handlebars functions?

Despite spending plenty of time searching, I am still unable to find detailed information on where exactly to place my custom handlebars helpers. Should they be added in a <script> tag within my webpage's .hbs file? Or should I include them in a ...

Using Angular 2 routes to navigate to various applications

I'm currently developing multiple versions of my application in different languages. Utilizing AOT (ahead of time) compilations, the end result is static deployable sites organized in a structure like this: dist - index.html -- default entry file f ...

The output is displaying an Object instead of a numerical value in JSON

When I try running the URL in Chrome, the output I receive is: { "Train_score": { "0": 0.9892473118 }, "Test_score": { "0": 0.9831932773 } } However, when I attempt to use the following code to retrieve the JSON data using Javascript, co ...

Hover over the sprites using the "spritely" plugin to see the magic unfold

I'm looking to initiate an animation when the mouse hovers over a sprite and have it stop when the mouse moves away. After exploring various solutions, I found one that seemed promising: Why does jQuery spritely animation play an extra frame on secon ...

Exploring the depths of JSON arrays: Understanding multidimensional structures

I have the following array: { "sEcho": 1, "iTotalRecords": 54, "iTotalDisplayRecords": 54, "aaData": [ [ "79", "testowy2", "testowy samochod", "12.00", "14.00", ...

Using Express.js to import a SQL file

I am facing challenges while attempting to import an sql file into Express JS. I have tried various codes involving the mssql and fs modules, but none seem to be working as expected. fs.readFile(__dirname +'/database.sql', function (err, sqlFile ...

Upgrade minimist is available

Recently, an error message has been appearing on my GitHub account: Upgrade minimist to version 0.2.1 High severity Vulnerable versions: < 0.2.1 Patched version: 0.2.1 Minimist before 1.2.2 could be manipulated into adding or changing properties of O ...

Having trouble displaying API values in b-form-select component in Vue.js?

I am using an API to fetch users data and I want to bind these users to a b-form-select component in Bootstrap Vue. However, after making the request, I only see "null" in the b-form-select. Here is my request: getAllUsers() { axios.get(&a ...

Issue with the page base and Jquery Ui tabs URL

Having an issue with jqueryui tabs. Here's the code I'm using: // jQuery Tabs $( "#tabs" ).tabs(); // Open correct tab based on URL hash var hash = window.location.hash; var index = $("#tabs a").index($('#link-'+hash.replace('#& ...

Prevent Repetition of Meta Descriptions and Keywords within Next.js

I'm currently working on my website using Next.js. In order to enhance the SEO performance of my site, I am making an effort to steer clear of duplicate meta tags. My Inquiry According to Next's official documentation, they suggest that using t ...

Utilizing the <Link> component in React Router to generate URLs based on the root domain

When users visit goodsite.com, they encounter a link created using react router. The link code snippet is as follows: <TableCell component={Link} to={`happy/${thing.id}`} > {thing.name} </TableCell> This particular link is situa ...

Getting started with imports in typescript package

Having trouble with imports in a TypeScript package. I have two classes, A and B, located in the models directory. Currently, I am importing class B into class A like this: import B from "models/B" interface A { b: B } export default A; H ...

Resolution for Vue3: Understanding why a component instance's template ref cannot locate a defined function

LoginInfo.vue <script setup lang="ts"> import { rules } from './config/AccountConfig' import { reactive } from 'vue' import { ref } from 'vue'; import { ElForm } from 'element-plus'; const info = reac ...

Transforming intricate state with Redux reducers

I'm struggling to understand the process of updating deeply-nested state in Redux. It's clear to me how to combine reducers and modify top-level state properties, but I'm unsure about modifying deeply-nested properties. Let's consider a ...

What is the best way to modify specific data retrieved from an API using Angular?

After successfully listing some data from an API using ngFor, I am facing an issue with editing the data. Whenever I click the edit button, it edits the entire data instead of just the specific row. Below is the code snippet for reference: HTML <table ...

Having trouble creating a polygon from JSON in ESRI ArcGIS Javascript framework?

Can anyone assist with understanding the issue of trying to draw a polygon using the ESRI ArcGIS Javascript API? The image, JSON string, and code snippets provided below outline the code, console output, and expected behavior. Any help would be greatly app ...

Hide a div using jQuery and then reveal it when clicked

I am trying to create a sliding effect for a div that appears behind another div when an arrow is clicked, and then hides again when a form button is clicked. While I can code most of this functionality, I am struggling with the specific implementation of ...