Discovering neighboring faces in PlaneBufferGeometry using ThreeJS

Seeking a more efficient method to locate neighboring faces that share the same edge within my PlaneBufferGeometry. Currently, my THREE.Raycaster is able to intersect with my object effectively using intersectObject. However, I am in need of finding all adjoining faces.

My current approach involves a somewhat crude method of identifying nearby faces. While it serves its purpose in my specific scenario, it doesn't feel like the ideal solution:

let rc = new THREE.Raycaster();
let intersects = [];
for (let i = -1; i <= 1; i++) {
    for (let j = -1; j <= 1; j++) {
        let v = new THREE.Vector3(x + i, y, z + j);
        rc.set(v, new THREE.Vector3(0, -1, 0));
        rc.near = 0;
        rc.far = 2;
        let subIntersects = rc.intersectObject(mesh);
        for (let n = 0; n < subIntersects.length; n++) {
            intersects.push(subIntersects[n]);
        }
    }
}

Is there a quicker way, perhaps involving

mesh.geometry.attributes.position.array
, to identify these faces? Any suggestions or insights would be greatly appreciated. Thank you in advance!

Answer №1

You noted that there are 50k items in the position array, which doesn't appear to be slow. In this specific case with a 10x10 example totaling 100 items in the array, it runs smoothly. However, when set to 200x200 with 40k items, it still performs fast enough.

'use strict';

function main() {
  const canvas = document.querySelector('#c');
  const renderer = new THREE.WebGLRenderer({canvas});

  // Remaining code is the same as original 

As I mentioned earlier, if you can identify it as PlaneBufferGeometry, you can examine the three.js code to determine the exact face layout. With that knowledge, calculating neighbors based on a faceIndex becomes more efficient. The provided code is generic and suitable for BufferGeometry with an index.

In the code block below, it outlines how to compute neighboring face indices after identifying them using the grid order for PlaneBufferGeometry:

// Code snippet demonstrating neighbor calculations for PlaneBufferGeometry
//
//  b --c
//  |\\1|
//  |0\\|
//  a-- d

// Remaining code snippets follow the same logic as original 

For complex geometries beyond PlaneBufferGeometry, you have the option to generate a pre-defined map of faceIndexs to neighbors. This approach provides an alternative solution if the initial method proves too sluggish.

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

Tips on showcasing information from a deeply nested JSON structure with jQuery

Today I am putting on my front-end hat and tackling a small challenge. I have created an API that generates a directory tree and provides me with a JSON structure like this: { "0": { "children": [ { "name": "still.t ...

Running javascript files in a node environment

Within my Hello.js file, the following script is written: var http = require('http'); http.createServer(function (req, res) { res.writeHead(200, {'Content-Type': 'text/plain'}); res.end('Hello World\n' ...

Exploring Array Iteration with For-Each in JavaScript

function retrieveMovieCredits(movie) { let counter = 0; let castIds = []; let movieIdList = movie; console.log("Movie ID array length-- ", movieIdList.length); movieIdList.forEach((movieId, index) => { let apiEndpoint = domain + 'mov ...

How can I alter the border of a div when hovering over it?

Greetings everyone, I am looking for a way to change the border of a div on mouse hover, with the added challenge that the id is dynamically changed. Any help or guidance would be greatly appreciated. Thank you! ...

Only incorporate the Angular service into the controller when it is necessary

When attempting to incorporate lazy loading for angular services, controllers, directives, and filters, I discovered a way to achieve something similar using RequireJS. However, I am struggling to find a way to dynamically load a service into a controller ...

How can you reset a variable counter while inside a forEach loop?

I am currently working on resetting a counter that is part of a recursive forEach loop within my code. Essentially, the code snippet provided calculates the length of the innermost key-value pair as follows: length = (key length) + (some strings inserted) ...

Change the names of all arrays within a complex nested structure containing multiple arrays

I'm currently working on a recursive "massage" function that requires me to rename specific property keys. So far, I've attempted various recursive methods but haven't had much luck. One task is to eliminate the word 'Array' from ...

Determine whether an image is retina or non-retina using just one picture

Is it possible to use just one picture that is already optimized for retina displays, but automatically resize or crop down to a non-retina version when viewed on a non-retina screen? I'm facing this issue specifically with IE while using a PC. The im ...

Stop Caching with Jquery Infinite Scroll (IAS)

I am using jQuery + IAS to implement infinite scroll functionality on my website. However, I have encountered an issue where the cache is being disabled when making requests for new content. Specifically, the URL that is accessed to retrieve the next page ...

If an error occurs at the top level subscription, then RxJs won't employ switchMap

If my initial Observable encounters an error, then I will not subscribe to the switchMap Observable. Is this achievable? this._profileService.updateProfile(profile).pipe( tap(profile => { this.profile = profile; this.saving = false; this. ...

Using regular expressions to eliminate text located outside of the tags within a string

Presented is a string consisting of an XML string below: var xmlString = "<str>rvrv</str>rvrv<q1>vrvv</q1>vrvrv<q2>rtvrvr</q2>"; I am seeking assistance on how to eliminate text that lies outside the tags (text no ...

Immersive 360-degree video player with global coordinates

I've developed a 360 video player using three.js, and I'm utilizing raycasting to determine the 3D world coordinates whenever I click the mouse. I have been able to successfully retrieve these coordinates. Is there a method to map these 3D world ...

What causes the vertical scroll bar to shift on its own?

I'm puzzled by the behavior of the vertical scroll bar when clicking on "Line 9" - it automatically moves to the top position and subsequent clicks don't affect it. Can someone shed light on why this happens and how to resolve it? I am using Fire ...

Picture in the form of a radio button

Is there a way to use an image instead of a radio button in my AngularJS form? I have managed to hide the radio button using CSS, but it disables the checked event. How can I make the image clickable? position: absolute; left: -9999px; Here is the code s ...

What is the best way to tally the elements of a nested object within a group of objects?

I am working with an array of objects that contain nested arrays of objects, similar to what is shown in Code snippet 1. My goal is to calculate the number of records within the nested array where the parent_user_id matches the id. Based on this criteria, ...

Refreshing a nested object by modifying its array elements in JavaScript

Hello, I am looking for assistance in updating the userSettings variable. Specifically, when a product is removed from the products array, I need to update the sortedProducts array within the userSettings.categories array. I have attempted to accomplish th ...

Exporting Makehuman.js Three.js using the THREE.OBJExporter can easily be accomplished

Currently utilizing https://github.com/makehuman-js/makehuman-js The example showcased exports the mesh from its original source. I am attempting to export it after modifications have been made in the scene. Upon trying to save my scene as an obj file, i ...

What is the best way to accurately slice a plane using 2D text shapes in three.js?

Many alphabets and numbers have characters with internal holes, such as a,b,d,e,g,o,p,q,0,4,6,8,9, etc. When using the 2D shapes of these characters to cut holes on a flat shape, only the outer outline will be cut, and the central hole of each character wi ...

How to Build an Express.js Query by Using a URL Parameter?

Is there a way to retrieve specific data from my MongoDB database based on the URL? For example, if I navigate to localhost:3000/phones, I want to fetch all entries with the category "phones", and if it's localhost:3000/laptops, I need data related to ...

JavaScript may not display height as anticipated

Currently, I am experimenting with JavaScript in order to achieve a website section that is 100% the height of the screen minus the "nav" bar. Imagine it as having two visible components - the first one being large and the second one small at the bottom. ...