Surprising outcome when utilizing ThreeCSG library

Exploring the capabilities of the ThreeCSG library, I'm currently trying to replace the sphere or normal geometry with a custom Shape, specifically the heart shape from the 3js examples.

However, I'm encountering an unusual result on the side facing the camera. Below is a snippet of the code:

(function onLoad() {
  var container, camera, scene, renderer;
  var grey = 0xD3D3D3;
  
  init();
  animate();

  function init() {
    container = document.getElementById('container');
    initScene();
    addGridHelper();
    addCamera();
    addLighting()
    addRenderer();
    addOrbitControls();

createHeartMesh();
    createDieCutHandle();
    
    // Creates a CSG cut on the cube based on the passed mesh
    createCSGDiecutHandle();
  }

  // More functions and scene setup...

})();

The cube on the left shows an extruded heart geometry protruding from inside a cube - they occupy the same position but are not merged.

On the right, we have the CSG version where I try to subtract the heart from the cube. The issue arises on the side facing the camera; it appears distorted compared to the backside. Adding side: THREE.DoubleSide did not resolve the problem.

I've also referred to this related question here, and attempted calling updateMatrix() on the heart mesh without success.

Any insights into what might be causing this behavior would be greatly appreciated. Thanks!

Answer №1

The ThreeCSG plugin might struggle with subtracting concave meshes accurately, but there is a workaround. It involves dividing the mesh into two halves - one convex and the other half as a heart shape.

To subtract the right half of the heart from a cuboid, you can use the following code snippet:

function getHeartShapeRight() {
    var x = 5, y = 10;
    var heartShape = new THREE.Shape();
    heartShape.moveTo(x - 5, y - 5);
    heartShape.bezierCurveTo(x - 5, y - 5, x - 4, y, x, y);
    heartShape.bezierCurveTo(x + 6, y, x + 6, y - 7, x + 6, y - 7);
    heartShape.bezierCurveTo(x + 6, y - 11, x + 3, y - 15.4, x - 5, y - 19);
    return heartShape;
}

You can also subtract the left half of the heart from a cuboid using similar logic:

function getHeartShapeLeft() {
    var x = 5, y = 10;
    var heartShape = new THREE.Shape();
    heartShape.moveTo(x - 5, y - 19);
    heartShape.bezierCurveTo(x - 12, y - 15.4, x - 16, y - 11, x - 16, y - 7);
    heartShape.bezierCurveTo(x - 16, y - 7, x - 16, y, x - 10, y);
    heartShape.bezierCurveTo(x - 7, y, x - 5, y - 5, x - 5, y - 5);
    return heartShape;
}

The final step involves creating a union of both halves to form the complete heart shape:

// Union of left and right halves
var unionBsp = resultBspL.union(resultBspR);
var unionMesh = unionBsp.toMesh(new THREE.MeshLambertMaterial({flatShading: true}));
scene.add(unionMesh);

If you'd like to see the full code snippet for implementing this technique, please refer to this link.

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

There seems to be a glitch causing data chunks to disappear during streaming in Node.js

