Combining shapes in three.js

I've encountered a scenario where my scene consists of multiple meshes, each with different shapes and sizes.

By looping through each mesh and utilizing geometry.merge(), I successfully created a new mesh from the geometries in the scene.

The challenge arises when attempting to mask the entire mesh with an alphaMask, as each geometry has the material applied individually.

You can view an example of this situation here - https://codepen.io/danlong/pen/KXOObr

   function addObjects(scene) {

    // merged geomoetry & material
    var mergedGeometry = new THREE.Geometry();
    var mergedMaterial = new THREE.MeshStandardMaterial({ color: "#444", transparent: true, side: THREE.DoubleSide, alphaTest: 0.5, opacity: 1, roughness: 1 });


    // multiple meshes
    var geometry = new THREE.IcosahedronGeometry(30, 5);
    var material = new THREE.MeshStandardMaterial({ color: "#444" });

    var geo1 = new THREE.IcosahedronGeometry(30, 5);
    var mesh1 = new THREE.Mesh( geo1, material );
    mesh1.position.x = 10;
    mesh1.position.y = 10;
    mesh1.position.z = 0;

    var geo2 = new THREE.IcosahedronGeometry(30, 5);
    var mesh2 = new THREE.Mesh( geo2, material );
    mesh2.position.x = 20;
    mesh2.position.y = 20;
    mesh2.position.z = 0;

    var geo3 = new THREE.IcosahedronGeometry(30, 5);
    var mesh3 = new THREE.Mesh( geo3, material );
    mesh3.position.x = 30;
    mesh3.position.y = 30;
    mesh3.position.z = 0;

    // scene.add(mesh1, mesh2, mesh3);
    mesh1.updateMatrix();
    mergedGeometry.merge(mesh1.geometry, mesh1.matrix);

    mesh2.updateMatrix();
    mergedGeometry.merge(mesh2.geometry, mesh2.matrix);

    mesh3.updateMatrix();
    mergedGeometry.merge(mesh3.geometry, mesh3.matrix);

    // alpha texture 
    var image = document.createElement('img');
    var alphaMap = new THREE.Texture(image);
    image.onload = function()  {
        alphaMap.needsUpdate = true;
    };
    image.src = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAgAAAAICAYAAADED76LAAAAGUlEQVQoU2NkYGD4z4AHMP7//x+/gmFhAgCXphP14bko/wAAAABJRU5ErkJggg==';
    mergedMaterial.alphaMap = alphaMap;
    mergedMaterial.alphaMap.magFilter = THREE.NearestFilter;
    mergedMaterial.alphaMap.wrapT = THREE.RepeatWrapping;
    mergedMaterial.alphaMap.repeat.y = 1;

    // merged geometry with alpha mask
    merge1 = new THREE.Mesh(mergedGeometry, mergedMaterial);
        merge1.rotation.z = -Math.PI/4;

    // merge geometry without alpha mask
    var merge2 = new THREE.Mesh(mergedGeometry, material);
    merge2.position.x = -100;
        merge2.rotation.z = -Math.PI/4;

    scene.add(merge1, merge2);
    return mesh;
}
 

The left-side mesh displays the result of merging geometries to which I aim to apply the alphaMask. The right-side mesh demonstrates that instead of applying the map to the entire mesh, it's being applied to each individual geometry within.

My question is whether there exists a method to mask the complete mesh without affecting each individual geometry?

-- three.js r86

EDIT: Efforts to implement a clipping plane on my mesh didn't yield the desired outcome. My goal is to apply an alphaMask across the entire mesh, unveiling it based on the mask image used. A visual representation of this desired effect can be viewed here - https://codepen.io/supah/pen/zwJxdb

Could the UV mapping of the original geometries play a role in this issue? Is there a need to modify them in any specific manner?

Answer №1

To achieve the effect you desire, consider using an overlaid mask. You can create this by rendering a single plane with an alpha map on top of your scene rendering. Utilize an orthographic camera and adjust the renderer settings accordingly, such as disabling automatic color clearing.

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

React.js: Passing an array as a property to an element results in the transformation of the array into an object

I'm trying to understand why passing an array as a prop to another element results in it getting transformed into an object with the array as its value. I need help understanding if this is a common JavaScript 'quirk' or specific to React an ...

Switching between languages dynamically with Angular JS using $translateProvider and JSON files

