three.js: Custom built shapes

I have been attempting to create a custom THREE.Geometry from scratch. However, when I try to use it with a THREE.Mesh, I encounter the following error:

TypeError: faces3 is undefined @ three.js:20426

The geometry is successfully created and displayed on the screen, although I am faced with this error message.

Below is the code snippet that I am currently using:

makePlane = function(width, height)
{
    var geom = new THREE.Geometry();

    var v0 = new THREE.Vector3(0, 0, 0);
    var v1 = new THREE.Vector3(width, 0, 0);
    var v2 = new THREE.Vector3(width, height, 0);

    geom.vertices.push(v0);
    geom.vertices.push(v1);
    geom.vertices.push(v2);

    var face = new THREE.Face3(0, 1, 2);

    geom.faces.push(face);

    return geom;
};

Can anyone provide insight into what additional steps may be required for proper functionality?

Update: The issue persists in version R62. This problem is not exclusive to custom geometries as it also occurs with geometry generated by three.js' built-in PlaneGeometry class. It's possible that this is a bug within three.js, and I intend to report it accordingly.

I must clarify that contrary to my previous statement, execution does not actually stop. I simply receive the error message and believe there should be no errors if everything is set up correctly.

Update 2: A related issue has been documented here, however, it appears to have been resolved and closed.

Answer №1

In order to ensure accurate results in subsequent computation steps, it is recommended to incorporate the following lines prior to returning the geom-Object:

geom.computeCentroids();
geom.computeFaceNormals();
geom.computeVertexNormals(); 

Answer №2

To start off, ensure that you have the most recent version of the three.js library installed.

For geometry to function properly, it is important to have certain elements in place. You will need a renderer, camera, scene, and the actual geometry.

According to the documentation for Geometry, you should include methods like computeCentroids() and computeBoundingBox() to process the geometry data.

Avoid returning values as it can prevent your object from appearing correctly. Here is a functional code snippet based on your requirements. Just double-check your reference to three.js:

<!DOCTYPE html>
<html lang="en">
<head>
    <title>Custom Geometry</title>
</head>
<body>
    <script src="three.min.js"></script>
    <script>

        var container,
            camera,
            scene,
            renderer,
            geom;

        init();

        function init() {
            container = document.createElement('div');
            document.body.appendChild(container);

	//renderer
            renderer = new THREE.CanvasRenderer(); //Using CanvasRenderer with simple geometry
            renderer.setSize(window.innerWidth, window.innerHeight);
	    //scene
            scene = new THREE.Scene();
            //camera
            camera = new THREE.PerspectiveCamera(60, window.innerWidth / window.innerHeight, 1, 2000);
            camera.position.z = 500;

            //Your custom geometry creation goes here
            makePlane(200, 300);

            container.appendChild(renderer.domElement);

            renderer.render(scene, camera);

        }

        function makePlane(width, height) {

            var material = new THREE.MeshBasicMaterial({ color: 0x297bbb });
            geom = new THREE.Geometry()

            geom.vertices.push(new THREE.Vector3(0, 0, 0));
            geom.vertices.push(new THREE.Vector3(width, 0, 0));
            geom.vertices.push(new THREE.Vector3(width, height, 0));

            geom.faces.push(new THREE.Face3(0, 1, 2));

            //return geom; //This line is not needed

            geom.computeBoundingSphere(); //<--Include necessary calculations here

            geoMesh = new THREE.Mesh(geom, material); //Adding custom geometry to a mesh using a particular material
            geoMesh.position.y = -50;

            scene.add(geoMesh);
        };

    </script>
</body>
</html>

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

Creating immersive 3D graphics using Three.js

Exploring three.js for the first time and experimenting with displaying a basic 3D model using three js, Vue, and Laravel. The 3D file can be found at /public/files/Tree1.3ds. Need help rendering the file in the Vue component with three js. Initially tried ...

Refresh and storing canvas after submitting form

