@damienmortini/three 0.1.183 → 0.1.185

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,96 +1,96 @@
1
- import { Object3D, Mesh, Color, PlaneGeometry, Texture, DoubleSide, LinearFilter } from '../../../three/src/Three.js'
2
-
3
- import THREEShaderMaterial from './THREEShaderMaterial.js'
4
-
5
- const CACHED_IMAGES = new Map()
6
-
7
- export default class Sprite extends Object3D {
8
- constructor(image, { data, frame, scale = 1 } = {}) {
9
- super()
10
-
11
- this._data = data
12
- this._image = image
13
- this._scale = scale
14
-
15
- // Optimise images decoding
16
- let canvas = CACHED_IMAGES.get(this.image)
17
-
18
- if (!canvas) {
19
- canvas = document.createElement('canvas')
20
- canvas.width = this.image.width
21
- canvas.height = this.image.height
22
- const context = canvas.getContext('2d')
23
- context.drawImage(this.image, 0, 0)
24
- CACHED_IMAGES.set(this.image, canvas)
25
- }
26
-
27
- this.mesh = new Mesh(new PlaneGeometry(1, 1), new THREEShaderMaterial({
28
- type: 'basic',
29
- transparent: true,
30
- side: DoubleSide,
31
- uniforms: {
32
- diffuse: new Color(0xffffff),
33
- map: new Texture(canvas),
34
- },
35
- }))
36
- this.mesh.scale.x = canvas.width * this._scale
37
- this.mesh.scale.y = canvas.height * this._scale
38
- this.mesh.material.map.minFilter = LinearFilter
39
- this.mesh.material.map.generateMipmaps = false
40
- this.mesh.material.map.needsUpdate = true
41
-
42
- this.add(this.mesh)
43
-
44
- if (frame) {
45
- this.frame = frame
46
- }
47
- }
48
-
49
- get image() {
50
- return this._image
51
- }
52
-
53
- get data() {
54
- return this._data
55
- }
56
-
57
- set material(value) {
58
- this.mesh.material = value
59
- this.frame = this.frame
60
- }
61
-
62
- get material() {
63
- return this.mesh.material
64
- }
65
-
66
- set frame(value) {
67
- if (!this.data) {
68
- return
69
- }
70
-
71
- this._frame = value
72
-
73
- const offsetRepeat = this.mesh.material.offsetRepeat
74
- const frameData = this.data.frames[this._frame]
75
-
76
- offsetRepeat.z = (frameData.rotated ? frameData.frame.h : frameData.frame.w) / this.image.width
77
- offsetRepeat.w = (frameData.rotated ? frameData.frame.w : frameData.frame.h) / this.image.height
78
-
79
- offsetRepeat.x = frameData.frame.x / this.image.width
80
- offsetRepeat.y = 1. - frameData.frame.y / this.image.height - offsetRepeat.w
81
-
82
- const scale = 1 / parseFloat(this.data.meta.scale)
83
-
84
- this.mesh.scale.x = (frameData.rotated ? frameData.frame.h : frameData.frame.w) * scale * this._scale
85
- this.mesh.scale.y = (frameData.rotated ? frameData.frame.w : frameData.frame.h) * scale * this._scale
86
-
87
- this.mesh.position.x = (-(frameData.sourceSize.w - frameData.frame.w) * .5 + frameData.spriteSourceSize.x + frameData.sourceSize.w * (.5 - frameData.pivot.x)) * scale * this._scale
88
- this.mesh.position.y = ((frameData.sourceSize.h - frameData.frame.h) * .5 - frameData.spriteSourceSize.y - frameData.sourceSize.h * (.5 - frameData.pivot.y)) * scale * this._scale
89
-
90
- this.mesh.rotation.z = frameData.rotated ? Math.PI * .5 : 0
91
- }
92
-
93
- get frame() {
94
- return this._frame
95
- }
96
- }
1
+ import { Object3D, Mesh, Color, PlaneGeometry, Texture, DoubleSide, LinearFilter } from '../../../three/src/Three.js'
2
+
3
+ import THREEShaderMaterial from './THREEShaderMaterial.js'
4
+
5
+ const CACHED_IMAGES = new Map()
6
+
7
+ export default class Sprite extends Object3D {
8
+ constructor(image, { data, frame, scale = 1 } = {}) {
9
+ super()
10
+
11
+ this._data = data
12
+ this._image = image
13
+ this._scale = scale
14
+
15
+ // Optimise images decoding
16
+ let canvas = CACHED_IMAGES.get(this.image)
17
+
18
+ if (!canvas) {
19
+ canvas = document.createElement('canvas')
20
+ canvas.width = this.image.width
21
+ canvas.height = this.image.height
22
+ const context = canvas.getContext('2d')
23
+ context.drawImage(this.image, 0, 0)
24
+ CACHED_IMAGES.set(this.image, canvas)
25
+ }
26
+
27
+ this.mesh = new Mesh(new PlaneGeometry(1, 1), new THREEShaderMaterial({
28
+ type: 'basic',
29
+ transparent: true,
30
+ side: DoubleSide,
31
+ uniforms: {
32
+ diffuse: new Color(0xffffff),
33
+ map: new Texture(canvas),
34
+ },
35
+ }))
36
+ this.mesh.scale.x = canvas.width * this._scale
37
+ this.mesh.scale.y = canvas.height * this._scale
38
+ this.mesh.material.map.minFilter = LinearFilter
39
+ this.mesh.material.map.generateMipmaps = false
40
+ this.mesh.material.map.needsUpdate = true
41
+
42
+ this.add(this.mesh)
43
+
44
+ if (frame) {
45
+ this.frame = frame
46
+ }
47
+ }
48
+
49
+ get image() {
50
+ return this._image
51
+ }
52
+
53
+ get data() {
54
+ return this._data
55
+ }
56
+
57
+ set material(value) {
58
+ this.mesh.material = value
59
+ this.frame = this.frame
60
+ }
61
+
62
+ get material() {
63
+ return this.mesh.material
64
+ }
65
+
66
+ set frame(value) {
67
+ if (!this.data) {
68
+ return
69
+ }
70
+
71
+ this._frame = value
72
+
73
+ const offsetRepeat = this.mesh.material.offsetRepeat
74
+ const frameData = this.data.frames[this._frame]
75
+
76
+ offsetRepeat.z = (frameData.rotated ? frameData.frame.h : frameData.frame.w) / this.image.width
77
+ offsetRepeat.w = (frameData.rotated ? frameData.frame.w : frameData.frame.h) / this.image.height
78
+
79
+ offsetRepeat.x = frameData.frame.x / this.image.width
80
+ offsetRepeat.y = 1. - frameData.frame.y / this.image.height - offsetRepeat.w
81
+
82
+ const scale = 1 / parseFloat(this.data.meta.scale)
83
+
84
+ this.mesh.scale.x = (frameData.rotated ? frameData.frame.h : frameData.frame.w) * scale * this._scale
85
+ this.mesh.scale.y = (frameData.rotated ? frameData.frame.w : frameData.frame.h) * scale * this._scale
86
+
87
+ this.mesh.position.x = (-(frameData.sourceSize.w - frameData.frame.w) * .5 + frameData.spriteSourceSize.x + frameData.sourceSize.w * (.5 - frameData.pivot.x)) * scale * this._scale
88
+ this.mesh.position.y = ((frameData.sourceSize.h - frameData.frame.h) * .5 - frameData.spriteSourceSize.y - frameData.sourceSize.h * (.5 - frameData.pivot.y)) * scale * this._scale
89
+
90
+ this.mesh.rotation.z = frameData.rotated ? Math.PI * .5 : 0
91
+ }
92
+
93
+ get frame() {
94
+ return this._frame
95
+ }
96
+ }
@@ -1,103 +1,103 @@
1
-
2
- import THREESprite from './THREESprite.js'
3
-
4
- import Signal from '@damienmortini/core/util/Signal'
5
- import Ticker from '@damienmortini/core/util/Ticker'
6
-
7
- const SPRITESHEETS = new Map()
8
-
9
- export default class THREESpriteAnimation extends THREESprite {
10
- constructor(image, data, animation, {
11
- speed = 1,
12
- fps = 25,
13
- loop = true,
14
- reverse = false,
15
- scale = 1,
16
- autoplay = true,
17
- } = {}) {
18
- let animations = SPRITESHEETS.get(data)
19
- if (!animations) {
20
- animations = new Map()
21
- for (const key in data.frames) {
22
- const match = /(.*?)([0-9]+)[$\.]/.exec(key)
23
- const animationName = match[1]
24
- let frames = animations.get(animationName)
25
- if (!frames) {
26
- frames = []
27
- animations.set(animationName, frames)
28
- }
29
- const position = parseInt(match[2])
30
- frames[position - 1] = key
31
- }
32
- SPRITESHEETS.set(data, animations)
33
- }
34
-
35
- super(image, {
36
- data,
37
- frame: animations.get(animation)[0],
38
- scale,
39
- })
40
-
41
- this.onAnimationEnd = new Signal()
42
-
43
- this._progress = 0
44
- this._animations = animations
45
-
46
- this.loop = loop
47
- this.reverse = reverse
48
- this.speed = speed
49
- this.fps = fps
50
- this.animation = animation
51
-
52
- if (autoplay) {
53
- this.play()
54
- }
55
- }
56
-
57
- set animation(value) {
58
- if (this._animation === value) {
59
- return
60
- }
61
- this._animation = value
62
-
63
- this.update()
64
- }
65
-
66
- get animation() {
67
- return this._animation
68
- }
69
-
70
- play() {
71
- Ticker.add(this._updateBound = this._updateBound || this.update.bind(this))
72
- }
73
-
74
- stop() {
75
- Ticker.delete(this._updateBound)
76
- }
77
-
78
- set progress(value) {
79
- if (this._progress === value) {
80
- return
81
- }
82
- const previousProgress = this._progress
83
- this._progress = value
84
- if (this.loop) {
85
- this._progress = ((this._progress % 1) + 1) % 1
86
- } else {
87
- this._progress = Math.min(Math.max(this._progress, 0), 1)
88
- if (previousProgress !== this._progress && (this._progress === 1 && !this.reverse || this._progress === 0 && this.reverse)) {
89
- this.onAnimationEnd.dispatch()
90
- }
91
- }
92
- }
93
-
94
- get progress() {
95
- return this._progress
96
- }
97
-
98
- update() {
99
- const frames = this._animations.get(this.animation)
100
- this.progress += (this.speed * (this.fps / 60) * Ticker.timeScale / frames.length) * (this.reverse ? -1 : 1)
101
- this.frame = frames[Math.round(this._progress * (frames.length - 1))]
102
- }
103
- }
1
+
2
+ import THREESprite from './THREESprite.js'
3
+
4
+ import Signal from '@damienmortini/core/util/Signal'
5
+ import Ticker from '@damienmortini/core/util/Ticker'
6
+
7
+ const SPRITESHEETS = new Map()
8
+
9
+ export default class THREESpriteAnimation extends THREESprite {
10
+ constructor(image, data, animation, {
11
+ speed = 1,
12
+ fps = 25,
13
+ loop = true,
14
+ reverse = false,
15
+ scale = 1,
16
+ autoplay = true,
17
+ } = {}) {
18
+ let animations = SPRITESHEETS.get(data)
19
+ if (!animations) {
20
+ animations = new Map()
21
+ for (const key in data.frames) {
22
+ const match = /(.*?)([0-9]+)[$\.]/.exec(key)
23
+ const animationName = match[1]
24
+ let frames = animations.get(animationName)
25
+ if (!frames) {
26
+ frames = []
27
+ animations.set(animationName, frames)
28
+ }
29
+ const position = parseInt(match[2])
30
+ frames[position - 1] = key
31
+ }
32
+ SPRITESHEETS.set(data, animations)
33
+ }
34
+
35
+ super(image, {
36
+ data,
37
+ frame: animations.get(animation)[0],
38
+ scale,
39
+ })
40
+
41
+ this.onAnimationEnd = new Signal()
42
+
43
+ this._progress = 0
44
+ this._animations = animations
45
+
46
+ this.loop = loop
47
+ this.reverse = reverse
48
+ this.speed = speed
49
+ this.fps = fps
50
+ this.animation = animation
51
+
52
+ if (autoplay) {
53
+ this.play()
54
+ }
55
+ }
56
+
57
+ set animation(value) {
58
+ if (this._animation === value) {
59
+ return
60
+ }
61
+ this._animation = value
62
+
63
+ this.update()
64
+ }
65
+
66
+ get animation() {
67
+ return this._animation
68
+ }
69
+
70
+ play() {
71
+ Ticker.add(this._updateBound = this._updateBound || this.update.bind(this))
72
+ }
73
+
74
+ stop() {
75
+ Ticker.delete(this._updateBound)
76
+ }
77
+
78
+ set progress(value) {
79
+ if (this._progress === value) {
80
+ return
81
+ }
82
+ const previousProgress = this._progress
83
+ this._progress = value
84
+ if (this.loop) {
85
+ this._progress = ((this._progress % 1) + 1) % 1
86
+ } else {
87
+ this._progress = Math.min(Math.max(this._progress, 0), 1)
88
+ if (previousProgress !== this._progress && (this._progress === 1 && !this.reverse || this._progress === 0 && this.reverse)) {
89
+ this.onAnimationEnd.dispatch()
90
+ }
91
+ }
92
+ }
93
+
94
+ get progress() {
95
+ return this._progress
96
+ }
97
+
98
+ update() {
99
+ const frames = this._animations.get(this.animation)
100
+ this.progress += (this.speed * (this.fps / 60) * Ticker.timeScale / frames.length) * (this.reverse ? -1 : 1)
101
+ this.frame = frames[Math.round(this._progress * (frames.length - 1))]
102
+ }
103
+ }