1
1
import { mat4 } from 'gl-matrix'
2
- import type { Resources , Voxel } from '../src/index.js'
3
- import { BlockDefinition , BlockModel , Identifier , ItemRenderer , ItemStack , NormalNoise , Structure , StructureRenderer , TextureAtlas , upperPowerOfTwo , VoxelRenderer , XoroshiroRandom } from '../src/index.js'
2
+ import type { ItemRendererResources , ItemRenderingContext , NbtTag , Resources , Voxel } from '../src/index.js'
3
+ import { BlockDefinition , BlockModel , Identifier , Item , ItemRenderer , ItemStack , NormalNoise , Structure , StructureRenderer , TextureAtlas , VoxelRenderer , XoroshiroRandom , jsonToNbt , upperPowerOfTwo } from '../src/index.js'
4
+ import { } from '../src/nbt/Util.js'
5
+ import { ItemModel } from '../src/render/ItemModel.js'
4
6
5
7
6
8
class InteractiveCanvas {
@@ -66,14 +68,16 @@ Promise.all([
66
68
fetch ( `${ MCMETA } registries/item/data.min.json` ) . then ( r => r . json ( ) ) ,
67
69
fetch ( `${ MCMETA } summary/assets/block_definition/data.min.json` ) . then ( r => r . json ( ) ) ,
68
70
fetch ( `${ MCMETA } summary/assets/model/data.min.json` ) . then ( r => r . json ( ) ) ,
71
+ fetch ( `${ MCMETA } summary/assets/item_definition/data.min.json` ) . then ( r => r . json ( ) ) ,
72
+ fetch ( `${ MCMETA } summary/item_components/data.min.json` ) . then ( r => r . json ( ) ) ,
69
73
fetch ( `${ MCMETA } atlas/all/data.min.json` ) . then ( r => r . json ( ) ) ,
70
74
new Promise < HTMLImageElement > ( res => {
71
75
const image = new Image ( )
72
76
image . onload = ( ) => res ( image )
73
77
image . crossOrigin = 'Anonymous'
74
78
image . src = `${ MCMETA } atlas/all/atlas.png`
75
79
} ) ,
76
- ] ) . then ( ( [ items , blockstates , models , uvMap , atlas ] ) => {
80
+ ] ) . then ( ( [ items , blockstates , models , item_models , item_components , uvMap , atlas ] ) => {
77
81
78
82
// === Prepare assets for item and structure rendering ===
79
83
@@ -97,6 +101,21 @@ Promise.all([
97
101
} )
98
102
Object . values ( blockModels ) . forEach ( ( m : any ) => m . flatten ( { getBlockModel : id => blockModels [ id ] } ) )
99
103
104
+
105
+ const itemModels : Record < string , ItemModel > = { }
106
+ Object . keys ( item_models ) . forEach ( id => {
107
+ itemModels [ 'minecraft:' + id ] = ItemModel . fromJson ( item_models [ id ] . model )
108
+ } )
109
+
110
+
111
+ Object . keys ( item_components ) . forEach ( id => {
112
+ const components = new Map < string , NbtTag > ( )
113
+ Object . keys ( item_components [ id ] ) . forEach ( c_id => {
114
+ components . set ( c_id , jsonToNbt ( item_components [ id ] [ c_id ] ) )
115
+ } )
116
+ Item . REGISTRY . register ( Identifier . create ( id ) , new Item ( components ) )
117
+ } )
118
+
100
119
const atlasCanvas = document . createElement ( 'canvas' )
101
120
const atlasSize = upperPowerOfTwo ( Math . max ( atlas . width , atlas . height ) )
102
121
atlasCanvas . width = atlasSize
@@ -112,28 +131,36 @@ Promise.all([
112
131
} )
113
132
const textureAtlas = new TextureAtlas ( atlasData , idMap )
114
133
115
- const resources : Resources = {
134
+ const resources : Resources & ItemRendererResources = {
116
135
getBlockDefinition ( id ) { return blockDefinitions [ id . toString ( ) ] } ,
117
136
getBlockModel ( id ) { return blockModels [ id . toString ( ) ] } ,
118
137
getTextureUV ( id ) { return textureAtlas . getTextureUV ( id ) } ,
119
138
getTextureAtlas ( ) { return textureAtlas . getTextureAtlas ( ) } ,
120
139
getBlockFlags ( id ) { return { opaque : false } } ,
121
140
getBlockProperties ( id ) { return null } ,
122
141
getDefaultBlockProperties ( id ) { return null } ,
142
+ getItemModel ( id ) { return itemModels [ id . toString ( ) ] } ,
123
143
}
124
144
125
145
// === Item rendering ===
126
146
147
+ const context : ItemRenderingContext = {
148
+ "bundle/selected_item" : 0
149
+ }
150
+
127
151
const itemCanvas = document . getElementById ( 'item-display' ) as HTMLCanvasElement
128
152
const itemGl = itemCanvas . getContext ( 'webgl' ) !
129
153
const itemInput = document . getElementById ( 'item-input' ) as HTMLInputElement
130
154
itemInput . value = localStorage . getItem ( 'deepslate_demo_item' ) ?? 'stone'
131
- const itemRenderer = new ItemRenderer ( itemGl , Identifier . parse ( itemInput . value ) , resources )
155
+ const itemStack = ItemStack . fromString ( itemInput . value )
156
+ const itemRenderer = new ItemRenderer ( itemGl , itemStack , resources , context )
132
157
133
158
itemInput . addEventListener ( 'keyup' , ( ) => {
134
159
try {
135
160
const id = itemInput . value
136
- itemRenderer . setItem ( new ItemStack ( Identifier . parse ( id ) , 1 ) )
161
+ const itemStack = ItemStack . fromString ( itemInput . value )
162
+ itemGl . clear ( itemGl . DEPTH_BUFFER_BIT | itemGl . COLOR_BUFFER_BIT ) ;
163
+ itemRenderer . setItem ( itemStack , context )
137
164
itemRenderer . drawItem ( )
138
165
itemInput . classList . remove ( 'invalid' )
139
166
localStorage . setItem ( 'deepslate_demo_item' , id )
0 commit comments