Upon receiving the response from axios, I am processing it as a stream. As the chunks are being processed, stream.on("end" is triggered. However, this has led to an error: Uncaught SyntaxError: Unexpected token with JSON.parse This issue seems to occur e ...

Tips for iterating through a list of checked values using v-for

Currently working on a feature to create a checkbox that allows only one selection. <div id="app"> <div v-for="(question, index) in questions"> <input type="checkbox" value="question.value" v-model ...

Inquiries about ngshow and the scope concept

I have a question about using AngularJS. I have multiple sections and only want to display one at a time using <section ng-show="section6_us"> </section> and <section ng-show="section7_us"> </section>. My scope has many variables. ...

"The click event doesn't seem to be functioning properly on HTML content loaded dynamically through JavaScript

I have set up a dynamic page that initially loads 10 elements. As the user scrolls down, I use JavaScript to append more elements to the page via AJAX. Additionally, when a tag is clicked, some JavaScript functionality is triggered. Specifically, I am uti ...

Is the Positioning of JS Scripts in Pug and Jade Documents Important?

Have you ever wondered why a page loads faster when certain lines are placed at the end of the tag instead of inside it? script(src="/Scripts/jquery.timeago.js") This phenomenon is often seen in code like this: //Jade file with JQuery !!! 5 html(lang=" ...

Tips on using constructor functions and the new keyword in Typescript

This is a demonstration taken from the MDN documentation showcasing the usage of the new keyword function Car(make, model, year) { this.make = make; this.model = model; this.year = year; } const car1 = new Car('Eagle', 'Talon TSi&apos ...

Passing the socket.io instance to an express route

I am currently working on developing a nodejs application that will utilize various web APIs to search for information. The goal is to send the results to the client in real-time using socket.io, with jQuery handling the front end display of the data. Wha ...

Is the "Angular" approach to creating responsive templates? Should matchMedia be used in the 'controller'?

I am currently working on the following: http://jsbin.com/EDovILI/1/edit Essentially, I am inserting an event listener into the controller. It doesn't feel like the most efficient way to do it, but I'm not sure how to abstract it out. The tem ...

Implementing a Basic jQuery Animation: How to Easily Display or Conceal DIV Elements Using fadeIn and fadeOut

SCENARIO: Include 2 links, MY INFO and LOG IN, within a list of ul, li elements. Hovering over one link should display a box with a form inside using the fadeIn animation. The other link should show a button in a box with a fadeIn animation when hovered o ...

javascript unable to delete cookie

After conducting extensive research across various articles and links on deleting cookies using JavaScript, I have encountered an issue where the JavaScript code does not seem to be functioning as expected. The code I utilized for setting cookie values usi ...

Angular 2 System.config map leading to a 404 error message

I am encountering a 404 error in the browser console while attempting to map the Auth0 module from node_modules in my Angular 2 project using the system.config in the index file. Index File <!-- 2. Configure SystemJS --> <script> System.con ...

Establishing a connection between the socket and the currently authenticated user through socket.io

My website consists of multiple pages, and when a user logs in, I need to establish a connection between their user ID (stored in MongoDB) and the socket for real-time messaging. Currently, user details are saved in the express session variables upon login ...

Transforming an array of strings into JSX Elements and strings in ReactенийTransforming an array of strings

Facing a challenging task: constructing a React code editor that styles various sections of ES6 code. The process involves converting all the code into a single string, then utilizing Regex to pinpoint where to insert <span>, <strong>, and < ...

Expandable Buttons - Bootstrap version 3.3.4

I'm currently working with a bootstrap collapsible button group and I'm facing an issue where only one group box should be visible at any given time. I tried creating a JavaScript function to remove the "in" class and update the aria-expanded att ...

Event to discard currently selected pushpin in Bing Maps v8

There are two event styles available for a pushpin in Bing. enableHoverStyle : Boolean enableClickedStyle : Boolean To see these events/styles in action, visit the link below: My goal is to deselect an already selected pin when another pin is selected. ...

Is there an rxjs operator that includes both on-next and on-error callbacks?

Is there a similar function to promise.then(onNextCallback,onErrorCallback) in rxjs that I can use? I've already tried alternatives like pipe(concatMap(),catchError) but they are not what I am looking for. ...

Achieving proper HTML element order?

Here is the HTML page I am working with: Click here to view the code on jsfiddle <!DOCTYPE html> <html> <head> <meta charset="UTF-8" /> <title>Extended UI</title> <style type="text/css"> .header{ padding-rig ...

Use an external javascript file in AngularJS if permitted

To ensure that my HTML page only loads an external JavaScript file when the variable $scope.jsallowed is set to true, I attempted the following code implementation within my AngularJS based page: <script src="assets/js/slider.min.js" data-ng-if="jsallo ...

Changing selections in jQuery may not work properly on certain mobile and IE browsers

I am currently working on creating a dependency between two select boxes. The jQuery code I have implemented works perfectly on most common browsers such as Chrome and Firefox, but it seems to have some issues with Internet Explorer, Edge, and some mobile ...

What is the best way to delete the "Click to sort Ascending" text from the header of each column in a bootstrap-vue table?

Recently, I came across a bootstrap-vue table that caught my attention: https://i.sstatic.net/5jENs.png Below is the code snippet for the table setup: <template> <div class="container"> <h1 class="pt-2 pb-3">Bo ...