Hello Prisoner,
How do you calculate the UV for the geometry of each box?
I understand that the shader is a general material and then you cut it to project it on each box.
While searching, I found this which explains that the texture must be at 0.5,0.5 in object coordinates:
http://paulyg.f2s.com/uv.htm
So I tried to increment the texture in different ways:
//uvs.push(_x / size, 1 - ((_z + 1) /size));
//uvs.push(1 / size*_x, 1/size* _z);
//uvs.push(.5 + _x / size,.5 + _z/size);
//uvs.push(.5 + _x / size,_z/size);
//uvs.push(.5 + _x / size,.5 * _z/size);
//uvs.push(_x,0.5 * _z/size);
//uvs.push(_z,0.5 * _x/size);
//uvs.push(_x,_z)
//uvs.push(_z,0.5 + _x/size)
//uvs.push(_z * _z/size,_x/size)
But the texture is still stuck on a single image that repeats in the z and x axis. No way at least to have different images, so I can figure out how to change my increment.
By having fun to cut the image in several images I succeeded, but it is laborious and your solution is obviously better. Can you point me in the right direction, as it is I don’t see a way out of this?
function create_grid() {
const geometry = new THREE.BoxGeometry(1, 1, 1);
const material = new THREE.MeshBasicMaterial({
color: 0xffd839
});
const texture = []
let f = 0
for (let i = 0; i < 5; i++) {
texture[i] = []
for (let j = 0; j < 5; j++) {
texture[i][j] = new THREE.TextureLoader().load('img/spritesheet/' + 'tile' + f + '.png');
f++
}
}
const cube = []
for (let i = 0; i < 5; i++) {
cube[i] = []
for (let j = 0; j < 5; j++) {
cube[i][j] = []
for (let k = 0; k < 2; k++) {
cube[i][j][k] = new THREE.Mesh(geometry, material);
cube[i][j][k].position.set(i * 1.1, k, j * 1.1)
scene.add(cube[i][j][k]);
}
}
}
let b = 0
for (let i = 0; i < 5; i++) {
for (let j = 0; j < 5; j++) {
cube[i][j][1].material = [
new THREE.MeshBasicMaterial({
color: 0xfaad80,
}),
new THREE.MeshBasicMaterial({
color: 0xfaad80,
}),
//top surface
new THREE.MeshPhongMaterial({
map: texture[j][i]
}),
new THREE.MeshBasicMaterial({
color: 0xfaad80,
}),
new THREE.MeshBasicMaterial({
color: 0xfaad80,
}),
new THREE.MeshBasicMaterial({
color: 0xfaad80,
}),
]
b++
}
}
}