Challenges with intercepting objects in THREE.js using Raycaster

Hey there! I'm currently working on generating rays from vertex to vertex and checking if they intersect with a plane geometry. If the intersection occurs, I'm capturing the point of interception and adding it to an array of vertices. However, I suspect that I may not be using the raycaster correctly since the code snippet below is not producing the desired result. Any assistance or advice on this matter would be highly appreciated!

   let vertexCount = geometry.vertices.length;
  let vertexArray = geometry.vertices;
  let planeY = plane.vertices[0].y; 
  let finalGeometry = new THREE.Geometry();
  let intersects;
  let intersectionPoint;

  let raycaster = new THREE.Raycaster();
  for (let i = 0; i < vertexCount; i++){
    for (let k = 0; k < vertexCount; k++){
        raycaster.set(vertexArray[i], vertexArray[k]);
      intersects = raycaster.intersectObject(plane);
      intersectionPoint = intersects.point;
      finalGeometry.vertices.push(intersectionPoint);
    }
  }

Answer №1

When determining if a point intersects a plane, it's important to consider the values of "i" and "k." Here is an example code snippet that demonstrates this concept:

var k;  
for (var i = 0; i < vertlen; i++){
    k = i + 1;
    if (k === vertlen) 
    {
      k = 0;
    }
    raycaster.set(vertarray[i], vertarray[k]);
    intersects = raycaster.intersectObject(plane);
    inpoint = intersects.point;
    final.vertices.push(inpoint);    
  }

However, it has been pointed out in the comments that my initial statement about "i" and "k" always being the same was incorrect. Please refer to the comments for clarification.

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

fileuploader with clipboard functionality and easy drag-and-drop feature using HTML and JavaScript

I am in need of a file uploader that can be implemented using HTML and JS. My goal is to have it work on multiple platforms, specifically Google Chrome, FireFox, and IE9+, with the capability of copying and pasting screenshots as well as dragging and dropp ...

Sorting elements in an array based on an 'in' condition in TypeScript

I am currently working with an arrayList that contains employee information such as employeename, grade, and designation. In my view, I have a multiselect component that returns an array of grades like [1,2,3] once we select grade1, grade2, grade3 from the ...

Enhancing data interpretation using jquery for json data

Is there a way to enhance this? I would like to streamline it, possibly using a loop because in the future I may need to display more than just 10 questions. I attempted to utilize variables with strings instead of "idX" but encountered issues. I prefer to ...

How does ES6 `import` syntax go about evaluating a module precisely?

Consider a scenario where we are dealing with four modules: A, B,C, and D Module A contains the following code: console.log("A evaluated") function AClass { console.log("A constructor") } var aObj = new AClass() export default aObj; In module B: impo ...

Using an Angular dynamic ng-model name within an accordion

Looking for a way to make the ng-model name dynamic in this specific situation. Whenever a department is selected, the form needs to be updated accordingly. Currently, it works with ng-model="department.name", but this is causing some unexpected issues du ...

Run the GetServerSideProps function every time the button is clicked

Struggling with my NextJS app development, I encountered an issue while trying to fetch new content from an API upon clicking a button. The server call is successful, but the update only happens when I refresh the page rather than triggering it with the bu ...

What is the most efficient way to substitute text within an HTML document?

Is there a way to switch between languages on a website to replace text on multiple pages with a simple button click? What is the most efficient method for achieving this? This code snippet only changes text in the first div. Is there a way to implement a ...

Upon installing a global npm package, the system encountered an error stating: 'File or directory not found: ENOENT'

After successfully publishing my first Node.js CLI tool package on npm, I encountered an issue when trying to test it by installing it locally. The warning message "Error: ENOENT: no such file or directory" kept showing up. Steps for Reproduction To start ...

Creating a canvas element within an iframe: A step-by-step guide

I have a unique situation where I have an iframe containing a DIV element named "pageContainer1". My goal is to insert a canvas element into that specific DIV and then be able to access it in order to draw something on it. Despite my attempts so far, this ...

Having trouble sending a POST request from my React frontend to the Node.js backend

My node.js portfolio page features a simple contact form that sends emails using the Sendgrid API. The details for the API request are stored in sendgridObj, which is then sent to my server at server.js via a POST request when the contact form is submitted ...

Displaying the second div once the first div has loaded, then concealing the first div

Current Approach: There are two divs occupying the same space, with div1 set to display:block and div2 set to display:none When a tab is clicked, jQuery hides one div over a period of 2000ms and reveals the other div. Challenge: The goal is for the ...

Vue.js Ajax call is throwing a bizarre error: TypeError - str.replace function not recognized

Recently, I encountered a puzzling error message: vue-resource.common.js Uncaught TypeError: str.replace is not a function while working on an ajax call to retrieve some data: export default { data: () => ({ recipes: [] }), ready() { ...

What could be causing my Apollo useLazyQuery to be triggered unexpectedly within a React hook?

import { useLazyQuery } from '@apollo/client'; import { useEffect, useState } from 'react'; import { ContestSessionResponseInfoObject, GetSessionDocument, HasAccessToRoundDocument, } from '@/graphql/generated/shikho-private- ...

Tips for adjusting height responsively with Bootstrap 4

Utilizing bootstrap to create div elements. Check out the fiddle link below: fiddle On my page, I have 4 divs - two on top and two at the bottom. The height of all 4 card divs should remain the same and not change. In the last div, there could be on ...

The art of sweet redirection with Sweetalert2

I am working on a PHP page where users can input a number in a text field. If the entered number exceeds their available credit, I need to display an error message. Upon clicking the submit button to send the form, the user is directed to makeTransfer.php ...

Error: req.body or req.params.id is not defined in the current context (PUT and PATCH requests)

I'm experiencing an issue where both req.body and req.params.id are returning undefined even though I am using express.json() before the app.patch. I have tried changing the route to /:id, but that did not resolve the problem. Interestingly, it works ...

I am a beginner in the world of MEAN stack development. Recently, I attempted to save the data from my form submissions to a MongoDB database, but unfortunately, I have encountered

const express = require('express'); const bodyParser = require('body-parser'); const mongoose = require('mongoose'); mongoose.connect('mongodb://localhost/test'); const Schema = new mongoose.Schema({ username: St ...

The websocket connection established with apollo-server is somehow producing nonsensical output for the connection params

onConnect should receive the connectionParams supplied by the client and then validate that the token has not expired by checking the token property on the connectionParams object. On the client-side, I send these parameters in the following manner: const ...

Deactivate a function when the dropdown input is selected

I'm currently using a function to attach scroll events to a slider element for navigating through slides. However, I want to temporarily disable this function in specific situations - such as when a dropdown is in focus - to allow smooth scrolling thr ...

"What is the most efficient method to display or hide multiple divs using

Hey, I'm trying to figure out how to work with showing or hiding multiple div elements quickly. Do I really need to number each div like "open,close,show_text"? It seems a bit repetitive if I have to do it for each div 10 times. Open 1 Close 1 hell ...