1+ import * as THREE from ' ./../../module/build/three.module.js'
2+
3+ class TextureAtlasCreator
4+ constructor : (options )->
5+ @textureX = options .textureX
6+ @textureMapping = options .textureMapping
7+ @size = 36
8+ @willSize = 27
9+ gen : (tick )->
10+ multi = {}
11+ for i of @textureMapping
12+ if i .includes " @"
13+ xd = @ decodeName i
14+ if multi[xd .pref ] is undefined
15+ multi[xd .pref ]= xd
16+ else
17+ multi[xd .pref ].x = Math .max multi[xd .pref ].x ,xd .x
18+ multi[xd .pref ].y = Math .max multi[xd .pref ].y ,xd .y
19+ canvasx = document .createElement ' canvas'
20+ ctx = canvasx .getContext " 2d"
21+ canvasx .width = @willSize * 16
22+ canvasx .height = @willSize * 16
23+ toxelX = 1
24+ toxelY = 1
25+ for i of @textureMapping
26+ if i .includes " @"
27+ xd = @ decodeName i
28+ if multi[xd .pref ].loaded is undefined
29+ multi[xd .pref ].loaded = true
30+ lol = @ getToxelForTick tick,multi[xd .pref ].x + 1 ,multi[xd .pref ].y + 1
31+ texmap = @textureMapping [" #{ xd .pref } @#{ lol .col } @#{ lol .row } " ]
32+ ctx .drawImage @textureX ,(texmap .x - 1 )* 16 ,(texmap .y - 1 )* 16 ,16 ,16 ,(toxelX- 1 )* 16 ,(toxelY- 1 )* 16 ,16 ,16
33+ toxelX++
34+ if toxelX> @willSize
35+ toxelX = 1
36+ toxelY++
37+ else
38+ ctx .drawImage @textureX ,(@textureMapping [i].x - 1 )* 16 ,(@textureMapping [i].y - 1 )* 16 ,16 ,16 ,(toxelX- 1 )* 16 ,(toxelY- 1 )* 16 ,16 ,16
39+ toxelX++
40+ if toxelX> @willSize
41+ toxelX = 1
42+ toxelY++
43+ return canvasx
44+ decodeName : (i )->
45+ m = null
46+ for j in [0 .. i .length - 1 ]
47+ if i[j] is " @"
48+ m = j
49+ break
50+ pref = i .substr 0 ,m
51+ sub = i .substr m,i .length
52+ m2 = null
53+ for j in [0 .. sub .length - 1 ]
54+ if sub[j] is " @"
55+ m2 = j
56+ x = parseInt sub .substr (1 ,m2- 1 )
57+ y = parseInt sub .substr (m2+ 1 ,sub .length )
58+ return {pref,x,y}
59+ getToxelForTick : (tick ,w ,h )->
60+ tick = tick% (w* h)+ 1
61+ # option1
62+ col = (tick- 1 )% w
63+ row = Math .ceil (tick/ w)- 1
64+ # option2
65+ col = Math .ceil (tick/ h)- 1
66+ row = (tick- 1 )% h;
67+ return {row,col}
68+ class AnimatedTextureAtlas
69+ constructor : (options )->
70+ _this = @
71+ @al = options .al
72+ @material = new THREE.MeshStandardMaterial ({
73+ side : 0
74+ map : null
75+ })
76+ @atlasCreator = new TextureAtlasCreator ({
77+ textureX : @al .get " blocksAtlasFull"
78+ textureMapping : @al .get " blocksMappingFull"
79+ })
80+ savedTextures = []
81+ for i in [0 .. 9 ]
82+ t = @atlasCreator .gen (i).toDataURL ()
83+ tekstura = new THREE.TextureLoader ().load t
84+ tekstura .magFilter = THREE .NearestFilter
85+ savedTextures .push tekstura
86+ tickq = 0
87+ setInterval (()->
88+ tickq++
89+ tekst = savedTextures[tickq% 9 ]
90+ _this .material .map = tekst
91+ _this .material .map .needsUpdate = true
92+ return
93+ ,100 )
94+
95+ export {AnimatedTextureAtlas}
0 commit comments