What could be causing compatibility issues with materials other than MeshNormalMaterial when using a mesh created with ThreeBSP.js?

When it comes to creating a dice, I've noticed that it only looks like a dice when using MeshNormalMaterial in the second last line (

result = resultBSP.toMesh(materialNormal);
). If any other material is used, it just ends up looking like a cube with no subtraction dots on it. The library I'm utilizing, ThreeBSP (an upgrade of ThreeCSG), can be found here.

There's no issue with the MeshNormalMaterial, but it lacks customization options compared to other materials as it doesn't accept parameters.

Below is the function I'm employing to build the dice:

function buildDice(){

    var materialNormal = new THREE.MeshNormalMaterial();
    var diceCube = new THREE.Mesh( new THREE.BoxGeometry(100,100,100), materialNormal);
    
    diceCube.position.x = 0;
    diceCube.position.y = 50;
    diceCube.position.z = 0;
    
    diceCube.geometry.computeFaceNormals();
    diceCube.geometry.computeVertexNormals();
    
    var cubeBSP = new ThreeBSP(diceCube);
    
    var sphereGeometry = new THREE.SphereGeometry(75,16,8);
    var sphereMesh = new THREE.Mesh(sphereGeometry, materialNormal);
    
    sphereMesh.scale.x = 0.17;
    sphereMesh.scale.y = 0.17;
    sphereMesh.scale.z = 0.17;
    
    //coords of the spheres 
    var xPositions = [....]; // coordinates for xPositions of sphereMesh
    var yPositions = [....];
    var zPositions = [....];
    
            
    var diceDots    = new THREE.Geometry();
    
    for(var i = 0; i < xPositions.length; i++){
        
        sphereMesh.position.x   = xPositions[i];
        sphereMesh.position.y   = yPositions[i];
        sphereMesh.position.z   = zPositions[i];
        THREE.GeometryUtils.merge(diceDots, sphereMesh);
    }
    
    var dotsMesh = new THREE.Mesh(diceDots, materialNormal);
    dotsMesh.geometry.computeFaceNormals();
    dotsMesh.geometry.computeVertexNormals();
    
    var dotsBSP = new ThreeBSP(dotsMesh);
    var resultBSP = cubeBSP.subtract(dotsBSP);
    
    result = resultBSP.toMesh(materialNormal);
    scene.add(result);

}

Answer №1

It is compatible with various other materials, such as THREE.MeshPhongMaterial. You can refer to this jsfiddle that utilizes your buildDice() function: http://jsfiddle.net/L0rdzbej/152/

https://i.sstatic.net/jrKPh.png

Make sure to update the mesh's matrix before merging the geometry, and avoid using deprecated functions whenever possible.

Using Three.js version r73

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

Script designed to track modifications on a specific webpage

Attempting to purchase items online has become a frustrating challenge as stock seems to disappear within minutes of being added :/ I am currently working on creating a custom grease/tampermonkey script to monitor the page and notify me when products beco ...

AngularJS: Understanding the difference between ng-show and using display:none

I recently encountered a scenario where I needed to hide an HTML element by default using CSS. Here's how I did it: HTML: <div class="box"> </div> CSS: .box { display: none; } However, I wanted to be able to show and hide the elem ...

Unable to trigger click event on dynamically added element using Chrome (jQuery)

My approach here involves dynamically creating a drop-down and binding the click event to the <option> within the <select> var $select = $('<select></select>'), $option1 = $('<option>a</option>') ...

What is the best way to adjust the maximum width of Reddit's embedded comment iframe?

Reddit provides a code that can be embedded to display their comments on a website, As an example: <div class="reddit-embed" data-embed-media="www.redditmedia.com" data-embed-parent="false" data-embed-live="false" data-embed-uuid="24a3e666-f855-4664 ...

JavaScript - Monitoring changes to a node before they occur

Using MutationObserver, I am able to detect node insertion in a tree, however, it runs after the node is inserted. If I need to trigger an event before the node is actually inserted, what steps should I take? For example: // create a new observer instan ...

Running JavaScript code for the iframe

Question about Manipulating Content in an iFrame: Invoking javascript in iframe from parent page I am attempting to load a HTML page with an iFramed website and then manipulate the content by removing an element, referred to as '#test'. < ...

Looking to transform this PHP function into a jQuery function that generates all the possible combinations of values in an array?

I stumbled upon this PHP code on a programming forum. Here's the original code snippet: function everyCombination($array) { $arrayCount = count($array); $maxCombinations = pow($arrayCount, $arrayCount); $returnArray = array(); $conve ...

Is Passport.js' serializeUser and deserializeUser functions never triggering?

Encountering an issue with Passport-local. It seems that neither serializeuser nor deserializeUser are being invoked. Upon researching similar problems on SO, it appears that many others facing this problem were not properly including bodyParser. Below is ...

Retrieve a different action instance variable within the view

In the scenario where I have a View called home.html.erb and the corresponding controller shown below: class StaticController < ApplicationController def home @people = Person.all end def filter @people = .... end def contact end ...

What steps do I need to take to create a delete button that will effectively remove a bookmark from an array?

Currently, I have created a form that allows users to input the website name and URL. Upon clicking the submit button, the output displays the website name along with two buttons: 1. one for visiting the site 2. another for removing the bookmark using on ...

Verify the presence of both class and id before modifying the content of the h1 tag and automatically redirecting the page

I'm encountering some issues triggering my JS/JQ on an HTML5 page. Essentially, I want to verify the existence of the following ID and class: ID: page_blog CLASS: page current <section class="page current" id="page_blog" style="z-index: 99; lef ...

The border of the Material UI Toggle Button is not appearing

There seems to be an issue with the left border not appearing in the toggle bar below that I created using MuiToggleButton. Any idea what could be causing this? Thank you in advance. view image here view image here Just a note: it works correctly in the ...

Integrating Gesture Handling in Leaflet JS for two-finger scrolling enforcement

Have you ever noticed that when you're using a mobile device and scrolling down a webpage with a Google map, the map goes dark and prompts you to "Use two fingers to move the map"? https://i.stack.imgur.com/4HD1M.jpg I am interested in incorporating ...

There was an error in the search: [parse_exception] The search source could not be parsed. A field name was expected, but instead [

I'm experiencing a query parsing exception while utilizing JavaScript for Elasticsearch configuration found in elastic.js file. Results are obtained when the filtered part is removed. However, upon adding it back, an exception occurs. var client = r ...

`The file cannot be modified at this time`

I am having an issue with updating a file using fs and then creating a zip in a different location. The file update seems to be working fine, but the zip file does not have the updated contents. Can someone point out what I am doing wrong in my code belo ...

Populating hidden fields in Javascript with mouseover event trigger - Pre-click action

Seeking a solution for another issue, I stumbled upon the capability to execute JavaScript on mouseover. Here is what I want to execute: $(document).ready(function(){ function myPayment() { var value = document.mortgagecalc.value_input.value; var rate ...

Navigating through embedded arrays using Jade

I am trying to display the data retrieved from the QPX API in my Jade view. The structure of the data is as follows: { kind: 'qpxExpress#tripsSearch', trips: { kind: 'qpxexpress#tripOptions', requestId: 'Oq ...

What is the best way to fetch all records where the property matches a portion of the specified parameter?

Below is the Mongoose Schema that I currently have var People= new Schema({ firstName: String, alias: [String] }); I am trying to find a method to retrieve all documents where one of the alias strings matches or is contained in some way withi ...

Why isn't jQuery working properly for showing/hiding elements?

I am attempting to create a functionality where the string remove field can be toggled to show or hide using a single button, depending on its current state. Initially, it should be hidden (using the hidden HTML attribute), and upon clicking the button, it ...

The webpage does not dynamically resize based on screen height

Encountered an issue with the excess space below the footer. Resolved it by implementing a jQuery sticky footer as CSS techniques were unsuccessful. However, facing a problem with the main content of the webpage. Adjustment of .bg-photo's height impa ...