Looking for advice on successfully implementing createMultiMaterialObject in THREE.js version r62?

I am having trouble getting createMultiMaterialObject to work as expected. My goal is to have a wireframe material displayed on top of a solid material. However, the multimaterial function only seems to display the first material in the array.

Here is the code snippet:

var geometry = new THREE.PlaneGeometry( this.model.density.width, this.model.density.height, this.model.density.x, this.model.density.y );
var mat1 = new THREE.MeshBasicMaterial( { color: 0xd02000, transparent: true } );
var blackLines = new THREE.MeshBasicMaterial( { color: 0xffffff, transparent:true, wireframe: true, wireframeLinewidth: 1 } );
var materials = [mat1, blackLines];

this.plane = THREE.SceneUtils.createMultiMaterialObject( geometry, materials );

In the image provided, the red 'ground' represents the mesh that should have the multimaterial applied to it. However, only one material (in this case, a red MeshBasicMaterial) is showing up.

Answer №1

Stumbled upon this awesome Codepen that demonstrates how to achieve this:

Take a look at the code below:

var renderer, scene, camera, cube;

var ww = window.innerWidth,
  wh = window.innerHeight;

function init() {

  renderer = new THREE.WebGLRenderer({
    canvas: document.getElementById('scene')
  });
  renderer.setSize(ww, wh);

  scene = new THREE.Scene();

  camera = new THREE.PerspectiveCamera(50, ww / wh, 0.1, 10000);
  camera.position.set(0, 0, 500);
  scene.add(camera);

  //Generate cube on the scene
  createCube();
}


var createCube = function() {

  //Simple box geometry
  var geometry = new THREE.BoxGeometry(150, 150, 150);

  //Create a basic material with green color
  var basicMaterial = new THREE.MeshBasicMaterial({
    color: 0x00ff00
  });

  //Create a material just for the wireframe
  var wireframeMaterial = new THREE.MeshBasicMaterial({
    wireframe: true
  });

  //createMultiMaterialObject will create a Mesh for each material in the array
  //We will get 2 cubes in a 3D object
  //You can check with the console that the scene has 2 children, the camera and a 3D object
  //The 3D object contains 2 Mesh with respective material (color for the 1st and wireframe for the 2nd)
  cube = THREE.SceneUtils.createMultiMaterialObject(geometry, [basicMaterial, wireframeMaterial]);

  //Add my cube on the scene
  if (scene) {
    scene.add(cube);
  }

  //Launch animation of the scene
  render();
};

var render = function() {
  requestAnimationFrame(render);

  if (cube) {
    cube.rotation.x += 0.01;
    cube.rotation.y += 0.01;
  }


  renderer.render(scene, camera);
};

init();
body,
html {
  width: 100%;
  height: 100%;
  padding: 0;
  margin: 0;
  overflow: hidden;
}
<canvas id="scene"></canvas>
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r79/three.min.js"></script>

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

Problem: Values are not being posted with AJAX when using $(form).serialize()

I'm encountering an issue with submitting a form using AJAX. I initially tried to pass the data using $("#myForm").serialize(), but for some reason, the receiving page doesn't receive the data. Take a look at my form: <form id="myForm"> ...

JavaScript code can be enhanced with HTML comments to improve readability

I have incorporated Google ad into my website using the following code. <script type="text/javascript"><!-- google_ad_client = "pub-"; /*Top 468x15 */ google_ad_slot = ""; google_ad_width = 468; google_ad_height = 15; //--> </script> < ...

Utilizing HTML and Javascript for streaming audio and recording voice

Working on a new project that involves streaming audio files (mp3) and recording voice messages. Initially considered using Flash, but the challenge is making the website iPhone-friendly as per the client's request. Is there a technology available th ...

Utilizing PHP, Javascript, and jQuery in mobile technology gadgets

Can anyone recommend mobile technology products that have been developed using PHP, Javascript, and jQuery? What are the newest mobile products available on the market that have been built using these languages? Do popular devices like iPhone, BlackBerry ...

for each and every object, execute the asynchronous function

I have a scenario where I need to loop through objects, call an async method ("search") with a callback, and then write the results (resultSet) to a JSON file once all objects are processed. The issue I'm facing is that the writeFile function is execu ...

