Using Three.js to implement clipping on a defined region of an object

I am currently using THREE.Plane to clip my STL model.

localPlane = new THREE.Plane( new THREE.Vector3( 0, -1, 0 ), 4);
.
.
.
material = new THREE.MeshPhongMaterial( {
    color: 0xffffff,
    side: THREE.DoubleSide,
    clippingPlanes: [
        localPlane,
    ],
    clipShadows: true
} );

The current setup is functional, but I am encountering an issue where the entire top of the object gets clipped by this infinitely sized plane. My intention is to only clip a small portion of it (Unfortunately, scaling THREE.Plane does not seem to be possible).

Furthermore, I attempted to utilize ThreeCSG.js, however, it proved to be less effective with STL objects!

Please refer to the image below for reference: https://i.sstatic.net/PPbFi.png

Answer №1

Absolutely, three.js fully supports the removal of the intersection of clipping planes. You can achieve this by implementing a code snippet similar to the following:

// Define clipping planes
var localPlanes = [
    new THREE.Plane( new THREE.Vector3( - 1, 0, 0 ), 1 ),
    new THREE.Plane( new THREE.Vector3( 0, - 1, 0 ), 1 )
];

// Set material properties
var material = new THREE.MeshPhongMaterial( {
    color: 0xffffff,
    side: THREE.DoubleSide,
    clippingPlanes: localPlanes,
    clipIntersection: true
} );

For further reference, you can explore the example provided in the official three.js documentation.

This functionality is available in three.js version r.85.

Answer №2

Update: Take the advice from WestLangley. I will keep this as an alternative method, although it may not be as efficient for clipping.

It's a fact that clipping planes are infinite. So, what's the workaround? Implement multiple clipping planes in multiple render passes!

To achieve this, you'll have to disable auto-clearing and manually clear the buffer yourself.

renderer = new THREE.WebGLRenderer();    
renderer.autoClear = false;

Assume that your current clipping plane is named plane1.

material = new THREE.MeshPhongMaterial( {
    ...
    clippingPlanes: [
        plane1,
    ],
    clipShadows: true
} );

var myMesh = new THREE.Mesh(geometry, material);

This will clip the top half of myMesh when you render. To work with the remaining portion, proceed with the following steps.

Firstly, create another plane, let's call it plane2, which is the inverse of plane1. This means that plane2 will clip the BOTTOM of myMesh. By rendering one pass using plane1 and another using plane2, you can get back the full mesh. Now, introduce a third clip plane, plane3, which only clips the desired half of myMesh. When you include both plane2 and plane3 in the same render pass, only 1/4 of myMesh will be rendered.

var pass1ClipPlanes = [
        plane1
    ],
    pass2ClipLanes = [
        plane2, // this plane is the inverse of plane 1, so it clips the opposite of plane1
        plane3 // this clips the left/right half of the model
    ];

When you're ready to render, make sure to clear the draw buffers first before executing two render passes while updating the clip planes between them.

// clear the draw buffers
renderer.clear();

// clip the top
myMesh.material.clipPlanes = pass1ClipPlanes;
renderer.render(scene, camera);

// clip the bottom and one side
myMesh.material.clipPlanes = pass2ClipPlanes;
renderer.render(scene, camera);

The initial pass will render the bottom part of the model, while the second pass will render half of the top part.

Estimated Time of Arrival (ETA): Example

var renderer, scene, camera, controls, stats;

var cube,
pass1ClipPlanes,
pass2ClipPlanes;

var WIDTH = window.innerWidth,
HEIGHT = window.innerHeight,
FOV = 35,
NEAR = 1,
FAR = 1000;

// Remaining code for initialization, resize, rendering, and animation...

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 am encountering an issue where my .glb file is not appearing in the dist/assets directory while working with react.js and three.js

I recently developed an app using react.js and Vitejs, where I integrated a 3D model using Theejs (.glb file). Surprisingly, when I run npm run dev, the 3D model works flawlessly without any issues. However, upon running npm run build, I noticed that the 3 ...

Implementing Title Attribute in Grid View Template Field

I have implemented a Grid View with a "TemplateField" that includes properties for Header Text and SortExpression set to true. Upon inspecting the browser, I noticed that it generates an anchor element with some JavaScript. How can I add a title tag to t ...

What could be causing my Vue application to not launch after executing `npm run serve`?

These past 24 hours have been a struggle for me. I recently embarked on the journey of learning Javascript, and my choice of JS framework was Vue JS. However, when I run npm run serve, my Vue JS app bombards me with numerous errors that seem to make no se ...

Is it feasible to merge Apollo queries within the context of Nuxt?

