如何在 Three.js 中重复纹理而不拉伸?

Roh*_*nde 4 javascript textures texture-mapping three.js

无论我做什么,我的纹理似乎都会被拉伸/缩放到我应用它们的网格的大小。我已经阅读了这个问题的很多答案,似乎没有一个解决方案可以为我解决问题,所以我发布了一个新的解决方案。只是一点信息,

  • 我的纹理都是 64x64 像素
  • 我正在预加载所有纹理
  • 我正在使用 Web GL 渲染器

这是我的代码

makeTestObject:function()
{
    var scope = this,
        tileGeometry = new THREE.BoxGeometry(TILE_SIZE , TILE_HEIGHT , TILE_SIZE),
        texture = new THREE.Texture(preloadedImageObject),
        textureMaterial = new THREE.MeshLambertMaterial({map:texture}),
        tile = new THREE.Mesh(tileGeometry , new THREE.MeshFaceMaterial(
        [
            textureMaterial, // +x
            textureMaterial, // -x
            textureMaterial, // +y
            textureMaterial, // -y
            textureMaterial, // +z
            textureMaterial // -z
        ]));

    texture.wrapS = THREE.RepeatWrapping;
    texture.wrapT = THREE.RepeatWrapping;

    tile.position.y = BASE_HEIGHT * 2;
    tile.castShadow = true;
    tile.receiveShadow = true;
    texture.needsUpdate = true;
    scope.scene.add(tile);
}
Run Code Online (Sandbox Code Playgroud)

如果我这样做texture.repeat.set(x , x)并设置x为任何类型的值,纹理似乎就会消失,并且留下平坦的颜色。

知道我做错了什么吗?

Roh*_*nde 5

好的,对于标准的盒子几何形状(正方形或矩形),解决方案是这样的;

makeTestObject:function()
{
    var scope = this,
        tileGeometry = new THREE.BoxGeometry(TILE_SIZE , TILE_HEIGHT , TILE_SIZE),
        texture = new THREE.Texture(preloadedImageObject),
        textureMaterial = new THREE.MeshLambertMaterial({map:texture}),
        tile = new THREE.Mesh(tileGeometry , new THREE.MeshFaceMaterial(
        [
            textureMaterial, // +x
            textureMaterial, // -x
            textureMaterial, // +y
            textureMaterial, // -y
            textureMaterial, // +z
            textureMaterial // -z
        ]));

    texture.wrapS = THREE.RepeatWrapping;
    texture.wrapT = THREE.RepeatWrapping;
    tile.geometry.computeBoundingBox();

    var max = tile.geometry.boundingBox.max;
    var min = tile.geometry.boundingBox.min;
    var height = max.y - min.y;
    var width = max.x - min.x;

    texture.repeat.set(width / TEXTURE_SIZE , height / TEXTURE_SIZE);

    texture.needsUpdate = true;
    scope.scene.add(tile);
}
Run Code Online (Sandbox Code Playgroud)

关键是正确设置纹理重复的比例。您可能还希望为每个面创建新材质,而不是一遍又一遍地引用相同的材​​质对象。