reze-engine 0.2.14 → 0.2.16

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/src/vmd-loader.ts CHANGED
@@ -1,179 +1,179 @@
1
- import { Quat } from "./math"
2
-
3
- export interface BoneFrame {
4
- boneName: string
5
- frame: number
6
- rotation: Quat
7
- }
8
-
9
- export interface VMDKeyFrame {
10
- time: number // in seconds
11
- boneFrames: BoneFrame[]
12
- }
13
-
14
- export class VMDLoader {
15
- private view: DataView
16
- private offset = 0
17
- private decoder: TextDecoder
18
-
19
- private constructor(buffer: ArrayBuffer) {
20
- this.view = new DataView(buffer)
21
- // Try to use Shift-JIS decoder, fallback to UTF-8 if not available
22
- try {
23
- this.decoder = new TextDecoder("shift-jis")
24
- } catch {
25
- // Fallback to UTF-8 if Shift-JIS is not supported
26
- this.decoder = new TextDecoder("utf-8")
27
- }
28
- }
29
-
30
- static async load(url: string): Promise<VMDKeyFrame[]> {
31
- const loader = new VMDLoader(await fetch(url).then((r) => r.arrayBuffer()))
32
- return loader.parse()
33
- }
34
-
35
- static loadFromBuffer(buffer: ArrayBuffer): VMDKeyFrame[] {
36
- const loader = new VMDLoader(buffer)
37
- return loader.parse()
38
- }
39
-
40
- private parse(): VMDKeyFrame[] {
41
- // Read header (30 bytes)
42
- const header = this.getString(30)
43
- if (!header.startsWith("Vocaloid Motion Data")) {
44
- throw new Error("Invalid VMD file header")
45
- }
46
-
47
- // Skip model name (20 bytes)
48
- this.skip(20)
49
-
50
- // Read bone frame count (4 bytes, u32 little endian)
51
- const boneFrameCount = this.getUint32()
52
-
53
- // Read all bone frames
54
- const allBoneFrames: Array<{ time: number; boneFrame: BoneFrame }> = []
55
-
56
- for (let i = 0; i < boneFrameCount; i++) {
57
- const boneFrame = this.readBoneFrame()
58
-
59
- // Convert frame number to time (assuming 30 FPS like the Rust code)
60
- const FRAME_RATE = 30.0
61
- const time = boneFrame.frame / FRAME_RATE
62
-
63
- allBoneFrames.push({ time, boneFrame })
64
- }
65
-
66
- // Group by time and convert to VMDKeyFrame format
67
- // Sort by time first
68
- allBoneFrames.sort((a, b) => a.time - b.time)
69
-
70
- const keyFrames: VMDKeyFrame[] = []
71
- let currentTime = -1.0
72
- let currentBoneFrames: BoneFrame[] = []
73
-
74
- for (const { time, boneFrame } of allBoneFrames) {
75
- if (Math.abs(time - currentTime) > 0.001) {
76
- // New time frame
77
- if (currentBoneFrames.length > 0) {
78
- keyFrames.push({
79
- time: currentTime,
80
- boneFrames: currentBoneFrames,
81
- })
82
- }
83
- currentTime = time
84
- currentBoneFrames = [boneFrame]
85
- } else {
86
- // Same time frame
87
- currentBoneFrames.push(boneFrame)
88
- }
89
- }
90
-
91
- // Add the last frame
92
- if (currentBoneFrames.length > 0) {
93
- keyFrames.push({
94
- time: currentTime,
95
- boneFrames: currentBoneFrames,
96
- })
97
- }
98
-
99
- return keyFrames
100
- }
101
-
102
- private readBoneFrame(): BoneFrame {
103
- // Read bone name (15 bytes)
104
- const nameBuffer = new Uint8Array(this.view.buffer, this.offset, 15)
105
- this.offset += 15
106
-
107
- // Find the actual length of the bone name (stop at first null byte)
108
- let nameLength = 15
109
- for (let i = 0; i < 15; i++) {
110
- if (nameBuffer[i] === 0) {
111
- nameLength = i
112
- break
113
- }
114
- }
115
-
116
- // Decode Shift-JIS bone name
117
- let boneName: string
118
- try {
119
- const nameSlice = nameBuffer.slice(0, nameLength)
120
- boneName = this.decoder.decode(nameSlice)
121
- } catch {
122
- // Fallback to lossy decoding if there were encoding errors
123
- boneName = String.fromCharCode(...nameBuffer.slice(0, nameLength))
124
- }
125
-
126
- // Read frame number (4 bytes, little endian)
127
- const frame = this.getUint32()
128
-
129
- // Skip position (12 bytes: 3 x f32, little endian)
130
- this.skip(12)
131
-
132
- // Read rotation quaternion (16 bytes: 4 x f32, little endian)
133
- const rotX = this.getFloat32()
134
- const rotY = this.getFloat32()
135
- const rotZ = this.getFloat32()
136
- const rotW = this.getFloat32()
137
- const rotation = new Quat(rotX, rotY, rotZ, rotW)
138
-
139
- // Skip interpolation parameters (64 bytes)
140
- this.skip(64)
141
-
142
- return {
143
- boneName,
144
- frame,
145
- rotation,
146
- }
147
- }
148
-
149
- private getUint32(): number {
150
- if (this.offset + 4 > this.view.buffer.byteLength) {
151
- throw new RangeError(`Offset ${this.offset} + 4 exceeds buffer bounds ${this.view.buffer.byteLength}`)
152
- }
153
- const v = this.view.getUint32(this.offset, true) // true = little endian
154
- this.offset += 4
155
- return v
156
- }
157
-
158
- private getFloat32(): number {
159
- if (this.offset + 4 > this.view.buffer.byteLength) {
160
- throw new RangeError(`Offset ${this.offset} + 4 exceeds buffer bounds ${this.view.buffer.byteLength}`)
161
- }
162
- const v = this.view.getFloat32(this.offset, true) // true = little endian
163
- this.offset += 4
164
- return v
165
- }
166
-
167
- private getString(len: number): string {
168
- const bytes = new Uint8Array(this.view.buffer, this.offset, len)
169
- this.offset += len
170
- return String.fromCharCode(...bytes)
171
- }
172
-
173
- private skip(bytes: number): void {
174
- if (this.offset + bytes > this.view.buffer.byteLength) {
175
- throw new RangeError(`Offset ${this.offset} + ${bytes} exceeds buffer bounds ${this.view.buffer.byteLength}`)
176
- }
177
- this.offset += bytes
178
- }
179
- }
1
+ import { Quat } from "./math"
2
+
3
+ export interface BoneFrame {
4
+ boneName: string
5
+ frame: number
6
+ rotation: Quat
7
+ }
8
+
9
+ export interface VMDKeyFrame {
10
+ time: number // in seconds
11
+ boneFrames: BoneFrame[]
12
+ }
13
+
14
+ export class VMDLoader {
15
+ private view: DataView
16
+ private offset = 0
17
+ private decoder: TextDecoder
18
+
19
+ private constructor(buffer: ArrayBuffer) {
20
+ this.view = new DataView(buffer)
21
+ // Try to use Shift-JIS decoder, fallback to UTF-8 if not available
22
+ try {
23
+ this.decoder = new TextDecoder("shift-jis")
24
+ } catch {
25
+ // Fallback to UTF-8 if Shift-JIS is not supported
26
+ this.decoder = new TextDecoder("utf-8")
27
+ }
28
+ }
29
+
30
+ static async load(url: string): Promise<VMDKeyFrame[]> {
31
+ const loader = new VMDLoader(await fetch(url).then((r) => r.arrayBuffer()))
32
+ return loader.parse()
33
+ }
34
+
35
+ static loadFromBuffer(buffer: ArrayBuffer): VMDKeyFrame[] {
36
+ const loader = new VMDLoader(buffer)
37
+ return loader.parse()
38
+ }
39
+
40
+ private parse(): VMDKeyFrame[] {
41
+ // Read header (30 bytes)
42
+ const header = this.getString(30)
43
+ if (!header.startsWith("Vocaloid Motion Data")) {
44
+ throw new Error("Invalid VMD file header")
45
+ }
46
+
47
+ // Skip model name (20 bytes)
48
+ this.skip(20)
49
+
50
+ // Read bone frame count (4 bytes, u32 little endian)
51
+ const boneFrameCount = this.getUint32()
52
+
53
+ // Read all bone frames
54
+ const allBoneFrames: Array<{ time: number; boneFrame: BoneFrame }> = []
55
+
56
+ for (let i = 0; i < boneFrameCount; i++) {
57
+ const boneFrame = this.readBoneFrame()
58
+
59
+ // Convert frame number to time (assuming 30 FPS like the Rust code)
60
+ const FRAME_RATE = 30.0
61
+ const time = boneFrame.frame / FRAME_RATE
62
+
63
+ allBoneFrames.push({ time, boneFrame })
64
+ }
65
+
66
+ // Group by time and convert to VMDKeyFrame format
67
+ // Sort by time first
68
+ allBoneFrames.sort((a, b) => a.time - b.time)
69
+
70
+ const keyFrames: VMDKeyFrame[] = []
71
+ let currentTime = -1.0
72
+ let currentBoneFrames: BoneFrame[] = []
73
+
74
+ for (const { time, boneFrame } of allBoneFrames) {
75
+ if (Math.abs(time - currentTime) > 0.001) {
76
+ // New time frame
77
+ if (currentBoneFrames.length > 0) {
78
+ keyFrames.push({
79
+ time: currentTime,
80
+ boneFrames: currentBoneFrames,
81
+ })
82
+ }
83
+ currentTime = time
84
+ currentBoneFrames = [boneFrame]
85
+ } else {
86
+ // Same time frame
87
+ currentBoneFrames.push(boneFrame)
88
+ }
89
+ }
90
+
91
+ // Add the last frame
92
+ if (currentBoneFrames.length > 0) {
93
+ keyFrames.push({
94
+ time: currentTime,
95
+ boneFrames: currentBoneFrames,
96
+ })
97
+ }
98
+
99
+ return keyFrames
100
+ }
101
+
102
+ private readBoneFrame(): BoneFrame {
103
+ // Read bone name (15 bytes)
104
+ const nameBuffer = new Uint8Array(this.view.buffer, this.offset, 15)
105
+ this.offset += 15
106
+
107
+ // Find the actual length of the bone name (stop at first null byte)
108
+ let nameLength = 15
109
+ for (let i = 0; i < 15; i++) {
110
+ if (nameBuffer[i] === 0) {
111
+ nameLength = i
112
+ break
113
+ }
114
+ }
115
+
116
+ // Decode Shift-JIS bone name
117
+ let boneName: string
118
+ try {
119
+ const nameSlice = nameBuffer.slice(0, nameLength)
120
+ boneName = this.decoder.decode(nameSlice)
121
+ } catch {
122
+ // Fallback to lossy decoding if there were encoding errors
123
+ boneName = String.fromCharCode(...nameBuffer.slice(0, nameLength))
124
+ }
125
+
126
+ // Read frame number (4 bytes, little endian)
127
+ const frame = this.getUint32()
128
+
129
+ // Skip position (12 bytes: 3 x f32, little endian)
130
+ this.skip(12)
131
+
132
+ // Read rotation quaternion (16 bytes: 4 x f32, little endian)
133
+ const rotX = this.getFloat32()
134
+ const rotY = this.getFloat32()
135
+ const rotZ = this.getFloat32()
136
+ const rotW = this.getFloat32()
137
+ const rotation = new Quat(rotX, rotY, rotZ, rotW)
138
+
139
+ // Skip interpolation parameters (64 bytes)
140
+ this.skip(64)
141
+
142
+ return {
143
+ boneName,
144
+ frame,
145
+ rotation,
146
+ }
147
+ }
148
+
149
+ private getUint32(): number {
150
+ if (this.offset + 4 > this.view.buffer.byteLength) {
151
+ throw new RangeError(`Offset ${this.offset} + 4 exceeds buffer bounds ${this.view.buffer.byteLength}`)
152
+ }
153
+ const v = this.view.getUint32(this.offset, true) // true = little endian
154
+ this.offset += 4
155
+ return v
156
+ }
157
+
158
+ private getFloat32(): number {
159
+ if (this.offset + 4 > this.view.buffer.byteLength) {
160
+ throw new RangeError(`Offset ${this.offset} + 4 exceeds buffer bounds ${this.view.buffer.byteLength}`)
161
+ }
162
+ const v = this.view.getFloat32(this.offset, true) // true = little endian
163
+ this.offset += 4
164
+ return v
165
+ }
166
+
167
+ private getString(len: number): string {
168
+ const bytes = new Uint8Array(this.view.buffer, this.offset, len)
169
+ this.offset += len
170
+ return String.fromCharCode(...bytes)
171
+ }
172
+
173
+ private skip(bytes: number): void {
174
+ if (this.offset + bytes > this.view.buffer.byteLength) {
175
+ throw new RangeError(`Offset ${this.offset} + ${bytes} exceeds buffer bounds ${this.view.buffer.byteLength}`)
176
+ }
177
+ this.offset += bytes
178
+ }
179
+ }
package/dist/pool.d.ts DELETED
@@ -1,38 +0,0 @@
1
- export interface PoolOptions {
2
- y?: number;
3
- size?: number;
4
- segments?: number;
5
- }
6
- export declare class Pool {
7
- private device;
8
- private vertexBuffer;
9
- private indexBuffer;
10
- private pipeline;
11
- private bindGroup;
12
- private bindGroupLayout;
13
- private uniformBuffer;
14
- private cameraBindGroupLayout;
15
- private cameraBindGroup;
16
- private cameraUniformBuffer;
17
- private indexCount;
18
- private y;
19
- private size;
20
- private segments;
21
- private seaColor;
22
- private seaLight;
23
- private startTime;
24
- constructor(device: GPUDevice, cameraBindGroupLayout: GPUBindGroupLayout, cameraUniformBuffer: GPUBuffer, options?: PoolOptions);
25
- init(): Promise<void>;
26
- private createGeometry;
27
- private createShader;
28
- private createUniforms;
29
- updateUniforms(): void;
30
- render(pass: GPURenderPassEncoder, restoreBuffers?: {
31
- vertexBuffer: GPUBuffer;
32
- jointsBuffer: GPUBuffer;
33
- weightsBuffer: GPUBuffer;
34
- indexBuffer: GPUBuffer;
35
- }): void;
36
- dispose(): void;
37
- }
38
- //# sourceMappingURL=pool.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"pool.d.ts","sourceRoot":"","sources":["../src/pool.ts"],"names":[],"mappings":"AAEA,MAAM,WAAW,WAAW;IAC1B,CAAC,CAAC,EAAE,MAAM,CAAA;IACV,IAAI,CAAC,EAAE,MAAM,CAAA;IACb,QAAQ,CAAC,EAAE,MAAM,CAAA;CAClB;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