Are you utilizing a render loop? If your rendering is only occurring once, the issue may be due to the texture not finishing loading. Textures are loaded asynchronously, so you must continue rendering until the sphere is drawn with the texture after it has fully loaded, or you can wait for the texture to load by implementing a callback in the loader.
If I were to execute your code as-is, it would result in a black sphere being displayed.
<canvas id="c"></canvas>
<script type="module">
import * as THREE from 'https://threejsfundamentals.org/threejs/resources/threejs/r115/build/three.module.js';
function main() {
const scene = new THREE.Scene();
var loader = new THREE.TextureLoader();
var texture = loader.load('https://i.imgur.com/eCpD7bM.jpg');
var geometry = new THREE.SphereGeometry(3.7, 32, 32);
var material = new THREE.MeshBasicMaterial({ map: texture });
var sphere = new THREE.Mesh(geometry, material);
sphere.position.y=4;
scene.add(sphere);
const canvas = document.querySelector('#c');
const renderer = new THREE.WebGLRenderer({canvas});
const fov = 75;
const aspect = 2; // the canvas default
const near = 0.1;
const far = 500;
const camera = new THREE.PerspectiveCamera(fov, aspect, near, far);
camera.position.set(0, 4, 8);
scene.background = new THREE.Color('red');
renderer.render(scene, camera);
}
main();
</script>
However, if I incorporate a requestAnimationFrame
loop in the rendering process, the texture will eventually appear on the sphere.
<canvas id="c"></canvas>
<script type="module">
import * as THREE from 'https://threejsfundamentals.org/threejs/resources/threejs/r115/build/three.module.js';
function main() {
const scene = new THREE.Scene();
var loader = new THREE.TextureLoader();
var texture = loader.load('https://i.imgur.com/eCpD7bM.jpg');
var geometry = new THREE.SphereGeometry(3.7, 32, 32);
var material = new THREE.MeshBasicMaterial({ map: texture });
var sphere = new THREE.Mesh(geometry, material);
sphere.position.y=4;
scene.add(sphere);
const canvas = document.querySelector('#c');
const renderer = new THREE.WebGLRenderer({canvas});
const fov = 75;
const aspect = 2; // the canvas default
const near = 0.1;
const far = 500;
const camera = new THREE.PerspectiveCamera(fov, aspect, near, far);
camera.position.set(0, 4, 8);
scene.background = new THREE.Color('red');
function render() {
renderer.render(scene, camera);
requestAnimationFrame(render);
}
requestAnimationFrame(render);
}
main();
</script>
If you are still unable to view the texture locally, then, as mentioned by others, loading textures in WebGL requires the use of a web server. You can use a simple one.
Additionally, familiarize yourself with the JavaScript console to identify any error messages.