I currently have a collection consisting of 6 different JSON files. en.json es.json fr.json it.json ja.json zh.json An illustration of the data present in each file is as follows (in this instance, considering en.json): { "SomeText": "Test in Englis ...

Is it possible to interact with Java SE jars using JavaScript?

I need to integrate an API that is written in Java with a Client written in JavaScript. Is there any way to use this API, possibly along with other Java-SE JARs, in Webstorm? Any assistance would be greatly appreciated. Thank you! ...

Replicate the styling of CSS class A and apply it to class B

Let's take a look at some code: <button id="test" class="ui-state-hover" value="Button"> In Context: I'm utilizing the JQuery UI CSS class ui-state-hover on an HTML button to ensure it always appears as if it's being hovered over. H ...

Tips for eliminating duplicate values from an array of objects in JavaScript

I am working with an array of objects where my goal is to remove duplicate values from the values array. I would like the final result to be [{name:'test1', values:['35,5', '35,2','35,3']}, {name:'test2', v ...

Searching for mustache variables in HTML using regular expressions is a helpful way to

As I work on parsing template files in my code, one of my first tasks involves removing all Mustache variables in the template. The majority of these variables follow this structure: {{variable}}, {{#loop}}{{content}}{{/loop}}, {{^not}}{{/not}}, {{! comme ...

Encountering parameter issues while working with Google Maps React in TypeScript

Currently, I'm utilizing TypeScript in this particular file. import React, {Component} from 'react' import {Map, InfoWindow, Marker, GoogleApiWrapper, mapEventHandler, markerEventHandler} from 'google-maps-react'; import { coordina ...

Mastering the art of utilizing particles.js

Particles.js doesn't seem to be functioning properly for me—I'm struggling to pinpoint the issue. Any guidance or suggestions would be greatly welcomed, as I'm unsure whether it's related to an external dependency... HTML: <div ...

Explore the functionality of the webix grid by clicking on the url column

I've recently created a table in Webix, and I have a column labeled "TBD" where I display a URL. My question is: how can I make this URL, 'https://pops.dra.com/TBD.php?jour=$adep&arc=$ads', clickable? while($row = mysqli_fetch_array($req ...

There is no need for updates as git is already current for some mysterious reason

As a newcomer to git, I've been trying to wrap my head around it but still struggling. Can someone help clarify this for me? My question pertains to the 'master' branch in git which contains the following code: const list = [ 'h&ap ...

By default, make the initial element of the list the selected option in an AngularJS select input

I'm running into an issue with setting the first element in my ng-repeat list within a select input. Here is the code snippet causing the problem: <div> <span >OF</span> <select ng-model="eclatementCourante.ordreFabricationId" n ...

React-dropzone experiencing delays in loading new files for readers

Is there a way to handle conditional responses from an API and assign the desired value to errorMessageUploaded? I'm looking for a solution to receive error messages from the API, but currently, the errormessageupload variable is not being set withou ...

What steps can be taken to safeguard the title of the document in the top window from being altered by any iframe or script?

My typical approach is to delete and redefine the title property like in the code snippet below: if (delete document.title) { Object.defineProperty(document, 'title', { get: getter, set: setter, enumerable: true, ...

Error encountered while trying to run a three.js example with iewebgl

I'm attempting to utilize the iewebgl and encountering difficulties while trying to run an example from three.js, specifically the webgl_loader_obj. Upon execution, I am facing the following error: SCRIPT445: Object doesn't support this action i ...

Tips for delaying the execution of numerous ajax success callbacks?

In my JavaScript code, I am facing the following situation: call_A(success_A) call_B(success_B) function call_A(success){ // make ajax request success(result) } function call_B(success){ //make ajax request success(result) } function success_A(){ ...

Struggling with integrating jQuery append into Backbone.js

Having trouble using jQuery.append() and backbonejs. Currently, when attempting to append, nothing happens (except the jQuery object is returned in the immediate window) and the count remains at 0. Manually adding the element has not been successful. I als ...

What is the best way to verify the status codes of multiple URLs using JavaScript?

In my JavaScript project, I am currently checking the status codes of various URLs. While I can successfully check the status for one URL at a time by writing the code manually, I am facing an issue as I have over 100 URLs to check. This is making my cod ...

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. ...

Loading Data in CodeMirror using XMLHttpRequest (XHR)

Is there any progress in loading the content into CodeMirror using XHR? ...

What are the steps for executing an API and entering data into it?

I have developed an API using NodeJS, ExpressJS and MongoDB to filter and sort school data based on location and fees. The main code snippet looks like this: const express = require('express'); const bodyparser = require('body-parser') ...