Unable to configure raycaster layers within Three.js framework

While attempting to configure the raycaster layer to only cast on a single layer, as outlined in the threejs documentation: - I encountered the following error

Uncaught TypeError: Cannot read properties of undefined (reading 'set')

What could be causing this issue? It appears to be a straightforward task

var scene = new THREE.Scene();  // Creating the scene

// Configuring Camera
var camera = new THREE.PerspectiveCamera(
    90, // Field of View (FOV)
    window.innerWidth / window.innerHeight, // Aspect Ratio
    0.1, // Inner Plane
    1000, // Far Plane
);

// Configuring renderer
var renderer = new THREE.WebGLRenderer({antialias: true});
renderer.setClearColor("#e5e5e5");
renderer.setSize(window.innerWidth, window.innerHeight);

// Configuring camera controls
var controls = new OrbitControls( camera, renderer.domElement );
camera.position.set( 5, 0, 3 );
camera.layers.enable(1);
controls.minDistance = 5;
controls.maxDistance = 50;
controls.maxPolarAngle = 1.5;
controls.enablePan = false; // Disables right mouse drag movement
controls.update();

document.body.appendChild(renderer.domElement);  // Attaching the renderer to the body

window.addEventListener('resize', () => {  // Dynamically adjusting canvas size to window
    renderer.setSize(window.innerWidth, window.innerHeight);
    camera.aspect = window.innerWidth / window.innerHeight;

    camera.updateProjectionMatrix();
})

var raycaster = new THREE.Raycaster();

raycaster.layers.set(0);

Answer №1

This issue could be related to the version of ThreeJS you are using.


Here is a solution that has worked for me:

let scene = new THREE.Scene(); // Create scene

// Set up Camera
let camera = new THREE.PerspectiveCamera(
  90, // Field of View (FOV)
  window.innerWidth / window.innerHeight, // Aspect Ratio
  0.1, // Inner Plane
  1000, // Far Plane
);

// Set up renderer
let renderer = new THREE.WebGLRenderer({
  antialias: true
});
renderer.setClearColor("#e5e5e5");
renderer.setSize(window.innerWidth, window.innerHeight);

// Set up camera controls
let controls = new THREE.OrbitControls(camera, renderer.domElement);
camera.position.set(5, 0, 3);
camera.layers.enable(1);
controls.minDistance = 5;
controls.maxDistance = 50;
controls.maxPolarAngle = 1.5;
controls.enablePan = false; // Disable right mouse drag movement
controls.update();

document.body.appendChild(renderer.domElement); // Render the scene on the body element

window.addEventListener('resize', () => { // Update canvas size dynamically on window resize
  renderer.setSize(window.innerWidth, window.innerHeight);
  camera.aspect = window.innerWidth / window.innerHeight;

  camera.updateProjectionMatrix();
})

let raycaster = new THREE.Raycaster();

