Issue with Three.js CanvasRenderer not displaying Mesh when using multiple materials

Encountering an issue with CanvasRenderer not rendering a Mesh that has multiple materials applied to a BoxBufferGeometry. The Mesh appears without any materials when using CanvasRenderer, but works as expected with WebGLRenderer.

Check out the example code below:

// three.js: multiple materials on a single mesh

var renderer, scene, camera, mesh;

init();
render();

function init() {

  // Setting up renderer
  renderer = new THREE.CanvasRenderer( { alpha: true } );
  renderer.setSize( window.innerWidth / 2, window.innerHeight );
  document.body.appendChild( renderer.domElement );

  // Creating a scene
  scene = new THREE.Scene();

  // Configuring camera
  camera = new THREE.PerspectiveCamera( 40, (window.innerWidth / 2) / window.innerHeight, 1, 1000 );
  camera.position.set( 15, 20, 30 );
  camera.lookAt(scene.position);
  scene.add( camera );


  // Adding ambient light
  scene.add( new THREE.AmbientLight( 0xffffff, 0.1 ) );

  // Adding point light source
  camera.add( new THREE.PointLight( 0xffffff, 1 ) );

  // Creating geometry
  var geometry = new THREE.BoxBufferGeometry( 10, 10, 10 );

  // Defining different materials
  var material0 = new THREE.MeshBasicMaterial({ color: 0xff0000 });
  var material1 = new THREE.MeshBasicMaterial({ color: 0x00ff00 });
  var material2 = new THREE.MeshBasicMaterial({ color: 0x0000ff });
  var material3 = new THREE.MeshBasicMaterial({ color: 0xffff00 });
  var material4 = new THREE.MeshBasicMaterial({ color: 0x00ffff });
  var material5 = new THREE.MeshBasicMaterial({ color: 0xff00ff });

  var materials = [ material0, material1, material2, material3, material4, material5 ];

  // Creating mesh
  mesh = new THREE.Mesh( geometry, materials );
  scene.add( mesh );

}

function render() {
  requestAnimationFrame(render);
  mesh.rotation.x += 0.005;
  mesh.rotation.y += 0.01;
  renderer.render( scene, camera );
}

For a visual demonstration and comparison between WebGLRenderer and CanvasRenderer, check out this fiddle

Seeking guidance from experienced individuals in troubleshooting this issue: - Is there an error in my implementation? - Could there be limitations within CanvasRenderer causing it to fail in rendering such a Mesh? - Should I consider reporting this as a bug on the three.js repository?

Your assistance is greatly appreciated!

Note: I am relatively new to Three.js, so please pardon any glaring mistakes. The usage of CanvasRenderer is essential for me due to the need for taking screenshots using phantom.js.

Using Three.js r93

Answer №1

CanvasRenderer doesn't seem to have built-in support for working with BufferGeometry and multi-materials.

To address this issue, you can switch to using Geometry instead.

var geometry = new THREE.BoxGeometry( 10, 10, 10 );

This workaround is applicable for three.js version r.93.

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

Transferring extra data from jQuery autocomplete to a PHP script

Hey there! I'm wondering if it's possible to pass extra parameters from jQuery autocomplete to a PHP page, which would then use them to query a database and return the results. While I know how to send the typed term from the input box, I'd ...

The efficiency of a for loop in Javascript can be influenced by the way the loop

I experimented with three different for loop cases in JavaScript, each performing a seemingly useless action 1000000000 times. Surprisingly, the speed of these codes varied from one another. Initially, I suspected that the difference in performance could ...

Improve the translation animation on an element containing numerous child nodes

Looking for ways to enhance the smoothness of the transition in the "infinity list" animation. While it's just a demo at the moment, the real app will have various elements emerging from each "pin". The main performance bottleneck seems to stem from t ...

Angular implementation of a dynamic vertical full page slider similar to the one seen on www.tumblr

I'm determined to add a full-page slider to the homepage of my Angular 1.x app After testing multiple libraries, I haven't had much luck. The instructions seem incomplete and there are some bugs present. These are the libraries I've experi ...

Handling empty values in JSON data when sending it to the controller through AJAX

