Three.js不显示球体上的纹理

时间:2012-04-18 12:36:18

标签: google-chrome textures webgl three.js

有一些关于纹理的线程没有显示出来。我已经尝试过所有这些,但没有任何帮助。

我现在花了几个小时。每当我最终看到一个黑色的球体。

我正在使用Chrome v18和Windows 7.我也尝试使用Firefox,但此浏览器并不支持Three.js。

这是剧本的主体:

<body>

    <script src="../build/Three.js"></script>

    <script src="js/Stats.js"></script>
    <script src="../build/jquery-1.7.2.min.js"></script>

这是脚本本身:

// stap1) camera, set the scene size
    var WIDTH = 400,
        HEIGHT = 300;

    // set some camera attributes
    var VIEW_ANGLE = 45,
        ASPECT = WIDTH / HEIGHT,
        NEAR = 0.1,
        FAR = 10000;

    var camera = new THREE.PerspectiveCamera(  
                VIEW_ANGLE,
          ASPECT,
          NEAR,
          FAR  );
// stap2) scene:                
    var scene = new THREE.Scene();
    // the camera starts at 0,0,0 so pull it back
   scene.add(camera);
     camera.position.z = +300;
    // get the DOM element to attach to
    // - assume we've got jQuery to hand
    var container = $('#container');

    // stap3)create a WebGL renderer:
    var renderer = new THREE.WebGLRenderer();
    // start the renderer
    renderer.setSize(WIDTH, HEIGHT);
    // attach the render-supplied DOM element
    container.append(renderer.domElement);

    // bol maken:
    // create the sphere's material
    // b.v: THREE.MeshBasicMaterial
    var sphereMaterial = new THREE.MeshLambertMaterial(
    {
        map: THREE.ImageUtils.loadTexture("http://dev.root.nl/tree/examples/textures/ash_uvgrid01.jpg")
    });

    // set up the sphere vars
    var radius = 50, segments = 16, rings = 16;
    var sphereGeometry = new THREE.SphereGeometry(radius, segments, rings); 
    // create a new mesh with sphere geometry -
    var sphere = new THREE.Mesh(
       sphereGeometry,
       sphereMaterial
         );

    sphere.position.x=0;
    var s=1;
    sphere.scale.set(s, s, s);  

    // add the sphere to the scene
    scene.add(sphere);
    // create a point light
    var pointLight = new THREE.PointLight( 0xFFFFFF );
    // set its position
    pointLight.position.x = 10;
    pointLight.position.y = 50;
    pointLight.position.z = 130;
    // add to the scene
    scene.add(pointLight);
    // draw!
    renderer.render(scene, camera);

1 个答案:

答案 0 :(得分:4)

您需要等到用作纹理的图像完全下载。

我已将您的代码放在网上:http://jsfiddle.net/4Qg7K/并添加了一个经典的“渲染循环”:

requestAnimationFrame(render);

function render(){
  requestAnimationFrame(render);

  sphere.rotation.y += 0.005; //rotation stuff, just for fun

  renderer.render(scene, camera);
};

requestAnimationFrame函数就像一个计时器,每次浏览器准备好更新网页时都会调用render函数。

BTW,Three.js可以正常使用Firefox。