@@ -12,90 +12,67 @@ class TextureAtlasCreator {
1212 const multi = { }
1313 for ( const i in this . textureMapping ) {
1414 if ( i . includes ( '@' ) ) {
15- const xd = this . decodeName ( i )
16- if ( multi [ xd . pref ] === undefined ) {
17- multi [ xd . pref ] = xd
15+ const block = this . decodeName ( i )
16+ if ( multi [ block . pref ] === undefined ) {
17+ multi [ block . pref ] = block
1818 } else {
19- multi [ xd . pref ] . x = Math . max ( multi [ xd . pref ] . x , xd . x )
20- multi [ xd . pref ] . y = Math . max ( multi [ xd . pref ] . y , xd . y )
19+ multi [ block . pref ] . x = Math . max ( multi [ block . pref ] . x , block . x )
20+ multi [ block . pref ] . y = Math . max ( multi [ block . pref ] . y , block . y )
2121 }
2222 }
2323 }
24- const canvasx = document . createElement ( 'canvas' )
25- const ctx = canvasx . getContext ( '2d' )
26- canvasx . width = this . willSize * ( 16 + 32 )
27- canvasx . height = this . willSize * ( 16 + 32 )
28- // console.log(this.willSize * (16 + 32))
29- let toxelX = 1
30- let toxelY = 1
24+ const canvas = document . createElement ( 'canvas' )
25+ const ctx = canvas . getContext ( '2d' )
26+ const size = this . willSize * ( 16 + 32 )
27+ canvas . width = size
28+ canvas . height = size
29+ const toxelCoord = { x : 1 , y : 1 }
3130 for ( const i in this . textureMapping ) {
3231 if ( i . includes ( '@' ) ) {
33- const xd = this . decodeName ( i )
34- if ( multi [ xd . pref ] . loaded === undefined ) {
35- multi [ xd . pref ] . loaded = true
36- const lol = this . getToxelForTick (
32+ const block = this . decodeName ( i )
33+ if ( multi [ block . pref ] . loaded === undefined ) {
34+ multi [ block . pref ] . loaded = true
35+ const toxel = this . getToxelForTick (
3736 tick ,
38- multi [ xd . pref ] . x + 1 ,
39- multi [ xd . pref ] . y + 1
37+ multi [ block . pref ] . x + 1 ,
38+ multi [ block . pref ] . y + 1
4039 )
41- const texmap = this . textureMapping [
42- `${ xd . pref } @${ lol . col } @${ lol . row } `
43- ]
44-
45- ctx . drawImage (
46- this . textureX ,
47- ( texmap . x - 1 ) * 16 ,
48- ( texmap . y - 1 ) * 16 ,
49- 16 ,
50- 16 ,
51- 16 + ( toxelX - 1 ) * ( 16 + 32 ) ,
52- 16 + ( toxelY - 1 ) * ( 16 + 32 ) ,
53- 16 ,
54- 16
55- )
56- const b = ctx . getImageData ( 16 + ( toxelX - 1 ) * ( 16 + 32 ) , 16 + ( toxelY - 1 ) * ( 16 + 32 ) , 16 , 16 )
57- for ( let j = 16 ; j > 0 ; j -- ) {
58- ctx . putImageData ( b , 16 + ( toxelX - 1 ) * ( 16 + 32 ) , 16 + ( toxelY - 1 ) * ( 16 + 32 ) - j )
59- ctx . putImageData ( b , 16 + ( toxelX - 1 ) * ( 16 + 32 ) - j , 16 + ( toxelY - 1 ) * ( 16 + 32 ) )
60- ctx . putImageData ( b , 16 + ( toxelX - 1 ) * ( 16 + 32 ) + j , 16 + ( toxelY - 1 ) * ( 16 + 32 ) )
61- ctx . putImageData ( b , 16 + ( toxelX - 1 ) * ( 16 + 32 ) , 16 + ( toxelY - 1 ) * ( 16 + 32 ) + j )
62- }
63- ctx . putImageData ( b , 16 + ( toxelX - 1 ) * ( 16 + 32 ) , 16 + ( toxelY - 1 ) * ( 16 + 32 ) )
64- toxelX ++
65- if ( toxelX > this . willSize ) {
66- toxelX = 1
67- toxelY ++
68- }
40+ const texmap = this . textureMapping [ `${ block . pref } @${ toxel . col } @${ toxel . row } ` ]
41+ this . addToxel ( ctx , texmap , toxelCoord )
6942 }
7043 } else {
71- ctx . drawImage (
72- this . textureX ,
73- ( this . textureMapping [ i ] . x - 1 ) * 16 ,
74- ( this . textureMapping [ i ] . y - 1 ) * 16 ,
75- 16 ,
76- 16 ,
77- 16 + ( toxelX - 1 ) * ( 16 + 32 ) ,
78- 16 + ( toxelY - 1 ) * ( 16 + 32 ) ,
79- 16 ,
80- 16
81- )
82- const b = ctx . getImageData ( 16 + ( toxelX - 1 ) * ( 16 + 32 ) , 16 + ( toxelY - 1 ) * ( 16 + 32 ) , 16 , 16 )
83- for ( let j = 16 ; j > 0 ; j -- ) {
84- ctx . putImageData ( b , 16 + ( toxelX - 1 ) * ( 16 + 32 ) , 16 + ( toxelY - 1 ) * ( 16 + 32 ) - j )
85- ctx . putImageData ( b , 16 + ( toxelX - 1 ) * ( 16 + 32 ) - j , 16 + ( toxelY - 1 ) * ( 16 + 32 ) )
86- ctx . putImageData ( b , 16 + ( toxelX - 1 ) * ( 16 + 32 ) + j , 16 + ( toxelY - 1 ) * ( 16 + 32 ) )
87- ctx . putImageData ( b , 16 + ( toxelX - 1 ) * ( 16 + 32 ) , 16 + ( toxelY - 1 ) * ( 16 + 32 ) + j )
88- }
89- ctx . putImageData ( b , 16 + ( toxelX - 1 ) * ( 16 + 32 ) , 16 + ( toxelY - 1 ) * ( 16 + 32 ) )
90- toxelX ++
91- if ( toxelX > this . willSize ) {
92- toxelX = 1
93- toxelY ++
94- }
44+ this . addToxel ( ctx , this . textureMapping [ i ] , toxelCoord )
9545 }
9646 }
97- // console.log(canvasx.toDataURL())
98- return canvasx
47+ // console.log(canvas.toDataURL())
48+ return canvas
49+ }
50+
51+ addToxel ( ctx , coord , toxelCoord ) {
52+ ctx . drawImage (
53+ this . textureX ,
54+ ( coord . x - 1 ) * 16 ,
55+ ( coord . y - 1 ) * 16 ,
56+ 16 ,
57+ 16 ,
58+ 16 + ( toxelCoord . x - 1 ) * ( 16 + 32 ) ,
59+ 16 + ( toxelCoord . y - 1 ) * ( 16 + 32 ) ,
60+ 16 ,
61+ 16
62+ )
63+ const b = ctx . getImageData ( 16 + ( toxelCoord . x - 1 ) * ( 16 + 32 ) , 16 + ( toxelCoord . y - 1 ) * ( 16 + 32 ) , 16 , 16 )
64+ for ( let j = 16 ; j > 0 ; j -- ) {
65+ ctx . putImageData ( b , 16 + ( toxelCoord . x - 1 ) * ( 16 + 32 ) , 16 + ( toxelCoord . y - 1 ) * ( 16 + 32 ) - j )
66+ ctx . putImageData ( b , 16 + ( toxelCoord . x - 1 ) * ( 16 + 32 ) - j , 16 + ( toxelCoord . y - 1 ) * ( 16 + 32 ) )
67+ ctx . putImageData ( b , 16 + ( toxelCoord . x - 1 ) * ( 16 + 32 ) + j , 16 + ( toxelCoord . y - 1 ) * ( 16 + 32 ) )
68+ ctx . putImageData ( b , 16 + ( toxelCoord . x - 1 ) * ( 16 + 32 ) , 16 + ( toxelCoord . y - 1 ) * ( 16 + 32 ) + j )
69+ }
70+ ctx . putImageData ( b , 16 + ( toxelCoord . x - 1 ) * ( 16 + 32 ) , 16 + ( toxelCoord . y - 1 ) * ( 16 + 32 ) )
71+ toxelCoord . x ++
72+ if ( toxelCoord . x > this . willSize ) {
73+ toxelCoord . x = 1
74+ toxelCoord . y ++
75+ }
9976 }
10077
10178 decodeName ( i ) {
@@ -122,11 +99,11 @@ class TextureAtlasCreator {
12299 getToxelForTick ( tick , w , h ) {
123100 tick = ( tick % ( w * h ) ) + 1
124101 // option1
125- let col = ( tick - 1 ) % w
126- let row = Math . ceil ( tick / w ) - 1
102+ // let col = (tick - 1) % w
103+ // let row = Math.ceil(tick / w) - 1
127104 // option2
128- col = Math . ceil ( tick / h ) - 1
129- row = ( tick - 1 ) % h
105+ const col = Math . ceil ( tick / h ) - 1
106+ const row = ( tick - 1 ) % h
130107 return { row, col }
131108 }
132109}
@@ -147,19 +124,18 @@ class AnimatedTextureAtlas {
147124 } )
148125 const savedTextures = [ ]
149126 for ( let i = 0 ; i < 10 ; i ++ ) {
150- const t = this . atlasCreator . gen ( i ) . toDataURL ( )
127+ const c = this . atlasCreator . gen ( i )
128+ const t = c . toDataURL ( )
151129 const tekstura = new TextureLoader ( ) . load ( t )
152130 tekstura . magFilter = NearestFilter
153131 tekstura . minFilter = NearestMipmapLinearFilter
154132 savedTextures . push ( tekstura )
155133 }
156134 let tickq = 0
157- setInterval ( ( ) => {
158- tickq ++
159- const tekst = savedTextures [ tickq % 9 ]
160- this . material . map = tekst
161- this . material . map . needsUpdate = true
162- } , 100 )
135+ tickq ++
136+ const tekst = savedTextures [ tickq % 9 ]
137+ this . material . map = tekst
138+ this . material . map . needsUpdate = true
163139 }
164140}
165141
0 commit comments