Error: An unhandled promise was rejected due to a TypeError: Attempting to access the 'length' property of undefined vertices in three.js

Currently in the process of transforming an html project into a laravel/vue setup, I stumbled upon a three.min.js file within the original html project. Unfortunately, the version of this file remains elusive to me along with some custom script files.

To remedy this, I imported all the necessary files for my vue component. However, issues persist with the three.min.js file prompting me to install version r69 of three.js due to recent changes in geometry structures. One of the custom scripts named test.js relies on three.js and has led to the following error in my console log:

Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'length') at test.js?t=1674045010699:37:39

The code snippet from test.js is as follows:

import * as THREE from "three";

// import ('../../assets/js/three.min.js')
// import "../js/anime.js"
// import "../js/three.js"

var canvas = document.querySelector("#scene");
// rest of the code...

While encountering these errors, it's worth mentioning that my primary objective revolves around converting the entire project into vue/laravel. If anyone has insights on resolving the issue at hand, your feedback would be greatly appreciated.

Answer №1

I have updated the code to a more recent version of three.js. The code now utilizes BufferGeometry (the new geometry format) which has resolved the original runtime error.

const renderer = new THREE.WebGLRenderer({antialias: true});
renderer.setPixelRatio(window.devicePixelRatio);
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);

window.addEventListener('resize', onWindowResize);

const scene = new THREE.Scene();
scene.background = new THREE.Color(0x161d31);

const camera = new THREE.PerspectiveCamera(100, window.innerWidth / window.innerHeight, 0.1, 10000);
camera.position.set(120, 0, 300);

const light = new THREE.HemisphereLight(0xffffff, 0x0c056d, 0.6);
scene.add(light);

const light1 = new THREE.DirectionalLight(0x590d82, 0.5);
light1.position.set(100, 300, 400);
scene.add(light1);

const light2 = light1.clone();
light2.position.set(-100, 300, 400);
scene.add(light2);

const material = new THREE.MeshLambertMaterial({
  emissive: 0x23f660,
  emissiveIntensity: 0.4
});

const geometry = new THREE.IcosahedronGeometry(120, 12);
const positionAttributeBase = geometry.getAttribute( 'position' ).clone();

const shape = new THREE.Mesh(geometry, material);
scene.add(shape);

const simplex = new THREE.SimplexNoise();
const vector = new THREE.Vector3();

function updateVertices(time) {

  const positionAttribute = geometry.getAttribute('position');

  for (let i = 0; i < positionAttributeBase.count; i++) {
    vector.fromBufferAttribute(positionAttributeBase, i);
    const noise = simplex.noise3d(
      vector.x * 0.006 + time * 0.0002,
      vector.y * 0.006 + time * 0.0003,
      vector.z * 0.006
    );
    const ratio = noise * 0.4 + 0.9;
    vector.multiplyScalar(ratio);
    positionAttribute.setXYZ(i, vector.x, vector.y, vector.z)
  }
  positionAttribute.needsUpdate = true;
}

function animate(time=0) {
  requestAnimationFrame(animate);
  updateVertices(time);
  renderer.render(scene, camera);
}

function onWindowResize() {

  camera.aspect = window.innerWidth / window.innerHeight;
  camera.updateProjectionMatrix();

  renderer.setSize(window.innerWidth, window.innerHeight);

}

animate();
body {
    margin: 0px;
}
<script src="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="77031f051212374759464340">[email protected]</a>/build/three.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="dca8b4aeb9b99cecf2ede8eb">[email protected]</a>/examples/js/math/SimplexNoise.js"></script>

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

Issues with basic calculator implemented in HTML

Recently, I've been dabbling in HTML. My goal is simple - to create a form where users can input two numbers, add them together, and see the result in one of the input fields. Here are the two input fields: <input type="text" id="Nu ...

box tick does not alter appearance

I've been struggling with this seemingly simple issue for an hour now. I have a radio button set up with two buttons: <input class="form-control icheck" id="cash_prize" name="cash_prize" type="radio" value="1" style="position: absolute; opacity: 0 ...

React- Error: Unhandled Type Mismatch in function call for _this4.getNotes

As I delve into learning React, I've created a basic application to manage notes with titles and descriptions. This application interacts with a REST API built using Go, enabling me to perform operations like retrieving, editing, creating, and deleti ...

The Evolution of Alternatives to contentEditable

