What could be the reason why csg.js in Three.js is not functioning correctly?

I attempted to utilize csg.js functions for cutting a sphere out of a box, but unfortunately it doesn't seem to be working correctly. I have gone through the tutorial on without success.

<html>
    <head>
        <title>Experiment</title>
    </head>

    <body>
        <script src="three_js\build\three.min.js"></script>
        <script src="ThreeCSG.js"></script> 
        <script src="csg.js"></script>          
        <script type="text/javascript"> 

    var scene = new THREE.Scene();
    var camera = new THREE.PerspectiveCamera( 75, window.innerWidth/window.innerHeight, 0.1, 1000 );

    var renderer = new THREE.WebGLRenderer();

    renderer.setSize( window.innerWidth-20, window.innerHeight -20);
    document.body.appendChild( renderer.domElement );   

    var geometry1 = new THREE.BoxGeometry( 10, 10, 10);
    var material = new THREE.MeshPhongMaterial( {specular: "#fdfb57", color: "#d8d613", emissive: "#6b6a0d", side: THREE.DoubleSide} );
    var box = new THREE.Mesh(geometry1, material);
    var sphere = new THREE.Mesh(new THREE.SphereGeometry(5, 32, 32), material);
    scene.add(box);
    scene.add(sphere);

    var boxCsg = THREE.CSG.toCSG(box);
    var sphereCsg = THREE.CSG.toCSG(sphere);

    boxCsg.substract(sphereCsg);    

    box = THREE.CSG.fromCSG(boxCsg);


    camera.position.z = 50;


    var directionalLight = new THREE.DirectionalLight( 0xffffff, 0.5 );
    directionalLight.position.set( 5, 10, 5 );
    scene.add( directionalLight );

                var render = function () {
                    requestAnimationFrame( render );
                    renderer.render(scene, camera);
                    //Manipulating window size here!
                    renderer.setSize(window.innerWidth - 20, window.innerHeight - 20);                  
                };


                render();   

            </script>
        </body>
    </html>

Answer №1

The content of the article is outdated and here is the updated syntax:

let cube = new THREE.Mesh( new THREE.BoxGeometry( 10, 1, 10 ) );
let cube_bsp = new ThreeBSP( cube );

let sphereGeo = new THREE.SphereGeometry( 1, 16, 8 );
let sphere = new THREE.Mesh( sphereGeo );
let subtract_bsp  = new ThreeBSP( sphere );
let result_bsp  = cube_bsp.subtract( subtract_bsp );

let resultMesh = result_bsp.toMesh();
scene.add( resultMesh );

For Three.js version r107: http://jsfiddle.net/r7suq1mv/2/

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

Initializing ng-app with a specific name will prevent the initialization of variables

Here is the code snippet I am working with: <div ng-app="" ng-init="show_login=false;count=0"> <button> ng-click="show_login=!show_login;count=count+1">INSPIRE</button> <form ng-show="show_login"> Username <input type="t ...

Is there a way to prevent an iframe from being recorded in Chrome's browsing history?

I'm in the process of developing a Chrome extension that inserts an Angular application into the user's browser window to create an interactive sidebar. I've been successful in most of my goals by inserting an iframe through a content script ...

I am unable to locate the appropriate TypeScript type for the `displayName` attribute when it is used as a prop for the `type` component

Having trouble finding the correct type as mentioned in the title. After inspecting with DevTools, I have confirmed that every component I am programmatically looking at has Component.type.displayName. For anything that is not an ElementType, the type is a ...

How to Create Indentation Without Adding a New Line in Handlebars Mustache Syntax?

Trying to format mustache code for readability, I encountered an issue: {{this.$.Name}} {{this.$.Name}} although the desired output is: namename Is it possible to achieve this format without losing indentation in the .hbs file? ...

Typescript headaches: Conflicting property types with restrictions

