Skip to content

Commit

Permalink
feat(AnimatedSprite): add asSprite prop
Browse files Browse the repository at this point in the history
  • Loading branch information
andretchen0 committed Apr 7, 2024
1 parent 9dd2673 commit 7a14b55
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 17 deletions.
7 changes: 6 additions & 1 deletion playground/src/pages/abstractions/AnimatedSprite.vue
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const gl = {
}
const { fps, animation, definitions, flipX, loop, paused,
reversed, resetOnEnd, centerX, centerY, scale, rotationX, rotationY, rotationZ, position } = useControls({
reversed, resetOnEnd, asSprite, centerX, centerY, scale, rotationX, rotationY, rotationZ, position } = useControls({
fps: { value: 10, min: 0, max: 120, step: 1 },
animation: { label: 'Animation', value: 'idle', options: ['idle', 'walk', 'blink'] },
definitions: { label: 'Definitions', value: '{}', options: ['{}', '{"idle":"0(10),1-5"}'] },
Expand All @@ -30,6 +30,7 @@ const { fps, animation, definitions, flipX, loop, paused,
paused: false,
reversed: false,
resetOnEnd: false,
asSprite: false,
centerX: { value: 0.5, min: 0, max: 1, step: 0.01 },
centerY: { value: 0.5, min: 0, max: 1, step: 0.01 },
scale: { value: 1, min: 0.1, max: 4, step: 0.01 },
Expand Down Expand Up @@ -141,6 +142,7 @@ const centerDemoImgData = (() => {
:fps="fps.value"
:loop="loop.value"
:reset-on-end="resetOnEnd.value"
:as-sprite="asSprite.value"
:center="[centerX.value, centerY.value]"
:reversed="reversed.value"
:scale="scale.value"
Expand Down Expand Up @@ -175,6 +177,7 @@ const centerDemoImgData = (() => {
:fps="fps.value"
:loop="loop.value"
:reset-on-end="resetOnEnd.value"
:as-sprite="asSprite.value"
:center="[centerX.value, centerY.value]"
:reversed="reversed.value"
:scale="scale.value"
Expand All @@ -194,6 +197,7 @@ const centerDemoImgData = (() => {
:fps="fps.value"
:loop="loop.value"
:reset-on-end="resetOnEnd.value"
:as-sprite="asSprite.value"
:center="[centerX.value, centerY.value]"
:reversed="reversed.value"
:scale="scale.value"
Expand All @@ -210,6 +214,7 @@ const centerDemoImgData = (() => {
:atlas="`${ASSETS_URL}cientosAtlas.json`"
:definitions="defsParsed"
:flip-x="flipX.value"
:as-sprite="asSprite.value"
:center="[centerX.value, centerY.value]"
:animation="animation.value"
:fps="fps.value"
Expand Down
50 changes: 34 additions & 16 deletions src/core/abstractions/AnimatedSprite/component.vue
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ export interface AnimatedSpriteProps {
flipX?: boolean
/** For a non-looping animation, when the animation ends, whether to display the zeroth frame. */
resetOnEnd?: boolean
/** Whether to display the object as a THREE.Sprite. [See THREE.Sprite](https://threejs.org/docs/?q=sprite#api/en/objects/Sprite) */
asSprite?: boolean
/** Anchor point of the object. A value of [0.5, 0.5] corresponds to the center. [0, 0] is left, bottom. */
center?: TresVector2
/** Alpha test value for the material. [See THREE.Material.alphaTest](https://threejs.org/docs/#api/en/materials/Material.alphaTest) */
Expand All @@ -59,6 +61,7 @@ const props = withDefaults(defineProps<AnimatedSpriteProps>(), {
reversed: false,
flipX: false,
resetOnEnd: false,
asSprite: true,
center: () => [0.5, 0.5],
alphaTest: 0.0,
depthTest: true,
Expand Down Expand Up @@ -201,22 +204,37 @@ watch(() => [props.definitions], () => {
<template>
<TresGroup v-bind="$attrs">
<Suspense :fallback="null">
<TresMesh
:scale="[scaleX, scaleY, 1]"
:position="[positionX, positionY, 0]"
@click="(intr: Intersection) => emit('click', intr)"
>
<TresPlaneGeometry :args="[1, 1]" />
<TresMeshBasicMaterial
:toneMapped="false"
:side="DoubleSide"
:map="texture"
:transparent="true"
:alphaTest="props.alphaTest"
:depthWrite="props.depthWrite"
:depthTest="props.depthTest"
/>
</TresMesh>
<template v-if="props.asSprite">
<TresSprite
:scale="[scaleX, scaleY, 1]"
:position="[positionX, positionY, 0]"
>
<TresSpriteMaterial
:toneMapped="false"
:map="texture"
:transparent="true"
:alphaTest="props.alphaTest"
/>
</TresSprite>
</template>
<template v-else>
<TresMesh
:scale="[scaleX, scaleY, 1]"
:position="[positionX, positionY, 0]"
@click="(intr: Intersection) => emit('click', intr)"
>
<TresPlaneGeometry :args="[1, 1]" />
<TresMeshBasicMaterial
:toneMapped="false"
:side="DoubleSide"
:map="texture"
:transparent="true"
:alphaTest="props.alphaTest"
:depthWrite="props.depthWrite"
:depthTest="props.depthTest"
/>
</TresMesh>
</template>
</Suspense>
<slot />
</TresGroup>
Expand Down

0 comments on commit 7a14b55

Please sign in to comment.