Reputation: 110
I am trying to put an image on an object, like this:
var texture = new THREE.TextureLoader().load( 'crate.gif' );
var geometry = new THREE.BoxBufferGeometry( 200, 200, 200 );
var material = new THREE.MeshBasicMaterial( { map: texture } );
mesh = new THREE.Mesh( geometry, material );
scene.add( mesh );
I have "crate.gif" in my local folder, but it does not appear on the box.
I am expected to either run a web server, or I can use a data url, because local image loading does not work, as re-iterated by the developer.
I realize the image may not be displaying because it hasn't loaded before being called. What is the easiest way to load an image?
Upvotes: 0
Views: 10083
Reputation: 59
Another way to load an image as a texture is with a require and using a relative path of your image:
I'm using React and works for me.
const textureImage = require('../assets/images/image.png');
const texture = new THREE.TextureLoader().load(textureImage);
const geometry = geometry = new THREE.BoxBufferGeometry( 200, 200, 200 );
const material = new THREE.MeshBasicMaterial( { map: texture } );
const mesh = new THREE.Mesh(geometry, material);
scene.add(mesh);
Upvotes: 4
Reputation: 110
It is just as easy as using a Data URI in place of an image filename, if it's done right:
var geometry = new THREE.SphereGeometry(0.5, 32, 32);
var texture = new THREE.TextureLoader().load( "data:image/jpeg;base64,/9j/4AAQSkZJRgA--(truncated-for-example)--BAgEASABIAAD" );
var material = new THREE.MeshBasicMaterial( { map: texture } );
var mesh = new THREE.Mesh(geometry, material);
scene.add(mesh);
Upvotes: 5