Creating see-through front walls in a 3D room using THREE.js

I am aiming to create a 3D room using three.js. My goal is to have the walls facing the camera rotate while becoming transparent.

If you need an example, check out this link: http://jsfiddle.net/tp2f2oo4/

It appears that adding THREE.BackSide to the material might be the solution.

var material2 = new THREE.MeshPhongMaterial( {
    color: 0xffffff, 
    transparent: false,
    side: THREE.BackSide
} );

This method works well when the room is represented by a single THREE.BoxGeometry. However, in my scenario, each wall, ceiling, and floor are separate THREE.BoxGeometry objects. Is there a way to either hide them or prevent them from rendering when they are facing the camera?

Answer №1

Are you looking to conceal a mesh under certain conditions? Utilizing the onBeforeRender() and onAfterRender() methods could be the solution you need.

// functions
var onBeforeRender = function() {

    var v = new THREE.Vector3();

    return function onBeforeRender( renderer, scene, camera, geometry, material, group ) {

        // Customize this based on your requirements.
        if ( v.subVectors( camera.position, this.position ).dot( this.userData.normal ) < 0 ) {

            geometry.setDrawRange( 0, 0 ); // Adjust visibility here instead of using visible = false

        }

    };

}();

var onAfterRender = function( renderer, scene, camera, geometry, material, group ) {

    geometry.setDrawRange( 0, Infinity );

};

// create mesh
mesh = new THREE.Mesh( geometry, material );
mesh.position.set( 0, 0, 10 );
mesh.rotation.set( 0, 0, 0 );
scene.add( mesh );
mesh.userData.normal = new THREE.Vector3( 0, 0, - 1 );
mesh.onBeforeRender = onBeforeRender;
mesh.onAfterRender = onAfterRender;

See it in action: http://jsfiddle.net/3qh815xa/

Using three.js version r.114

Answer №2

To determine which walls to hide, you can utilize ray-casting. The concept involves projecting a ray from your camera and adjusting the transparency of any walls it intersects.

For a basic example, refer to the updateWallTransparency_simple function in the code snippet below. This function shoots a ray from the center of the camera and modifies the transparency of the wall it hits.

In certain scenarios, such as navigating corners, you may encounter specific challenges. To address this, implement a secondary raycast with modified offsets to prevent hitting the same wall at a corner. You can find an illustration of this technique in the updateWallTransparency_corners function.

Remember that this is a simplified demonstration. If your walls have complex shapes, additional checks will be necessary to control which walls should remain visible. Additionally, tilting the camera excessively upwards might cause rear walls to disappear. There are several factors to consider when dealing with this problem, but these initial steps should set you on the right path.

// Your custom JavaScript code goes here
// Feel free to modify and enhance as needed

function customFunctionality() {
  // Add your unique logic here
  
}

// Consider integrating more features and functionalities as required

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

The expected React component's generic type was 0 arguments, however, it received 1 argument

