minecraft-renderer 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +297 -0
- package/dist/index.html +83 -0
- package/dist/static/image/arrow.6f27b59f.png +0 -0
- package/dist/static/image/blocksAtlasLatest.7850afa3.png +0 -0
- package/dist/static/image/blocksAtlasLegacy.5c76823d.png +0 -0
- package/dist/static/image/itemsAtlasLatest.36036f95.png +0 -0
- package/dist/static/image/itemsAtlasLegacy.dcb1b58d.png +0 -0
- package/dist/static/image/tipped_arrow.6f27b59f.png +0 -0
- package/dist/static/js/365.f05233ab.js +8462 -0
- package/dist/static/js/365.f05233ab.js.LICENSE.txt +52 -0
- package/dist/static/js/async/738.efa27644.js +1 -0
- package/dist/static/js/index.092ec5be.js +56 -0
- package/dist/static/js/lib-polyfill.98986ac5.js +1 -0
- package/dist/static/js/lib-react.5c9129e0.js +2 -0
- package/dist/static/js/lib-react.5c9129e0.js.LICENSE.txt +39 -0
- package/package.json +104 -0
- package/src/assets/destroy_stage_0.png +0 -0
- package/src/assets/destroy_stage_1.png +0 -0
- package/src/assets/destroy_stage_2.png +0 -0
- package/src/assets/destroy_stage_3.png +0 -0
- package/src/assets/destroy_stage_4.png +0 -0
- package/src/assets/destroy_stage_5.png +0 -0
- package/src/assets/destroy_stage_6.png +0 -0
- package/src/assets/destroy_stage_7.png +0 -0
- package/src/assets/destroy_stage_8.png +0 -0
- package/src/assets/destroy_stage_9.png +0 -0
- package/src/examples/README.md +146 -0
- package/src/examples/appViewerExample.ts +205 -0
- package/src/examples/initialMenuStart.ts +161 -0
- package/src/graphicsBackend/appViewer.ts +297 -0
- package/src/graphicsBackend/config.ts +119 -0
- package/src/graphicsBackend/index.ts +10 -0
- package/src/graphicsBackend/playerState.ts +61 -0
- package/src/graphicsBackend/types.ts +143 -0
- package/src/index.ts +97 -0
- package/src/lib/DebugGui.ts +190 -0
- package/src/lib/animationController.ts +85 -0
- package/src/lib/buildSharedConfig.mjs +1 -0
- package/src/lib/cameraBobbing.ts +94 -0
- package/src/lib/canvas2DOverlay.example.ts +361 -0
- package/src/lib/canvas2DOverlay.quickstart.ts +242 -0
- package/src/lib/canvas2DOverlay.ts +381 -0
- package/src/lib/cleanupDecorator.ts +29 -0
- package/src/lib/createPlayerObject.ts +55 -0
- package/src/lib/frameTimingCollector.ts +164 -0
- package/src/lib/guiRenderer.ts +283 -0
- package/src/lib/items.ts +140 -0
- package/src/lib/mesherlogReader.ts +131 -0
- package/src/lib/moreBlockDataGenerated.json +714 -0
- package/src/lib/preflatMap.json +1741 -0
- package/src/lib/simpleUtils.ts +40 -0
- package/src/lib/smoothSwitcher.ts +168 -0
- package/src/lib/spiral.ts +29 -0
- package/src/lib/ui/newStats.ts +120 -0
- package/src/lib/utils/proxy.ts +23 -0
- package/src/lib/utils/skins.ts +63 -0
- package/src/lib/utils.ts +76 -0
- package/src/lib/workerProxy.ts +342 -0
- package/src/lib/worldrendererCommon.ts +1088 -0
- package/src/mesher/mesher.ts +253 -0
- package/src/mesher/models.ts +769 -0
- package/src/mesher/modelsGeometryCommon.ts +142 -0
- package/src/mesher/shared.ts +80 -0
- package/src/mesher/standaloneRenderer.ts +270 -0
- package/src/mesher/test/a.ts +3 -0
- package/src/mesher/test/mesherTester.ts +76 -0
- package/src/mesher/test/playground.ts +19 -0
- package/src/mesher/test/test-perf.ts +74 -0
- package/src/mesher/test/tests.test.ts +56 -0
- package/src/mesher/world.ts +294 -0
- package/src/mesher/worldConstants.ts +1 -0
- package/src/modules/index.ts +11 -0
- package/src/modules/starfield.ts +313 -0
- package/src/modules/types.ts +110 -0
- package/src/playerState/playerState.ts +78 -0
- package/src/playerState/types.ts +36 -0
- package/src/playground/allEntitiesDebug.ts +170 -0
- package/src/playground/baseScene.ts +587 -0
- package/src/playground/mobileControls.tsx +268 -0
- package/src/playground/playground.html +83 -0
- package/src/playground/playground.ts +11 -0
- package/src/playground/playgroundUi.tsx +140 -0
- package/src/playground/reactUtils.ts +71 -0
- package/src/playground/scenes/allEntities.ts +13 -0
- package/src/playground/scenes/entities.ts +37 -0
- package/src/playground/scenes/floorRandom.ts +33 -0
- package/src/playground/scenes/frequentUpdates.ts +148 -0
- package/src/playground/scenes/geometryExport.ts +142 -0
- package/src/playground/scenes/index.ts +12 -0
- package/src/playground/scenes/lightingStarfield.ts +40 -0
- package/src/playground/scenes/main.ts +313 -0
- package/src/playground/scenes/railsCobweb.ts +14 -0
- package/src/playground/scenes/rotationIssue.ts +7 -0
- package/src/playground/scenes/slabsOptimization.ts +16 -0
- package/src/playground/scenes/transparencyIssue.ts +11 -0
- package/src/playground/shared.ts +79 -0
- package/src/resourcesManager/index.ts +5 -0
- package/src/resourcesManager/resourcesManager.ts +314 -0
- package/src/shims/minecraftData.ts +41 -0
- package/src/sign-renderer/index.html +21 -0
- package/src/sign-renderer/index.ts +216 -0
- package/src/sign-renderer/noop.js +1 -0
- package/src/sign-renderer/playground.ts +38 -0
- package/src/sign-renderer/tests.test.ts +69 -0
- package/src/sign-renderer/vite.config.ts +10 -0
- package/src/three/appShared.ts +75 -0
- package/src/three/bannerRenderer.ts +275 -0
- package/src/three/cameraShake.ts +120 -0
- package/src/three/cinimaticScript.ts +350 -0
- package/src/three/documentRenderer.ts +491 -0
- package/src/three/entities.ts +1580 -0
- package/src/three/entity/EntityMesh.ts +707 -0
- package/src/three/entity/animations.js +171 -0
- package/src/three/entity/armorModels.json +204 -0
- package/src/three/entity/armorModels.ts +36 -0
- package/src/three/entity/entities.json +6230 -0
- package/src/three/entity/exportedModels.js +38 -0
- package/src/three/entity/externalTextures.json +1 -0
- package/src/three/entity/models/allay.obj +325 -0
- package/src/three/entity/models/arrow.obj +60 -0
- package/src/three/entity/models/axolotl.obj +509 -0
- package/src/three/entity/models/blaze.obj +601 -0
- package/src/three/entity/models/boat.obj +417 -0
- package/src/three/entity/models/camel.obj +1061 -0
- package/src/three/entity/models/cat.obj +509 -0
- package/src/three/entity/models/chicken.obj +371 -0
- package/src/three/entity/models/cod.obj +371 -0
- package/src/three/entity/models/creeper.obj +279 -0
- package/src/three/entity/models/dolphin.obj +371 -0
- package/src/three/entity/models/ender_dragon.obj +2993 -0
- package/src/three/entity/models/enderman.obj +325 -0
- package/src/three/entity/models/endermite.obj +187 -0
- package/src/three/entity/models/fox.obj +463 -0
- package/src/three/entity/models/frog.obj +739 -0
- package/src/three/entity/models/ghast.obj +463 -0
- package/src/three/entity/models/goat.obj +601 -0
- package/src/three/entity/models/guardian.obj +1015 -0
- package/src/three/entity/models/horse.obj +1061 -0
- package/src/three/entity/models/llama.obj +509 -0
- package/src/three/entity/models/minecart.obj +233 -0
- package/src/three/entity/models/parrot.obj +509 -0
- package/src/three/entity/models/piglin.obj +739 -0
- package/src/three/entity/models/pillager.obj +371 -0
- package/src/three/entity/models/rabbit.obj +555 -0
- package/src/three/entity/models/sheep.obj +555 -0
- package/src/three/entity/models/shulker.obj +141 -0
- package/src/three/entity/models/sniffer.obj +693 -0
- package/src/three/entity/models/spider.obj +509 -0
- package/src/three/entity/models/tadpole.obj +95 -0
- package/src/three/entity/models/turtle.obj +371 -0
- package/src/three/entity/models/vex.obj +325 -0
- package/src/three/entity/models/villager.obj +509 -0
- package/src/three/entity/models/warden.obj +463 -0
- package/src/three/entity/models/witch.obj +647 -0
- package/src/three/entity/models/wolf.obj +509 -0
- package/src/three/entity/models/zombie_villager.obj +463 -0
- package/src/three/entity/objModels.js +1 -0
- package/src/three/fireworks.ts +661 -0
- package/src/three/fireworksRenderer.ts +434 -0
- package/src/three/globals.d.ts +7 -0
- package/src/three/graphicsBackend.ts +274 -0
- package/src/three/graphicsBackendOffThread.ts +107 -0
- package/src/three/hand.ts +89 -0
- package/src/three/holdingBlock.ts +926 -0
- package/src/three/index.ts +20 -0
- package/src/three/itemMesh.ts +427 -0
- package/src/three/modules.d.ts +14 -0
- package/src/three/panorama.ts +308 -0
- package/src/three/panoramaShared.ts +1 -0
- package/src/three/renderSlot.ts +82 -0
- package/src/three/skyboxRenderer.ts +406 -0
- package/src/three/starField.ts +13 -0
- package/src/three/threeJsMedia.ts +731 -0
- package/src/three/threeJsMethods.ts +15 -0
- package/src/three/threeJsParticles.ts +160 -0
- package/src/three/threeJsSound.ts +95 -0
- package/src/three/threeJsUtils.ts +90 -0
- package/src/three/waypointSprite.ts +435 -0
- package/src/three/waypoints.ts +163 -0
- package/src/three/world/cursorBlock.ts +172 -0
- package/src/three/world/vr.ts +257 -0
- package/src/three/worldGeometryExport.ts +259 -0
- package/src/three/worldGeometryHandler.ts +279 -0
- package/src/three/worldRendererThree.ts +1381 -0
- package/src/worldView/index.ts +6 -0
- package/src/worldView/types.ts +66 -0
- package/src/worldView/worldView.ts +424 -0
|
@@ -0,0 +1,253 @@
|
|
|
1
|
+
import { Vec3 } from 'vec3'
|
|
2
|
+
import { World } from './world'
|
|
3
|
+
import { getSectionGeometry, setBlockStatesData as setMesherData } from './models'
|
|
4
|
+
import { BlockStateModelInfo } from './shared'
|
|
5
|
+
import { INVISIBLE_BLOCKS } from './worldConstants'
|
|
6
|
+
|
|
7
|
+
globalThis.structuredClone ??= (value) => JSON.parse(JSON.stringify(value))
|
|
8
|
+
|
|
9
|
+
if (globalThis.module && module.require) {
|
|
10
|
+
// If we are in a node environement, we need to fake some env variables
|
|
11
|
+
const r = module.require
|
|
12
|
+
const { parentPort } = r('worker_threads')
|
|
13
|
+
global.self = parentPort
|
|
14
|
+
global.postMessage = (value, transferList) => { parentPort.postMessage(value, transferList) }
|
|
15
|
+
global.performance = r('perf_hooks').performance
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
let workerIndex = 0
|
|
19
|
+
let world: World
|
|
20
|
+
let dirtySections = new Map<string, number>()
|
|
21
|
+
let allDataReady = false
|
|
22
|
+
|
|
23
|
+
function sectionKey(x, y, z) {
|
|
24
|
+
return `${x},${y},${z}`
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
const batchMessagesLimit = 100
|
|
28
|
+
|
|
29
|
+
let queuedMessages = [] as any[]
|
|
30
|
+
let queueWaiting = false
|
|
31
|
+
const postMessage = (data, transferList = []) => {
|
|
32
|
+
queuedMessages.push({ data, transferList })
|
|
33
|
+
if (queuedMessages.length > batchMessagesLimit) {
|
|
34
|
+
drainQueue(0, batchMessagesLimit)
|
|
35
|
+
}
|
|
36
|
+
if (queueWaiting) return
|
|
37
|
+
queueWaiting = true
|
|
38
|
+
setTimeout(() => {
|
|
39
|
+
queueWaiting = false
|
|
40
|
+
drainQueue(0, queuedMessages.length)
|
|
41
|
+
})
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function drainQueue(from, to) {
|
|
45
|
+
const messages = queuedMessages.slice(from, to)
|
|
46
|
+
global.postMessage(messages.map(m => m.data), messages.flatMap(m => m.transferList) as unknown as string)
|
|
47
|
+
queuedMessages = queuedMessages.slice(to)
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function setSectionDirty(pos, value = true) {
|
|
51
|
+
const x = Math.floor(pos.x / 16) * 16
|
|
52
|
+
const y = Math.floor(pos.y / 16) * 16
|
|
53
|
+
const z = Math.floor(pos.z / 16) * 16
|
|
54
|
+
const key = sectionKey(x, y, z)
|
|
55
|
+
if (!value) {
|
|
56
|
+
dirtySections.delete(key)
|
|
57
|
+
postMessage({ type: 'sectionFinished', key, workerIndex })
|
|
58
|
+
return
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
const chunk = world.getColumn(x, z)
|
|
62
|
+
if (chunk?.getSection(pos)) {
|
|
63
|
+
dirtySections.set(key, (dirtySections.get(key) || 0) + 1)
|
|
64
|
+
} else {
|
|
65
|
+
postMessage({ type: 'sectionFinished', key, workerIndex })
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
const softCleanup = () => {
|
|
70
|
+
// clean block cache and loaded chunks
|
|
71
|
+
world = new World(world.config.version)
|
|
72
|
+
globalThis.world = world
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
let sideControl = false
|
|
76
|
+
const handleMessage = data => {
|
|
77
|
+
const globalVar: any = globalThis
|
|
78
|
+
|
|
79
|
+
if (data.type === 'mcData') {
|
|
80
|
+
globalVar.mcData = data.mcData
|
|
81
|
+
globalVar.loadedData = data.mcData
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
if (sideControl) return
|
|
85
|
+
|
|
86
|
+
if (data.config) {
|
|
87
|
+
if (data.type === 'mesherData' && world) {
|
|
88
|
+
// reset models
|
|
89
|
+
world.blockCache = {}
|
|
90
|
+
world.erroredBlockModel = undefined
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
world ??= new World(data.config.version)
|
|
94
|
+
world.config = { ...world.config, ...data.config }
|
|
95
|
+
globalThis.world = world
|
|
96
|
+
globalThis.Vec3 = Vec3
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
switch (data.type) {
|
|
100
|
+
case 'sideControl': {
|
|
101
|
+
if (data.value === 'graphicsBackendThree') {
|
|
102
|
+
sideControl = true
|
|
103
|
+
void import('../three/graphicsBackend').then(module => {
|
|
104
|
+
const graphicsBackend = module.createGraphicsBackendBase()
|
|
105
|
+
graphicsBackend.workerProxy()
|
|
106
|
+
global.postMessage({ type: 'sideControlTookOver' })
|
|
107
|
+
})
|
|
108
|
+
}
|
|
109
|
+
break
|
|
110
|
+
}
|
|
111
|
+
case 'mesherData': {
|
|
112
|
+
setMesherData(data.blockstatesModels, data.blocksAtlas, data.config.outputFormat === 'webgpu')
|
|
113
|
+
allDataReady = true
|
|
114
|
+
workerIndex = data.workerIndex
|
|
115
|
+
|
|
116
|
+
break
|
|
117
|
+
}
|
|
118
|
+
case 'dirty': {
|
|
119
|
+
const loc = new Vec3(data.x, data.y, data.z)
|
|
120
|
+
setSectionDirty(loc, data.value)
|
|
121
|
+
|
|
122
|
+
break
|
|
123
|
+
}
|
|
124
|
+
case 'chunk': {
|
|
125
|
+
world.addColumn(data.x, data.z, data.chunk)
|
|
126
|
+
if (data.customBlockModels) {
|
|
127
|
+
const chunkKey = `${data.x},${data.z}`
|
|
128
|
+
world.customBlockModels.set(chunkKey, data.customBlockModels)
|
|
129
|
+
}
|
|
130
|
+
break
|
|
131
|
+
}
|
|
132
|
+
case 'unloadChunk': {
|
|
133
|
+
world.removeColumn(data.x, data.z)
|
|
134
|
+
world.customBlockModels.delete(`${data.x},${data.z}`)
|
|
135
|
+
if (Object.keys(world.columns).length === 0) softCleanup()
|
|
136
|
+
break
|
|
137
|
+
}
|
|
138
|
+
case 'blockUpdate': {
|
|
139
|
+
const loc = new Vec3(data.pos.x, data.pos.y, data.pos.z).floored()
|
|
140
|
+
if (data.stateId !== undefined && data.stateId !== null) {
|
|
141
|
+
world?.setBlockStateId(loc, data.stateId)
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
const chunkKey = `${Math.floor(loc.x / 16) * 16},${Math.floor(loc.z / 16) * 16}`
|
|
145
|
+
if (data.customBlockModels) {
|
|
146
|
+
world?.customBlockModels.set(chunkKey, data.customBlockModels)
|
|
147
|
+
}
|
|
148
|
+
break
|
|
149
|
+
}
|
|
150
|
+
case 'reset': {
|
|
151
|
+
world = undefined as any
|
|
152
|
+
// blocksStates = null
|
|
153
|
+
dirtySections = new Map()
|
|
154
|
+
// todo also remove cached
|
|
155
|
+
globalVar.mcData = null
|
|
156
|
+
globalVar.loadedData = null
|
|
157
|
+
allDataReady = false
|
|
158
|
+
|
|
159
|
+
break
|
|
160
|
+
}
|
|
161
|
+
case 'getCustomBlockModel': {
|
|
162
|
+
const pos = new Vec3(data.pos.x, data.pos.y, data.pos.z)
|
|
163
|
+
const chunkKey = `${Math.floor(pos.x / 16) * 16},${Math.floor(pos.z / 16) * 16}`
|
|
164
|
+
const customBlockModel = world.customBlockModels.get(chunkKey)?.[`${pos.x},${pos.y},${pos.z}`]
|
|
165
|
+
global.postMessage({ type: 'customBlockModel', chunkKey, customBlockModel })
|
|
166
|
+
break
|
|
167
|
+
}
|
|
168
|
+
case 'getHeightmap': {
|
|
169
|
+
const heightmap = new Uint8Array(256)
|
|
170
|
+
|
|
171
|
+
const blockPos = new Vec3(0, 0, 0)
|
|
172
|
+
for (let z = 0; z < 16; z++) {
|
|
173
|
+
for (let x = 0; x < 16; x++) {
|
|
174
|
+
const blockX = x + data.x
|
|
175
|
+
const blockZ = z + data.z
|
|
176
|
+
blockPos.x = blockX
|
|
177
|
+
blockPos.z = blockZ
|
|
178
|
+
blockPos.y = world.config.worldMaxY
|
|
179
|
+
let block = world.getBlock(blockPos)
|
|
180
|
+
while (block && INVISIBLE_BLOCKS.has(block.name) && blockPos.y > world.config.worldMinY) {
|
|
181
|
+
blockPos.y -= 1
|
|
182
|
+
block = world.getBlock(blockPos)
|
|
183
|
+
}
|
|
184
|
+
const index = z * 16 + x
|
|
185
|
+
heightmap[index] = block ? blockPos.y : 0
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
postMessage({ type: 'heightmap', key: `${Math.floor(data.x / 16)},${Math.floor(data.z / 16)}`, heightmap })
|
|
189
|
+
|
|
190
|
+
break
|
|
191
|
+
}
|
|
192
|
+
// No default
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
// eslint-disable-next-line no-restricted-globals -- TODO
|
|
197
|
+
self.onmessage = ({ data }) => {
|
|
198
|
+
if (Array.isArray(data)) {
|
|
199
|
+
// eslint-disable-next-line unicorn/no-array-for-each
|
|
200
|
+
data.forEach(handleMessage)
|
|
201
|
+
return
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
handleMessage(data)
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
setInterval(() => {
|
|
208
|
+
if (world === null || !allDataReady) return
|
|
209
|
+
|
|
210
|
+
if (dirtySections.size === 0) return
|
|
211
|
+
// console.log(sections.length + ' dirty sections')
|
|
212
|
+
|
|
213
|
+
// const start = performance.now()
|
|
214
|
+
for (const key of dirtySections.keys()) {
|
|
215
|
+
const [x, y, z] = key.split(',').map(v => parseInt(v, 10))
|
|
216
|
+
const chunk = world.getColumn(x, z)
|
|
217
|
+
let processTime = 0
|
|
218
|
+
if (chunk?.getSection(new Vec3(x, y, z))) {
|
|
219
|
+
const start = performance.now()
|
|
220
|
+
const geometry = getSectionGeometry(x, y, z, world)
|
|
221
|
+
const transferable = [geometry.positions?.buffer, geometry.normals?.buffer, geometry.colors?.buffer, geometry.uvs?.buffer].filter(Boolean)
|
|
222
|
+
//@ts-expect-error
|
|
223
|
+
postMessage({ type: 'geometry', key, geometry, workerIndex }, transferable)
|
|
224
|
+
processTime = performance.now() - start
|
|
225
|
+
} else {
|
|
226
|
+
// console.info('[mesher] Missing section', x, y, z)
|
|
227
|
+
}
|
|
228
|
+
const dirtyTimes = dirtySections.get(key)
|
|
229
|
+
if (!dirtyTimes) throw new Error('dirtySections.get(key) is falsy')
|
|
230
|
+
for (let i = 0; i < dirtyTimes; i++) {
|
|
231
|
+
postMessage({ type: 'sectionFinished', key, workerIndex, processTime })
|
|
232
|
+
processTime = 0
|
|
233
|
+
}
|
|
234
|
+
dirtySections.delete(key)
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
// Send new block state model info if any
|
|
238
|
+
if (world.blockStateModelInfo.size > 0) {
|
|
239
|
+
const newBlockStateInfo: Record<string, BlockStateModelInfo> = {}
|
|
240
|
+
for (const [cacheKey, info] of world.blockStateModelInfo) {
|
|
241
|
+
if (!world.sentBlockStateModels.has(cacheKey)) {
|
|
242
|
+
newBlockStateInfo[cacheKey] = info
|
|
243
|
+
world.sentBlockStateModels.add(cacheKey)
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
if (Object.keys(newBlockStateInfo).length > 0) {
|
|
247
|
+
postMessage({ type: 'blockStateModelInfo', info: newBlockStateInfo })
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
// const time = performance.now() - start
|
|
252
|
+
// console.log(`Processed ${sections.length} sections in ${time} ms (${time / sections.length} ms/section)`)
|
|
253
|
+
}, 50)
|