Currently, I am in the process of familiarizing myself with Typescript through its application in a validation library that I am constructing. types.ts export type Value = string | boolean | number | null | undefined; export type ExceptionResult = { _ ...

Troubleshooting CSS Hover Not Displaying Properly in Angular 14

In the footer class of my HTML, there is a code snippet with chevrons: <div class="link-list"> <div *ngFor="let link of insideLinksLeft | originalOrderKeyValue" class="link"> <a [href]="link.val ...

The d3.js circles mysteriously disappear when attempting to implement the update pattern

I find myself in a challenging situation once again - there may come a day when I am the one offering help, but for now, I admit that I am feeling quite lost. I am very new to d3.js and my understanding of JavaScript is still quite basic. My current proje ...

Getting the href values of dynamically changing links with Selenium in Python: A step-by-step guide

Is there a way to extract all hrefs(links) located in anchor tags using JavaScript code with Selenium Python, especially when these links are dynamically updated? The tag I am trying to click on is as follows: enter image description here I have managed t ...

Tips for obtaining a specific sorting order based on a wildcard property name

Here's the structure of my JSON object, and I need to sort it based on properties starting with sort_ { "sort_11832": "1", "productsId": [ "11832", "160", "180" ], "sort_160": "0", "sort_180": " ...

What is the best way to connect the user-input data with text in a span element?

Recently, I've started learning Vue.js and I'm facing an issue with binding data from an input field to a span element. Instead of appending the data, it's showing me undefined Here is the code snippet: new Vue({ el: "#app", data: { ...

Transform a JavaScript array using key value pairs sourced from a JSON document

I am currently working on developing a feature to detect and flag inappropriate comments within my application. One approach I am taking involves splitting the comment into an array of strings. I am now looking to implement a JavaScript function that can ...

The showdown between AngularJS Directive Restrict A and E

In our small team, we are currently working on a project in AngularJS and striving to uphold basic standards and best practices. Since we are still relatively new to Angular, we have some questions regarding Directives, specifically the `restrict` options. ...

Use Ajax to dynamically update the href attribute of an ID

I have a page with songs playing in a small audio player, and the songs change every 1-2 minutes or when I choose to (like a radio). Users can vote on each song using the following format: <span class='up'><a href="" class="vote" id="&l ...

Automatically submit form in Javascript upon detecting a specific string in textarea

Just getting started with JS and I have a question that's been bugging me. I have a simple form set up like this: <form method="POST" action="foo.php"> <textarea name="inputBox123"></textarea> <input type="submit" value="Go" name ...

Tips to prevent redundancy in a one-line 'if' statement in JavaScript

While working on a piece of code, I came across something bothersome and couldn't find a solution even after doing some research. It's functional, but it feels redundant to have to type this.className three times: this.className != 'selecte ...

I encountered a data discrepancy while attempting to link up with a weather API

This is my debut app venture utilizing node.js and express for the first time. The concept behind this basic application involves connecting to an external API to retrieve temperature data, while also allowing users to input their zip code and feelings whi ...

React state variable resetting to its initial value unexpectedly

I have encountered a puzzling issue with the React useState hook. After setting the state of a value, it gets reset to null unexpectedly. The main problem lies in the click event that should update the startDate value to the current date and time. This cli ...

Obtaining POST request parameters in an Express server

Below is the code snippet for adding a product using an angular service. The product is passed as the body. addProduct(product): Observable<any> { return this.http.post<Observable<any>>( `http://localhost:4401/api/products`, ...

Format the date using the Datepicker

I am looking to update the date format in my date picker from "12/25/2022" (for example) to "25/December/2022" Here is the code I have: $(".date-picker").datepicker({ format: 'dd-mm-yyyy', minDate: 0, numberOfMonths: \[3,1&b ...

Encountering a "variable not found" error when trying to run the JavaScript code to manipulate the web element

Upon executing the command to change the value of a web element text, I encountered an error stating "Can't find variable: e." sel=webdriver.PhantomJS() sel.get=('http://stackoverflow.com/questions?pagesize=50&sort=newest') elements=sel ...