Creating a real-time face generator for a three.js shape

Currently, I am developing webgl software for generating 3D models that heavily relies on dynamic geometry. During my work, I have encountered some unusual behavior which I have managed to isolate in a JSFiddle demo.

It appears that any new faces added after adding a geometry instance to the scene will not be properly rendered. In wireframe mode, the new geometry is not rendered at all. Additionally, when using textured materials, I have noticed that sometimes new geometry is not rendered depending on the camera angle.

You can see this issue in action by watching this video.

Going back to the JSFiddle, I utilized an existing Three.js code sample (misc_exporter_obj.html) as a base, but on line 7, I created a generic function to add a triangle to the geometry. The addGeometry function is called on startup, and if you uncomment line 36, you can see the expected result.

var material = new THREE.MeshBasicMaterial( { wireframe : true} );

geometry = new THREE.Geometry();
addTriangle(-50, -50, 50, -50, 50, 50);
//addTriangle(-50, -50, -50, 50, 50, 50); // UNCOMMENT TO TEST WHAT FINAL OUTPUT SHOULD LOOK LIKE.

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

As recommended in the Three.js guide on updating things, lines 43-47 try to add a new triangle when you click the "transform triangle" button by setting the verticesNeedUpdate and elementsNeedUpdate flags:

function addTriangleFace(){
    addTriangle(-50, -50, -50, 50, 50, 50);
    geometry.verticesNeedUpdate = true;
    geometry.elementsNeedUpdate = true;
}

Am I making a mistake in my approach? Should I consider submitting a bug report?

Thank you.

Disappearing Mesh Update:

I believe I have identified the reason behind the strange behavior that caused my mesh to disappear based on camera orientation. According to this answer, Three.js might have thought that the mesh was outside the camera's frustum.

It seems that the new vertices were not considered when determining if the object was within the frustum, so I decided to disable culling since the object being drawn is the main element in the scene.

Answer №1

If you're looking to incorporate faces into an existing geometry, one effective approach is to transition to BufferGeometry. By preallocating buffers of sufficient size and adjusting the drawRange, you can achieve the desired outcome. Check out this helpful response on Stack Overflow for more insights. Another informative discussion can be found in this linked answer.

When introducing new vertices, it's essential to recalculate the bounding sphere in order for frustum culling to function accurately:

geometry.computeBoundingSphere();

Alternatively, you have the option to deactivate frustum culling as you suggested:

mesh.frustumCulled = false;

Version utilized: three.js.r.91

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

I can't figure out why I'm receiving undefined even though all the variables are populated with the necessary

I have been working on a project that involves implementing email and password authentication using Firebase. However, I encountered an error when the user submits their credentials: FirebaseError: Firebase: Error (auth/admin-restricted-operation). at ...

What could be the reason for data-id coming back as undefined?

I'm having trouble figuring out why the code below isn't working for me. The console is showing 'undefined' for data-id. href='#detailsModal' class='btn btn-info btn-xs' data-toggle='modal' data-id='x ...

"Unlocking the door: a step-by-step guide to logging in with ajax and json for your hybrid

As a beginner coder, I am currently working on a project to create a mobile web login form using json and ajax. To test my code, I followed the tutorial provided here. This is the code I have developed: <!DOCTYPE html> <html> <head> ...

Instead of returning a single array of data from a form as expected, Jquery is returning two arrays

Having a bit of trouble with my code involving radio buttons and arrays using Jquery. I'm trying to record the selected values into one array, but it's creating separate arrays for each fieldset. Here's what I have: <script> $(doc ...

Convert TypeScript-specific statements into standard JavaScript code

For my nextjs frontend, I want to integrate authentication using a keycloak server. I came across this helpful example on how to implement it. The only issue is that the example is in typescript and I need to adapt it for my javascript application. Being u ...

What is the best way to eliminate the "1 empty item" from a JSON object using JavaScript?

