What is the process for combining two distinct geometries with different materials into a single entity in three.js?

I created a 2D square line diagram followed by another square diagram with a sample image in the center position. However, one of them is visible while the other is hidden. I used two different geometries and materials - one with a line-based material set to a specific color and the second with an image material. I tried to merge them together but encountered some issues. Can anyone provide some insights or identify what mistake might be present in my code?


var combined = new THREE.Geometry(); 

var squareGeometry = new THREE.Geometry();
squareGeometry.vertices.push(
new THREE.Vector3(startX, startY, 0),
new THREE.Vector3(endX, startY, 0),
new THREE.Vector3(endX, endY, 0),
new THREE.Vector3(startX, endY, 0),
new THREE.Vector3(startX, startY, 0)
);
var squareMaterial = new THREE.LineBasicMaterial({color: color});
var square = new THREE.Line(squareGeometry, squareMaterial);

var iconGeometry = new THREE.PlaneGeometry(objSize.width/2, objSize.height/2);
var iconMaterial = new THREE.MeshBasicMaterial({map:THREE.ImageUtils.loadTexture('images/plantIcon.png')
});
var icon = new THREE.Mesh(iconGeometry,iconMaterial);


// square.holes.push( icon);
// THREE.GeometryUtils.merge(square, icon);

combined.merge(square.geometry, square.matrix);
combined.merge(icon.geometry, icon.matrix);

var merge = new THREE.Mesh(combined,new THREE.MeshBasicMaterial({
map:THREE.ImageUtils.loadTexture('images/plantIcon.png'),//color: color
}));

merge.position.x = objPosition.x;
merge.position.y = objPosition.z;

scene.add(merge);

Answer №1

In my project, I utilized two different types of Mesh. One was the THREE.Line() Mesh and the other was the THREE.Mesh. To combine them together, I used the grouping method: var group = new THREE.Object3D(). You can find my code below which is working perfectly:

var group = new THREE.Object3D();
var squareGeometry = new THREE.Geometry();
squareGeometry.vertices.push(
    new THREE.Vector3(startX, startY, 0),
    new THREE.Vector3(endX, startY, 0),
    new THREE.Vector3(endX, endY, 0),
    new THREE.Vector3(startX, endY, 0),
    new THREE.Vector3(startX, startY, 0)
);
var squareMaterial = new THREE.MeshBasicMaterial({color: color});
var square = new THREE.Line(squareGeometry, squareMaterial);

var iconGeometry = new THREE.PlaneGeometry((objSize.width/2), (objSize.depth/2));
var iconMaterial = new THREE.MeshBasicMaterial({ map:THREE.ImageUtils.loadTexture('images/plantIcon.png') });
var icon = new THREE.Mesh(iconGeometry,iconMaterial);
icon.position.x = objPosition.x;
icon.position.y = objPosition.z;

group.add(square);
group.add(icon);

scene.add(group);

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

The absence of the iframe in ie8 is causing problems that cannot be fixed with relative positioning

On a website, I am integrating an external 2-factor authentication solution called Duo Web using their PHP and Javascript. It works smoothly on all browsers except for IE8. When the user passes the initial login screen, the 2FA login page loads, but the if ...

Exploring the Wonders of React Memo

I recently started delving into the world of React. One interesting observation I've made is that when interacting with componentized buttons, clicking on one button triggers a re-render of all button components, as well as the parent component. impo ...

Encountering CORS Error while trying to access Guest App in Virtualbox using Vue, Express, and Axios

I encountered an issue while trying to access my Vue app in Virtualbox from the host, both running on Linux Mint 20. Although I can now reach the login page from my host, I am consistently faced with a CORS error during login attempts: Cross-Origin Request ...

What triggers the onmouseout event to occur?

Is the event triggered continuously whenever the mouse is not hovering over the element? Or is it a one-time action when the mouse exits the element? This distinction is crucial for me to determine when the mouse pointer leaves the element, while only wa ...

Manipulating div position interactively with vanilla javascript during scroll

