【问题标题】:Three.js does not show texture on sphereThree.js 在球体上不显示纹理
【发布时间】:2012-04-29 20:53:17
【问题描述】:

有一些关于纹理的线程没有显示出来。我都试过了,但没有任何帮助。

我现在已经为此花费了几个小时。每次我最终看到一个黑色的球体。

我正在使用 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);

【问题讨论】:

    标签: google-chrome textures webgl three.js


    【解决方案1】:

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

    我已将您的代码放到网上: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 函数。

    顺便说一句,Three.js 在 Firefox 上运行良好。

    【讨论】:

      猜你喜欢
      • 2014-02-27
      • 2013-06-22
      • 2013-12-19
      • 2011-10-18
      • 2015-09-22
      • 1970-01-01
      • 2013-12-23
      • 2022-11-23
      • 2019-02-11
      相关资源
      最近更新 更多