This is an example json object that has been modified to remove certain values. { name: { first: 'Robert', middle: '', last: 'Smith' }, age: 25, DOB: '-', hobbies: [ 'running', 'coding', & ...

What is the best way to incorporate multiple functions within a React button?

I need to implement a functionality where the startClick function is triggered on BackButton click before executing the dispatch (giveForm2PreviousStep(props.currentStep, props.goToStep)) method. How can this be achieved? Inquiry JS const Inquiry = props ...

Determine the number of items (within an array) that were created within the past few days, weeks, and months leading up to the 'current time'

Having an array filled with objects containing timestamps: Here is a glimpse of the data: const dataList = [ { _id: "602102db3acc4515d4b2f687", createdDt: "2021-02-08T09:22:35.000Z", }, { _id: "6021024da706a260d89 ...

Looking to make some changes to the javascript countdown script

Hello, I have come across a JavaScript countdown timer code on stackoverflow that seems to be the perfect solution for my countdown timer project. The current code counts down from 30 minutes to 1 minute and then starts over again. However, it provides the ...

Can the warning about a missing dependency in useEffect be incorrect at times?

After using hooks for some time, I still don't quite grasp why React insists on including certain dependencies in my useEffect that I don't want. My understanding of the 'dependencies' in a useEffect hook is as follows: You should add ...

Angular 2 Aot Issue: CRITICAL ERROR: CALL_AND_RETRY_LAST Allocation unsuccessful - JavaScript heap exhausted

Encountered an issue while running Angular 2 AOT rollup: <--- Last few GCs ---> 144518 ms: Mark-sweep 1317.0 (1404.4) -> 1317.0 (1404.4) MB, 1522.9 / 0.0 ms [allocation failure] [GC in old space requested]. 146029 ms: Mark-sweep 1317.0 (1404 ...

Separating express routes into individual files

After trying multiple solutions from Stack Overflow and various patterns for organizing routes in Node.js, I still can't seem to get it right. The endpoint either throws errors or returns a 404. Despite following suggestions from different sources lik ...

I am encountering an issue with the return ( statement and I'm unable to comprehend the reason behind it

import { connect } from 'react-redux' import { Link } from 'react-router-dom' class MyFavoriteStories extends React.Component { markAsFavorite = (e) => { this.setState({ bgColor: "blue" }) } render () { con ...

Using the DatePicker component with non-escaped Latin alphabet characters in date-fns for ReactJS

While attempting to integrate the DatePicker component from Material UI into my React project, I encountered an error. Although many attributed the issue to a version discrepancy, what ultimately resolved the problem for me was assigning a value to the Da ...

"I'm having trouble with the Icosahedron geometry not appearing correctly

I'm currently experimenting with three.js and I've managed to successfully draw and animate a cube. However, when I try to change the geometry to an Icosahedron, nothing shows up on the screen. Within my js source folder, I only have the three.m ...

Are mutations in Vuex guaranteed to be atomic?

I'm currently investigating the atomicity of mutations in Vuex. The code snippet I'm reviewing has me questioning whether the CHANGE_A mutation could potentially be triggered while CHANGE_B is still in progress: const mutations = { [CHANGE_A]( ...

React.map does not retrieve the specific value

I am facing an issue with my list of items. I have implemented it using mui list, and I have also added a button for editing the list. However, when I click on an item, I am getting the value of the last item instead of the one I clicked on. Below is my f ...

What is the best way to manage the back button functionality on pages that use templates?

I am currently developing a website using angularjs. The layout consists of two main sections: the menu and the content area. For instance This is an example page: /mainpage <div> <div id="menu"> <div ng-click="setTemplate('fi ...

Can someone explain the significance of this in an HTML form?

Can anyone explain the following code snippet found in a web form? I found this button code on a webpage, but I'm having trouble understanding it. Could someone provide some clarity? <input type="submit" name="ctl00$ContentPlaceHolder1$Button8" v ...

Nodemon isn't functioning properly: nodemon isn't loading as expected

db.js const mongoose = require('mongoose'); const mongoURI = "mongodb://localhost:27017/?readPreference=primary&appname=MongoDB%20Compass&ssl=false"; const connectToMongoDb = ()=>{ mongoose.connect(mongoURI, ()=>{ ...