I'm trying to create an effect where an image inside a div moves vertically downwards as the user scrolls, stopping only at the end of the page. I've attempted a solution using a script from another post, but it doesn't seem to be working. ...

Removing an HTML element entirely

Although I am utilizing .remove() method and it is effectively removing the desired element, upon inspecting the page source by right-clicking in a browser window, I can still see those removed elements. It seems like they are not being permanently delet ...

Utilizing Jquery functions within the AngularJS framework

Utilizing Uikit along with its pagination feature, I am attempting to implement this function for changing the page: $('[data-uk-pagination]').on('uk-select-page', function(e, pageIndex){ console.log("page " + pageIndex); ...

Dividing ReactJS into different assemblies

I have a flexible .NET application that utilizes plugins to load controllers, views, and components from different assemblies. I am interested in learning React (completely new to me) and I have a question. Can React split its components into separate as ...

Leveraging the AngularJS promise/defer feature alongside the Quickblox framework, learn how to efficiently upload images and subsequently upload public URLs to a custom

I am currently developing an application that requires users to upload 5 images of themselves. Using a backend-as-a-service platform like Quickblox, I have to create and upload blob files individually. Once each image is uploaded, I receive a success call ...

Utilizing Material-UI components for a seamless navigation button experience

After reviewing the example on https://material-ui.com/guides/composition/#button, I have implemented a button in my main page: <BrowserRouter> <Box m={2}> <Button size="large" variant="cont ...

Is the unit test for attribute failure specific to running it on only one node?

I am currently using Enzyme and Jest to verify the presence of an id attribute in a dropdown list. import React from "react"; import { mount } from "enzyme"; import ListPicker from './ListPicker.js' describe("ListPicker", () => { let props ...

Emit data asynchronously upon returning

In my node.js application, I have a background process implemented using the EventEmitter. Here is a snippet of how it is used: var event = { returnValue: undefined }; eventEmitter.emit('name', event, argument); return event.returnValue; // This ...

Is there a glitch in JavaScript when comparing dates?

What's wrong with this code? function test() { var start = new Date(2012, 3, 31, 19, 0, 0); // Start: 3/31/2012 7:00 PM var end = new Date(2012, 4, 1, 1, 0, 0); // End: 4/01/2012 1:00 AM if (end < start) console.log("oops ...

What is the best way to launch a Popup window that spans from the top to the bottom of the screen?

I'm attempting to create a popup window that stretches from the top of the screen to the "applications" bar in Windows. Here's the code I'm using: function windowOpener(windowHeight, windowWidth, windowName, windowUri) { var windowHeight = ...

jQuery - Endless paging

My attempt at implementing infinite scrolling on my page seems to have hit a snag. Instead of triggering at the bottom of the page, it's activating when I scroll back to the top. Can anyone shed some light on what might be causing this issue? $(windo ...

Express.js refuses to serve static assets

I've been struggling to set up my express server to serve static files, but no matter what I try, I can't seem to get it working. I've attempted various solutions without success. Here is my folder structure: App - Web -- public --- images ...

Python for Asynchronous HTTP Requests

I am currently working on a Python script to extract the value of the href attribute from an anchor element on a web page. The challenge is that the div element containing the anchor element's content is loaded through AJAX jQuery calls when the web p ...

What is the best way to rotate a mesh group around the axis of a randomly selected 3D element within the Three.js environment?

I'm currently delving into the intricacies of three.js in order to experiment with a basic mechanism. My end goal is to visualize board tilt (front view) in relation to axle orientation (plan view) for different designs on an XY plot, although that&ap ...

Find the mean of three numbers stored in an array of objects

I am currently working on a school assignment that requires me to develop a function for calculating the average mark of 3 students using an existing object in an array. Below, you can see the array and function that I have been working on as part of this ...

Exploring the process of setting up Jasmine tests for both services and controllers in Angular.js

I have been facing challenges in creating accurate tests for my Angular services and controllers, even though they are functioning correctly. I have searched extensively for a solution to this problem. Below is an example of my controller: angular.module ...