type TCommonField = { label?: string, dataKey?: string, required?: boolean, loading?: boolean, placeholder?: string, getListOptionsPromissoryCallback?: unknown, listingPromissoryOptions?: unknown, renderOption?: unknown, getOptionLabelFor ...

HTML5 enables the creation of an imperceptible scrollbar

I am looking to make my scrollbar invisible without it being visible however, I still want it to function when hovering over the box Also, I need it to work smoothly on both tablets and computers, which is why I have opted for using html5 I would great ...

Invoking the callback function within the containing scope in Typescript

I am facing an issue with my Angular component where I have a class that includes common services and functions. While passing some functions as callbacks, the scope is getting lost during execution. Let me demonstrate the problem through the code below: @ ...

Leveraging the power of React in conjunction with an HTML template

After purchasing an HTML template from theme forest, I am now looking to incorporate React into it. While I find implementing Angular easy, I would like to expand my skills and learn how to use React with this template. Can anyone provide guidance on how ...

Pass a JSON document to a pug template using Express and Node

Apologies for my inexperience with express, but I am currently working on a pug template for a basic homepage. Alongside this, I have a json file stored on the server containing a list of events. My goal is to retrieve the data from this json file and disp ...

Revising the input value by updating the properties

After clicking on the Reset link, I encounter an issue where the input value fails to update properly. class DiscountEditor extends Component { render() { <div className="inline field"> <a className="ui reset" onClick={thi ...

Is there a way to invoke a class method from the HTML that is specified within its constructor function?

class Welcome { constructor() { this.handlePress = this.handlePress.bind(this); this.htmlContent = `<a onclick="this.handlePress">Link</a>`; } handlePress(e) { console.log('planet'); } } The HTML structure appears ...

Unexpected '->' found on page during loading

After migrating my WordPress site from localhost to the live server, I encountered an issue where "-->" appeared before the page finished loading. Can anyone explain this strange behavior? In addition to this issue, the jQuery scripts I had implemented ...

Can React components be saved in an array?

I am currently working on enhancing the code snippet provided below. This code is intended to iterate through elements of an array using keys to locate images within a lengthy SVG file directly embedded in the document under the identifier "SomelongUglySVG ...

Issue with Jquery autocomplete not populating data in additional fields

Issue: I have implemented a dropdown list for selecting suppliers, and based on the selected supplier, users can search for items using JQuery Autocomplete. However, when an item is selected from the search results, the text boxes for 'description&apo ...

Angular code causing an unexpected blank page to be printed again

Trying to display the content of my HTML page is proving to be a challenge. I've been utilizing angularPrint, but an issue persists in the form of a second blank page appearing in the preview alongside the actual content. Eliminating this unwanted sec ...

Building a Dynamic CSS Grid

Today is the first time I'm reaching out for help rather than figuring things out on my own. I'm currently working on a website layout that consists of visible tiles all with equal sizes, forming a grid structure. The challenge I face is making t ...

Encountering an issue while trying to import the D3.js library into my React.js application

I'm having trouble with properly importing the d3.js libraries in my react.js app. I am using import * as d3 from 'd3' to import everything and store it in the d3 namespace, but I keep getting an error that says - Cannot read property ' ...

Finding the center of a Three.js Scene

I'm trying to find the center of a Scene that I've loaded using ColladaLoader in Three.js. Once I have the center, I want to set it as the target for the Camera. ...

Updating part of a page while also changing the navigation

Apologies in advance, as this is my first attempt at coding a website. I have a specific need where I want to update only one div on my web page when a link in the navigation bar is clicked. <body> <div id="wrapper"> <header id= ...

Accessing Row Data from a Material Table using a Button Click, Not through Row Selection

My React component features a material table view, shown here: https://i.stack.imgur.com/OUVOD.png Whenever the delete icon is clicked in the table, I want to access the rowdata associated with that particular row. However, all I am able to retrieve is ...

Using Vue.js to Authenticate Users with JWT Tokens Sent in Registration Emails

Hey everyone, I could really use some assistance with JWT tokens. I created a registration form using Vue.js and sent the data to the database using axios. Now, I need help figuring out how to save the token I receive in the confirmation email so that I ca ...

What is the best way to format Select2 pre-population using Ajax?

We have come across different methods of pre-populating selected options for Select2, but none of them seem to address the issue of formatted lists and selection options. A JS fiddle showcasing this problem can be found at https://jsfiddle.net/gpsx62de/26/ ...

Troubleshooting tip for React JS: encountered an unexpected object error when trying to import components

I've been working on building a react app with Create React App, and everything was going smoothly until I encountered a frustrating error message: Element type is invalid: expected a string (for built-in components) or a class/function (for composite ...

JavaScript - asynchronous XMLHttpRequest with increment operation

Hello, it's my debut on SO, although I've been a regular user for quite some time and found all the answers I needed here...until now. I'm currently grappling with an intricate issue involving XHR. While I consider myself a bit of a JS newb ...