我可以使用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位于同一目录中。
感谢您的时间,
答案 0 :(得分:0)
尝试添加图像加载器。 在将图片用作纹理之前,必须完整加载图片。 更多详情:https://github.com/mrdoob/three.js/issues/1751