1- import { MeshStandardMaterial , TextureLoader , NearestFilter , NearestMipmapLinearFilter } from 'three'
1+ import {
2+ MeshStandardMaterial ,
3+ TextureLoader ,
4+ NearestFilter ,
5+ NearestMipmapLinearFilter ,
6+ CanvasTexture
7+ } from 'three'
28
39class TextureAtlasCreator {
410 constructor ( options ) {
@@ -12,67 +18,71 @@ class TextureAtlasCreator {
1218 const multi = { }
1319 for ( const i in this . textureMapping ) {
1420 if ( i . includes ( '@' ) ) {
15- const block = this . decodeName ( i )
16- if ( multi [ block . pref ] === undefined ) {
17- multi [ block . pref ] = block
21+ const xd = this . decodeName ( i )
22+ if ( multi [ xd . pref ] === undefined ) {
23+ multi [ xd . pref ] = xd
1824 } else {
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 )
25+ multi [ xd . pref ] . x = Math . max ( multi [ xd . pref ] . x , xd . x )
26+ multi [ xd . pref ] . y = Math . max ( multi [ xd . pref ] . y , xd . y )
2127 }
2228 }
2329 }
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 }
30+ const canvasx = document . createElement ( 'canvas' )
31+ const ctx = canvasx . getContext ( '2d' )
32+ canvasx . width = this . willSize * 16
33+ canvasx . height = this . willSize * 16
34+ let toxelX = 1
35+ let toxelY = 1
3036 for ( const i in this . textureMapping ) {
3137 if ( i . includes ( '@' ) ) {
32- const block = this . decodeName ( i )
33- if ( multi [ block . pref ] . loaded === undefined ) {
34- multi [ block . pref ] . loaded = true
35- const toxel = this . getToxelForTick (
38+ const xd = this . decodeName ( i )
39+ if ( multi [ xd . pref ] . loaded === undefined ) {
40+ multi [ xd . pref ] . loaded = true
41+ const lol = this . getToxelForTick (
3642 tick ,
37- multi [ block . pref ] . x + 1 ,
38- multi [ block . pref ] . y + 1
43+ multi [ xd . pref ] . x + 1 ,
44+ multi [ xd . pref ] . y + 1
3945 )
40- const texmap = this . textureMapping [ `${ block . pref } @${ toxel . col } @${ toxel . row } ` ]
41- this . addToxel ( ctx , texmap , toxelCoord )
46+ const texmap = this . textureMapping [
47+ `${ xd . pref } @${ lol . col } @${ lol . row } `
48+ ]
49+ ctx . drawImage (
50+ this . textureX ,
51+ ( texmap . x - 1 ) * 16 ,
52+ ( texmap . y - 1 ) * 16 ,
53+ 16 ,
54+ 16 ,
55+ ( toxelX - 1 ) * 16 ,
56+ ( toxelY - 1 ) * 16 ,
57+ 16 ,
58+ 16
59+ )
60+ toxelX ++
61+ if ( toxelX > this . willSize ) {
62+ toxelX = 1
63+ toxelY ++
64+ }
4265 }
4366 } else {
44- this . addToxel ( ctx , this . textureMapping [ i ] , toxelCoord )
67+ ctx . drawImage (
68+ this . textureX ,
69+ ( this . textureMapping [ i ] . x - 1 ) * 16 ,
70+ ( this . textureMapping [ i ] . y - 1 ) * 16 ,
71+ 16 ,
72+ 16 ,
73+ ( toxelX - 1 ) * 16 ,
74+ ( toxelY - 1 ) * 16 ,
75+ 16 ,
76+ 16
77+ )
78+ toxelX ++
79+ if ( toxelX > this . willSize ) {
80+ toxelX = 1
81+ toxelY ++
82+ }
4583 }
4684 }
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- }
85+ return canvasx
7686 }
7787
7888 decodeName ( i ) {
@@ -99,43 +109,37 @@ class TextureAtlasCreator {
99109 getToxelForTick ( tick , w , h ) {
100110 tick = ( tick % ( w * h ) ) + 1
101111 // option1
102- // let col = (tick - 1) % w
103- // let row = Math.ceil(tick / w) - 1
112+ let col = ( tick - 1 ) % w
113+ let row = Math . ceil ( tick / w ) - 1
104114 // option2
105- const col = Math . ceil ( tick / h ) - 1
106- const row = ( tick - 1 ) % h
115+ col = Math . ceil ( tick / h ) - 1
116+ row = ( tick - 1 ) % h
107117 return { row, col }
108118 }
109119}
110120
111121class AnimatedTextureAtlas {
112122 constructor ( game ) {
113123 this . game = game
124+
125+ this . atlasCreator = new TextureAtlasCreator ( {
126+ textureX : this . game . al . get ( 'blocksAtlasFull' ) ,
127+ textureMapping : this . game . al . get ( 'blocksMappingFull' )
128+ } )
129+ const canvas = this . atlasCreator . gen ( 0 )
130+ const texture = new CanvasTexture ( canvas )
131+ texture . magFilter = NearestFilter
132+ texture . minFilter = NearestMipmapLinearFilter
133+
114134 this . material = new MeshStandardMaterial ( {
115135 side : 0 ,
116- map : null ,
136+ map : texture ,
117137 vertexColors : true ,
118138 transparent : true ,
119139 alphaTest : 0.1
120140 } )
121- this . atlasCreator = new TextureAtlasCreator ( {
122- textureX : this . game . al . get ( 'blocksAtlasFull' ) ,
123- textureMapping : this . game . al . get ( 'blocksMappingFull' )
124- } )
125- const savedTextures = [ ]
126- for ( let i = 0 ; i < 10 ; i ++ ) {
127- const c = this . atlasCreator . gen ( i )
128- const t = c . toDataURL ( )
129- const tekstura = new TextureLoader ( ) . load ( t )
130- tekstura . magFilter = NearestFilter
131- tekstura . minFilter = NearestMipmapLinearFilter
132- savedTextures . push ( tekstura )
133- }
134- let tickq = 0
135- tickq ++
136- const tekst = savedTextures [ tickq % 9 ]
137- this . material . map = tekst
138- this . material . map . needsUpdate = true
141+
142+ console . log ( this . material )
139143 }
140144}
141145
0 commit comments