在three.js中使用球体上的纹理

Using textures on spheres in three.js

本文关键字:纹理 three js      更新时间:2023-09-26

我能够使用javascript和three.js库创建一个球体。然而,我有一个我想覆盖在球体顶部的图像,每当我这样做时,球体就会变成一个黑色球体,上面没有投影图像。以下是我如何实现它的:var renderer=new THREE.WebGLRenderer();renderer.setSize(window.innerWidth,window.innerHeight);document.body.appendChild(renderer.domElement);

  // camera
  var camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 1, 1000);
  camera.position.z = 500;
  // scene
  var scene = new THREE.Scene();
  // sphere
  // the first argument of THREE.SphereGeometry is the radius, the second argument is
  // the segmentsWidth, and the third argument is the segmentsHeight.  Increasing the 
  // segmentsWidth and segmentsHeight will yield a more perfect circle, but will degrade
  // rendering performance
  var texture = THREE.ImageUtils.loadTexture('beach.jpg', {}, function() {
  renderer.render(scene, camera);
  });
  texture.needsUpdate = true;
//  texture.wrapS = texture.wrapT = THREE.ClampToEdgeWrapping;
//  texture.repeat.set( 125, 125 );
//  texture.offset.set( 15, 15 );
 // texture.needsUpdate = true;
  var material = new THREE.MeshBasicMaterial({map: texture});
  var sphere = new THREE.Mesh(new THREE.SphereGeometry(150, 100, 100), material);      
  //mesh = new THREE.Mesh(sphere, material);
  //scene.add(mesh);
 // var sphere = new THREE.Mesh(new THREE.SphereGeometry(150, 100, 100), new THREE.MeshNormalMaterial());
 // sphere.overdraw = true;
  scene.add(sphere);

  renderer.render(scene, camera);

我已经尝试了所有的方法,但图像并没有成功地覆盖在球体的顶部,我该如何使用three.js来完成这项工作?我的"beach.jpg"文件与index.html.位于同一目录中

感谢您抽出时间,

尝试包含一个图像加载器。图片必须完整加载,然后才能用作纹理。更多详细信息:https://github.com/mrdoob/three.js/issues/1751