generated from Sceat/service-starter
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
61d31a4
commit de01b89
Showing
24 changed files
with
432 additions
and
381 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,28 @@ | ||
import { BlocksBatch } from "../datacontainers/BlocksBatch" | ||
import { WorldUtils } from "../index" | ||
import { ProcessType, WorldProcess } from "../utils/types" | ||
import { BlocksBatch } from '../datacontainers/BlocksBatch' | ||
import { WorldUtils } from '../index' | ||
import { ProcessType, WorldProcess } from '../utils/types' | ||
|
||
type WorldProcessImpl = new (args: any) => WorldProcess //typeof WorldProcess | ||
type WorldProcessImpl = new (args: any) => WorldProcess // typeof WorldProcess | ||
|
||
export const ProcessMapping: Record<ProcessType, WorldProcessImpl> = { | ||
[ProcessType.BlocksBatch]: BlocksBatch | ||
[ProcessType.BlocksBatch]: BlocksBatch, | ||
} | ||
|
||
export class WorldProcessing { | ||
static parseArgs(rawArgs: any) { | ||
const args = rawArgs.map((arg: any) => | ||
arg instanceof Array | ||
? arg.map(item => WorldUtils.convert.parseThreeStub(item)) | ||
: WorldUtils.convert.parseThreeStub(arg), | ||
) | ||
return args | ||
} | ||
static async process(processType: ProcessType, processArgs: any) { | ||
processArgs = WorldProcessing.parseArgs(processArgs) | ||
const processClass = ProcessMapping[processType] | ||
const processInstance = new processClass(processArgs) | ||
await processInstance.process() | ||
return processInstance.toStub() | ||
} | ||
static parseArgs(rawArgs: any) { | ||
const args = rawArgs.map((arg: any) => | ||
arg instanceof Array | ||
? arg.map(item => WorldUtils.convert.parseThreeStub(item)) | ||
: WorldUtils.convert.parseThreeStub(arg), | ||
) | ||
return args | ||
} | ||
|
||
static async process(processType: ProcessType, processArgs: any) { | ||
processArgs = WorldProcessing.parseArgs(processArgs) | ||
const ProcessClass = ProcessMapping[processType] | ||
const processInstance = new ProcessClass(processArgs) | ||
await processInstance.process() | ||
return processInstance.toStub() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,118 +1,126 @@ | ||
import { Vector2 } from "three" | ||
import { computeGroundBlock } from "../api/world-compute" | ||
import { WorldEnv, Biome, WorldUtils, WorldComputeProxy } from "../index" | ||
import { serializePatchId, getPatchId, asVect3 } from "../utils/convert" | ||
import { PatchKey, GroundBlock, WorldProcess, ProcessType } from "../utils/types" | ||
import { GroundPatch } from "./GroundPatch" | ||
import { Vector2 } from 'three' | ||
|
||
import { computeGroundBlock } from '../api/world-compute' | ||
import { WorldEnv, Biome, WorldUtils, WorldComputeProxy } from '../index' | ||
import { serializePatchId, getPatchId, asVect3 } from '../utils/convert' | ||
import { | ||
PatchKey, | ||
GroundBlock, | ||
WorldProcess, | ||
ProcessType, | ||
} from '../utils/types' | ||
|
||
import { GroundPatch } from './GroundPatch' | ||
|
||
export type BlocksBatchArgs = { | ||
posBatch: Vector2[] | ||
posBatch: Vector2[] | ||
} | ||
|
||
export class BlocksBatch implements WorldProcess { | ||
patchIndex: Record<PatchKey, Vector2[]> = {} | ||
blocks: any[] = [] | ||
constructor(posBatch: Vector2[]) { | ||
// sort input blocks by patch | ||
// const blocksByPatch: Record<PatchKey, GroundBlock[]> = {} | ||
for (const pos of posBatch) { | ||
const patchId = getPatchId(pos, WorldEnv.current.patchDimensions) | ||
const patchKey = serializePatchId(patchId) | ||
this.patchIndex[patchKey] = this.patchIndex[patchKey] || [] | ||
this.patchIndex[patchKey]?.push(pos) | ||
} | ||
patchIndex: Record<PatchKey, Vector2[]> = {} | ||
blocks: any[] = [] | ||
constructor(posBatch: Vector2[]) { | ||
// sort input blocks by patch | ||
// const blocksByPatch: Record<PatchKey, GroundBlock[]> = {} | ||
for (const pos of posBatch) { | ||
const patchId = getPatchId(pos, WorldEnv.current.patchDimensions) | ||
const patchKey = serializePatchId(patchId) | ||
this.patchIndex[patchKey] = this.patchIndex[patchKey] || [] | ||
this.patchIndex[patchKey]?.push(pos) | ||
} | ||
} | ||
|
||
bake() { | ||
|
||
} | ||
bake() {} | ||
|
||
async process() { | ||
// const blocksBatch = posBatch.map(pos => { | ||
async process() { | ||
// const blocksBatch = posBatch.map(pos => { | ||
|
||
// const data: BlockData = { | ||
// level: 0, | ||
// type: BlockType.NONE, | ||
// } | ||
// const block: Block<BlockData> = { | ||
// pos: asVect3(pos), | ||
// data, | ||
// } | ||
// const data: BlockData = { | ||
// level: 0, | ||
// type: BlockType.NONE, | ||
// } | ||
// const block: Block<BlockData> = { | ||
// pos: asVect3(pos), | ||
// data, | ||
// } | ||
|
||
// return block | ||
// }) | ||
const blocksBatch = [] | ||
for (const [patchKey, posBatch] of Object.entries(this.patchIndex)) { | ||
const groundPatch = new GroundPatch(patchKey) | ||
const biomeBoundsInfluences = Biome.instance.getBoundsInfluences(groundPatch.bounds) | ||
for (const blockPos of posBatch) { | ||
|
||
|
||
const blockBiome = WorldUtils.process.getBlockBiome( | ||
blockPos, | ||
groundPatch.bounds, | ||
biomeBoundsInfluences, | ||
) | ||
const blockData = computeGroundBlock(asVect3(blockPos), blockBiome) | ||
// const {level, type } = | ||
// override with last block if specified | ||
// if (params.includeEntitiesBlocks) { | ||
// const lastBlockData = await queryLastBlockData(blockPos) | ||
// block.data = | ||
// lastBlockData.level > 0 && lastBlockData.type | ||
// ? lastBlockData | ||
// : (block.data as any) | ||
// } | ||
blockPos.y = blockData.level | ||
const block = { | ||
pos: asVect3(blockPos), | ||
data: blockData | ||
} | ||
blocksBatch.push(block) | ||
} | ||
// return block | ||
// }) | ||
const blocksBatch = [] | ||
for (const [patchKey, posBatch] of Object.entries(this.patchIndex)) { | ||
const groundPatch = new GroundPatch(patchKey) | ||
const biomeBoundsInfluences = Biome.instance.getBoundsInfluences( | ||
groundPatch.bounds, | ||
) | ||
for (const blockPos of posBatch) { | ||
const blockBiome = WorldUtils.process.getBlockBiome( | ||
blockPos, | ||
groundPatch.bounds, | ||
biomeBoundsInfluences, | ||
) | ||
const blockData = computeGroundBlock(asVect3(blockPos), blockBiome) | ||
// const {level, type } = | ||
// override with last block if specified | ||
// if (params.includeEntitiesBlocks) { | ||
// const lastBlockData = await queryLastBlockData(blockPos) | ||
// block.data = | ||
// lastBlockData.level > 0 && lastBlockData.type | ||
// ? lastBlockData | ||
// : (block.data as any) | ||
// } | ||
blockPos.y = blockData.level | ||
const block = { | ||
pos: asVect3(blockPos), | ||
data: blockData, | ||
} | ||
this.blocks = blocksBatch | ||
// return blocksBatch | ||
// const blocksBatch = blockPosBatch.map((pos) => { | ||
// const blockPos = asVect3(pos) | ||
// const blockData = computeGroundBlock(blockPos) | ||
// const { spawnableItems } = blockData | ||
// const queriedLoc = new Box2().setFromPoints([asVect2(blockPos)]) | ||
// queriedLoc.max.addScalar(1) | ||
// false && includeEntitiesBlocks && spawnableItems.forEach(itemType => { | ||
// // several (overlapping) objects may be found at queried position | ||
// const [spawnedEntity] = ItemsInventory.querySpawnedEntities(itemType, queriedLoc) | ||
// const lastBlockIndex = blocksBuffer?.findLastIndex(elt => elt) | ||
// if (blocksBuffer && lastBlockIndex && lastBlockIndex >= 0) { | ||
// blockData.level += lastBlockIndex | ||
// blockData.type = blocksBuffer[lastBlockIndex] as BlockType | ||
// } | ||
// }) | ||
// blockPos.y = blockData.level | ||
// const block: Block = { | ||
// pos: blockPos, | ||
// data: blockData, | ||
// } | ||
// return block | ||
// }) | ||
blocksBatch.push(block) | ||
} | ||
} | ||
this.blocks = blocksBatch | ||
// return blocksBatch | ||
// const blocksBatch = blockPosBatch.map((pos) => { | ||
// const blockPos = asVect3(pos) | ||
// const blockData = computeGroundBlock(blockPos) | ||
// const { spawnableItems } = blockData | ||
// const queriedLoc = new Box2().setFromPoints([asVect2(blockPos)]) | ||
// queriedLoc.max.addScalar(1) | ||
// false && includeEntitiesBlocks && spawnableItems.forEach(itemType => { | ||
// // several (overlapping) objects may be found at queried position | ||
// const [spawnedEntity] = ItemsInventory.querySpawnedEntities(itemType, queriedLoc) | ||
// const lastBlockIndex = blocksBuffer?.findLastIndex(elt => elt) | ||
// if (blocksBuffer && lastBlockIndex && lastBlockIndex >= 0) { | ||
// blockData.level += lastBlockIndex | ||
// blockData.type = blocksBuffer[lastBlockIndex] as BlockType | ||
// } | ||
// }) | ||
// blockPos.y = blockData.level | ||
// const block: Block = { | ||
// pos: blockPos, | ||
// data: blockData, | ||
// } | ||
// return block | ||
// }) | ||
} | ||
|
||
toStub() { | ||
return this.blocks | ||
} | ||
toStub() { | ||
return this.blocks | ||
} | ||
|
||
// byProxy | ||
static async proxyGen(posBatch: Vector2[], processingUnit = WorldComputeProxy.workerPool) { | ||
const res = await processingUnit | ||
.exec(ProcessType.BlocksBatch, [posBatch]) | ||
.then((blocksStubs: GroundBlock[]) => { | ||
// parse worker's data to recreate original objects | ||
const blocks = blocksStubs.map(blockStub => { | ||
blockStub.pos = WorldUtils.convert.parseThreeStub(blockStub.pos) | ||
return blockStub | ||
}) as GroundBlock[] | ||
return blocks | ||
}) as GroundBlock[] | ||
return res | ||
} | ||
} | ||
// byProxy | ||
static async proxyGen( | ||
posBatch: Vector2[], | ||
processingUnit = WorldComputeProxy.workerPool, | ||
) { | ||
const res = (await processingUnit | ||
.exec(ProcessType.BlocksBatch, [posBatch]) | ||
.then((blocksStubs: GroundBlock[]) => { | ||
// parse worker's data to recreate original objects | ||
const blocks = blocksStubs.map(blockStub => { | ||
blockStub.pos = WorldUtils.convert.parseThreeStub(blockStub.pos) | ||
return blockStub | ||
}) as GroundBlock[] | ||
return blocks | ||
})) as GroundBlock[] | ||
return res | ||
} | ||
} |
Oops, something went wrong.