Before submitting a form, I am executing the following code. The process involves changing an image and then redrawing a canvas before saving it. Unfortunately, the issue is that the saved image does not reflect the changes made (it still shows the previ ...

Main.js in Electron cannot find the NODE_ENV environment variable

Currently, I am in the process of developing an application which involves: React 16.4.0 Electron 2.0.2 Webpack 4.11.0 After successfully compiling and running the app using webpack (webpack dev server), my focus now shifts to showing only Chr ...

I am interested in discovering the optimal method for loading a vehicle to its maximum capacity by exploring various combinations

My current task involves developing an algorithm to optimize the filling of a vehicle to its capacity based on various input combinations. Although the core problem has been resolved, the algorithm's efficiency is inadequate when dealing with a higher ...

Is the "supports" feature supported by the browser?

As I make my return to web development after some time away, I can't help but wonder if the ancient art of CSS hacks could be the solution to bridging the gap between modern @supports queries and outdated browsers that don't support these feature ...

The vertical navigation pills in Bootstrap 4 are not functioning properly as anticipated

I've been referring to the bootstrap 4 documentation for my project. After copying and pasting the code snippet for vertical pills, I noticed it wasn't functioning correctly. Instead of displaying text next to the buttons, it was appearing belo ...

Use a for loop to iterate through elements and retrieve input values using getElementById()

As I work through a for loop in JavaScript, I am utilizing the getElementById() method to fetch multiple input values. To begin, I dynamically created various input boxes and assigned distinct id's using a for loop. Subsequently, I am employing anoth ...

Starting up a React application with the constructor

I am trying to understand the logic behind having to instantiate a new AppInitializer in order to run it. Why is it necessary to use new AppInitializer().run(); instead of just AppInitializer.run();? class AppInitializer { run() { render( &l ...

Moving Image Progress Indicator

I am currently developing a progress bar animation that is designed to animate from 0 to a specified percentage when the progress bar becomes visible within the browser's viewport. The animation must always trigger when the element is scrolled into vi ...

Unable to establish connection between Router.post and AJAX

I am currently in the process of developing an application to convert temperatures. I have implemented a POST call in my temp.js class, which should trigger my ajax.js class to handle the data and perform calculations needed to generate the desired output. ...

Modify an element in an array seamlessly

Seeking a Solution: Is there a way to ensure atomicity when checking and updating a document's child array in MongoDB? Specifically, I want to see if an entry exists, update it if it does, or add it if it doesn't, all while also adjusting a coun ...

Issues with Async Ajax functionality are being reported specifically in Safari browsers when a redirect

Encountering issues with an async Ajax call not functioning in Safari while working perfectly fine in other browsers. The problem arises when there is a redirect/link on the button or anchor that triggers the Ajax function. Here's a basic example: & ...

How can I restrict the number of input data to 10 on a single page in a TodoList application?

As a newcomer to web development, I recently built a todoList app. It's functioning flawlessly and allows me to add multiple lists to my webpage with ease. The technologies used are React js and Bootstrap. However, I now wish to implement a limitati ...

The error message "global.HermesInternal - Property 'HermesInternal' is not found in the type 'Global & typeof globalThis'" appeared

I ran the auto-generated code below: $ react-native init RepeatAloud App.tsx /** * Sample React Native App * https://github.com/facebook/react-native * * @format * @flow strict-local * ...

Time picker in Bootstrap - Ensuring end time is not earlier than start time validation

How to prevent users from selecting a past time for the end time in Bootstrap time picker with start time validation <html> <head> </head> <body> <link href="https://maxcdn.bootst ...

Struggling with a straightforward issue in Ajax that is proving to be a challenge to resolve

I've written a simple code to call an asmx webservice (xml). function maxTransaccion() { $.ajax({ type: "POST", url: "WebService.asmx/MAxTransaccion", contentType: "application/json; charset=utf-8", dataType: "json ...

What is the best way to ensure that a specific number of XHR callbacks have successfully completed before proceeding with further actions?

I have four requests that each have their own callback and can fire in any order. However, I need all the callbacks to finish successfully before executing mergeData. The issue with my current approach is that the initial parameter values do not refresh ...

Having difficulty interpreting the date

Currently, I am attempting to extract the date from the given format: dateFormat: "d-M-y" // results in 10-Oct-13 To format the date, I am utilizing jQuery UI. Here is the code snippet I am using: var d1 = new Date("10-Oct-13"); alert(d1); //Works cor ...

Avoid repeating the same options in a dropdown menu

Here is the HTML code I am working with: <div layout="row" layout-align="center center"> <md-select placeholder="Property type" ng-model="id" md-on-open="loadProperties()" style="min-width: 200px; padding: 20px;"> ...

Utilizing the Flex property on the parent div ensures that all child elements receive an equal width

I am currently working on a messaging application and I want the layout to be similar to Twitter or Whatsapp. Specifically, I want the messages from the person the user is chatting with to appear on the left side, while the user's own messages should ...