Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat/add scale ratio for useSpriteLoader #2002

Merged
merged 28 commits into from
Jun 20, 2024
Merged
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
6b3af00
feat: added optional Sprite or Plane geometry for SpriteAnimator
netgfx Sep 6, 2023
2c3b3d5
Merge remote-tracking branch 'origin/master'
netgfx Sep 13, 2023
66a09ec
Merge branch 'pmndrs:master' into master
netgfx Sep 19, 2023
f2422a0
Merge branch 'pmndrs:master' into master
netgfx Sep 29, 2023
4ea2d8c
Merge branch 'pmndrs:master' into master
netgfx Oct 7, 2023
67689d4
Merge branch 'pmndrs:master' into master
netgfx Oct 9, 2023
3de0966
Merge branch 'pmndrs:master' into master
netgfx Oct 19, 2023
723b3a1
Merge branch 'pmndrs:master' into master
netgfx Oct 26, 2023
b73ccde
Merge branch 'pmndrs:master' into master
netgfx Nov 15, 2023
583564f
Merge branch 'pmndrs:master' into master
netgfx Dec 18, 2023
29bb924
Merge branch 'pmndrs:master' into master
netgfx Jan 5, 2024
edb486a
Merge branch 'pmndrs:master' into master
netgfx Jan 21, 2024
6163ed9
Merge branch 'pmndrs:master' into master
netgfx Jan 31, 2024
197c444
Merge branch 'pmndrs:master' into master
netgfx Feb 3, 2024
1d97ca9
Merge branch 'pmndrs:master' into master
netgfx Feb 5, 2024
bfd6851
Merge branch 'pmndrs:master' into master
netgfx Feb 10, 2024
a1a3e18
Merge branch 'pmndrs:master' into master
netgfx Feb 11, 2024
f236ea3
Merge branch 'pmndrs:master' into master
netgfx Feb 24, 2024
7e25331
Merge branch 'pmndrs:master' into master
netgfx Mar 5, 2024
cf80ba3
Merge branch 'pmndrs:master' into master
netgfx Mar 16, 2024
4ab5687
Merge branch 'pmndrs:master' into master
netgfx Apr 12, 2024
dee87b1
Merge branch 'pmndrs:master' into master
netgfx May 31, 2024
abde9ab
Merge branch 'pmndrs:master' into master
netgfx Jun 10, 2024
e6758ca
Merge branch 'pmndrs:master' into master
netgfx Jun 11, 2024
b966985
Merge branch 'pmndrs:master' into master
netgfx Jun 20, 2024
862f0ee
feat/add scale-ratio calculation for each frame on useSpriteLoader
netgfx Jun 20, 2024
f2ddc00
chore/corrected formatting
netgfx Jun 20, 2024
05de992
fix/texture-encoding and color-space constant
netgfx Jun 20, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 46 additions & 3 deletions src/core/useSpriteLoader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -175,9 +175,13 @@ export function useSpriteLoader<Url extends string>(

aspect = calculateAspectRatio(frameWidth, frameHeight, aspectFactor, v)
}

//scale ratio for stadalone sprite
spriteDataRef.current.frames = calculateScaleRatio(spriteDataRef.current.frames)
} else if (_spriteTexture) {
spriteDataRef.current = json
spriteDataRef.current.frames = parseFrames()

totalFrames.current = Array.isArray(json.frames) ? json.frames.length : Object.keys(json.frames).length
const { w, h } = getFirstItem(json.frames).sourceSize
aspect = calculateAspectRatio(w, h, aspectFactor, v)
Expand All @@ -187,7 +191,7 @@ export function useSpriteLoader<Url extends string>(
// @ts-ignore
if ('encoding' in _spriteTexture) _spriteTexture.encoding = 3001 // sRGBEncoding
// @ts-ignore
else _spriteTexture.colorSpace = 'srgb'
else _spriteTexture.colorSpace = THREE.SRGBColorSpace
setSpriteTexture(_spriteTexture)
setSpriteObj({
spriteTexture: _spriteTexture,
Expand Down Expand Up @@ -244,7 +248,7 @@ export function useSpriteLoader<Url extends string>(

// for frame based JSON Hash sprite data
const parseFrames = (): any => {
const sprites: any = {}
const sprites: Record<string, any> = {}
const data = spriteDataRef.current
const delimiters = animationNames

Expand Down Expand Up @@ -277,6 +281,11 @@ export function useSpriteLoader<Url extends string>(
}
}
}

for (const frame in sprites) {
sprites[frame].frame = calculateScaleRatio(sprites[frame])
}

return sprites
} else if (delimiters && typeof data['frames'] === 'object') {
for (let i = 0; i < delimiters.length; i++) {
Expand Down Expand Up @@ -305,18 +314,52 @@ export function useSpriteLoader<Url extends string>(
}
}
}

for (const frame in sprites) {
sprites[frame].frame = calculateScaleRatio(sprites[frame])
}

return sprites
} else {
// we need to convert it into an array
const spritesArr: any[] = []
let spritesArr: any[] = []

for (const key in data.frames) {
spritesArr.push(data.frames[key])
}

spritesArr = calculateScaleRatio(spritesArr)

return spritesArr
}
}

// calculate scale ratio for the frames
const calculateScaleRatio = (frames: any[]) => {
// Find the largest frame
let largestFrame: any = null
for (const frame of frames) {
const { w, h } = frame.frame
const area = w * h
if (!largestFrame || area > largestFrame.area) {
largestFrame = { ...frame.frame, area }
}
}

// Set scaleRatio property on each frame
for (const frame of frames) {
const { w, h } = frame.frame
const area = w * h
if (area === largestFrame?.area) {
frame.scaleRatio = 1
} else {
frame.scaleRatio = Math.sqrt(area / largestFrame?.area)
}
}

return frames
}

React.useLayoutEffect(() => {
onLoad?.(spriteTexture, spriteData)
}, [spriteTexture, spriteData])
Expand Down
Loading