Skip to content

Commit

Permalink
fix: spriteAnimator change frame bug (#1814)
Browse files Browse the repository at this point in the history
* fix: cached frame when changing animation sequence

* fix for useSpriteLoader
  • Loading branch information
Michael Dobekidis authored Feb 5, 2024
1 parent 514be0d commit 6ad2cc1
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 16 deletions.
7 changes: 3 additions & 4 deletions src/core/SpriteAnimator.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -168,12 +168,12 @@ export const SpriteAnimator: React.FC<SpriteAnimatorProps> = /* @__PURE__ */ Rea
}
}, [autoPlay])

React.useEffect(() => {
React.useLayoutEffect(() => {
if (currentFrameName.current !== frameName && frameName) {
currentFrame.current = 0
currentFrameName.current = frameName
state.hasEnded = false
modifySpritePosition()
// modifySpritePosition()
if (spriteData.current) {
const { w, h } = getFirstItem(spriteData.current.frames).sourceSize
const _aspect = calculateAspectRatio(w, h)
Expand Down Expand Up @@ -389,7 +389,6 @@ export const SpriteAnimator: React.FC<SpriteAnimatorProps> = /* @__PURE__ */ Rea
frameIndex = Math.max(0, Math.min(frameIndex, spriteFrames.length - 1))

if (isNaN(frameIndex)) {
console.log('nan frame detected')
frameIndex = 0 //fallback
}
currentFrame.current = frameIndex
Expand Down Expand Up @@ -449,7 +448,7 @@ export const SpriteAnimator: React.FC<SpriteAnimatorProps> = /* @__PURE__ */ Rea
)}
{!displayAsSprite && (
<Instances
limit={maxItems} // Optional: max amount of items (for calculating buffer size)
limit={maxItems ?? 1} // Optional: max amount of items (for calculating buffer size)
>
<planeGeometry args={[1, 1]} />
<meshBasicMaterial
Expand Down
13 changes: 1 addition & 12 deletions src/core/useSpriteLoader.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Texture, TextureLoader } from 'three'
import { useLoader, useThree } from '@react-three/fiber'
import { useEffect, useState } from 'react'
import { useState } from 'react'
import * as React from 'react'
import * as THREE from 'three'

Expand Down Expand Up @@ -44,7 +44,6 @@ export function useSpriteLoader<Url extends string>(
onLoad?: (texture: Texture, textureData?: any) => void
): any {
const v = useThree((state) => state.viewport)
const gl = useThree((state) => state.gl)
const spriteDataRef = React.useRef<any>(null)
const totalFrames = React.useRef<number>(0)
const aspectFactor = 0.1
Expand Down Expand Up @@ -201,16 +200,6 @@ export function useSpriteLoader<Url extends string>(
onLoad?.(spriteTexture, spriteData)
}, [spriteTexture, spriteData])

// https://github.com/mrdoob/three.js/issues/22696
// Upload the texture to the GPU immediately instead of waiting for the first render
// NOTE: only available for WebGLRenderer
useEffect(() => {
if ('initTexture' in gl) {
const array = Array.isArray(spriteTexture) ? spriteTexture : [spriteTexture]
array.forEach(gl.initTexture)
}
}, [gl, spriteTexture])

return { spriteObj, loadJsonAndTexture }
}

Expand Down

0 comments on commit 6ad2cc1

Please sign in to comment.