I’m trying to solve the problem of mapping different textures in InstancedMesh using a texture atlas by shifting uv. But for some reason I don’t have any image displayed. What is the problem? I have 1 row and 6 columns in my sprite. The pictures are 48x48 each.
const materials = new ShaderMaterial({
uniforms: {
textureAtlas: { value: imageLoader.load('sprite.png'), (texture) => {
texture.wrapS = RepeatWrapping;
texture.wrapT = RepeatWrapping;
texture.needsUpdate = true;
}) },
atlasSize: { value: new Vector2(6, 1) }
},
vertexShader: `
attribute vec2 offset;
varying vec2 vUv;
void main() {
vUv = uv / atlasSize + offset / atlasSize;
gl_Position = projectionMatrix * modelViewMatrix * instanceMatrix * vec4(position, 1.0);
}
`,
fragmentShader: `
uniform sampler2D textureAtlas;
varying vec2 vUv;
void main() {
gl_FragColor = texture2D(textureAtlas, vUv);
}
`,
});
const mesh = new InstancedMesh(imageVO.geometry, materials, item.length);
const offsets = [];
for (let i = 0; i < item.length; i++) {
const col = i % 6;
offsets.push(col / 6, 1);
}
const offsetAttribute = new InstancedBufferAttribute(new Float32Array(offsets), 1);
mesh.geometry.setAttribute('offset', offsetAttribute);
scene.add(mesh)