@haiyue/extensions 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/LICENSE +21 -0
- package/README.md +30 -0
- package/dist/animation/Animation2DComponent.d.ts +46 -0
- package/dist/animation/Animation2DExtensionRegistry.d.ts +27 -0
- package/dist/animation/Animation2DRenderSystem.d.ts +89 -0
- package/dist/animation/Animation2DRuntime.d.ts +99 -0
- package/dist/animation/Animation2DStateMachine.d.ts +13 -0
- package/dist/animation/Animation2DStateMachineComponent.d.ts +54 -0
- package/dist/animation/Animation2DStateMachineRuntime.d.ts +30 -0
- package/dist/animation/Animation2DStateMachineSystem.d.ts +14 -0
- package/dist/animation/Animation2DStateMachineVisualRuntime.d.ts +27 -0
- package/dist/animation/Animation2DSystem.d.ts +14 -0
- package/dist/animation/AnimationAssetLoader.d.ts +9 -0
- package/dist/animation/AnimationAudioClip.d.ts +26 -0
- package/dist/animation/AnimationPathTessellator.d.ts +52 -0
- package/dist/animation/AnimationTextRasterizer.d.ts +21 -0
- package/dist/animation/AnimationVisual2D.d.ts +68 -0
- package/dist/animation/HyaAnimation2DClipAdapter.d.ts +16 -0
- package/dist/animation/Particle2DDescriptorAdapter.d.ts +5 -0
- package/dist/animation/runtime/mixer/Action.d.ts +150 -0
- package/dist/animation/runtime/mixer/Mixer.d.ts +54 -0
- package/dist/animation/runtime/mixer/PoseBuffer.d.ts +23 -0
- package/dist/animation/runtime/mixer/Sampler.d.ts +17 -0
- package/dist/animation/runtime/mixer/Types.d.ts +101 -0
- package/dist/animation/runtime/mixer/index.d.ts +6 -0
- package/dist/animation-state-machine/AnimationStateMachine.d.ts +111 -0
- package/dist/animation-state-machine/AnimationStateMachineChannels.d.ts +23 -0
- package/dist/animation-state-machine/runtime/Animation2DStateMachineMixerAdapter.d.ts +51 -0
- package/dist/animation-state-machine/runtime/AnimationStateMachineBlendTreeWeights.d.ts +14 -0
- package/dist/animation-state-machine/runtime/AnimationStateMachineCompiler.d.ts +109 -0
- package/dist/animation-state-machine/runtime/AnimationStateMachineController.d.ts +103 -0
- package/dist/animation-state-machine/runtime/AnimationStateMachineMixerPort.d.ts +35 -0
- package/dist/animation-state-machine/runtime/index.d.ts +7 -0
- package/dist/animation.d.ts +15 -0
- package/dist/animation.js +1171 -0
- package/dist/animation3d/Animation3DAction.d.ts +46 -0
- package/dist/animation3d/Animation3DBinding.d.ts +85 -0
- package/dist/animation3d/Animation3DClip.d.ts +20 -0
- package/dist/animation3d/Animation3DError.d.ts +13 -0
- package/dist/animation3d/Animation3DMixer.d.ts +27 -0
- package/dist/animation3d/Animation3DPose.d.ts +40 -0
- package/dist/animation3d/Animation3DPoseBuffer.d.ts +24 -0
- package/dist/animation3d/Animation3DResource.d.ts +28 -0
- package/dist/animation3d/Animation3DStateMachine.d.ts +95 -0
- package/dist/animation3d/Animation3DStateMachineRuntime.d.ts +70 -0
- package/dist/animation3d/Animation3DTrack.d.ts +19 -0
- package/dist/animation3d/hya/HyaAnimation3DRuntime.d.ts +73 -0
- package/dist/animation3d/hya/HyaAnimation3DStateMachineRuntime.d.ts +65 -0
- package/dist/animation3d/hya/HyaAnimation3DTypes.d.ts +170 -0
- package/dist/animation3d/hya/index.d.ts +4 -0
- package/dist/animation3d/index.d.ts +14 -0
- package/dist/animation3d/runtime/integration/Animation3DStateMachineMixerAdapter.d.ts +81 -0
- package/dist/animation3d/runtime/integration/Animation3DStateMachineMixerIntegration.d.ts +39 -0
- package/dist/animation3d/runtime/integration/index.d.ts +2 -0
- package/dist/animation3d/runtime/mixer/Action.d.ts +150 -0
- package/dist/animation3d/runtime/mixer/Animation3DMixerRuntimeStore.d.ts +6 -0
- package/dist/animation3d/runtime/mixer/Mixer.d.ts +59 -0
- package/dist/animation3d/runtime/mixer/PoseBuffer.d.ts +1 -0
- package/dist/animation3d/runtime/mixer/TrackSampler.d.ts +24 -0
- package/dist/animation3d/runtime/mixer/index.d.ts +4 -0
- package/dist/animation3d/runtime/state-machine/Animation3DBlendTreeWeights.d.ts +1 -0
- package/dist/animation3d/runtime/state-machine/Animation3DStateMachineCompiler.d.ts +1 -0
- package/dist/animation3d/runtime/state-machine/Animation3DStateMachineController.d.ts +1 -0
- package/dist/animation3d/runtime/state-machine/Animation3DStateMachineMixerPort.d.ts +1 -0
- package/dist/animation3d/runtime/state-machine/index.d.ts +6 -0
- package/dist/animation3d.d.ts +5 -0
- package/dist/animation3d.js +1708 -0
- package/dist/benchmark.d.ts +30 -0
- package/dist/benchmark.js +119 -0
- package/dist/canvas-text/CanvasText2DRenderSystem.d.ts +24 -0
- package/dist/canvas-text/CanvasTextComponent.d.ts +18 -0
- package/dist/canvas-text.d.ts +4 -0
- package/dist/canvas-text.js +276 -0
- package/dist/chunks/2d-ui-animation-2d.generated-D4aHzFU2.js +4 -0
- package/dist/chunks/2d-ui-canvas-text-2d.generated-C0Eaej2m.js +4 -0
- package/dist/chunks/2d-ui-spine2d.generated-CUUi8xjl.js +4 -0
- package/dist/chunks/2d-ui-tilemap2d.generated-gebw4mX7.js +4 -0
- package/dist/chunks/Animation2DStateMachine-9IonguNa.js +3245 -0
- package/dist/chunks/AnimationStateMachineController-BIcAh-sT.js +1438 -0
- package/dist/chunks/GltfAnimation3DAdapter-Cu3rQQsW.js +2240 -0
- package/dist/chunks/GltfAnimationRuntime-CRHsLycN.js +631 -0
- package/dist/chunks/RenderSystem2DBase-DDT61dAu.js +227 -0
- package/dist/chunks/Spine2DRuntime-D9M9LAQR.js +3056 -0
- package/dist/chunks/SpineAssetParser-BSDmPge1.js +378 -0
- package/dist/chunks/gltfLoader-Dm22jLYB.js +2085 -0
- package/dist/chunks/pluginUtils-CvwroRMd.js +33 -0
- package/dist/experimental-gltf-worker.d.ts +4 -0
- package/dist/experimental-gltf-worker.js +138 -0
- package/dist/experimental-spine-worker.d.ts +4 -0
- package/dist/experimental-spine-worker.js +110 -0
- package/dist/gltf/GltfAccessorReader.d.ts +7 -0
- package/dist/gltf/GltfAnimation3DAdapter.d.ts +47 -0
- package/dist/gltf/GltfAnimationRuntime.d.ts +10 -0
- package/dist/gltf/GltfAssetWorkerClient.d.ts +20 -0
- package/dist/gltf/GltfCompatibilityReport.d.ts +70 -0
- package/dist/gltf/GltfConservativeBounds.d.ts +18 -0
- package/dist/gltf/GltfDracoDecoder.d.ts +4 -0
- package/dist/gltf/GltfExtensionAdapter.d.ts +54 -0
- package/dist/gltf/GltfLoaderContract.d.ts +249 -0
- package/dist/gltf/GltfLoaderErrors.d.ts +4 -0
- package/dist/gltf/GltfMaterialDescriptor.d.ts +23 -0
- package/dist/gltf/GltfMaterialEncoder.d.ts +20 -0
- package/dist/gltf/GltfMaterialLoader.d.ts +6 -0
- package/dist/gltf/GltfModelComponent.d.ts +72 -0
- package/dist/gltf/GltfModelSystem.d.ts +37 -0
- package/dist/gltf/GltfPlugin.d.ts +6 -0
- package/dist/gltf/GltfSchema.d.ts +225 -0
- package/dist/gltf/GltfUvSemanticPlanner.d.ts +33 -0
- package/dist/gltf/gltfLoader.d.ts +9 -0
- package/dist/gltf-animation3d.d.ts +3 -0
- package/dist/gltf-animation3d.js +8 -0
- package/dist/gltf-worker-runtime.d.ts +7 -0
- package/dist/gltf-worker-runtime.js +6981 -0
- package/dist/gltf.d.ts +12 -0
- package/dist/gltf.js +517 -0
- package/dist/grid/Grid2DComponent.d.ts +25 -0
- package/dist/grid.d.ts +2 -0
- package/dist/grid.js +56 -0
- package/dist/hya-state-machine.d.ts +7 -0
- package/dist/hya-state-machine.js +2934 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +7 -0
- package/dist/internal/2d-ui-shader-artifact.js +870 -0
- package/dist/plugins/pluginUtils.d.ts +20 -0
- package/dist/plugins.d.ts +6 -0
- package/dist/shaders/generated/2d-ui-artifact.generated.d.ts +658 -0
- package/dist/spine/Spine2DComponent.d.ts +116 -0
- package/dist/spine/Spine2DGpuRenderer.d.ts +65 -0
- package/dist/spine/Spine2DRenderSystem.d.ts +27 -0
- package/dist/spine/Spine2DRuntime.d.ts +103 -0
- package/dist/spine/SpineAnimationBoneTimelines.d.ts +8 -0
- package/dist/spine/SpineAssetParser.d.ts +18 -0
- package/dist/spine/SpineAssetWorkerClient.d.ts +19 -0
- package/dist/spine/SpineAssetWorkerContract.d.ts +6 -0
- package/dist/spine/SpineAtlasParser.d.ts +21 -0
- package/dist/spine/SpineFloatBuilder.d.ts +15 -0
- package/dist/spine/SpinePathConstraintSolver.d.ts +32 -0
- package/dist/spine/SpinePlugin.d.ts +8 -0
- package/dist/spine/SpineSkeletonRuntime.d.ts +185 -0
- package/dist/spine/SpineSlotAnimation.d.ts +17 -0
- package/dist/spine/SpineTimelineRuntime.d.ts +25 -0
- package/dist/spine/SpineVertexBuilder.d.ts +61 -0
- package/dist/spine.d.ts +7 -0
- package/dist/spine.js +578 -0
- package/dist/tilemap/Tilemap2DComponent.d.ts +110 -0
- package/dist/tilemap/Tilemap2DRenderSystem.d.ts +11 -0
- package/dist/tilemap/Tilemap2DRenderer.d.ts +24 -0
- package/dist/tilemap/TilemapPlugin.d.ts +8 -0
- package/dist/tilemap.d.ts +6 -0
- package/dist/tilemap.js +535 -0
- package/dist/tween/Tween2DComponent.d.ts +70 -0
- package/dist/tween/Tween2DSystem.d.ts +12 -0
- package/dist/tween.d.ts +4 -0
- package/dist/tween.js +138 -0
- package/dist/utils/RenderSystem2DBase.d.ts +42 -0
- package/dist/utils/arrayAccess.d.ts +4 -0
- package/dist/utils/camera2d.d.ts +12 -0
- package/dist/utils/render2dGpu.d.ts +32 -0
- package/package.json +124 -0
- package/src/animation/Animation2DComponent.ts +135 -0
- package/src/animation/Animation2DExtensionRegistry.ts +52 -0
- package/src/animation/Animation2DRenderSystem.ts +1026 -0
- package/src/animation/Animation2DRuntime.ts +1326 -0
- package/src/animation/Animation2DStateMachine.ts +39 -0
- package/src/animation/Animation2DStateMachineComponent.ts +228 -0
- package/src/animation/Animation2DStateMachineRuntime.ts +141 -0
- package/src/animation/Animation2DStateMachineSystem.ts +60 -0
- package/src/animation/Animation2DStateMachineVisualRuntime.ts +282 -0
- package/src/animation/Animation2DSystem.ts +72 -0
- package/src/animation/AnimationAssetLoader.ts +84 -0
- package/src/animation/AnimationAudioClip.ts +140 -0
- package/src/animation/AnimationPathTessellator.ts +691 -0
- package/src/animation/AnimationTextRasterizer.ts +340 -0
- package/src/animation/AnimationVisual2D.ts +153 -0
- package/src/animation/HyaAnimation2DClipAdapter.ts +845 -0
- package/src/animation/Particle2DDescriptorAdapter.ts +35 -0
- package/src/animation/runtime/mixer/Action.ts +876 -0
- package/src/animation/runtime/mixer/Mixer.ts +505 -0
- package/src/animation/runtime/mixer/PoseBuffer.ts +105 -0
- package/src/animation/runtime/mixer/Sampler.ts +252 -0
- package/src/animation/runtime/mixer/Types.ts +138 -0
- package/src/animation/runtime/mixer/index.ts +31 -0
- package/src/animation-state-machine/AnimationStateMachine.ts +146 -0
- package/src/animation-state-machine/AnimationStateMachineChannels.ts +116 -0
- package/src/animation-state-machine/runtime/Animation2DStateMachineMixerAdapter.ts +197 -0
- package/src/animation-state-machine/runtime/AnimationStateMachineBlendTreeWeights.ts +503 -0
- package/src/animation-state-machine/runtime/AnimationStateMachineCompiler.ts +763 -0
- package/src/animation-state-machine/runtime/AnimationStateMachineController.ts +987 -0
- package/src/animation-state-machine/runtime/AnimationStateMachineMixerPort.ts +54 -0
- package/src/animation-state-machine/runtime/index.ts +47 -0
- package/src/animation.ts +33 -0
- package/src/animation3d/Animation3DAction.ts +73 -0
- package/src/animation3d/Animation3DBinding.ts +116 -0
- package/src/animation3d/Animation3DClip.ts +22 -0
- package/src/animation3d/Animation3DError.ts +34 -0
- package/src/animation3d/Animation3DMixer.ts +82 -0
- package/src/animation3d/Animation3DPose.ts +100 -0
- package/src/animation3d/Animation3DPoseBuffer.ts +85 -0
- package/src/animation3d/Animation3DResource.ts +49 -0
- package/src/animation3d/Animation3DStateMachine.ts +126 -0
- package/src/animation3d/Animation3DStateMachineRuntime.ts +191 -0
- package/src/animation3d/Animation3DTrack.ts +29 -0
- package/src/animation3d/hya/HyaAnimation3DRuntime.ts +756 -0
- package/src/animation3d/hya/HyaAnimation3DStateMachineRuntime.ts +618 -0
- package/src/animation3d/hya/HyaAnimation3DTypes.ts +168 -0
- package/src/animation3d/hya/index.ts +10 -0
- package/src/animation3d/index.ts +76 -0
- package/src/animation3d/runtime/integration/Animation3DStateMachineMixerAdapter.ts +325 -0
- package/src/animation3d/runtime/integration/Animation3DStateMachineMixerIntegration.ts +158 -0
- package/src/animation3d/runtime/integration/index.ts +9 -0
- package/src/animation3d/runtime/mixer/Action.ts +833 -0
- package/src/animation3d/runtime/mixer/Animation3DMixerRuntimeStore.ts +26 -0
- package/src/animation3d/runtime/mixer/Mixer.ts +562 -0
- package/src/animation3d/runtime/mixer/PoseBuffer.ts +1 -0
- package/src/animation3d/runtime/mixer/TrackSampler.ts +399 -0
- package/src/animation3d/runtime/mixer/index.ts +8 -0
- package/src/animation3d/runtime/state-machine/Animation3DBlendTreeWeights.ts +4 -0
- package/src/animation3d/runtime/state-machine/Animation3DStateMachineCompiler.ts +1 -0
- package/src/animation3d/runtime/state-machine/Animation3DStateMachineController.ts +1 -0
- package/src/animation3d/runtime/state-machine/Animation3DStateMachineMixerPort.ts +1 -0
- package/src/animation3d/runtime/state-machine/index.ts +45 -0
- package/src/animation3d.ts +5 -0
- package/src/benchmark.ts +168 -0
- package/src/canvas-text/CanvasText2DRenderSystem.ts +292 -0
- package/src/canvas-text/CanvasTextComponent.ts +47 -0
- package/src/canvas-text.ts +4 -0
- package/src/experimental-gltf-worker.ts +18 -0
- package/src/experimental-spine-worker.ts +9 -0
- package/src/gltf/GltfAccessorReader.ts +271 -0
- package/src/gltf/GltfAnimation3DAdapter.ts +680 -0
- package/src/gltf/GltfAnimationRuntime.ts +392 -0
- package/src/gltf/GltfAssetWorkerClient.ts +136 -0
- package/src/gltf/GltfCompatibilityReport.ts +214 -0
- package/src/gltf/GltfConservativeBounds.ts +219 -0
- package/src/gltf/GltfDracoDecoder.ts +251 -0
- package/src/gltf/GltfExtensionAdapter.ts +340 -0
- package/src/gltf/GltfLoaderContract.ts +333 -0
- package/src/gltf/GltfLoaderErrors.ts +59 -0
- package/src/gltf/GltfMaterialDescriptor.ts +134 -0
- package/src/gltf/GltfMaterialEncoder.ts +173 -0
- package/src/gltf/GltfMaterialLoader.ts +293 -0
- package/src/gltf/GltfModelComponent.ts +115 -0
- package/src/gltf/GltfModelSystem.ts +208 -0
- package/src/gltf/GltfPlugin.ts +84 -0
- package/src/gltf/GltfSchema.ts +229 -0
- package/src/gltf/GltfUvSemanticPlanner.ts +166 -0
- package/src/gltf/gltfLoader.ts +910 -0
- package/src/gltf-animation3d.ts +10 -0
- package/src/gltf-worker-runtime.ts +10 -0
- package/src/gltf.ts +56 -0
- package/src/grid/Grid2DComponent.ts +69 -0
- package/src/grid.ts +2 -0
- package/src/hya-state-machine.ts +19 -0
- package/src/index.ts +2 -0
- package/src/plugins/pluginUtils.ts +50 -0
- package/src/plugins.ts +6 -0
- package/src/shaders/generated/2d-ui-animation-2d.generated.wgsl +242 -0
- package/src/shaders/generated/2d-ui-artifact.generated.ts +867 -0
- package/src/shaders/generated/2d-ui-canvas-text-2d.generated.wgsl +38 -0
- package/src/shaders/generated/2d-ui-spine2d.generated.wgsl +41 -0
- package/src/shaders/generated/2d-ui-tilemap2d.generated.wgsl +36 -0
- package/src/spine/Spine2DComponent.ts +168 -0
- package/src/spine/Spine2DGpuRenderer.ts +449 -0
- package/src/spine/Spine2DRenderSystem.ts +199 -0
- package/src/spine/Spine2DRuntime.ts +1529 -0
- package/src/spine/SpineAnimationBoneTimelines.ts +30 -0
- package/src/spine/SpineAssetParser.ts +20 -0
- package/src/spine/SpineAssetWorkerClient.ts +112 -0
- package/src/spine/SpineAssetWorkerContract.ts +4 -0
- package/src/spine/SpineAtlasParser.ts +127 -0
- package/src/spine/SpineFloatBuilder.ts +66 -0
- package/src/spine/SpinePathConstraintSolver.ts +582 -0
- package/src/spine/SpinePlugin.ts +76 -0
- package/src/spine/SpineSkeletonRuntime.ts +521 -0
- package/src/spine/SpineSlotAnimation.ts +195 -0
- package/src/spine/SpineTimelineRuntime.ts +453 -0
- package/src/spine/SpineVertexBuilder.ts +686 -0
- package/src/spine.ts +7 -0
- package/src/tilemap/Tilemap2DComponent.ts +232 -0
- package/src/tilemap/Tilemap2DRenderSystem.ts +58 -0
- package/src/tilemap/Tilemap2DRenderer.ts +289 -0
- package/src/tilemap/TilemapPlugin.ts +75 -0
- package/src/tilemap.ts +6 -0
- package/src/tween/Tween2DComponent.ts +83 -0
- package/src/tween/Tween2DSystem.ts +78 -0
- package/src/tween.ts +4 -0
- package/src/types/earcut.d.ts +7 -0
- package/src/types/wgsl.d.ts +4 -0
- package/src/utils/RenderSystem2DBase.ts +164 -0
- package/src/utils/arrayAccess.ts +17 -0
- package/src/utils/camera2d.ts +68 -0
- package/src/utils/render2dGpu.ts +132 -0
- package/test/fixtures/gltf/animation-characterization.gltf +344 -0
|
@@ -0,0 +1,292 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1,47 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1,4 @@
|
|
|
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';
|
|
@@ -0,0 +1,18 @@
|
|
|
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';
|
|
@@ -0,0 +1,9 @@
|
|
|
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';
|
|
@@ -0,0 +1,271 @@
|
|
|
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
|
+
}
|