我正在尝试使用three.js在旋转圆柱体的外部(而不是顶部或底部)显示多个图像。我能够显示1张图片,但我的目标是同时显示多个图片。我在材质数组中添加了3个纹理,但仅显示第一个。任何帮助表示赞赏。

<html>
<head>
    <title>My first three.js app</title>
    <span>Test</span>
    <style>
        body { margin: 0; }
        canvas { width: 100%; height: 100% }
    </style>
</head>
<body>
    <script src="js/three.js"></script>
    <script>
        var scene = new THREE.Scene();
        var camera = new THREE.PerspectiveCamera( 100, window.innerWidth / window.innerHeight, 0.1, 1000 );
        camera.position.y = 24;

        var renderer = new THREE.WebGLRenderer();
        renderer.setSize( window.innerWidth, window.innerHeight );
        document.body.appendChild( renderer.domElement );

        var diameter = 20;
        var geometry = new THREE.CylinderGeometry( diameter, diameter, 15, 32 );

        var texture1 = new THREE.TextureLoader().load( 'images/image1.jpg' );
        var texture2 = new THREE.TextureLoader().load( 'images/image2.jpg' );
        var texture3 = new THREE.TextureLoader().load( 'images/image3.png' );

        texture1.wrapS = THREE.RepeatWrapping;
        //texture.wrapT = THREE.RepeatWrapping;
        //texture.repeat.set( 1, 4 );

        var materials = [];
        materials.push(new THREE.MeshBasicMaterial({ map: texture1 }));
        materials.push(new THREE.MeshBasicMaterial({ map: texture2 }));
        materials.push(new THREE.MeshBasicMaterial({ map: texture3 }));

        var cylinder = new THREE.Mesh( geometry, materials );
        cylinder.position.y = 25;
        scene.add( cylinder);

        camera.position.z = 40;


        function render() {
            requestAnimationFrame(render);
            //cylinder.rotation.z += 0.05;
          cylinder.rotation.y += 0.005;
            renderer.render(scene, camera);
        }
        render();
    </script>
</body>

最佳答案

另一种方法是使用geometry.faces[i].materialIndex属性将纹理分配给几何的每个面。在这种情况下,您应该使用的径向段数是三个的倍数(如果有3个纹理)。



<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/89/three.min.js"></script>
<html>
<head>
    <title>My first three.js app</title>
    <style>
        body { margin: 0; overflow:hidden;}
        canvas { width: 100%; height: 100% }
    </style>
</head>
<body>
<script>
    var scene = new THREE.Scene();
    var camera = new THREE.PerspectiveCamera( 100, window.innerWidth / window.innerHeight, 0.1, 1000 );
    camera.position.y = 24;

    var renderer = new THREE.WebGLRenderer();
    renderer.setSize( window.innerWidth, window.innerHeight );
    document.body.appendChild( renderer.domElement );

    var diameter = 20;
    var radialSegments = 33;
    var geometry = new THREE.CylinderGeometry( diameter, diameter, 15, radialSegments );

    var img1 = "http://d2gg9evh47fn9z.cloudfront.net/800px_COLOURBOX9108127.jpg";
    var img2 = "http://d2gg9evh47fn9z.cloudfront.net/thumb_COLOURBOX8923432.jpg";
    var img3 = "http://d2gg9evh47fn9z.cloudfront.net/800px_COLOURBOX19377428.jpg";

    var texture1 = new THREE.TextureLoader().load( img1 );
    var texture2 = new THREE.TextureLoader().load( img2 );
    var texture3 = new THREE.TextureLoader().load( img3 );

    THREE.DefaultLoadingManager.onLoad = function () {
        var materials = [];
        materials.push(new THREE.MeshBasicMaterial({ map: texture1 }));
        materials.push(new THREE.MeshBasicMaterial({ map: texture2 }));
        materials.push(new THREE.MeshBasicMaterial({ map: texture3 }));
        var l = geometry.faces.length;
        for (var i = 0; i < l; i++) {
            if (geometry.faces[i].normal.y !== 0) {
                // these are caps
                geometry.faces[i].materialIndex = 0;
            } else {
                // each segment has 2 faces
                geometry.faces[i].materialIndex = Math.floor(i * 3 / (radialSegments * 2));
            }
        }

        var cylinder = new THREE.Mesh( geometry, materials);
        cylinder.position.y = 25;
        scene.add( cylinder);

        camera.position.z = 40;

        function render() {
            requestAnimationFrame(render);
            cylinder.rotation.y += 0.005;
            renderer.render(scene, camera);
        }
        render();
    }
</script>
</body>

09-25 16:41