Attempting to transmit the following JSON data from View to Controller: { "allselected": false, "selectedIds": ["", "all"], "targetControl": "Studieniva", "targetSource": "studienivalist", "dependency": [{ "name": "ShopNo", ...

Tips for showcasing an image on an HTML page

I am encountering an issue with my HTML page that includes a Button. When clicked, it is supposed to display an image using an Ajax call. The functionality works properly in Postman Rest Client. However, when accessed through a browser, it only shows raw ...

Utilize Node.js to encrypt data from an extensive file

Hello, this is my initial inquiry. English isn't my native language and I need some help. I have a large file with about 800K lines that I need to read and encrypt using the sjcl library. So far, I've only managed to write the following code snip ...

A step-by-step guide on integrating HTML form input with an API object array

I'm looking to combine two code "snippets" but I'm not sure how to do it. One part of the code turns HTML form input into a JSON object. You can see the CodePen example here. What I want is when the "Add note" button is clicked, I want to grab ...

Adjust the vertical alignment of an HTML element

I've been trying to make an image move using the variable counter, but for some reason, it's not working. If I set document.getElementById("movee").style.top directly to a number, it works fine. Combining it with the counter variable should theor ...

Dealing with React Native: Navigating App Store Rejections and Embracing IPv6

In my react-native client for a website, the login page offers two options: manually enter the login code or scan it using a barcode scanner. I have extensively tested the app on real devices and emulators, and it's functioning correctly. However, all ...

Exploring the Scope of LocalStorage in Javascript

I've recently begun working with local storage and encountered some scope issues. Whenever I try to console.log(test), the second if statement returns undefined. What am I missing here? This is my current code; $("document").ready(function(){ ...

Here's a unique rewrite: "Learn how to manipulate the CSS class of a textarea element (specifically in NicEdit) by utilizing the addClass

I am currently validating a textarea using the NicEdit plugin. var getContent = nicEditors.findEditor("SubSliderDescription").getContent(); bValid = bValid && checkBlankTextArea(getContent, "SubSliderDescription") function checkBlankTextArea(o, ...

Tips for retrying an insertion into the database when a duplicate unique value is already present

After thorough searching, I couldn't find any existing patterns. My goal is to store a unique key value in my MySQL database. I generate it on the server side using this code: var pc = require('password-creator'); var key = pc.create(20); ...

Blip of an image then vanishes

Within my web application, I have implemented code to display images and allow users to navigate through them using Next and Previous buttons. The functionality works as expected, but there is an issue where the image briefly flashes and then disappears wh ...

Leveraging the wheelDelta property in the onmousewheel event handler with Script#

When working with Script#, I often utilize mouse events using the ElementEvent argument. However, one thing seems to be missing - the WheelDelta property for handling the onmousewheel event. Can anyone provide guidance on how to access this property in t ...

Unnecessary Page Diversion

Within my index.php file, I have a download button with the id of "render". Using AJAX, I am sending a request to the server. The JavaScript code being utilized is as follows: $('#render').click(function(e){ $('html,body').animat ...

What causes the tweets' IDs to be altered by axios when parsing the response from the Twitter search API?

I am currently utilizing axios to send a GET request to the Twitter search API in order to fetch recent tweets that utilize a specific hashtag. To begin with, I ran tests on the twitter search API via Postman and noticed that the id and id_str tweet statu ...

Need to know how to show a DIV for a specific time frame using Javascript?

One of the features I am looking to implement in my Django application is a display that notifies the user when one of their posts is about to start. The idea is to show a DIV element with the message: <div>“Will start soon”</div>, 15 minut ...

How to implement datepicker on multiple input fields

Below are the two input fields that have datepicker functionality: <div class="row"> <input type="text" class="form-control" datepicker-popup="{{format}}" ng-model="dt" is-open="opened" min="minDate" max="'2015-06-22&apos ...

Is there a different method to retrieve language bundles with next-i18next instead of using a customized Node server?

Currently, I am developing a Next.js application that will utilize i18next translations organized in the recommended file structure for bundles. For example: static/locales/en/common.js static/locales/de/common.js You can refer to this article: https://m ...