Greetings people!
I hope you are doing well.
I would be very grateful if anyone can help me fix this issue.
I am loading HTML images “using Array.from()” to use them as textures in Shader Material.
The code below is what I am using now which works, but, when the page first loads I get a black plane which shows that the images are not fully loaded.
However, on page refresh then all the images get rendered on the plane.
I would prefer the images get loaded and then fire the page-load to avoid the black screens.
What I can do to fix this problem? Thank you!
Here is my code:
// HTML to WebGL
let allMeshes = [];
let imagesToGL = Array.from(document.getElementsByClassName("work-gl-img"));
let geometry = new THREE.PlaneBufferGeometry(1, 1, 89, 89);
function htmlToWebgl() {
imagesToGL.forEach((image) => {
let texture = new THREE.Texture(image);
texture.needsUpdate = true;
let material = new THREE.ShaderMaterial({
uniforms: {
uTime: { type: "f", value: 0 },
uDirection: { type: "f", value: 0 },
uProgress: { type: "f", value: 0 },
uMouseSpeed: { type: "f", value: 0 },
uMouse: { type: "v2", value: new THREE.Vector2(0, 0) },
uTexture: new THREE.Uniform(texture),
uResolution: { type: "v4", value: new THREE.Vector4() },
},
side: THREE.DoubleSided,
vertexShader: vertex,
fragmentShader: fragment,
});
let mesh = new THREE.Mesh(geometry, material);
mesh.userData.image = image;
mesh.userData.material = material;
setScalePosition(mesh);
allMeshes.push(mesh);
scene.add(mesh);
});
}
htmlToWebgl();