如何将图像放入Three.js纹理中

时间:2016-05-15 15:14:42

标签: javascript three.js textures

我正在尝试将纹理放在Three.js对象中,但是当我应用图像时,对象全部变黑。某些警报显示在代码上但没有错误。上图显示结果。请问一些建议吗?

    <!DOCTYPE html>
<html>
<head>

<title>Meu Primeiro WebGL</title>
    <style type="text/css">canvas {width: 100%; height: 100%}</style>
     <meta name="viewport" content="width=device-width, initial-scale=1.0">


</head>
<body>
<script src="three.js"></script>


<script>
       var scene = new THREE.Scene();
       var loader = new THREE.ImageLoader();

            var camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);

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

            //primeiro cubo
            var cubegeometry = new THREE.CubeGeometry(5,0.45,5);
            var cubematerial = new THREE.MeshBasicMaterial({wireframe: false, color: 0x0033FF});
            var cube = new THREE.Mesh(cubegeometry, cubematerial);



            //segundo objeto 
            var texture = THREE.ImageUtils.loadTexture('wood.jpg');
            var cbmaterial = new THREE.MeshPhongMaterial({map: texture});
            var cbgeometry = new THREE.CubeGeometry(5.05,0.5,3.75);

            var cb = new THREE.Mesh(cbgeometry, cbmaterial);



            scene.add(cube);
            scene.add(cb);
            camera.position.z = 6

            var render = function(){
                requestAnimationFrame(render);

                cube.rotation.y += 0.02;
                cube.rotation.x += 0.01;
                cube.rotation.z += 0.00;

1 个答案:

答案 0 :(得分:1)

如果您在场景中没有任何光线,那可能就是问题所在。添加至少一些环境光。例如:

var ambient = new THREE.AmbientLight( 0x555555 );
scene.add( ambient );