Incorporating nuxt and apollo together using the https://github.com/nuxt-community/apollo-module module has been a successful venture. A GraphQL query was crafted and tested in GraphiQL to obtain information about a specific page along with general SEO de ...

When using React, appending a React Link tag to an existing list item may result in the addition of two objects instead of the desired

Trying to create a loop that checks if an object's date matches with a date on a calendar. If it does, I want to add a React Link tag to the respective li element. The loop logic works well, but the issue is when appending the Link tag using createTex ...

Error in MongoDB Connection: Timeout issue caused by an unresolved Promise

Issue Overview Whenever I attempt to execute nodemon server, a timeout error is displayed indicating [nodemon] app crashed - waiting for file changes before starting.... Detailed Problem Description I have three files located at the following paths: ...

The unique identifier for retrieving an object from the database is dynamic and subject to change with each

Click here for image description I am currently building an app using Express and Mongoose MongoDB. However, when I try to access /musics/:id to go to the details page, the app redirects me to the correct page but crashes because the id is being changed. ...

Enhance and streamline custom module code within a Node.js Express application

I am seeking suggestions on how to optimize the code within my custom module. Below is the code for my module which you can review and provide feedback on. var employee = { all: function (req, res) { jwt.verify(req.token, 'novatureso ...

An issue of "SignatureDoesNotMatch" arises while trying to send an email using Node AWS SDK for the Simple Email Service

I am facing an issue while attempting to send an email using the @aws-sdk/client-ses SDK in Node. The error I encounter is: SignatureDoesNotMatch: The request signature we calculated does not match the signature you provided. Check your AWS Secret Access ...

What is causing the ng-show function to malfunction after building with phonegap?

My current javascript framework for my PhoneGap app is the Ionic Framework. I have a specific item on one of my pages that I want to make expandable and collapsible by clicking an anchor on the page. Here is what the code looks like: In the HTML file: & ...

Is it possible to show a pop-up window containing aggregated data when the jQuery double-click event

How can I create a pop-up window to display aggregated data when the Double-click event is triggered in jQuery? In my code, each questionId has multiple related reasons. When a user clicks or selects a questionId button/event, the selected questionId will ...

Error: The function exec in matchExpr[type] is not defined

I made some changes to Object.prototype and now I'm running into errors with jQuery's methods on selectors. The error message I'm getting is: Uncaught TypeError: matchExpr[type].exec is not a function Additionally, when trying to use $.po ...

Is CefSharp compatible with JavaScript promises?

I noticed that when I run the JavaScript code below in the console of my browser, it works perfectly fine. However, when I try to use this code in CefSharp, it returns null. I am currently using CefSharp version 100.0.120-pre. Does CefSharp 100.0.120-pre s ...

Button clicking to zoom in and out with three.js

I am looking to include two buttons for zooming in and zooming out. How can I achieve this? I attempted to use the following code, but it did not work for me. var controls = new THREE.OrbitControls(camera, renderer.domElement); function zoomModel(isZoo ...

Developing NodeJS applications locally using Docker

How can I effectively develop an app locally within a container with access to my laptop's file system for the node_modules? Currently, my Dockerfile includes the following settings: COPY package.json /app/ RUN npm install COPY . /app/ And my docke ...

The requested resource at / could not be found on the server

Currently, I am in the process of developing a webpage that utilizes Angular to dynamically alter DOM elements. The project includes a public folder housing HTML, CSS, JavaScript, and JSON files. To share the project, I have set up Node to run it from loca ...

Exploring various ways to implement HTTP GET requests within the PrimeVue DatatableUsing a mix

I am facing a challenge where I need to use different GET requests to populate my Datatable with data from separate tables in the Database. Despite trying different approaches, I am unable to figure out how to make this work successfully. I have realized t ...

Generate a JSON (Jquery) structured table matrix outlining various roles and corresponding permissions such as read, write, delete, and write special

I would like to create a table matrix that displays roles and permissions (read, write, delete, write special) using jQuery with JSON data. The table should contain checkboxes for each permission type, and the checkboxes for read, write, delete, and write ...

The Keen.io JavaScript API is returning an error code of "UnknownError" even though the event creation was successful

I'm attempting to generate a Keen event from parse.com cloud code (node.js). Utilizing a JS module (https://github.com/roycef/keen-parse) that appears to be properly configured. To validate, I've established a straightforward test and provided th ...

How to keep a window/tab active without physically staying on that specific tab

Whenever I'm watching a video on a website and switch to another tab, the video stops playing. But when I switch back to the original tab, the video resumes. Is there a trick to prevent the video from stopping? I've already tried using scrollInto ...