Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Documentation: InstancedMesh constructor docs omit array of Material possibility #28414

Closed
finnbear opened this issue May 18, 2024 · 1 comment · Fixed by #28415
Closed

Documentation: InstancedMesh constructor docs omit array of Material possibility #28414

finnbear opened this issue May 18, 2024 · 1 comment · Fixed by #28415
Milestone

Comments

@finnbear
Copy link
Sponsor Contributor

Description

The InstancedMesh constructor docs say:

material - an instance of Material. Default is a new MeshBasicMaterial.

But InstancedMesh also supports an array of materials, like Mesh.

Reproduction steps

https://threejs.org/docs/#api/en/objects/InstancedMesh

Code

import * as THREE from 'three';

const camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 0.01, 10 );
camera.position.z = 1;

const scene = new THREE.Scene();

const geometry = new THREE.BoxGeometry( 0.2, 0.2, 0.2 );
const material1 = new THREE.MeshBasicMaterial({color: 0xff0000});
const material2 = new THREE.MeshBasicMaterial({color: 0x0000ff});
geometry.addGroup(0, geometry.index.count / 2, 0);
geometry.addGroup(geometry.index.count / 2, geometry.index.count, 1);

const mesh = new THREE.InstancedMesh( geometry, [material1, material2], 1 );
mesh.setMatrixAt(0, new THREE.Matrix4());
scene.add( mesh );

const renderer = new THREE.WebGLRenderer( { antialias: true } );
renderer.setSize( window.innerWidth, window.innerHeight );
renderer.setAnimationLoop( animation );
document.body.appendChild( renderer.domElement );

function animation( time ) {
  mesh.rotation.x = time / 2000;
  mesh.rotation.y = time / 1000;

  renderer.render( scene, camera );
}

-> https://playcode.io/three

Live example

Paste into https://playcode.io/three

Screenshots

image

Version

r164 (latest)

Device

Desktop

Browser

Chrome

OS

Linux

@Mugen87
Copy link
Collaborator

Mugen87 commented May 20, 2024

I guess it wasn't documented because using multiple materials contradicts the idea of reducing draw calls. But technically, yes, it is possible to use multiple materials with InstancedMesh.

Regarding your code example: I was a bit confused that your code produced four render calls although you create only two groups. Be aware that BoxGeometry already generates own groups so you can assign a material per box side. Using geometry.clearGroups() right after the creation produced the expected result (2 draw calls).

https://jsfiddle.net/aw1gxbhz/1/

@Mugen87 Mugen87 added this to the r165 milestone May 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants