Animated GIF as texture in THREE.js
Asked Answered
P

5

11

I'm looking for a way to use a GIF animation as texture in THREE.js. I am currently able to load a texture (even GIF format), but it doesn't play its animation.

Is there any way to do it? I found some links like these:

https://github.com/JordiRos/GLGif

http://stemkoski.github.io/Three.js/Texture-Animation.html

But I need to play GIF animation as a texture, not in a Canvas.

Purpleness answered 11/2, 2014 at 18:26 Comment(2)
You say "I need to play GIF animation as a texture, not in a Canvas" but Three.js textures are rendered to a canvas.Captive
What do you mean "not in a Canvas"?Unwitting
B
5

What you're seeing isn't an animated GIF as a texture. The sites you linked use libraries to render each individual frame of the GIF as a texture and then cycle through them by changing the offset of the textured image.

Look at the source for the TextureAnimation link and see line 157. That is the object that performs this. You can see that the running animation isn't a GIF at all:

http://stemkoski.github.io/Three.js/images/run.png

Bacteriolysis answered 25/10, 2014 at 21:21 Comment(0)
G
4

I had a similiar issue with animations using threejs. So I created a simple package to solve that: https://github.com/MaciejWWojcik/three-plain-animator

I hope that will help someone who get here for answers about threejs animations.

Grinnell answered 27/4, 2019 at 23:30 Comment(2)
the example from stemkoski was very helpful for creating thisGrinnell
Gif files are not supported.Hemianopsia
B
2

Use gifuct-js to render a gif into a Canvas and then use CanvasTexture with that canvas as a source.

Here's my POC that seems to work(may need refreshing the preview after it loads) https://codesandbox.io/s/giftexture-51rmw

Bigler answered 5/11, 2021 at 21:7 Comment(0)
G
2

Create a video element, load that element using VideoTexture and add the texture to your mesh.

// Create video and play
let textureVid = document.createElement("video")
textureVid.src = `texture.mp4`; // transform gif to mp4
textureVid.loop = true;
textureVid.play();


// Load video texture
let videoTexture = new THREE.VideoTexture(textureVid);
videoTexture.format = THREE.RGBFormat;
videoTexture.minFilter = THREE.NearestFilter;
videoTexture.maxFilter = THREE.NearestFilter;
videoTexture.generateMipmaps = false;

// Create mesh
var geometry = new THREE.SphereGeometry( 1, 1, 1 );
var material = new THREE.MeshBasicMaterial( { map: videoTexture } );
mesh = new THREE.Mesh( geometry, material );
scene.add(mesh);
Galliard answered 21/2, 2022 at 21:57 Comment(1)
Thanks, this allowed me to have a cloud animation video on my Earth sphere. I had to add textureVid.crossOrigin = 'anonymous'; and use textureVid.src = 'http://localhost:8080/texture.mp4'; to have locally served files in the element, textureVid.muted = true; to have the element working in the first place, and videoTexture.format = THREE.RGBAFormat; since it seems that the RGBFormat was removed from Three.js recently.Jap
L
0

An alternative to using an animated GIF as texture could be to use a tool such as ffmpeg to convert the animated gif into a video file, and then use that video as a texture, which is supported by WebGL.

Logos answered 28/6, 2020 at 14:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.