Yes, that’s what I’ve been looking at as well, although I don’t fully grasp what’s happening there. My material is a PBRMetallicRoughnessMaterial
.
Here are some of the (simplified) manipulations on the material and instances I’m running in case it helps:
export const makeMaterial = ({
scene,
name,
materialParameters: {
baseColor,
metallicRatio = 0,
roughnessRatio = 1,
alphaRatio = 1
},
onError = () => {}
}) => {
const material = new PBRMetallicRoughnessMaterial(name, scene)
material.baseColor = Color3.FromHexString(baseColor)
material.metallic = metallicRatio
material.roughness = roughnessRatio
material.alpha = alphaRatio
material.onError = onError
return material
}
export const instantiateMesh = ({
mesh,
instances,
scalingFn,
colorFn
}) => {
// prepare root mesh for instancing
mesh.alwaysSelectAsActiveMesh = true
if (mesh.isPickable === true) {
mesh.thinInstanceEnablePicking = true
}
// prepare custom buffer for instances color
// https://doc.babylonjs.com/divingDeeper/mesh/copies/instances#custom-buffers
let bufferColors = new Float32Array(4 * instances.length)
// create thin instances
let bufferMatrices = new Float32Array(16 * instances.length)
let index = 0
const addInstanceToBuffers = ({
instance,
__instanceIndex
}) => {
// position mesh
const { position, rotation, scaling } = computeMeshPosition({
instance,
useQuaternion: true
})
const instanceSpecificScaling = scaling.scale(scalingFn(instance))
const matrix = Matrix.Compose(instanceSpecificScaling, rotation, position)
matrix.copyToArray(bufferMatrices, index * 16)
// instance base color in RGBA (with multi-materials suport)
const color = Color3.FromHexString(colorFn(instance))
bufferColors[index * 4 + 0] = color.r
bufferColors[index * 4 + 1] = color.g
bufferColors[index * 4 + 2] = color.b
bufferColors[index * 4 + 3] = 1 // we don't support alpha on instance due to render order issues, see babylon forum
// next
index++
}
instances.forEach((instance, __instanceIndex) =>
addInstanceToBuffers({ instance, __instanceIndex })
)
const staticMatrices = false
const staticColors = false
mesh.thinInstanceSetBuffer('matrix', bufferMatrices, 16, staticMatrices)
mesh.thinInstanceSetBuffer('color', bufferColors, 4, staticColors)
}
I had thought maybe I needed to name the buffer “instanceColor” instead of “color” (looking at the changes in the commit) but it’s doesn’t solve the issue and the PG still works with “color”. Is it sure the shader is reading from the “color” buffer?
I’m still trying to isolate a repro.