forked from michaljaz/webmc
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAnimatedTextureAtlas.js
More file actions
147 lines (141 loc) · 3.69 KB
/
AnimatedTextureAtlas.js
File metadata and controls
147 lines (141 loc) · 3.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import { MeshStandardMaterial, TextureLoader, NearestFilter } from 'three'
class TextureAtlasCreator {
constructor (options) {
this.textureX = options.textureX
this.textureMapping = options.textureMapping
this.size = 36
this.willSize = 27
}
gen (tick) {
const multi = {}
for (const i in this.textureMapping) {
if (i.includes('@')) {
const xd = this.decodeName(i)
if (multi[xd.pref] === undefined) {
multi[xd.pref] = xd
} else {
multi[xd.pref].x = Math.max(multi[xd.pref].x, xd.x)
multi[xd.pref].y = Math.max(multi[xd.pref].y, xd.y)
}
}
}
const canvasx = document.createElement('canvas')
const ctx = canvasx.getContext('2d')
canvasx.width = this.willSize * 16
canvasx.height = this.willSize * 16
let toxelX = 1
let toxelY = 1
for (const i in this.textureMapping) {
if (i.includes('@')) {
const xd = this.decodeName(i)
if (multi[xd.pref].loaded === undefined) {
multi[xd.pref].loaded = true
const lol = this.getToxelForTick(
tick,
multi[xd.pref].x + 1,
multi[xd.pref].y + 1
)
const texmap = this.textureMapping[
`${xd.pref}@${lol.col}@${lol.row}`
]
ctx.drawImage(
this.textureX,
(texmap.x - 1) * 16,
(texmap.y - 1) * 16,
16,
16,
(toxelX - 1) * 16,
(toxelY - 1) * 16,
16,
16
)
toxelX++
if (toxelX > this.willSize) {
toxelX = 1
toxelY++
}
}
} else {
ctx.drawImage(
this.textureX,
(this.textureMapping[i].x - 1) * 16,
(this.textureMapping[i].y - 1) * 16,
16,
16,
(toxelX - 1) * 16,
(toxelY - 1) * 16,
16,
16
)
toxelX++
if (toxelX > this.willSize) {
toxelX = 1
toxelY++
}
}
}
return canvasx
}
decodeName (i) {
let m = null
for (let j = 0; j < i.length; j++) {
if (i[j] === '@') {
m = j
break
}
}
const pref = i.substr(0, m)
const sub = i.substr(m, i.length)
let m2 = null
for (let j = 0; j < sub.length; j++) {
if (sub[j] === '@') {
m2 = j
}
}
const x = parseInt(sub.substr(1, m2 - 1))
const y = parseInt(sub.substr(m2 + 1, sub.length))
return { pref, x, y }
}
getToxelForTick (tick, w, h) {
tick = (tick % (w * h)) + 1
// option1
let col = (tick - 1) % w
let row = Math.ceil(tick / w) - 1
// option2
col = Math.ceil(tick / h) - 1
row = (tick - 1) % h
return { row, col }
}
}
class AnimatedTextureAtlas {
constructor (game) {
this.game = game
this.material = new MeshStandardMaterial({
side: 0,
map: null,
vertexColors: true,
transparent: true,
alphaTest: 0.1
})
this.atlasCreator = new TextureAtlasCreator({
textureX: this.game.al.get('blocksAtlasFull'),
textureMapping: this.game.al.get('blocksMappingFull')
})
const savedTextures = []
for (let i = 0; i < 10; i++) {
const t = this.atlasCreator.gen(i).toDataURL()
const tekstura = new TextureLoader().load(t)
tekstura.magFilter = NearestFilter
tekstura.minFilter = NearestFilter
savedTextures.push(tekstura)
}
let tickq = 0
setInterval(() => {
tickq++
const tekst = savedTextures[tickq % 9]
this.material.map = tekst
this.material.map.needsUpdate = true
}, 100)
}
}
export { AnimatedTextureAtlas, TextureAtlasCreator }