raycaster.layers.set(0);
body {
  overflow: hidden;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r128/three.js"></script>
<script src="https://threejs.org/examples/js/controls/OrbitControls.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

Error: The window object is not found in this context when initializing the Commerce module from the commerce.export.js file located in the node_modules folder within the @chec

I have been working with the pages router and custom app file (_app.js) https://nextjs.org/docs/pages/building-your-application/routing/custom-app It seems that the issue may be within my _app.js file, where I have the following code: import '../glo ...

Get Angular events in the proper order

I am currently facing challenges with event handling in AngularJs. In my service, I send out events using the following line of code : $rootScope.$emit("FNH."+this.status, flowNodeHelper); When receiving the event in service "S1," I handle it as follows ...

What is the best way to pass a dynamically generated component as the ng-content for another dynamically generated component?

Need help with creating a modal for our library using viewRef.createComponent. I want to generate Component A and embed Component B inside it, utilizing <ng-content> to fetch content from Component B. Component A serves as the modal template, while ...

Node.js applications on Openshift frequently encounter 503 errors

I am currently in the process of setting up my initial Node.js + express web application utilizing Openshift's complimentary service. After installing node + npm and Openshift tools on my computer, I attempted to run my application. It functions perf ...

Unable to access attributes of null (reading 'title) and other information

When the parameter 'edit=true' is present, I expect a form with pre-filled data to render; otherwise, redirect me. However, I am encountering an issue where I keep getting redirected and receiving a console error stating 'Cannot read propert ...

Exploring the Realm of Javacript Template Literal Capabilities

Is it possible to define a variable inside a template literal and then utilize it within the same template? If this isn't feasible, it appears to be a significant feature that is lacking. const sample = tag` some random words, ${let myvar = 55} addit ...

Exploring the benefits of event subscription nesting

One feature I'm currently utilizing is the Angular dragula drag and drop functionality, which enables me to effortlessly move around Bootstrap cards within the view. When an item is "dropped," it triggers the this.dragulaService.drop.subscribe() funct ...

Execute the getJSON calls in a loop for a count exceeding 100, and trigger another function once all

In order to process a large grid of data, I need to read it and then make a call to a $getJSON URL. This grid can contain over 100 lines of data. The $getJSON function returns a list of values separated by commas, which I add to an array. After the loop fi ...

Design an interactive vertical divider using d3

I am interested in developing a vertical rule similar to the one demonstrated at that updates its value dynamically based on the user's mouse movement. The example provided uses cubism.js, however, I would like to achieve the same functionality usin ...

What is the best way to modify or revise meta fields for individual products in order to retrieve multiple images for each product in Shopify?

What is the process for updating or editing meta fields to display multiple images of each product on Shopify? ...

Ways to update the color of the mat-dialog-title using the material theme

Currently, I am utilizing the Angular Material Dialog and have been attempting to dynamically change the title color of the dialog based on the material theme. <h1 mat-dialog-title color="primary">{{ title }}</h1> Even though setting ...

Trouble with implementing an onclick event listener in a foreach loop

While generating and adding HTML in a for loop, I noticed that adding onclick events within the same loop is not functioning correctly: items.forEach(item => { itemHtml = `<div class="${item.id}">${item.id}</div>`; $(".it ...

differentiating between user click and jquery's .click() method

There are <a href=".. tags on a page with an inline onclick attribute. If a user clicks one without being logged in, they will be prompted to log in. After logging in and the page refreshes, jQuery will trigger .click() on the <a> element to redir ...

Concealing video when the source is inaccessible

I am facing an issue with a video element that retrieves its source from a database. The video tag is placed inside a repeater that is bound to the database. My goal is to hide the video if the source is not found in the database. I attempted to use Javasc ...

Encountering an unusual issue: Unable to access undefined properties (specifically 'get')

I'm struggling to get the order history screen to display the order history of a specific user. Every time I navigate to the path, I encounter the error mentioned in the title. I double-checked the path for accuracy and made sure there are no spelling ...

Manipulate a JavaScript object with AngularJS - viewing and editing capabilities

I'm looking to create a dynamic list of objects where clicking on an entry displays the object details and an edit button. When clicking on the edit button, the details should disappear and a form for editing the object should appear on the right side ...

Creating applications with Angular2 and TypeScript is possible even without utilizing NPM for development

I've seen all the guides recommend installing npm, but I'm determined to find an alternative method. I found Angular2 files available here, however, none of them are in TypeScript code. What is the best course of action? Do I need Angular2.ts, ...

Can a value be concealed within a dropdown list on a form (using PHP or JavaScript)?

Within my form, there is a drop down box listing the 50 states in the U.S. This list is generated using a PHP for loop. Users are required to select their residential state and later on, they must also choose a mailing state if it differs from their resi ...

Having trouble getting card animations to slide down using React Spring

I am currently learning React and attempting to create a slide-down animation for my div element using react-spring. However, I am facing an issue where the slide-down effect is not functioning as expected even though I followed a tutorial for implementati ...

Retrieve information using an AJAX call

There is JSON data stored in a file that I need to read. The correct file data is printed to the console.log. What steps should I take to assign this data to variable x? Currently, when I execute the code, x ends up being undefined. function getData() { ...