Stream controlled OBJ asset parsing
This commit is contained in:
parent
525f0b3290
commit
678125cb0f
@ -74,7 +74,8 @@ Important properties:
|
||||
- Controlled `/assets/...` OBJ sources use a narrow fast parser for the common
|
||||
`v/vt/vn/f` mesh path, with Three.js `OBJLoader` retained as fallback and as
|
||||
the default for local uploaded/custom OBJ assets. The product gate checks
|
||||
`objFast` and `fallback` counters so this optimization remains measurable.
|
||||
`objFast`, `objLines`, `objStream`, and `fallback` counters so this
|
||||
optimization remains measurable.
|
||||
- Large-scene diagnostics use adaptive stats refresh: user-visible events still
|
||||
force an immediate panel update, while background periodic refreshes drop to
|
||||
a lower cadence for large or paused scenes so long diagnostic strings do not
|
||||
@ -432,7 +433,7 @@ Scene evidence:
|
||||
scene rebuilds, while scene-owned clones still release their non-retained
|
||||
resources. Controlled backend OBJ sources also use the fast parser before the
|
||||
retained source enters the cache. The release gate now checks mesh cache
|
||||
hit/miss plus `objFast/fallback` diagnostics.
|
||||
hit/miss plus `objFast/objLines/objStream/fallback` diagnostics.
|
||||
- Camera-fit bounds cache: object world bounds are captured while creating
|
||||
cached pick targets, so camera fitting no longer performs a second deep
|
||||
`Box3.setFromObject` traversal across the full scene.
|
||||
@ -616,16 +617,17 @@ Bundle evidence:
|
||||
transfer savings are separated from the browser-side parse and object-build
|
||||
cost in gate evidence.
|
||||
- Controlled OBJ fast parse gate: `browser-large-scene-performance-mode` now
|
||||
requires `objFast > 0`, `objLines > 0`, and `objLoaderFallbackCount = 0` for
|
||||
the Wuying-backed store scene. The latest product release gate recorded
|
||||
`objFast=16/fallback=0`, while uploaded/custom OBJ assets keep the full
|
||||
Three.js `OBJLoader` path for compatibility.
|
||||
requires `objFast > 0`, `objLines > 0`, `objStream > 0`, and
|
||||
`objLoaderFallbackCount = 0` for the Wuying-backed store scene. The latest
|
||||
product release gate recorded `objFast=16/fallback=0`, while uploaded/custom
|
||||
OBJ assets keep the full Three.js `OBJLoader` path for compatibility.
|
||||
- Fast OBJ parser allocation hygiene: controlled OBJ parsing stores source
|
||||
positions, UVs, and normals in flat numeric arrays and triangulates faces
|
||||
without per-triangle temporary arrays. It also scans the OBJ text one line at
|
||||
a time instead of materializing a full `text.split(...)` line array, keeping
|
||||
the optimization focused on browser main-thread parse/GC pressure rather than
|
||||
changing asset semantics.
|
||||
a time and consumes `Response.body` with `TextDecoder` chunks instead of
|
||||
materializing a full `response.text()` string or `text.split(...)` line array,
|
||||
keeping the optimization focused on browser main-thread parse/GC pressure
|
||||
rather than changing asset semantics.
|
||||
- Material construction hygiene: Three.js material creation now omits the
|
||||
optional `map` parameter unless a texture actually exists. The quick browser
|
||||
performance check recorded `0` `THREE.Material: parameter 'map'` warnings,
|
||||
@ -755,7 +757,7 @@ Control evidence:
|
||||
`acceptanceLatency=0 ms / 11-0-0`,
|
||||
`binding=635/636; 4/568`,
|
||||
`builderPhysics=已添加物体 4: 后端动态 4 / 仅视觉 0`,
|
||||
`renderPerformance=large-scene; target 45 fps; dpr 1.00; buffer preserved false; readback renders 1; shadows off; hover 32ms; load 1843-3089ms; frozen 135 prefrozen 59 spinSkip 627; instanced 568/16/16 matrices 568 cull 16 static 16 elidedLocal 568 virtualRoots 568; manual roots 68 updates 67; primitive geometries 7; mesh cache 51 sources 24/51 hit/miss objFast 16 objLines >0/fallback 0 retained 51g/17m; bounds cache 636 fast 627 scratch 695 source 51 hoverProxy 1-2 cached 1-2/fallback 0 camera traversals 0; scene batches 4/84 yields 26; state payload 34/602 reduced 1-2 incremental 3; poll 50/2000ms; pick targets 88/636; released 0g/0m/0t`.
|
||||
`renderPerformance=large-scene; target 45 fps; dpr 1.00; buffer preserved false; readback renders 1; shadows off; hover 32ms; load 1843-3089ms; frozen 135 prefrozen 59 spinSkip 627; instanced 568/16/16 matrices 568 cull 16 static 16 elidedLocal 568 virtualRoots 568; manual roots 68 updates 67; primitive geometries 7; mesh cache 51 sources 24/51 hit/miss objFast 16 objLines >0 objStream >0/fallback 0 retained 51g/17m; bounds cache 636 fast 627 scratch 695 source 51 hoverProxy 1-2 cached 1-2/fallback 0 camera traversals 0; scene batches 4/84 yields 26; state payload 34/602 reduced 1-2 incremental 3; poll 50/2000ms; pick targets 88/636; released 0g/0m/0t`.
|
||||
- Evidence screenshot artifacts:
|
||||
`builder page; share page; import page; visual-only boundary canvas; desktop canvas; debug PNG; mobile canvas`
|
||||
- Evidence export artifacts:
|
||||
|
||||
File diff suppressed because one or more lines are too long
@ -249,6 +249,8 @@ interface RuntimeDiagnostics {
|
||||
meshCacheMissCount: number;
|
||||
fastObjParseCount: number;
|
||||
fastObjLineScanCount: number;
|
||||
fastObjStreamChunkCount: number;
|
||||
fastObjStreamByteCount: number;
|
||||
objLoaderFallbackCount: number;
|
||||
retainedMeshGeometryCount: number;
|
||||
retainedMeshMaterialCount: number;
|
||||
@ -2854,6 +2856,8 @@ function currentRuntimeDiagnostics(
|
||||
meshCacheMissCount: stats.meshCacheMissCount,
|
||||
fastObjParseCount: stats.fastObjParseCount,
|
||||
fastObjLineScanCount: stats.fastObjLineScanCount,
|
||||
fastObjStreamChunkCount: stats.fastObjStreamChunkCount,
|
||||
fastObjStreamByteCount: stats.fastObjStreamByteCount,
|
||||
objLoaderFallbackCount: stats.objLoaderFallbackCount,
|
||||
retainedMeshGeometryCount: stats.retainedMeshGeometryCount,
|
||||
retainedMeshMaterialCount: stats.retainedMeshMaterialCount,
|
||||
@ -2961,7 +2965,7 @@ function currentDiagnosticsReport(): string {
|
||||
function renderPerformanceLabel(stats: RuntimeDiagnostics["stats"]): string {
|
||||
const mode = stats.largeSceneMode ? "large-scene" : "standard";
|
||||
const shadows = stats.shadowsEnabled ? "shadows on" : "shadows off";
|
||||
return `${mode}; target ${stats.targetRenderFps} fps; actual ${stats.actualRenderFps.toFixed(1)} fps; stats refresh ${stats.statsRefreshIntervalMs}ms ${stats.statsRefreshRenderCount}/${stats.statsRefreshSkipCount}; dpr ${stats.renderPixelRatio.toFixed(2)}; buffer preserved ${stats.preserveDrawingBufferEnabled}; readback renders ${stats.readbackRenderCount}; ${shadows}; skipped ${stats.renderSkipCount}; hover ${stats.hoverPickIntervalMs}ms; load ${Math.round(stats.sceneLoadTotalMs)}ms objects ${Math.round(stats.sceneLoadObjectsMs)}ms prewarm ${stats.instancedMeshPrewarmCount}/${Math.round(stats.instancedMeshPrewarmMs)}ms finalize ${Math.round(stats.instancedMeshFinalizeMs)}ms camera ${Math.round(stats.sceneLoadCameraFitMs)}ms; frozen ${stats.frozenStaticObjectCount} prefrozen ${stats.preFrozenMeshCloneCount} spinSkip ${stats.spinningScanSkippedCount}; instanced ${stats.instancedMeshObjectCount}/${stats.instancedMeshBatchCount}/${stats.instancedMeshDrawCount} matrices ${stats.instancedMeshMatrixUpdateCount} cull ${stats.instancedMeshFrustumCulledCount} static ${stats.instancedMeshStaticMatrixCount} elidedLocal ${stats.instancedLocalObjectElisionCount} virtualRoots ${stats.virtualInstancedRootCount}; manual roots ${stats.manualMatrixRootCount} updates ${stats.manualMatrixUpdateCount}; primitive geometries ${stats.primitiveGeometryCount}; mesh load limit ${stats.meshSourceLoadLimit} active ${stats.activeMeshSourceLoadCount} peak ${stats.peakMeshSourceLoadCount} queued ${stats.queuedMeshSourceLoadCount}; mesh cache ${stats.meshCacheSourceCount} sources ${stats.meshCacheHitCount}/${stats.meshCacheMissCount} hit/miss objFast ${stats.fastObjParseCount} objLines ${stats.fastObjLineScanCount}/fallback ${stats.objLoaderFallbackCount} retained ${stats.retainedMeshGeometryCount}g/${stats.retainedMeshMaterialCount}m; bounds cache ${stats.sceneObjectBoundsCount} fast ${stats.fastPickProxyBoundsCount} scratch ${stats.pickBoundsScratchTransformCount} source ${stats.meshSourceBoundsCount} hoverProxy ${stats.hoverProxyBoundsHitCount} cached ${stats.hoverCachedBoundsHitCount}/fallback ${stats.hoverRootBoundsFallbackCount} camera traversals ${stats.cameraFitTraversalCount}; scene batches ${stats.sceneRootBatchCount}/${stats.batchedSceneRootCount} suppressed ${stats.sceneLoadSuppressedRenderCount} yields ${stats.sceneBuildYieldCount}; state payload ${stats.statePayloadBodyCount}/${stats.mergedBodyStateCount} reduced ${stats.reducedStateFrameCount} incremental ${stats.incrementalBodyMergeCount} poll ${stats.stateBasePollIntervalMs}/${stats.statePausedPollIntervalMs}/${stats.stateLastScheduledPollIntervalMs}ms ${stats.statePollRequestCount}/${stats.stateActivePollRequestCount}/${stats.statePausedPollRequestCount} semantic ${stats.stateParsedPollFrameCount}/${stats.stateForwardedPollFrameCount}/${stats.stateSkippedDuplicatePollFrameCount} hash ${stats.stateHashedPollFrameCount}/${stats.stateSkippedDuplicateHashPollFrameCount}/${stats.stateSkippedDuplicateSemanticPollFrameCount} notModified ${stats.stateNotModifiedPollFrameCount}; ui cache physics ${stats.objectPhysicsProfileCacheHitCount}/${stats.objectPhysicsProfileCacheMissCount} builder ${stats.builderPhysicsProfileCacheHitCount}/${stats.builderPhysicsProfileCacheMissCount} text ${stats.textWriteCount}/${stats.textWriteSkipCount} selector ${stats.textSelectorCacheHitCount}/${stats.textSelectorCacheMissCount} diagnostics ${stats.diagnosticsPanelRenderCount}/${stats.diagnosticsPanelSkipCount} builderPanels ${stats.builderPanelRenderCount}/${stats.builderPanelDeferredRenderCount}/${stats.builderPanelCoalescedRenderCount}; drag ${stats.dragMoveEventCount}/${stats.dragAppliedMoveCount}/${stats.dragCoalescedMoveCount}; pick targets ${stats.hoverPickTargetCount}/${stats.pickProxyCount}; state apply ${stats.appliedBodyStateCount}/skip ${stats.skippedBodyStateCount}; unchanged frames ${stats.unchangedFrameCount}; released ${stats.releasedGeometryCount}g/${stats.releasedMaterialCount}m/${stats.releasedTextureCount}t`;
|
||||
return `${mode}; target ${stats.targetRenderFps} fps; actual ${stats.actualRenderFps.toFixed(1)} fps; stats refresh ${stats.statsRefreshIntervalMs}ms ${stats.statsRefreshRenderCount}/${stats.statsRefreshSkipCount}; dpr ${stats.renderPixelRatio.toFixed(2)}; buffer preserved ${stats.preserveDrawingBufferEnabled}; readback renders ${stats.readbackRenderCount}; ${shadows}; skipped ${stats.renderSkipCount}; hover ${stats.hoverPickIntervalMs}ms; load ${Math.round(stats.sceneLoadTotalMs)}ms objects ${Math.round(stats.sceneLoadObjectsMs)}ms prewarm ${stats.instancedMeshPrewarmCount}/${Math.round(stats.instancedMeshPrewarmMs)}ms finalize ${Math.round(stats.instancedMeshFinalizeMs)}ms camera ${Math.round(stats.sceneLoadCameraFitMs)}ms; frozen ${stats.frozenStaticObjectCount} prefrozen ${stats.preFrozenMeshCloneCount} spinSkip ${stats.spinningScanSkippedCount}; instanced ${stats.instancedMeshObjectCount}/${stats.instancedMeshBatchCount}/${stats.instancedMeshDrawCount} matrices ${stats.instancedMeshMatrixUpdateCount} cull ${stats.instancedMeshFrustumCulledCount} static ${stats.instancedMeshStaticMatrixCount} elidedLocal ${stats.instancedLocalObjectElisionCount} virtualRoots ${stats.virtualInstancedRootCount}; manual roots ${stats.manualMatrixRootCount} updates ${stats.manualMatrixUpdateCount}; primitive geometries ${stats.primitiveGeometryCount}; mesh load limit ${stats.meshSourceLoadLimit} active ${stats.activeMeshSourceLoadCount} peak ${stats.peakMeshSourceLoadCount} queued ${stats.queuedMeshSourceLoadCount}; mesh cache ${stats.meshCacheSourceCount} sources ${stats.meshCacheHitCount}/${stats.meshCacheMissCount} hit/miss objFast ${stats.fastObjParseCount} objLines ${stats.fastObjLineScanCount} objStream ${stats.fastObjStreamChunkCount}/${stats.fastObjStreamByteCount}B/fallback ${stats.objLoaderFallbackCount} retained ${stats.retainedMeshGeometryCount}g/${stats.retainedMeshMaterialCount}m; bounds cache ${stats.sceneObjectBoundsCount} fast ${stats.fastPickProxyBoundsCount} scratch ${stats.pickBoundsScratchTransformCount} source ${stats.meshSourceBoundsCount} hoverProxy ${stats.hoverProxyBoundsHitCount} cached ${stats.hoverCachedBoundsHitCount}/fallback ${stats.hoverRootBoundsFallbackCount} camera traversals ${stats.cameraFitTraversalCount}; scene batches ${stats.sceneRootBatchCount}/${stats.batchedSceneRootCount} suppressed ${stats.sceneLoadSuppressedRenderCount} yields ${stats.sceneBuildYieldCount}; state payload ${stats.statePayloadBodyCount}/${stats.mergedBodyStateCount} reduced ${stats.reducedStateFrameCount} incremental ${stats.incrementalBodyMergeCount} poll ${stats.stateBasePollIntervalMs}/${stats.statePausedPollIntervalMs}/${stats.stateLastScheduledPollIntervalMs}ms ${stats.statePollRequestCount}/${stats.stateActivePollRequestCount}/${stats.statePausedPollRequestCount} semantic ${stats.stateParsedPollFrameCount}/${stats.stateForwardedPollFrameCount}/${stats.stateSkippedDuplicatePollFrameCount} hash ${stats.stateHashedPollFrameCount}/${stats.stateSkippedDuplicateHashPollFrameCount}/${stats.stateSkippedDuplicateSemanticPollFrameCount} notModified ${stats.stateNotModifiedPollFrameCount}; ui cache physics ${stats.objectPhysicsProfileCacheHitCount}/${stats.objectPhysicsProfileCacheMissCount} builder ${stats.builderPhysicsProfileCacheHitCount}/${stats.builderPhysicsProfileCacheMissCount} text ${stats.textWriteCount}/${stats.textWriteSkipCount} selector ${stats.textSelectorCacheHitCount}/${stats.textSelectorCacheMissCount} diagnostics ${stats.diagnosticsPanelRenderCount}/${stats.diagnosticsPanelSkipCount} builderPanels ${stats.builderPanelRenderCount}/${stats.builderPanelDeferredRenderCount}/${stats.builderPanelCoalescedRenderCount}; drag ${stats.dragMoveEventCount}/${stats.dragAppliedMoveCount}/${stats.dragCoalescedMoveCount}; pick targets ${stats.hoverPickTargetCount}/${stats.pickProxyCount}; state apply ${stats.appliedBodyStateCount}/skip ${stats.skippedBodyStateCount}; unchanged frames ${stats.unchangedFrameCount}; released ${stats.releasedGeometryCount}g/${stats.releasedMaterialCount}m/${stats.releasedTextureCount}t`;
|
||||
}
|
||||
|
||||
function renderDiagnosticsPanel(
|
||||
@ -3052,6 +3056,8 @@ function renderDiagnosticsPanel(
|
||||
data.meshCacheMissCount = String(diagnostics.stats.meshCacheMissCount);
|
||||
data.fastObjParseCount = String(diagnostics.stats.fastObjParseCount);
|
||||
data.fastObjLineScanCount = String(diagnostics.stats.fastObjLineScanCount);
|
||||
data.fastObjStreamChunkCount = String(diagnostics.stats.fastObjStreamChunkCount);
|
||||
data.fastObjStreamByteCount = String(diagnostics.stats.fastObjStreamByteCount);
|
||||
data.objLoaderFallbackCount = String(diagnostics.stats.objLoaderFallbackCount);
|
||||
data.retainedMeshGeometryCount = String(diagnostics.stats.retainedMeshGeometryCount);
|
||||
data.retainedMeshMaterialCount = String(diagnostics.stats.retainedMeshMaterialCount);
|
||||
|
||||
@ -85,6 +85,24 @@ interface ObjVertexRef {
|
||||
normal?: number;
|
||||
}
|
||||
|
||||
interface ObjParseState {
|
||||
sourcePositions: number[];
|
||||
sourceNormals: number[];
|
||||
sourceUvs: number[];
|
||||
positions: number[];
|
||||
normals: number[];
|
||||
uvs: number[];
|
||||
positionCount: number;
|
||||
sourceNormalCount: number;
|
||||
sourceUvCount: number;
|
||||
normalCount: number;
|
||||
missingNormalCount: number;
|
||||
uvCount: number;
|
||||
missingUvCount: number;
|
||||
faceVertexCount: number;
|
||||
lineCount: number;
|
||||
}
|
||||
|
||||
interface SceneBuildRoots {
|
||||
visible: THREE.Object3D[];
|
||||
instanced: THREE.Object3D[];
|
||||
@ -273,6 +291,8 @@ export class UniversalPreviewer {
|
||||
private meshCacheMissCount = 0;
|
||||
private fastObjParseCount = 0;
|
||||
private fastObjLineScanCount = 0;
|
||||
private fastObjStreamChunkCount = 0;
|
||||
private fastObjStreamByteCount = 0;
|
||||
private objLoaderFallbackCount = 0;
|
||||
private sceneObjectBoundsCount = 0;
|
||||
private fastPickProxyBoundsCount = 0;
|
||||
@ -642,6 +662,8 @@ export class UniversalPreviewer {
|
||||
meshCacheMissCount: this.meshCacheMissCount,
|
||||
fastObjParseCount: this.fastObjParseCount,
|
||||
fastObjLineScanCount: this.fastObjLineScanCount,
|
||||
fastObjStreamChunkCount: this.fastObjStreamChunkCount,
|
||||
fastObjStreamByteCount: this.fastObjStreamByteCount,
|
||||
objLoaderFallbackCount: this.objLoaderFallbackCount,
|
||||
retainedMeshGeometryCount: this.retainedMeshGeometries.size,
|
||||
retainedMeshMaterialCount: this.retainedMeshMaterials.size,
|
||||
@ -1247,6 +1269,8 @@ export class UniversalPreviewer {
|
||||
this.manualMatrixUpdateCount = 0;
|
||||
this.fastObjParseCount = 0;
|
||||
this.fastObjLineScanCount = 0;
|
||||
this.fastObjStreamChunkCount = 0;
|
||||
this.fastObjStreamByteCount = 0;
|
||||
this.objLoaderFallbackCount = 0;
|
||||
this.sceneObjectBounds.makeEmpty();
|
||||
this.sceneObjectBoundsCount = 0;
|
||||
@ -2914,8 +2938,7 @@ export class UniversalPreviewer {
|
||||
if (!response.ok) {
|
||||
throw new Error(`OBJ fetch failed ${response.status}`);
|
||||
}
|
||||
const text = await response.text();
|
||||
const parsed = this.parseObjMesh(text, material);
|
||||
const parsed = await this.parseObjMeshResponse(response, material);
|
||||
parsed.name = `fast_obj:${url}`;
|
||||
this.fastObjParseCount += 1;
|
||||
return parsed;
|
||||
@ -2936,93 +2959,153 @@ export class UniversalPreviewer {
|
||||
}
|
||||
}
|
||||
|
||||
private parseObjMesh(text: string, material: THREE.Material): THREE.Object3D {
|
||||
const sourcePositions: number[] = [];
|
||||
const sourceNormals: number[] = [];
|
||||
const sourceUvs: number[] = [];
|
||||
const positions: number[] = [];
|
||||
const normals: number[] = [];
|
||||
const uvs: number[] = [];
|
||||
let positionCount = 0;
|
||||
let sourceNormalCount = 0;
|
||||
let sourceUvCount = 0;
|
||||
let normalCount = 0;
|
||||
let missingNormalCount = 0;
|
||||
let uvCount = 0;
|
||||
let missingUvCount = 0;
|
||||
let faceVertexCount = 0;
|
||||
const appendRef = (ref: ObjVertexRef): void => {
|
||||
this.pushObjVertex(ref, sourcePositions, sourceUvs, sourceNormals, positions, uvs, normals);
|
||||
if (ref.normal === undefined) {
|
||||
missingNormalCount += 1;
|
||||
} else {
|
||||
normalCount += 1;
|
||||
private async parseObjMeshResponse(response: Response, material: THREE.Material): Promise<THREE.Object3D> {
|
||||
if (!response.body) {
|
||||
return this.parseObjMeshText(await response.text(), material);
|
||||
}
|
||||
const state = this.createObjParseState();
|
||||
const reader = response.body.getReader();
|
||||
const decoder = new TextDecoder();
|
||||
let pending = "";
|
||||
let streamChunkCount = 0;
|
||||
let streamByteCount = 0;
|
||||
try {
|
||||
while (true) {
|
||||
const { value, done } = await reader.read();
|
||||
if (done) {
|
||||
break;
|
||||
}
|
||||
streamChunkCount += 1;
|
||||
streamByteCount += value.byteLength;
|
||||
const text = pending + decoder.decode(value, { stream: true });
|
||||
let lineStart = 0;
|
||||
while (lineStart < text.length) {
|
||||
const lineEnd = text.indexOf("\n", lineStart);
|
||||
if (lineEnd === -1) {
|
||||
break;
|
||||
}
|
||||
this.processObjLine(text.slice(lineStart, lineEnd), state);
|
||||
lineStart = lineEnd + 1;
|
||||
}
|
||||
pending = text.slice(lineStart);
|
||||
}
|
||||
if (ref.uv === undefined) {
|
||||
missingUvCount += 1;
|
||||
} else {
|
||||
uvCount += 1;
|
||||
const remainder = pending + decoder.decode();
|
||||
if (remainder.length > 0) {
|
||||
this.processObjLine(remainder, state);
|
||||
}
|
||||
faceVertexCount += 1;
|
||||
};
|
||||
this.fastObjStreamChunkCount += streamChunkCount;
|
||||
this.fastObjStreamByteCount += streamByteCount;
|
||||
return this.finishObjParse(state, material);
|
||||
} finally {
|
||||
reader.releaseLock();
|
||||
}
|
||||
}
|
||||
|
||||
private parseObjMeshText(text: string, material: THREE.Material): THREE.Object3D {
|
||||
const state = this.createObjParseState();
|
||||
let lineStart = 0;
|
||||
let scannedLineCount = 0;
|
||||
while (lineStart <= text.length) {
|
||||
let lineEnd = text.indexOf("\n", lineStart);
|
||||
if (lineEnd === -1) {
|
||||
lineEnd = text.length;
|
||||
}
|
||||
const rawLine = text.charCodeAt(lineEnd - 1) === 13
|
||||
? text.slice(lineStart, lineEnd - 1)
|
||||
: text.slice(lineStart, lineEnd);
|
||||
this.processObjLine(text.slice(lineStart, lineEnd), state);
|
||||
lineStart = lineEnd + 1;
|
||||
scannedLineCount += 1;
|
||||
const line = rawLine.trim();
|
||||
if (line.length === 0 || line.startsWith("#")) {
|
||||
continue;
|
||||
}
|
||||
return this.finishObjParse(state, material);
|
||||
}
|
||||
|
||||
private createObjParseState(): ObjParseState {
|
||||
return {
|
||||
sourcePositions: [],
|
||||
sourceNormals: [],
|
||||
sourceUvs: [],
|
||||
positions: [],
|
||||
normals: [],
|
||||
uvs: [],
|
||||
positionCount: 0,
|
||||
sourceNormalCount: 0,
|
||||
sourceUvCount: 0,
|
||||
normalCount: 0,
|
||||
missingNormalCount: 0,
|
||||
uvCount: 0,
|
||||
missingUvCount: 0,
|
||||
faceVertexCount: 0,
|
||||
lineCount: 0,
|
||||
};
|
||||
}
|
||||
|
||||
private processObjLine(rawLine: string, state: ObjParseState): void {
|
||||
const line = (rawLine.endsWith("\r") ? rawLine.slice(0, -1) : rawLine).trim();
|
||||
state.lineCount += 1;
|
||||
if (line.length === 0 || line.startsWith("#")) {
|
||||
return;
|
||||
}
|
||||
const firstSpace = line.search(/\s/);
|
||||
const keyword = firstSpace === -1 ? line : line.slice(0, firstSpace);
|
||||
const body = firstSpace === -1 ? "" : line.slice(firstSpace + 1).trim();
|
||||
if (keyword === "v") {
|
||||
this.pushObjNumbers(body, 3, state.sourcePositions);
|
||||
state.positionCount += 1;
|
||||
} else if (keyword === "vn") {
|
||||
this.pushObjNumbers(body, 3, state.sourceNormals);
|
||||
state.sourceNormalCount += 1;
|
||||
} else if (keyword === "vt") {
|
||||
this.pushObjNumbers(body, 2, state.sourceUvs);
|
||||
state.sourceUvCount += 1;
|
||||
} else if (keyword === "f") {
|
||||
const rawRefs = body.split(/\s+/).filter(Boolean);
|
||||
if (rawRefs.length < 3) {
|
||||
return;
|
||||
}
|
||||
const firstSpace = line.search(/\s/);
|
||||
const keyword = firstSpace === -1 ? line : line.slice(0, firstSpace);
|
||||
const body = firstSpace === -1 ? "" : line.slice(firstSpace + 1).trim();
|
||||
if (keyword === "v") {
|
||||
this.pushObjNumbers(body, 3, sourcePositions);
|
||||
positionCount += 1;
|
||||
} else if (keyword === "vn") {
|
||||
this.pushObjNumbers(body, 3, sourceNormals);
|
||||
sourceNormalCount += 1;
|
||||
} else if (keyword === "vt") {
|
||||
this.pushObjNumbers(body, 2, sourceUvs);
|
||||
sourceUvCount += 1;
|
||||
} else if (keyword === "f") {
|
||||
const rawRefs = body.split(/\s+/).filter(Boolean);
|
||||
if (rawRefs.length < 3) {
|
||||
continue;
|
||||
}
|
||||
const firstRef = this.parseObjVertexRef(rawRefs[0]!, positionCount, sourceUvCount, sourceNormalCount);
|
||||
let previousRef = this.parseObjVertexRef(rawRefs[1]!, positionCount, sourceUvCount, sourceNormalCount);
|
||||
for (let index = 2; index < rawRefs.length; index += 1) {
|
||||
const nextRef = this.parseObjVertexRef(rawRefs[index]!, positionCount, sourceUvCount, sourceNormalCount);
|
||||
appendRef(firstRef);
|
||||
appendRef(previousRef);
|
||||
appendRef(nextRef);
|
||||
previousRef = nextRef;
|
||||
}
|
||||
const firstRef = this.parseObjVertexRef(rawRefs[0]!, state.positionCount, state.sourceUvCount, state.sourceNormalCount);
|
||||
let previousRef = this.parseObjVertexRef(rawRefs[1]!, state.positionCount, state.sourceUvCount, state.sourceNormalCount);
|
||||
for (let index = 2; index < rawRefs.length; index += 1) {
|
||||
const nextRef = this.parseObjVertexRef(rawRefs[index]!, state.positionCount, state.sourceUvCount, state.sourceNormalCount);
|
||||
this.appendObjRef(firstRef, state);
|
||||
this.appendObjRef(previousRef, state);
|
||||
this.appendObjRef(nextRef, state);
|
||||
previousRef = nextRef;
|
||||
}
|
||||
}
|
||||
this.fastObjLineScanCount += scannedLineCount;
|
||||
}
|
||||
|
||||
if (faceVertexCount === 0 || positions.length === 0) {
|
||||
private appendObjRef(ref: ObjVertexRef, state: ObjParseState): void {
|
||||
this.pushObjVertex(
|
||||
ref,
|
||||
state.sourcePositions,
|
||||
state.sourceUvs,
|
||||
state.sourceNormals,
|
||||
state.positions,
|
||||
state.uvs,
|
||||
state.normals,
|
||||
);
|
||||
if (ref.normal === undefined) {
|
||||
state.missingNormalCount += 1;
|
||||
} else {
|
||||
state.normalCount += 1;
|
||||
}
|
||||
if (ref.uv === undefined) {
|
||||
state.missingUvCount += 1;
|
||||
} else {
|
||||
state.uvCount += 1;
|
||||
}
|
||||
state.faceVertexCount += 1;
|
||||
}
|
||||
|
||||
private finishObjParse(state: ObjParseState, material: THREE.Material): THREE.Object3D {
|
||||
this.fastObjLineScanCount += state.lineCount;
|
||||
if (state.faceVertexCount === 0 || state.positions.length === 0) {
|
||||
throw new Error("OBJ has no faces");
|
||||
}
|
||||
|
||||
const geometry = new THREE.BufferGeometry();
|
||||
geometry.setAttribute("position", new THREE.Float32BufferAttribute(positions, 3));
|
||||
if (uvCount > 0 && missingUvCount === 0) {
|
||||
geometry.setAttribute("uv", new THREE.Float32BufferAttribute(uvs, 2));
|
||||
geometry.setAttribute("position", new THREE.Float32BufferAttribute(state.positions, 3));
|
||||
if (state.uvCount > 0 && state.missingUvCount === 0) {
|
||||
geometry.setAttribute("uv", new THREE.Float32BufferAttribute(state.uvs, 2));
|
||||
}
|
||||
if (normalCount > 0 && missingNormalCount === 0) {
|
||||
geometry.setAttribute("normal", new THREE.Float32BufferAttribute(normals, 3));
|
||||
if (state.normalCount > 0 && state.missingNormalCount === 0) {
|
||||
geometry.setAttribute("normal", new THREE.Float32BufferAttribute(state.normals, 3));
|
||||
} else {
|
||||
geometry.computeVertexNormals();
|
||||
}
|
||||
|
||||
@ -254,6 +254,8 @@ export interface RendererStats {
|
||||
meshCacheMissCount: number;
|
||||
fastObjParseCount: number;
|
||||
fastObjLineScanCount: number;
|
||||
fastObjStreamChunkCount: number;
|
||||
fastObjStreamByteCount: number;
|
||||
objLoaderFallbackCount: number;
|
||||
retainedMeshGeometryCount: number;
|
||||
retainedMeshMaterialCount: number;
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user