Related: ContentEditable Alternative I am curious about the timeline of online WYSIWYG editors prior to the existence of contentEditable. I remember using GDocs and GMail with rich-text features that functioned similarly to contentEditable. I would appre ...

Navigating JSON encoded strings with jQuery

Currently, I am utilizing Django to create JSON encoded objects that are then retrieved using jQuery.getJSON(). The standard simplejson encoder encodes strings following the JSON "standard". For example, any string containing a '/' is converted t ...

The Three.js camera imported from Collada is unable to properly focus on an object within the scene

Having some trouble grasping the concept of Collada Animation in Three.js! I have an animation with a moving camera in 3Dsmax, and exported the scene into Collada. loader.load( ColladaName, function ( collada ) { model = collada.scene; model.upda ...

Fixing the Timeout Error in Node.js & Mongoose: A Step-by-Step Guide

After developing an API, I encountered the following issue: const express = require("express"); const router = express.Router(); const {getAttendanceSheet,getDailyAttendance} = require("../../services/attendanceService"); router.get(& ...

Can you provide instructions for setting a projection matrix in Three.js that accomplishes this specific type of mapping?

Can you provide guidance on creating a projection matrix in Three.js that follows a specific rule for mapping 3D points to screen coordinates? screen_x = x - z*0.5 screen_y = y - z*0.5 depth = z ...

Alphabetic divider for organizing lists in Ionic

I am currently working with a list in ionic that is fetched from a controller and stored in localStorage. My goal is to add alphabetic dividers to the list, but I am facing some confusion on how to achieve this. Here is a snippet of the code: app.js $ion ...

Execute function prior to redirection of iframe

I have a specific challenge with an iframe on my webpage. I am looking to trigger a function right before the iframe reloads a new page, rather than after it has finished loading. I need this function to be triggered before the iframe redirects to the new ...

Real-time Data Stream and Navigation Bar Location

Utilizing the EventSource API, I am pulling data from a MySQL database and showcasing it on a website. Everything is running smoothly as planned, but my goal is to exhibit the data in a fixed height div, with the scrollbar constantly positioned at the bott ...

Another class is overriding the border of the text-box

I'm facing a challenge in customizing the material ui css. Specifically, I want to set the border color of a textbox to red. The issue arises when the bottom border gets overwritten. Upon investigation, I discovered that the culprit is the class MuiIn ...

Spin an object around the global axis using the tween.js library

I'm trying to achieve a gradual rotation of a cube on the world axis using tween. Initially, I was able to rotate the cube around the world axis without tweening with the following code: rotateAroundWorldAxis(cube[1], new THREE.Vector3(0,1,0),degreeT ...

Error throwing when attempting to use additional plugins in Angular CKEditor

I have been attempting to utilize the angular ckeditor extension found at: https://github.com/lemonde/angular-ckeditor UPDATE: I have already injected 'ckeditor' into the app modules. Within my partial html, I am including the following direc ...

What is the best MySQL data type for storing JavaScript code with PHP?

I am creating a platform that resembles jsfiddle, allowing users to store their JavaScript codes and retrieve them in an organized manner. I am unsure about which data type would be most suitable for saving the codes, or if storing them in text files wou ...

Shorten a string once a particular word is found in either Smarty or JavaScript/jQuery

I'm currently working on a website and encountering a minor bug related to the addition of content at the end of some strings. For instance: style="background-image:url({$sub.image});" displays style="background-image:url(http://blablalba.fr/phoenix ...

Mongoose stores data in two separate collections

Hey everyone, I could really use some assistance with this issue that has been bothering me for the past few days. model/user.js var UserSchema = mongoose.Schema({ username:{ type: String, unique: true, index:true }, password:{ type:Strin ...

I am struggling to capture the user's input and display it on a webpage using HTML and JavaScript. Can you help me identify where I may be going wrong in this process?

Good day! I am fairly new to the programming world and have recently embarked on my first JavaScript project. I've chosen to create a simple budgeting app. However, I'm struggling with displaying user input on the page. Something seems off with ...

Send a response from socket.io to the client's browser

I am working on a project where I need to retrieve the ID of the current drawer from the server and pass it to the client. Here is the code I have so far: Client Side: socket.emit('returnDrawer'); socket.on('returnDrawer', fu ...

Encountering issues with resolving dependencies in webdriverIO

I'm attempting to execute my WebdriverIo Specs using (npm run test-local) and encountering an error even though I have all the necessary dependencies listed in my package.json as shown below: [0-2] Error: Failed to create a session. Error forwardin ...