I'm a beginner with three.js and I'm struggling to get my texture to render properly in my scene. Despite following the documentation closely, all I see is a blank canvas with no errors in the console. Can anyone offer any guidance on why my code isn't rendering the texture as expected? Any help would be greatly appreciated. Here is the JavaScript code I'm using:
`var camera, scene, renderer, texture, fog; var mesh, geometry, material, buffer;
var start_time = Date.now();
init();
animate();
function init() {
var container = $('#clouds');
// Background gradient
var canvas = document.createElement('canvas');
canvas.width = 32;
canvas.height = window.innerHeight;
var context = canvas.getContext('2d');
var gradient = context.createLinearGradient(0, 0, 0, canvas.height);
gradient.addColorStop(0, "#1e4877");
gradient.addColorStop(0.5, "#4584b4");
context.fillStyle = gradient;
context.fillRect(0, 0, canvas.width, canvas.height);
container.css('background', 'url(' + canvas.toDataURL('image/png') + ')');
container.css('background-size', '32px 100%');
//
camera = new THREE.PerspectiveCamera(30, window.innerWidth / window.innerHeight, 1, 3000);
camera.position.z = 6000;
scene = new THREE.Scene();
geometry = new THREE.Geometry();
var texture = new THREE.TextureLoader().load('/media/cloud-element.png', animate);
THREE.LinearMipMapNearestFilter;
THREE.LinearMipMapLinearFilter;
var fog = new THREE.Fog(0x4584b4, -100, 3000);
material = new THREE.ShaderMaterial({
uniforms: {
"map": { type: "t", value: texture },
"fogColor": { type: "c", value: fog.color },
"fogNear": { type: "f", value: fog.near },
"fogFar": { type: "f", value: fog.far },
},
depthWrite: false,
depthTest: false,
transparent: true,
color: 0xf2f2f2
});
var plane = new THREE.Mesh(new THREE.PlaneGeometry(64, 64));
for (var i = 0; i < 8000; i++) {
plane.position.x = Math.random() * 1000 - 500;
plane.position.y = - Math.random() * Math.random() * 200 - 15;
plane.position.z = i;
plane.rotation.z = Math.random() * Math.PI;
plane.scale.x = plane.scale.y = Math.random() * Math.random() * 1.5 + 0.5;
THREE.GeometryUtils.merge(geometry, plane);
}
mesh = new THREE.Mesh(geometry, material);
scene.add(mesh);
mesh = new THREE.Mesh(geometry, material);
mesh.position.z = -8000;
scene.add(mesh);
renderer = new THREE.WebGLRenderer();
renderer.setSize(window.innerWidth, window.innerHeight);
container.append(renderer.domElement);
window.addEventListener('resize', onWindowResize, false);
}
function onWindowResize(event) {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
}
function animate() {
requestAnimationFrame(animate);
var position = ((Date.now() - start_time) * 0.03) % 8000;
camera.position.x += (camera.position.x) * 0.01;
camera.position.y += (camera.position.y) * 0.01;
camera.position.z = -position + 8000;
renderer.render(scene, camera);
}
}`