@haiyue/extensions 0.1.0 → 0.2.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 +25 -0
- package/dist/animation/Animation2DComponent.d.ts +18 -0
- package/dist/animation/Animation2DExtensionRegistry.d.ts +7 -0
- package/dist/animation/Animation2DRenderSystem.d.ts +6 -0
- package/dist/animation/Animation2DRuntime.d.ts +6 -2
- package/dist/animation/AnimationTextRasterizer.d.ts +4 -0
- package/dist/animation/AnimationVisual2D.d.ts +58 -2
- package/dist/animation/interaction/InteractionRuntime.d.ts +83 -0
- package/dist/animation/interaction/runtime-types.d.ts +257 -0
- package/dist/animation.d.ts +3 -1
- package/dist/animation.js +551 -59
- package/dist/animation3d.js +1 -1
- package/dist/canvas-text.js +1 -1
- package/dist/chunks/2d-ui-animation-2d.generated-FpziAeGJ.js +4 -0
- package/dist/chunks/2d-ui-indexed-sprite.generated-DmKR0CC3.js +4 -0
- package/dist/chunks/Animation2DStateMachine-BHcA6Cu3.js +18212 -0
- package/dist/chunks/{AnimationStateMachineController-BIcAh-sT.js → AnimationStateMachineController-4sKaqh9E.js} +2 -2
- package/dist/chunks/AnimationVisual2D-8y3qp0_U.js +230 -0
- package/dist/chunks/{RenderSystem2DBase-DDT61dAu.js → RenderSystem2DBase-CF2s00vg.js} +2 -2
- package/dist/controls/VirtualJoystickControls.d.ts +46 -0
- package/dist/controls/contracts.d.ts +91 -0
- package/dist/controls.d.ts +2 -0
- package/dist/controls.js +358 -0
- package/dist/deformable-animation/runtime/DeformableMesh2DRuntimeExtension.d.ts +11 -0
- package/dist/deformable-animation/runtime/DeformableMesh2DSampler.d.ts +11 -0
- package/dist/deformable-animation.d.ts +5 -0
- package/dist/deformable-animation.js +316 -0
- package/dist/experimental-indexed-sprite.d.ts +5 -0
- package/dist/experimental-indexed-sprite.js +482 -0
- package/dist/gltf-worker-runtime.js +9 -9
- package/dist/hya-state-machine.js +3 -2
- package/dist/index.js +1 -1
- package/dist/indexed-sprite/AtlasLayout.d.ts +2 -0
- package/dist/indexed-sprite/IndexedSpriteRenderer.d.ts +23 -0
- package/dist/indexed-sprite/contracts.d.ts +89 -0
- package/dist/internal/2d-ui-shader-artifact.js +232 -23
- package/dist/ray-tracing/acceleration/bvh.d.ts +37 -0
- package/dist/ray-tracing/acceleration/index.d.ts +6 -0
- package/dist/ray-tracing/acceleration/pack.d.ts +152 -0
- package/dist/ray-tracing/acceleration/query.d.ts +4 -0
- package/dist/ray-tracing/acceleration/runtime.d.ts +30 -0
- package/dist/ray-tracing/acceleration/tlas.d.ts +4 -0
- package/dist/ray-tracing/acceleration/types.d.ts +165 -0
- package/dist/ray-tracing/denoise/index.d.ts +3 -0
- package/dist/ray-tracing/denoise/layout.d.ts +9 -0
- package/dist/ray-tracing/denoise/runtime.d.ts +23 -0
- package/dist/ray-tracing/denoise/types.d.ts +28 -0
- package/dist/ray-tracing/hybrid/contract.d.ts +2 -0
- package/dist/ray-tracing/hybrid/index.d.ts +5 -0
- package/dist/ray-tracing/hybrid/layout.d.ts +17 -0
- package/dist/ray-tracing/hybrid/plan.d.ts +2 -0
- package/dist/ray-tracing/hybrid/runtime.d.ts +31 -0
- package/dist/ray-tracing/hybrid/shaders/hybrid-artifact.generated.d.ts +625 -0
- package/dist/ray-tracing/hybrid/types.d.ts +144 -0
- package/dist/ray-tracing/lifecycle/device-recovery.d.ts +31 -0
- package/dist/ray-tracing/lifecycle/index.d.ts +2 -0
- package/dist/ray-tracing/lifecycle/types.d.ts +37 -0
- package/dist/ray-tracing/material/index.d.ts +2 -0
- package/dist/ray-tracing/material/pack.d.ts +5 -0
- package/dist/ray-tracing/material/types.d.ts +63 -0
- package/dist/ray-tracing/reference/index.d.ts +122 -0
- package/dist/ray-tracing/renderer/index.d.ts +7 -0
- package/dist/ray-tracing/renderer/layout.d.ts +20 -0
- package/dist/ray-tracing/renderer/plan.d.ts +2 -0
- package/dist/ray-tracing/renderer/reference.d.ts +17 -0
- package/dist/ray-tracing/renderer/runtime.d.ts +39 -0
- package/dist/ray-tracing/renderer/scene.d.ts +4 -0
- package/dist/ray-tracing/renderer/types.d.ts +115 -0
- package/dist/ray-tracing/sampling/index.d.ts +8 -0
- package/dist/ray-tracing/sampling/layout.d.ts +12 -0
- package/dist/ray-tracing/sampling/plan.d.ts +5 -0
- package/dist/ray-tracing/sampling/revision.d.ts +15 -0
- package/dist/ray-tracing/sampling/runtime.d.ts +44 -0
- package/dist/ray-tracing/sampling/sequence.d.ts +2 -0
- package/dist/ray-tracing/sampling/types.d.ts +96 -0
- package/dist/ray-tracing/scene/index.d.ts +41 -0
- package/dist/ray-tracing/traversal/index.d.ts +4 -0
- package/dist/ray-tracing/traversal/layout.d.ts +16 -0
- package/dist/ray-tracing/traversal/plan.d.ts +2 -0
- package/dist/ray-tracing/traversal/runtime.d.ts +25 -0
- package/dist/ray-tracing/traversal/types.d.ts +79 -0
- package/dist/ray-tracing/worker/client.d.ts +25 -0
- package/dist/ray-tracing/worker/index.d.ts +5 -0
- package/dist/ray-tracing/worker/runtime.d.ts +11 -0
- package/dist/ray-tracing/worker/serialization.d.ts +16 -0
- package/dist/ray-tracing/worker/source.d.ts +1 -0
- package/dist/ray-tracing/worker/types.d.ts +91 -0
- package/dist/ray-tracing.d.ts +17 -0
- package/dist/ray-tracing.js +7666 -0
- package/dist/spine.js +1 -1
- package/dist/tilemap.js +1 -1
- package/package.json +26 -23
- package/dist/animation/Animation2DStateMachineVisualRuntime.d.ts +0 -27
- package/dist/animation/Particle2DDescriptorAdapter.d.ts +0 -5
- package/dist/animation3d/Animation3DResource.d.ts +0 -28
- package/dist/animation3d/hya/HyaAnimation3DStateMachineRuntime.d.ts +0 -65
- package/dist/animation3d/hya/index.d.ts +0 -4
- package/dist/animation3d/runtime/integration/Animation3DStateMachineMixerAdapter.d.ts +0 -81
- package/dist/animation3d/runtime/integration/Animation3DStateMachineMixerIntegration.d.ts +0 -39
- package/dist/animation3d/runtime/integration/index.d.ts +0 -2
- package/dist/animation3d/runtime/mixer/Action.d.ts +0 -150
- package/dist/animation3d/runtime/mixer/Animation3DMixerRuntimeStore.d.ts +0 -6
- package/dist/animation3d/runtime/mixer/Mixer.d.ts +0 -59
- package/dist/animation3d/runtime/mixer/PoseBuffer.d.ts +0 -1
- package/dist/animation3d/runtime/mixer/TrackSampler.d.ts +0 -24
- package/dist/animation3d/runtime/mixer/index.d.ts +0 -4
- package/dist/animation3d/runtime/state-machine/Animation3DBlendTreeWeights.d.ts +0 -1
- package/dist/animation3d/runtime/state-machine/Animation3DStateMachineCompiler.d.ts +0 -1
- package/dist/animation3d/runtime/state-machine/Animation3DStateMachineController.d.ts +0 -1
- package/dist/animation3d/runtime/state-machine/Animation3DStateMachineMixerPort.d.ts +0 -1
- package/dist/animation3d/runtime/state-machine/index.d.ts +0 -6
- package/dist/benchmark.d.ts +0 -30
- package/dist/chunks/2d-ui-animation-2d.generated-D4aHzFU2.js +0 -4
- package/dist/chunks/Animation2DStateMachine-9IonguNa.js +0 -3245
- package/dist/gltf/GltfAccessorReader.d.ts +0 -7
- package/dist/gltf/GltfDracoDecoder.d.ts +0 -4
- package/dist/gltf/GltfLoaderErrors.d.ts +0 -4
- package/dist/gltf/GltfMaterialDescriptor.d.ts +0 -23
- package/dist/gltf/GltfMaterialLoader.d.ts +0 -6
- package/dist/gltf-worker-runtime.d.ts +0 -7
- package/dist/plugins/pluginUtils.d.ts +0 -20
- package/dist/plugins.d.ts +0 -6
- package/dist/shaders/generated/2d-ui-artifact.generated.d.ts +0 -658
- package/dist/spine/Spine2DGpuRenderer.d.ts +0 -65
- package/dist/spine/Spine2DRuntime.d.ts +0 -103
- package/dist/spine/SpineAnimationBoneTimelines.d.ts +0 -8
- package/dist/spine/SpineFloatBuilder.d.ts +0 -15
- package/dist/spine/SpinePathConstraintSolver.d.ts +0 -32
- package/dist/spine/SpineSkeletonRuntime.d.ts +0 -185
- package/dist/spine/SpineSlotAnimation.d.ts +0 -17
- package/dist/spine/SpineTimelineRuntime.d.ts +0 -25
- package/dist/spine/SpineVertexBuilder.d.ts +0 -61
- package/dist/tilemap/Tilemap2DRenderer.d.ts +0 -24
- package/dist/utils/arrayAccess.d.ts +0 -4
- package/dist/utils/camera2d.d.ts +0 -12
- package/src/animation/Animation2DComponent.ts +0 -135
- package/src/animation/Animation2DExtensionRegistry.ts +0 -52
- package/src/animation/Animation2DRenderSystem.ts +0 -1026
- package/src/animation/Animation2DRuntime.ts +0 -1326
- package/src/animation/Animation2DStateMachine.ts +0 -39
- package/src/animation/Animation2DStateMachineComponent.ts +0 -228
- package/src/animation/Animation2DStateMachineRuntime.ts +0 -141
- package/src/animation/Animation2DStateMachineSystem.ts +0 -60
- package/src/animation/Animation2DStateMachineVisualRuntime.ts +0 -282
- package/src/animation/Animation2DSystem.ts +0 -72
- package/src/animation/AnimationAssetLoader.ts +0 -84
- package/src/animation/AnimationAudioClip.ts +0 -140
- package/src/animation/AnimationPathTessellator.ts +0 -691
- package/src/animation/AnimationTextRasterizer.ts +0 -340
- package/src/animation/AnimationVisual2D.ts +0 -153
- package/src/animation/HyaAnimation2DClipAdapter.ts +0 -845
- package/src/animation/Particle2DDescriptorAdapter.ts +0 -35
- package/src/animation/runtime/mixer/Action.ts +0 -876
- package/src/animation/runtime/mixer/Mixer.ts +0 -505
- package/src/animation/runtime/mixer/PoseBuffer.ts +0 -105
- package/src/animation/runtime/mixer/Sampler.ts +0 -252
- package/src/animation/runtime/mixer/Types.ts +0 -138
- package/src/animation/runtime/mixer/index.ts +0 -31
- package/src/animation-state-machine/AnimationStateMachine.ts +0 -146
- package/src/animation-state-machine/AnimationStateMachineChannels.ts +0 -116
- package/src/animation-state-machine/runtime/Animation2DStateMachineMixerAdapter.ts +0 -197
- package/src/animation-state-machine/runtime/AnimationStateMachineBlendTreeWeights.ts +0 -503
- package/src/animation-state-machine/runtime/AnimationStateMachineCompiler.ts +0 -763
- package/src/animation-state-machine/runtime/AnimationStateMachineController.ts +0 -987
- package/src/animation-state-machine/runtime/AnimationStateMachineMixerPort.ts +0 -54
- package/src/animation-state-machine/runtime/index.ts +0 -47
- package/src/animation.ts +0 -33
- package/src/animation3d/Animation3DAction.ts +0 -73
- package/src/animation3d/Animation3DBinding.ts +0 -116
- package/src/animation3d/Animation3DClip.ts +0 -22
- package/src/animation3d/Animation3DError.ts +0 -34
- package/src/animation3d/Animation3DMixer.ts +0 -82
- package/src/animation3d/Animation3DPose.ts +0 -100
- package/src/animation3d/Animation3DPoseBuffer.ts +0 -85
- package/src/animation3d/Animation3DResource.ts +0 -49
- package/src/animation3d/Animation3DStateMachine.ts +0 -126
- package/src/animation3d/Animation3DStateMachineRuntime.ts +0 -191
- package/src/animation3d/Animation3DTrack.ts +0 -29
- package/src/animation3d/hya/HyaAnimation3DRuntime.ts +0 -756
- package/src/animation3d/hya/HyaAnimation3DStateMachineRuntime.ts +0 -618
- package/src/animation3d/hya/HyaAnimation3DTypes.ts +0 -168
- package/src/animation3d/hya/index.ts +0 -10
- package/src/animation3d/index.ts +0 -76
- package/src/animation3d/runtime/integration/Animation3DStateMachineMixerAdapter.ts +0 -325
- package/src/animation3d/runtime/integration/Animation3DStateMachineMixerIntegration.ts +0 -158
- package/src/animation3d/runtime/integration/index.ts +0 -9
- package/src/animation3d/runtime/mixer/Action.ts +0 -833
- package/src/animation3d/runtime/mixer/Animation3DMixerRuntimeStore.ts +0 -26
- package/src/animation3d/runtime/mixer/Mixer.ts +0 -562
- package/src/animation3d/runtime/mixer/PoseBuffer.ts +0 -1
- package/src/animation3d/runtime/mixer/TrackSampler.ts +0 -399
- package/src/animation3d/runtime/mixer/index.ts +0 -8
- package/src/animation3d/runtime/state-machine/Animation3DBlendTreeWeights.ts +0 -4
- package/src/animation3d/runtime/state-machine/Animation3DStateMachineCompiler.ts +0 -1
- package/src/animation3d/runtime/state-machine/Animation3DStateMachineController.ts +0 -1
- package/src/animation3d/runtime/state-machine/Animation3DStateMachineMixerPort.ts +0 -1
- package/src/animation3d/runtime/state-machine/index.ts +0 -45
- package/src/animation3d.ts +0 -5
- package/src/benchmark.ts +0 -168
- package/src/canvas-text/CanvasText2DRenderSystem.ts +0 -292
- package/src/canvas-text/CanvasTextComponent.ts +0 -47
- package/src/canvas-text.ts +0 -4
- package/src/experimental-gltf-worker.ts +0 -18
- package/src/experimental-spine-worker.ts +0 -9
- package/src/gltf/GltfAccessorReader.ts +0 -271
- package/src/gltf/GltfAnimation3DAdapter.ts +0 -680
- package/src/gltf/GltfAnimationRuntime.ts +0 -392
- package/src/gltf/GltfAssetWorkerClient.ts +0 -136
- package/src/gltf/GltfCompatibilityReport.ts +0 -214
- package/src/gltf/GltfConservativeBounds.ts +0 -219
- package/src/gltf/GltfDracoDecoder.ts +0 -251
- package/src/gltf/GltfExtensionAdapter.ts +0 -340
- package/src/gltf/GltfLoaderContract.ts +0 -333
- package/src/gltf/GltfLoaderErrors.ts +0 -59
- package/src/gltf/GltfMaterialDescriptor.ts +0 -134
- package/src/gltf/GltfMaterialEncoder.ts +0 -173
- package/src/gltf/GltfMaterialLoader.ts +0 -293
- package/src/gltf/GltfModelComponent.ts +0 -115
- package/src/gltf/GltfModelSystem.ts +0 -208
- package/src/gltf/GltfPlugin.ts +0 -84
- package/src/gltf/GltfSchema.ts +0 -229
- package/src/gltf/GltfUvSemanticPlanner.ts +0 -166
- package/src/gltf/gltfLoader.ts +0 -910
- package/src/gltf-animation3d.ts +0 -10
- package/src/gltf-worker-runtime.ts +0 -10
- package/src/gltf.ts +0 -56
- package/src/grid/Grid2DComponent.ts +0 -69
- package/src/grid.ts +0 -2
- package/src/hya-state-machine.ts +0 -19
- package/src/index.ts +0 -2
- package/src/plugins/pluginUtils.ts +0 -50
- package/src/plugins.ts +0 -6
- package/src/shaders/generated/2d-ui-animation-2d.generated.wgsl +0 -242
- package/src/shaders/generated/2d-ui-artifact.generated.ts +0 -867
- package/src/shaders/generated/2d-ui-canvas-text-2d.generated.wgsl +0 -38
- package/src/shaders/generated/2d-ui-spine2d.generated.wgsl +0 -41
- package/src/shaders/generated/2d-ui-tilemap2d.generated.wgsl +0 -36
- package/src/spine/Spine2DComponent.ts +0 -168
- package/src/spine/Spine2DGpuRenderer.ts +0 -449
- package/src/spine/Spine2DRenderSystem.ts +0 -199
- package/src/spine/Spine2DRuntime.ts +0 -1529
- package/src/spine/SpineAnimationBoneTimelines.ts +0 -30
- package/src/spine/SpineAssetParser.ts +0 -20
- package/src/spine/SpineAssetWorkerClient.ts +0 -112
- package/src/spine/SpineAssetWorkerContract.ts +0 -4
- package/src/spine/SpineAtlasParser.ts +0 -127
- package/src/spine/SpineFloatBuilder.ts +0 -66
- package/src/spine/SpinePathConstraintSolver.ts +0 -582
- package/src/spine/SpinePlugin.ts +0 -76
- package/src/spine/SpineSkeletonRuntime.ts +0 -521
- package/src/spine/SpineSlotAnimation.ts +0 -195
- package/src/spine/SpineTimelineRuntime.ts +0 -453
- package/src/spine/SpineVertexBuilder.ts +0 -686
- package/src/spine.ts +0 -7
- package/src/tilemap/Tilemap2DComponent.ts +0 -232
- package/src/tilemap/Tilemap2DRenderSystem.ts +0 -58
- package/src/tilemap/Tilemap2DRenderer.ts +0 -289
- package/src/tilemap/TilemapPlugin.ts +0 -75
- package/src/tilemap.ts +0 -6
- package/src/tween/Tween2DComponent.ts +0 -83
- package/src/tween/Tween2DSystem.ts +0 -78
- package/src/tween.ts +0 -4
- package/src/types/earcut.d.ts +0 -7
- package/src/types/wgsl.d.ts +0 -4
- package/src/utils/RenderSystem2DBase.ts +0 -164
- package/src/utils/arrayAccess.ts +0 -17
- package/src/utils/camera2d.ts +0 -68
- package/src/utils/render2dGpu.ts +0 -132
- package/test/fixtures/gltf/animation-characterization.gltf +0 -344
|
@@ -1,292 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
beginRenderCommandPass,
|
|
3
|
-
getExtensionGPUResourceTracker,
|
|
4
|
-
requireEngineDevice,
|
|
5
|
-
type IEngine,
|
|
6
|
-
type RenderCommandContext,
|
|
7
|
-
} from '@haiyue/engine/extension-authoring';
|
|
8
|
-
import { Transform2D } from '@haiyue/engine/components';
|
|
9
|
-
import { Entity, type World } from '@haiyue/engine/ecs';
|
|
10
|
-
import { CanvasTextComponent } from './CanvasTextComponent';
|
|
11
|
-
import { RenderSystem2DBase, type RenderSystem2DBaseOptions } from '../utils/RenderSystem2DBase';
|
|
12
|
-
import {
|
|
13
|
-
createCamera2DGpu,
|
|
14
|
-
createObject2DLayout,
|
|
15
|
-
createTexture2DGpu,
|
|
16
|
-
destroyCamera2DGpu,
|
|
17
|
-
destroyTexture2DGpu,
|
|
18
|
-
writeObjectMatrixIfChanged,
|
|
19
|
-
type Camera2DGpu,
|
|
20
|
-
type Object2DGpu,
|
|
21
|
-
type Texture2DGpu,
|
|
22
|
-
} from '../utils/render2dGpu';
|
|
23
|
-
import canvasText2dWgsl from '../shaders/generated/2d-ui-canvas-text-2d.generated.wgsl';
|
|
24
|
-
|
|
25
|
-
export type CanvasText2DRenderSystemOptions = RenderSystem2DBaseOptions;
|
|
26
|
-
|
|
27
|
-
const CANVAS_TEXT_2D_WGSL = canvasText2dWgsl;
|
|
28
|
-
|
|
29
|
-
interface EntityGpu {
|
|
30
|
-
vertexBuffer: GPUBuffer;
|
|
31
|
-
vertexBufferSize: number;
|
|
32
|
-
vertexData: Float32Array;
|
|
33
|
-
vertexDirty: boolean;
|
|
34
|
-
vertexWidth: number;
|
|
35
|
-
vertexHeight: number;
|
|
36
|
-
objectGpu: Object2DGpu;
|
|
37
|
-
texture: GPUTexture | null;
|
|
38
|
-
textureSource: unknown;
|
|
39
|
-
textureVersion: number;
|
|
40
|
-
textureWidth: number;
|
|
41
|
-
textureHeight: number;
|
|
42
|
-
textureBindGroup: GPUBindGroup | null;
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
function textureSize(source: ImageBitmap | HTMLCanvasElement | HTMLImageElement): { width: number; height: number } {
|
|
46
|
-
return {
|
|
47
|
-
width: Math.max(1, source instanceof HTMLImageElement ? source.naturalWidth || source.width : source.width),
|
|
48
|
-
height: Math.max(1, source instanceof HTMLImageElement ? source.naturalHeight || source.height : source.height),
|
|
49
|
-
};
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
export class CanvasText2DRenderSystem extends RenderSystem2DBase {
|
|
53
|
-
private rendererReady = false;
|
|
54
|
-
|
|
55
|
-
private cameraGpu!: Camera2DGpu;
|
|
56
|
-
private objectLayout!: GPUBindGroupLayout;
|
|
57
|
-
private textureGpu!: Texture2DGpu;
|
|
58
|
-
private shaderModule!: GPUShaderModule;
|
|
59
|
-
private pipelineLayout!: GPUPipelineLayout;
|
|
60
|
-
private pipelineCache = new Map<number, GPURenderPipeline>();
|
|
61
|
-
private entityGpu = new Map<number, EntityGpu>();
|
|
62
|
-
|
|
63
|
-
constructor(engine: IEngine, cameraEntity: Entity, options: CanvasText2DRenderSystemOptions = {}) {
|
|
64
|
-
super(
|
|
65
|
-
{ all: [CanvasTextComponent, Transform2D] },
|
|
66
|
-
engine,
|
|
67
|
-
cameraEntity,
|
|
68
|
-
options,
|
|
69
|
-
'CanvasText2DRenderSystem',
|
|
70
|
-
);
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
record(world: World, context: RenderCommandContext): this {
|
|
74
|
-
if (this.disabled) return this;
|
|
75
|
-
const { device } = context;
|
|
76
|
-
if (!this.rendererReady) this.prepare();
|
|
77
|
-
const liveEntities = this.beginLiveEntityTracking();
|
|
78
|
-
|
|
79
|
-
if (!this.writeCameraBuffer(device.queue, this.cameraGpu.buffer, context)) return this;
|
|
80
|
-
|
|
81
|
-
const { passEncoder, ownsPass } = beginRenderCommandPass(context);
|
|
82
|
-
passEncoder.setPipeline(this.getPipeline(
|
|
83
|
-
context.view?.reverseZ ?? this.engine.reverseZ,
|
|
84
|
-
context.view?.sampleCount ?? this.engine.msaaSamples,
|
|
85
|
-
));
|
|
86
|
-
passEncoder.setBindGroup(0, this.cameraGpu.bindGroup);
|
|
87
|
-
|
|
88
|
-
const entities = this.entitySet.get(world);
|
|
89
|
-
if (entities) for (const entity of entities) {
|
|
90
|
-
if (!this.isEntityRenderable(entity)) continue;
|
|
91
|
-
const component = entity.getComponent(CanvasTextComponent);
|
|
92
|
-
if (!component) continue;
|
|
93
|
-
const textureBindGroup = this.getTextureBindGroup(entity, component);
|
|
94
|
-
const gpu = this.getEntityGpu(entity);
|
|
95
|
-
const vertices = this.buildVertices(component, gpu);
|
|
96
|
-
let shouldUploadVertices = gpu.vertexDirty;
|
|
97
|
-
if (vertices.byteLength > gpu.vertexBufferSize) {
|
|
98
|
-
getExtensionGPUResourceTracker(this.engine)?.untrackBuffer(gpu.vertexBuffer);
|
|
99
|
-
gpu.vertexBuffer.destroy();
|
|
100
|
-
gpu.vertexBufferSize = Math.max(vertices.byteLength, gpu.vertexBufferSize * 2);
|
|
101
|
-
gpu.vertexBuffer = device.createBuffer({
|
|
102
|
-
size: gpu.vertexBufferSize,
|
|
103
|
-
usage: GPUBufferUsage.VERTEX | GPUBufferUsage.COPY_DST,
|
|
104
|
-
});
|
|
105
|
-
getExtensionGPUResourceTracker(this.engine)?.trackBuffer(gpu.vertexBuffer, 'CanvasText2D.vertexBuffer', gpu.vertexBufferSize);
|
|
106
|
-
shouldUploadVertices = true;
|
|
107
|
-
}
|
|
108
|
-
if (shouldUploadVertices) {
|
|
109
|
-
device.queue.writeBuffer(gpu.vertexBuffer, 0, vertices.buffer as ArrayBuffer, vertices.byteOffset, vertices.byteLength);
|
|
110
|
-
gpu.vertexDirty = false;
|
|
111
|
-
}
|
|
112
|
-
const worldMatrix = this.getWorldMatrix2D(entity, context);
|
|
113
|
-
writeObjectMatrixIfChanged(device.queue, gpu.objectGpu, worldMatrix);
|
|
114
|
-
passEncoder.setBindGroup(1, gpu.objectGpu.bindGroup);
|
|
115
|
-
passEncoder.setBindGroup(2, textureBindGroup);
|
|
116
|
-
passEncoder.setVertexBuffer(0, gpu.vertexBuffer);
|
|
117
|
-
passEncoder.draw(6);
|
|
118
|
-
this.markEntityLive(entity);
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
if (ownsPass) passEncoder.end();
|
|
122
|
-
this.releaseEntityGpuEntriesNotIn(this.entityGpu, gpu => this.destroyEntityGpu(gpu), liveEntities);
|
|
123
|
-
return this;
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
override destroy(): this {
|
|
127
|
-
this.releaseGpuResourcesForRecovery();
|
|
128
|
-
return super.destroy();
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
protected releaseGpuResourcesForRecovery(): void {
|
|
132
|
-
if (this.cameraGpu) destroyCamera2DGpu(this.cameraGpu);
|
|
133
|
-
if (this.textureGpu) destroyTexture2DGpu(this.textureGpu);
|
|
134
|
-
this.destroyEntityGpuEntries(this.entityGpu, gpu => this.destroyEntityGpu(gpu));
|
|
135
|
-
this.pipelineCache.clear();
|
|
136
|
-
this.rendererReady = false;
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
private prepare(): void {
|
|
140
|
-
this.rendererReady = true;
|
|
141
|
-
const device = requireEngineDevice(this.engine);
|
|
142
|
-
this.cameraGpu = createCamera2DGpu(device, getExtensionGPUResourceTracker(this.engine));
|
|
143
|
-
this.objectLayout = createObject2DLayout(device);
|
|
144
|
-
this.textureGpu = createTexture2DGpu(device, 0, getExtensionGPUResourceTracker(this.engine));
|
|
145
|
-
this.shaderModule = device.createShaderModule({ code: CANVAS_TEXT_2D_WGSL });
|
|
146
|
-
this.pipelineLayout = device.createPipelineLayout({
|
|
147
|
-
bindGroupLayouts: [this.cameraGpu.layout, this.objectLayout, this.textureGpu.layout],
|
|
148
|
-
});
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
private getPipeline(reverseZ: boolean, sampleCount: 1 | 4): GPURenderPipeline {
|
|
152
|
-
const key = (reverseZ ? 1 : 0) | ((sampleCount > 1 ? 1 : 0) << 1);
|
|
153
|
-
const cached = this.pipelineCache.get(key);
|
|
154
|
-
if (cached) return cached;
|
|
155
|
-
|
|
156
|
-
const device = requireEngineDevice(this.engine);
|
|
157
|
-
const { format } = this.engine;
|
|
158
|
-
const pipeline = device.createRenderPipeline({
|
|
159
|
-
layout: this.pipelineLayout,
|
|
160
|
-
vertex: {
|
|
161
|
-
module: this.shaderModule,
|
|
162
|
-
entryPoint: 'vs_main',
|
|
163
|
-
buffers: [{
|
|
164
|
-
arrayStride: 16,
|
|
165
|
-
attributes: [
|
|
166
|
-
{ shaderLocation: 0, offset: 0, format: 'float32x2' },
|
|
167
|
-
{ shaderLocation: 1, offset: 8, format: 'float32x2' },
|
|
168
|
-
],
|
|
169
|
-
}],
|
|
170
|
-
},
|
|
171
|
-
fragment: {
|
|
172
|
-
module: this.shaderModule,
|
|
173
|
-
entryPoint: 'fs_main',
|
|
174
|
-
targets: [{
|
|
175
|
-
format,
|
|
176
|
-
blend: {
|
|
177
|
-
color: { srcFactor: 'src-alpha', dstFactor: 'one-minus-src-alpha', operation: 'add' },
|
|
178
|
-
alpha: { srcFactor: 'one', dstFactor: 'one-minus-src-alpha', operation: 'add' },
|
|
179
|
-
},
|
|
180
|
-
}],
|
|
181
|
-
},
|
|
182
|
-
primitive: { topology: 'triangle-list', cullMode: 'none' },
|
|
183
|
-
depthStencil: {
|
|
184
|
-
format: this.engine.getDepthFormat(reverseZ),
|
|
185
|
-
depthWriteEnabled: false,
|
|
186
|
-
depthCompare: 'always',
|
|
187
|
-
},
|
|
188
|
-
multisample: { count: sampleCount },
|
|
189
|
-
});
|
|
190
|
-
this.pipelineCache.set(key, pipeline);
|
|
191
|
-
return pipeline;
|
|
192
|
-
}
|
|
193
|
-
|
|
194
|
-
private getEntityGpu(entity: Entity): EntityGpu {
|
|
195
|
-
return this.getOrCreateEntityGpu(this.entityGpu, entity, () => {
|
|
196
|
-
const device = requireEngineDevice(this.engine);
|
|
197
|
-
const gpu = {
|
|
198
|
-
vertexBuffer: device.createBuffer({ size: 96, usage: GPUBufferUsage.VERTEX | GPUBufferUsage.COPY_DST }),
|
|
199
|
-
vertexBufferSize: 96,
|
|
200
|
-
vertexData: new Float32Array(24),
|
|
201
|
-
vertexDirty: true,
|
|
202
|
-
vertexWidth: 0,
|
|
203
|
-
vertexHeight: 0,
|
|
204
|
-
objectGpu: this.createObjectGpu(this.objectLayout),
|
|
205
|
-
texture: null,
|
|
206
|
-
textureSource: null,
|
|
207
|
-
textureVersion: -1,
|
|
208
|
-
textureWidth: 0,
|
|
209
|
-
textureHeight: 0,
|
|
210
|
-
textureBindGroup: null,
|
|
211
|
-
};
|
|
212
|
-
getExtensionGPUResourceTracker(this.engine)?.trackBuffer(gpu.vertexBuffer, 'CanvasText2D.vertexBuffer', gpu.vertexBufferSize);
|
|
213
|
-
return gpu;
|
|
214
|
-
});
|
|
215
|
-
}
|
|
216
|
-
|
|
217
|
-
private getTextureBindGroup(entity: Entity, component: CanvasTextComponent): GPUBindGroup {
|
|
218
|
-
const device = requireEngineDevice(this.engine);
|
|
219
|
-
const source = component.material.texture;
|
|
220
|
-
if (!source || !(source instanceof ImageBitmap || source instanceof HTMLCanvasElement || source instanceof HTMLImageElement)) {
|
|
221
|
-
return this.textureGpu.fallbackBindGroup;
|
|
222
|
-
}
|
|
223
|
-
const gpu = this.getEntityGpu(entity);
|
|
224
|
-
const size = textureSize(source);
|
|
225
|
-
const textureVersion = component.material.textureVersion;
|
|
226
|
-
if (
|
|
227
|
-
gpu.textureBindGroup &&
|
|
228
|
-
gpu.textureSource === source &&
|
|
229
|
-
gpu.textureVersion === textureVersion &&
|
|
230
|
-
gpu.textureWidth === size.width &&
|
|
231
|
-
gpu.textureHeight === size.height
|
|
232
|
-
) {
|
|
233
|
-
return gpu.textureBindGroup;
|
|
234
|
-
}
|
|
235
|
-
|
|
236
|
-
const canReuseTexture = gpu.texture && gpu.textureWidth === size.width && gpu.textureHeight === size.height;
|
|
237
|
-
if (!canReuseTexture) {
|
|
238
|
-
if (gpu.texture) getExtensionGPUResourceTracker(this.engine)?.untrackTexture(gpu.texture);
|
|
239
|
-
gpu.texture?.destroy();
|
|
240
|
-
const texture = device.createTexture({
|
|
241
|
-
size: [size.width, size.height],
|
|
242
|
-
format: 'rgba8unorm',
|
|
243
|
-
usage: GPUTextureUsage.TEXTURE_BINDING | GPUTextureUsage.COPY_DST | GPUTextureUsage.RENDER_ATTACHMENT,
|
|
244
|
-
});
|
|
245
|
-
gpu.texture = texture;
|
|
246
|
-
getExtensionGPUResourceTracker(this.engine)?.trackTexture(texture, 'CanvasText2D.texture', size.width * size.height * 4);
|
|
247
|
-
gpu.textureBindGroup = device.createBindGroup({
|
|
248
|
-
layout: this.textureGpu.layout,
|
|
249
|
-
entries: [
|
|
250
|
-
{ binding: 0, resource: texture.createView() },
|
|
251
|
-
{ binding: 1, resource: this.textureGpu.sampler },
|
|
252
|
-
],
|
|
253
|
-
});
|
|
254
|
-
}
|
|
255
|
-
const texture = gpu.texture;
|
|
256
|
-
const textureBindGroup = gpu.textureBindGroup;
|
|
257
|
-
if (!texture || !textureBindGroup) throw new Error('CanvasText2D texture GPU state is incomplete.');
|
|
258
|
-
device.queue.copyExternalImageToTexture({ source }, { texture }, [size.width, size.height]);
|
|
259
|
-
gpu.textureSource = source;
|
|
260
|
-
gpu.textureVersion = textureVersion;
|
|
261
|
-
gpu.textureWidth = size.width;
|
|
262
|
-
gpu.textureHeight = size.height;
|
|
263
|
-
return textureBindGroup;
|
|
264
|
-
}
|
|
265
|
-
|
|
266
|
-
private buildVertices(component: CanvasTextComponent, gpu: EntityGpu): Float32Array {
|
|
267
|
-
const width = Math.max(1, Number(component.style.width ?? 256));
|
|
268
|
-
const height = Math.max(1, Number(component.style.height ?? 96));
|
|
269
|
-
if (gpu.vertexWidth !== width || gpu.vertexHeight !== height) {
|
|
270
|
-
gpu.vertexWidth = width;
|
|
271
|
-
gpu.vertexHeight = height;
|
|
272
|
-
gpu.vertexData.set([
|
|
273
|
-
0, 0, 0, 1,
|
|
274
|
-
width, 0, 1, 1,
|
|
275
|
-
width, height, 1, 0,
|
|
276
|
-
0, 0, 0, 1,
|
|
277
|
-
width, height, 1, 0,
|
|
278
|
-
0, height, 0, 0,
|
|
279
|
-
]);
|
|
280
|
-
gpu.vertexDirty = true;
|
|
281
|
-
}
|
|
282
|
-
return gpu.vertexData;
|
|
283
|
-
}
|
|
284
|
-
|
|
285
|
-
private destroyEntityGpu(gpu: EntityGpu): void {
|
|
286
|
-
getExtensionGPUResourceTracker(this.engine)?.untrackBuffer(gpu.vertexBuffer);
|
|
287
|
-
gpu.vertexBuffer.destroy();
|
|
288
|
-
this.destroyObjectGpu(gpu.objectGpu);
|
|
289
|
-
if (gpu.texture) getExtensionGPUResourceTracker(this.engine)?.untrackTexture(gpu.texture);
|
|
290
|
-
gpu.texture?.destroy();
|
|
291
|
-
}
|
|
292
|
-
}
|
|
@@ -1,47 +0,0 @@
|
|
|
1
|
-
import { Component } from '@haiyue/engine';
|
|
2
|
-
import { UniqueCheckType } from '@haiyue/engine/ecs';
|
|
3
|
-
import { CssMaterial, type CssMaterialStyle } from '@haiyue/engine/material';
|
|
4
|
-
|
|
5
|
-
export interface CanvasTextComponentOptions {
|
|
6
|
-
text?: string;
|
|
7
|
-
style?: CssMaterialStyle;
|
|
8
|
-
material?: CssMaterial;
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
export class CanvasTextComponent extends Component {
|
|
12
|
-
static override UniqueCheckType = UniqueCheckType.SYMBOL | UniqueCheckType.REPLACE;
|
|
13
|
-
static override UniqueSymbol = Symbol.for('CanvasTextComponent');
|
|
14
|
-
|
|
15
|
-
material: CssMaterial;
|
|
16
|
-
|
|
17
|
-
constructor(options: CanvasTextComponentOptions = {}) {
|
|
18
|
-
super('CanvasTextComponent');
|
|
19
|
-
this.material = options.material ?? new CssMaterial({
|
|
20
|
-
...(options.text === undefined ? {} : { text: options.text }),
|
|
21
|
-
...(options.style === undefined ? {} : { style: options.style }),
|
|
22
|
-
});
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
get text(): string {
|
|
26
|
-
return this.material.text;
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
set text(value: string) {
|
|
30
|
-
this.material.setText(value);
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
get style(): CssMaterialStyle {
|
|
34
|
-
return this.material.style;
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
set style(value: CssMaterialStyle) {
|
|
38
|
-
this.material.setStyle(value);
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
override clone(): CanvasTextComponent {
|
|
42
|
-
return new CanvasTextComponent({
|
|
43
|
-
text: this.text,
|
|
44
|
-
style: { ...this.style },
|
|
45
|
-
});
|
|
46
|
-
}
|
|
47
|
-
}
|
package/src/canvas-text.ts
DELETED
|
@@ -1,4 +0,0 @@
|
|
|
1
|
-
export { CanvasTextComponent } from './canvas-text/CanvasTextComponent';
|
|
2
|
-
export type { CanvasTextComponentOptions } from './canvas-text/CanvasTextComponent';
|
|
3
|
-
export { CanvasText2DRenderSystem } from './canvas-text/CanvasText2DRenderSystem';
|
|
4
|
-
export type { CanvasText2DRenderSystemOptions } from './canvas-text/CanvasText2DRenderSystem';
|
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
/** Experimental worker transport and low-level parsing contracts for glTF. */
|
|
2
|
-
export {
|
|
3
|
-
GltfAssetWorkerClient,
|
|
4
|
-
createGltfAssetWorkerClientFromUrl,
|
|
5
|
-
createGltfAssetWorkerSource,
|
|
6
|
-
createInlineGltfAssetWorkerClient,
|
|
7
|
-
} from './gltf/GltfAssetWorkerClient';
|
|
8
|
-
export {
|
|
9
|
-
loadParsedGltfAsset,
|
|
10
|
-
prepareGltfGeometryPayloads,
|
|
11
|
-
} from './gltf/gltfLoader';
|
|
12
|
-
export type {
|
|
13
|
-
GltfAnimationChannelRuntime,
|
|
14
|
-
GltfAnimationTarget,
|
|
15
|
-
GltfGeometryPayloadMatrix,
|
|
16
|
-
GltfParsedAsset,
|
|
17
|
-
GltfPrimitiveGeometryPayload,
|
|
18
|
-
} from './gltf/gltfLoader';
|
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
export { SPINE_ASSET_PARSER, parseSpineAssetPayload } from './spine/SpineAssetParser';
|
|
2
|
-
export type { SpineAssetParserInput, SpineParsedAsset } from './spine/SpineAssetParser';
|
|
3
|
-
export {
|
|
4
|
-
SpineAssetWorkerClient,
|
|
5
|
-
createInlineSpineAssetWorkerClient,
|
|
6
|
-
createSpineAssetWorkerClientFromUrl,
|
|
7
|
-
createSpineAssetWorkerSource,
|
|
8
|
-
} from './spine/SpineAssetWorkerClient';
|
|
9
|
-
export type { SpineAssetWorker } from './spine/SpineAssetWorkerContract';
|
|
@@ -1,271 +0,0 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
GltfAccessor,
|
|
3
|
-
GltfAccessorType,
|
|
4
|
-
GltfAsset,
|
|
5
|
-
GltfBufferView,
|
|
6
|
-
GltfComponentType,
|
|
7
|
-
} from './GltfSchema';
|
|
8
|
-
import { gltfDataError } from './GltfLoaderErrors';
|
|
9
|
-
|
|
10
|
-
const COMPONENT_BYTE_SIZE: Record<GltfComponentType, number> = {
|
|
11
|
-
5120: 1,
|
|
12
|
-
5121: 1,
|
|
13
|
-
5122: 2,
|
|
14
|
-
5123: 2,
|
|
15
|
-
5125: 4,
|
|
16
|
-
5126: 4,
|
|
17
|
-
};
|
|
18
|
-
|
|
19
|
-
const TYPE_SIZE: Record<GltfAccessorType, number> = {
|
|
20
|
-
SCALAR: 1,
|
|
21
|
-
VEC2: 2,
|
|
22
|
-
VEC3: 3,
|
|
23
|
-
VEC4: 4,
|
|
24
|
-
MAT2: 4,
|
|
25
|
-
MAT3: 9,
|
|
26
|
-
MAT4: 16,
|
|
27
|
-
};
|
|
28
|
-
|
|
29
|
-
export function readAccessorFloat(
|
|
30
|
-
gltf: GltfAsset,
|
|
31
|
-
buffers: ArrayBuffer[],
|
|
32
|
-
accessorIndex: number,
|
|
33
|
-
expectedSize: number,
|
|
34
|
-
): Float32Array {
|
|
35
|
-
const accessor = getAccessor(gltf, accessorIndex);
|
|
36
|
-
const itemSize = TYPE_SIZE[accessor.type];
|
|
37
|
-
if (itemSize < expectedSize) throw gltfDataError(`Accessor ${accessorIndex} has ${itemSize} components, expected ${expectedSize}.`);
|
|
38
|
-
const out = new Float32Array(accessor.count * expectedSize);
|
|
39
|
-
readAccessorValues(gltf, buffers, accessor, (values, index) => {
|
|
40
|
-
for (let i = 0; i < expectedSize; i++) out[index * expectedSize + i] = values[i] ?? 0;
|
|
41
|
-
});
|
|
42
|
-
return out;
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
export function readAccessorMat4(gltf: GltfAsset, buffers: ArrayBuffer[], accessorIndex: number): Float32Array[] {
|
|
46
|
-
const accessor = getAccessor(gltf, accessorIndex);
|
|
47
|
-
if (TYPE_SIZE[accessor.type] !== 16) throw gltfDataError(`Accessor ${accessorIndex} is not MAT4.`);
|
|
48
|
-
const out: Float32Array[] = [];
|
|
49
|
-
readAccessorValues(gltf, buffers, accessor, values => {
|
|
50
|
-
const matrix = new Float32Array(16);
|
|
51
|
-
for (let i = 0; i < 16; i++) matrix[i] = values[i] ?? 0;
|
|
52
|
-
out.push(matrix);
|
|
53
|
-
});
|
|
54
|
-
return out;
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
export function readAccessorUnsigned(
|
|
58
|
-
gltf: GltfAsset,
|
|
59
|
-
buffers: ArrayBuffer[],
|
|
60
|
-
accessorIndex: number,
|
|
61
|
-
expectedSize: number,
|
|
62
|
-
): Uint16Array | Uint32Array {
|
|
63
|
-
const accessor = getAccessor(gltf, accessorIndex);
|
|
64
|
-
const itemSize = TYPE_SIZE[accessor.type];
|
|
65
|
-
if (itemSize < expectedSize) throw gltfDataError(`Accessor ${accessorIndex} has ${itemSize} components, expected ${expectedSize}.`);
|
|
66
|
-
const out = accessor.componentType === 5125
|
|
67
|
-
? new Uint32Array(accessor.count * expectedSize)
|
|
68
|
-
: new Uint16Array(accessor.count * expectedSize);
|
|
69
|
-
readAccessorValues(gltf, buffers, accessor, (values, index) => {
|
|
70
|
-
for (let i = 0; i < expectedSize; i++) out[index * expectedSize + i] = values[i] ?? 0;
|
|
71
|
-
});
|
|
72
|
-
return out;
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
export function readAccessorIndices(
|
|
76
|
-
gltf: GltfAsset,
|
|
77
|
-
buffers: ArrayBuffer[],
|
|
78
|
-
accessorIndex: number,
|
|
79
|
-
): Uint16Array | Uint32Array {
|
|
80
|
-
const accessor = getAccessor(gltf, accessorIndex);
|
|
81
|
-
const out = accessor.componentType === 5125 ? new Uint32Array(accessor.count) : new Uint16Array(accessor.count);
|
|
82
|
-
readAccessorValues(gltf, buffers, accessor, (values, index) => {
|
|
83
|
-
out[index] = values[0] ?? 0;
|
|
84
|
-
});
|
|
85
|
-
return out;
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
export function generateFlatNormals(
|
|
89
|
-
positions: Float32Array,
|
|
90
|
-
indices: Uint16Array | Uint32Array | null,
|
|
91
|
-
): Float32Array {
|
|
92
|
-
const normals = new Float32Array(positions.length);
|
|
93
|
-
const triangleCount = indices ? indices.length / 3 : positions.length / 9;
|
|
94
|
-
for (let tri = 0; tri < triangleCount; tri++) {
|
|
95
|
-
const ia = indices ? indices[tri * 3] : tri * 3;
|
|
96
|
-
const ib = indices ? indices[tri * 3 + 1] : tri * 3 + 1;
|
|
97
|
-
const ic = indices ? indices[tri * 3 + 2] : tri * 3 + 2;
|
|
98
|
-
if (ia === undefined || ib === undefined || ic === undefined) continue;
|
|
99
|
-
const ax = requiredFiniteArrayValue(positions, ia * 3, 'POSITION');
|
|
100
|
-
const ay = requiredFiniteArrayValue(positions, ia * 3 + 1, 'POSITION');
|
|
101
|
-
const az = requiredFiniteArrayValue(positions, ia * 3 + 2, 'POSITION');
|
|
102
|
-
const bx = requiredFiniteArrayValue(positions, ib * 3, 'POSITION');
|
|
103
|
-
const by = requiredFiniteArrayValue(positions, ib * 3 + 1, 'POSITION');
|
|
104
|
-
const bz = requiredFiniteArrayValue(positions, ib * 3 + 2, 'POSITION');
|
|
105
|
-
const cx = requiredFiniteArrayValue(positions, ic * 3, 'POSITION');
|
|
106
|
-
const cy = requiredFiniteArrayValue(positions, ic * 3 + 1, 'POSITION');
|
|
107
|
-
const cz = requiredFiniteArrayValue(positions, ic * 3 + 2, 'POSITION');
|
|
108
|
-
const abx = bx - ax, aby = by - ay, abz = bz - az;
|
|
109
|
-
const acx = cx - ax, acy = cy - ay, acz = cz - az;
|
|
110
|
-
const nx = aby * acz - abz * acy;
|
|
111
|
-
const ny = abz * acx - abx * acz;
|
|
112
|
-
const nz = abx * acy - aby * acx;
|
|
113
|
-
const len = Math.sqrt(nx * nx + ny * ny + nz * nz) || 1;
|
|
114
|
-
const nnx = nx / len;
|
|
115
|
-
const nny = ny / len;
|
|
116
|
-
const nnz = nz / len;
|
|
117
|
-
addNormal(normals, ia, nnx, nny, nnz);
|
|
118
|
-
addNormal(normals, ib, nnx, nny, nnz);
|
|
119
|
-
addNormal(normals, ic, nnx, nny, nnz);
|
|
120
|
-
}
|
|
121
|
-
for (let i = 0; i < normals.length; i += 3) {
|
|
122
|
-
const nx = requiredFiniteArrayValue(normals, i, 'generated normals');
|
|
123
|
-
const ny = requiredFiniteArrayValue(normals, i + 1, 'generated normals');
|
|
124
|
-
const nz = requiredFiniteArrayValue(normals, i + 2, 'generated normals');
|
|
125
|
-
const len = Math.sqrt(nx * nx + ny * ny + nz * nz) || 1;
|
|
126
|
-
normals[i] = nx / len;
|
|
127
|
-
normals[i + 1] = ny / len;
|
|
128
|
-
normals[i + 2] = nz / len;
|
|
129
|
-
}
|
|
130
|
-
return normals;
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
function readAccessorValues(
|
|
134
|
-
gltf: GltfAsset,
|
|
135
|
-
buffers: ArrayBuffer[],
|
|
136
|
-
accessor: GltfAccessor,
|
|
137
|
-
write: (values: ArrayLike<number>, index: number) => void,
|
|
138
|
-
): void {
|
|
139
|
-
const itemSize = TYPE_SIZE[accessor.type];
|
|
140
|
-
const componentSize = COMPONENT_BYTE_SIZE[accessor.componentType];
|
|
141
|
-
const values = new Float64Array(itemSize);
|
|
142
|
-
if (accessor.bufferView !== undefined) {
|
|
143
|
-
const view = getBufferView(gltf, accessor.bufferView);
|
|
144
|
-
const dataView = new DataView(getBuffer(buffers, view.buffer));
|
|
145
|
-
const stride = view.byteStride ?? itemSize * componentSize;
|
|
146
|
-
const start = (view.byteOffset ?? 0) + (accessor.byteOffset ?? 0);
|
|
147
|
-
for (let index = 0; index < accessor.count; index++) {
|
|
148
|
-
readAccessorTuple(dataView, start + index * stride, itemSize, componentSize, accessor.componentType, Boolean(accessor.normalized), values);
|
|
149
|
-
write(values, index);
|
|
150
|
-
}
|
|
151
|
-
} else {
|
|
152
|
-
values.fill(0);
|
|
153
|
-
for (let index = 0; index < accessor.count; index++) write(values, index);
|
|
154
|
-
}
|
|
155
|
-
if (accessor.sparse?.count) readSparseAccessorValues(gltf, buffers, accessor, values, write);
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
function readSparseAccessorValues(
|
|
159
|
-
gltf: GltfAsset,
|
|
160
|
-
buffers: ArrayBuffer[],
|
|
161
|
-
accessor: GltfAccessor,
|
|
162
|
-
values: Float64Array,
|
|
163
|
-
write: (values: ArrayLike<number>, index: number) => void,
|
|
164
|
-
): void {
|
|
165
|
-
const sparse = accessor.sparse;
|
|
166
|
-
if (!sparse) return;
|
|
167
|
-
const itemSize = TYPE_SIZE[accessor.type];
|
|
168
|
-
const componentSize = COMPONENT_BYTE_SIZE[accessor.componentType];
|
|
169
|
-
const indexComponentSize = COMPONENT_BYTE_SIZE[sparse.indices.componentType];
|
|
170
|
-
const indicesView = getBufferView(gltf, sparse.indices.bufferView);
|
|
171
|
-
const valuesView = getBufferView(gltf, sparse.values.bufferView);
|
|
172
|
-
const indicesData = new DataView(getBuffer(buffers, indicesView.buffer));
|
|
173
|
-
const valuesData = new DataView(getBuffer(buffers, valuesView.buffer));
|
|
174
|
-
const indicesStart = (indicesView.byteOffset ?? 0) + (sparse.indices.byteOffset ?? 0);
|
|
175
|
-
const valuesStart = (valuesView.byteOffset ?? 0) + (sparse.values.byteOffset ?? 0);
|
|
176
|
-
for (let sparseIndex = 0; sparseIndex < sparse.count; sparseIndex++) {
|
|
177
|
-
const index = readSparseIndex(indicesData, indicesStart + sparseIndex * indexComponentSize, sparse.indices.componentType);
|
|
178
|
-
if (index < 0 || index >= accessor.count) throw gltfDataError(`Sparse accessor index ${index} is out of bounds.`);
|
|
179
|
-
readAccessorTuple(
|
|
180
|
-
valuesData,
|
|
181
|
-
valuesStart + sparseIndex * itemSize * componentSize,
|
|
182
|
-
itemSize,
|
|
183
|
-
componentSize,
|
|
184
|
-
accessor.componentType,
|
|
185
|
-
Boolean(accessor.normalized),
|
|
186
|
-
values,
|
|
187
|
-
);
|
|
188
|
-
write(values, index);
|
|
189
|
-
}
|
|
190
|
-
}
|
|
191
|
-
|
|
192
|
-
function readAccessorTuple(
|
|
193
|
-
dataView: DataView,
|
|
194
|
-
offset: number,
|
|
195
|
-
itemSize: number,
|
|
196
|
-
componentSize: number,
|
|
197
|
-
componentType: GltfComponentType,
|
|
198
|
-
normalized: boolean,
|
|
199
|
-
out: Float64Array,
|
|
200
|
-
): void {
|
|
201
|
-
for (let component = 0; component < itemSize; component++) {
|
|
202
|
-
out[component] = readComponent(dataView, offset + component * componentSize, componentType, normalized);
|
|
203
|
-
}
|
|
204
|
-
}
|
|
205
|
-
|
|
206
|
-
function readSparseIndex(view: DataView, offset: number, componentType: 5121 | 5123 | 5125): number {
|
|
207
|
-
switch (componentType) {
|
|
208
|
-
case 5121: return view.getUint8(offset);
|
|
209
|
-
case 5123: return view.getUint16(offset, true);
|
|
210
|
-
case 5125: return view.getUint32(offset, true);
|
|
211
|
-
}
|
|
212
|
-
}
|
|
213
|
-
|
|
214
|
-
function getBufferView(gltf: GltfAsset, bufferViewIndex: number): GltfBufferView {
|
|
215
|
-
const view = gltf.bufferViews?.[bufferViewIndex];
|
|
216
|
-
if (!view) throw gltfDataError(`Missing bufferView ${bufferViewIndex}.`);
|
|
217
|
-
return view;
|
|
218
|
-
}
|
|
219
|
-
|
|
220
|
-
function getBuffer(buffers: ArrayBuffer[], bufferIndex: number): ArrayBuffer {
|
|
221
|
-
const buffer = buffers[bufferIndex];
|
|
222
|
-
if (!buffer) throw gltfDataError(`Missing buffer ${bufferIndex}.`);
|
|
223
|
-
return buffer;
|
|
224
|
-
}
|
|
225
|
-
|
|
226
|
-
function getAccessor(gltf: GltfAsset, accessorIndex: number): GltfAccessor {
|
|
227
|
-
const accessor = gltf.accessors?.[accessorIndex];
|
|
228
|
-
if (!accessor) throw gltfDataError(`Missing accessor ${accessorIndex}.`);
|
|
229
|
-
return accessor;
|
|
230
|
-
}
|
|
231
|
-
|
|
232
|
-
function readComponent(view: DataView, offset: number, componentType: GltfComponentType, normalized: boolean): number {
|
|
233
|
-
switch (componentType) {
|
|
234
|
-
case 5120: {
|
|
235
|
-
const value = view.getInt8(offset);
|
|
236
|
-
return normalized ? Math.max(value / 127, -1) : value;
|
|
237
|
-
}
|
|
238
|
-
case 5121: {
|
|
239
|
-
const value = view.getUint8(offset);
|
|
240
|
-
return normalized ? value / 255 : value;
|
|
241
|
-
}
|
|
242
|
-
case 5122: {
|
|
243
|
-
const value = view.getInt16(offset, true);
|
|
244
|
-
return normalized ? Math.max(value / 32767, -1) : value;
|
|
245
|
-
}
|
|
246
|
-
case 5123: {
|
|
247
|
-
const value = view.getUint16(offset, true);
|
|
248
|
-
return normalized ? value / 65535 : value;
|
|
249
|
-
}
|
|
250
|
-
case 5125: return view.getUint32(offset, true);
|
|
251
|
-
case 5126: return view.getFloat32(offset, true);
|
|
252
|
-
}
|
|
253
|
-
}
|
|
254
|
-
|
|
255
|
-
function addNormal(normals: Float32Array, index: number, x: number, y: number, z: number): void {
|
|
256
|
-
const offset = index * 3;
|
|
257
|
-
if (offset < 0 || offset + 2 >= normals.length) {
|
|
258
|
-
throw gltfDataError(`Triangle index ${index} is outside the POSITION accessor.`);
|
|
259
|
-
}
|
|
260
|
-
normals[offset] = (normals[offset] ?? 0) + x;
|
|
261
|
-
normals[offset + 1] = (normals[offset + 1] ?? 0) + y;
|
|
262
|
-
normals[offset + 2] = (normals[offset + 2] ?? 0) + z;
|
|
263
|
-
}
|
|
264
|
-
|
|
265
|
-
export function requiredFiniteArrayValue(values: ArrayLike<number>, index: number, label: string): number {
|
|
266
|
-
const value = values[index];
|
|
267
|
-
if (value === undefined || !Number.isFinite(value)) {
|
|
268
|
-
throw gltfDataError(`${label} is missing a finite value at index ${index}.`);
|
|
269
|
-
}
|
|
270
|
-
return value;
|
|
271
|
-
}
|