I´ve been trying to make an sphere whith three.js with an image texture but without a server, i mean im just learning and i jjust want to use vs code but ether way this an local image or an https image online it gives this error:
Access to image at 'file:///C:/Users/***/...../***/sun.jpg' from origin 'null' has been blocked by
CORS policy: Cross origin requests are only supported for protocol schemes: http, data, isolated-app,
chrome-extension, chrome, https, chrome-untrusted.
GET file:///C:/Users/...../sun.jpg net::ERR_FAILED
And here the code im using:
const geometry = new THREE.SphereGeometry(5, 32, 32);
const cargador = new THREE.TextureLoader().load("sun.jpg");
const cargaTextura = new THREE.MeshBasicMaterial({
map: cargador
});
const sphere = new THREE.Mesh(geometry, material);
scene.add(sphere);
im not a profesional programer so if someone can explain everthing it would be great
thanks for all help.
2
Answers
Browsers restrict loading files from the local file system.
You have several options:
You can serve the image using a web server like express, nginx…
Use a cloud storage bucket and access it via URI
You can convert the image to base64 or something and display it directly by doing
new THREE.TextureLoader().load(yourBase64string)
here you can try this to get you started, you’ll need the internet to load three.js in this model
use the file button to select a file from your local harddrive
you can paste a url into the text field and press enter
or click the mesh button