Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
196 views
in Technique[技术] by (71.8m points)

javascript - Plane always black

I want to add a texture to my plane that repeats horizontal and vertical. The thing is, when I try to apply the texture, it is always black. I don't get any errors, and i already tried to add some lights, but the problem is still there; I don't know how to solve it... Here is what I did:

window.onload = function init()
{
  scene = new THREE.Scene();

  var camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.1, 1000);
  camera.position.x = -30;
  camera.position.y = 40;
  camera.position.z = 30;
  camera.lookAt(scene.position);

  var light = new THREE.AmbientLight( 0x404040 ); // soft white light
  scene.add( light );

  var spotlight = new THREE.SpotLight( 0xffffff);
  spotlight.position.set( -50, 40, 0 );
  scene.add( spotlight );


var axes = new THREE.AxisHelper( 20 ); scene.add(axes);

  var renderer = new THREE.WebGLRenderer();
  renderer.setClearColor(0xEEEEEE);
  renderer.setSize(window.innerWidth, window.innerHeight);

  desenhaMapa();

  document.body.appendChild( renderer.domElement );
  renderer.render(scene, camera);

}

    function desenhaMapa()
{
  labirinto = new THREE.Object3D();

  var texturaPlano = new THREE.TextureLoader().load("texturaPac.jpg");

  geometryPlano = new THREE.PlaneGeometry(50,50);
  materialPlano = new THREE.MeshPhongMaterial( {map: texturaPlano} );
  var planoPacMan = new THREE.Mesh(geometryPlano,materialPlano);
  planoPacMan.rotation.x = -0.5 * Math.PI;
  scene.add(planoPacMan);
}

Any suggestions?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

TextureLoader.load() is an asynchronous method. That is why it has an onload argument.

You are calling render() before the texture loads. One solution is to call render() in the loader callback.

var loader = new THREE.TextureLoader();

var texture = loader.load( 'myTexture.jpg', function ( texture ) {

    renderer.render( scene, camera );

} );

Another solution is to have an animation loop. But that is not required for static scenes.

three.js r.78


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...