The caption below the image is not functioning correctly when hovering over it

I'm having trouble getting the text to appear correctly underneath the image. Whenever I hover over the image, the text seems to overlap it. I am sure there is a simple solution to this issue, but I can't seem to figure it out. Removing the inlin ...

How to use RegExp to locate the final return statement within a JavaScript code string

Take a look at this code snippet: cont x = 10; function foo() { return x; // ;; end of function ;; // /* here is a some text here too */ } function bar() { return 10 } return foo() + bar(); // ;;done;; // /* yolo yolo */ This string cont ...

Tips for creating a responsive iframe without relying on aspect ratio assumptions

Is there a way to create a responsive iframe without relying on a fixed aspect ratio? The width and height of the content are unknown until rendering. Keep in mind, using Javascript is an option. For instance: <div id="iframe-container"> <i ...

"Changing background color, incorporating hover effects, utilizing !important, and manipulating .css properties with

Encountered a dilemma. I devised a "tabs" feature illustrated in the following demo: http://jsfiddle.net/4FLCe/ The original intention was for the tab to change color to A when hovered over, and to color B when clicked on. However, as shown in the demo, ...

Choosing multiple items using ng-checked and ng-model in AngularJS

I am working with an Ionic application and encountering a minor issue, much like AngularJS. <ion-list class="list-inset subcategory" ng-repeat="item in shops"> <ion-checkbox class="item item-divider item-checkbox-right" ng-model="selectAll" ...

Lack of animation on the button

Having trouble with this issue for 48 hours straight. Every time I attempt to click a button in the top bar, there is no animation. The intended behavior is for the width to increase and the left border color to change to green, but that's not what&ap ...

The JavaScript file specified in the script tag's src attribute was successfully downloaded, but it did not execute as expected after being fetched

I've implemented an ajax call using jQuery in the following manner: $.ajax({ type: 'GET', url: 'edit.htm', success: function(data){ container.html(data); }, }); After making the ajax call, the data returned includes s ...

Using jQuery and JavaScript to swap images depending on the option chosen in a dropdown menu

On my existing ecommerce website, I have a dropdown menu with the following code: <select data-optgroup="10201" class="prodoption detailprodoption" onchange="updateoptimage(0,0)" name="optn0" id="optn0x0" size="1"><option value="">Please Selec ...

Incorporating CKEditor with ASP.NET: A Seamless Integration

I am currently in the process of integrating a content management system (CMS) for a website. My familiarity with Javascript, jQuery, C#, etc., is limited as I primarily work with Java, SQL, and C++. My query pertains to the CKEditor instance loaded on the ...

Incorporated React component from Rails-assets.org into a Rails application

I have been trying to incorporate a react component from rails-assets.org into my Rails application. Currently, I am using the react-rails gem to write react view in my application. Now, I want to use other react components such as react-router, react-sel ...

Utilize Vue.js and express.js to distribute HTML files securely

Currently, my tech stack includes Vue.js for the frontend and Express.js for the backend. When I kick off the express.js server using npm start, my goal is to serve the Vue frontend component. By utilizing the Vue generator and Express generator, I attemp ...

Using AngularJS, generate a JSON array with a specified key

Looking to create a JSON array structure with keys using AngularJS, but unsure how to push data in order to achieve this. The goal is to generate a JSON array based on the provided data below. $scope.category = [{"id": 20, "name": "vegetable"}, {"id": ...

What is the best way to make a canvas element fit the entire browser window without causing scroll bars to appear?

My window size isn't working properly, despite attempting the following code: Javascript var game; function game() { this.canvas = document.getElementById('canvas'); this.canvasWidth = window.innerWidth; this.canvasHeight = wi ...

When it comes to MongoDB aggregation, the order of indexes is not taken into consideration

To retrieve the 100 latest documents from a MongoDB collection, where each document is a combination of multiple documents with a similar field (in this case timestamp), I have implemented a series of queries using Node.js: return q.ninvoke(collec ...

My Node/Express service seems to be generating a span in the logs, but I am unable to locate my service in the Ja

Currently, I am in the process of setting up Jaeger tracing for my Node.js microservice using Express.js. After adding a basic GET request handler in my Express app and hitting the endpoint via curl, I notice that a span is created in the logs. However, w ...