DEV Community

Cover image for Animated texture library for react-three-fiber available in react's hooks base
j1ngzoue
j1ngzoue

Posted on

Animated texture library for react-three-fiber available in react's hooks base

Introduction

We have created a simple library that will load animation textures by specifying an image file (gif/png/apng) file.
Repository

Frames are parsed using gifuct-js and UPNG.js. The frame parsing process has a large impact on the main thread, so it is executed on the web worker.

It can be used without preparing a sprited image.

Install

npm i animation-texture
Enter fullscreen mode Exit fullscreen mode

Usage

import React, { useRef, useEffect } from "react";
import * as THREE from "three";
import { useAnimationTexture } from "animation-texture";

interface Props {
  url: string;
}

export function Model({ url }: Props) {
  const { animationTexture } = useAnimationTexture({ url });
  const meshRef = useRef();

  useEffect(() => {
    if (meshRef.current && animationTexture) {
      meshRef.current.material.map = animationTexture;
      meshRef.current.material.needsUpdate = true;
    }
  }, [animationTexture]);

  return (
    <mesh ref={meshRef} position={new THREE.Vector3(0, 0, 0)}>
      <planeGeometry args={[1, 1]} />
      <meshBasicMaterial transparent side={THREE.FrontSide} />
    </mesh>
  );
}
Enter fullscreen mode Exit fullscreen mode

Pre-load if necessary.

import React from "react";
import * as THREE from "three";
import { preLoad } from "animation-texture";

export default function App() {
  preLoad('/sample.png');
  return ...
}

Enter fullscreen mode Exit fullscreen mode

Demo

Image description

I believe there is still significant room for performance improvement.

Image of Datadog

The Essential Toolkit for Front-end Developers

Take a user-centric approach to front-end monitoring that evolves alongside increasingly complex frameworks and single-page applications.

Get The Kit

Top comments (0)

nextjs tutorial video

Youtube Tutorial Series 📺

So you built a Next.js app, but you need a clear view of the entire operation flow to be able to identify performance bottlenecks before you launch. But how do you get started? Get the essentials on tracing for Next.js from @nikolovlazar in this video series 👀

Watch the Youtube series

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay