topazcube 0.1.31 → 0.1.35

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.
Files changed (103) hide show
  1. package/LICENSE.txt +0 -0
  2. package/README.md +0 -0
  3. package/dist/Renderer.cjs +20844 -0
  4. package/dist/Renderer.cjs.map +1 -0
  5. package/dist/Renderer.js +20827 -0
  6. package/dist/Renderer.js.map +1 -0
  7. package/dist/client.cjs +91 -260
  8. package/dist/client.cjs.map +1 -1
  9. package/dist/client.js +68 -215
  10. package/dist/client.js.map +1 -1
  11. package/dist/server.cjs +165 -432
  12. package/dist/server.cjs.map +1 -1
  13. package/dist/server.js +117 -370
  14. package/dist/server.js.map +1 -1
  15. package/dist/terminal.cjs +113 -200
  16. package/dist/terminal.cjs.map +1 -1
  17. package/dist/terminal.js +50 -51
  18. package/dist/terminal.js.map +1 -1
  19. package/dist/utils-CRhi1BDa.cjs +259 -0
  20. package/dist/utils-CRhi1BDa.cjs.map +1 -0
  21. package/dist/utils-D7tXt6-2.js +260 -0
  22. package/dist/utils-D7tXt6-2.js.map +1 -0
  23. package/package.json +19 -15
  24. package/src/{client.ts → network/client.js} +170 -403
  25. package/src/{compress-browser.ts → network/compress-browser.js} +2 -4
  26. package/src/{compress-node.ts → network/compress-node.js} +8 -14
  27. package/src/{server.ts → network/server.js} +229 -317
  28. package/src/{terminal.js → network/terminal.js} +0 -0
  29. package/src/{topazcube.ts → network/topazcube.js} +2 -2
  30. package/src/network/utils.js +375 -0
  31. package/src/renderer/Camera.js +191 -0
  32. package/src/renderer/DebugUI.js +703 -0
  33. package/src/renderer/Geometry.js +1049 -0
  34. package/src/renderer/Material.js +64 -0
  35. package/src/renderer/Mesh.js +211 -0
  36. package/src/renderer/Node.js +112 -0
  37. package/src/renderer/Pipeline.js +645 -0
  38. package/src/renderer/Renderer.js +1496 -0
  39. package/src/renderer/Skin.js +792 -0
  40. package/src/renderer/Texture.js +584 -0
  41. package/src/renderer/core/AssetManager.js +394 -0
  42. package/src/renderer/core/CullingSystem.js +308 -0
  43. package/src/renderer/core/EntityManager.js +541 -0
  44. package/src/renderer/core/InstanceManager.js +343 -0
  45. package/src/renderer/core/ParticleEmitter.js +358 -0
  46. package/src/renderer/core/ParticleSystem.js +564 -0
  47. package/src/renderer/core/SpriteSystem.js +349 -0
  48. package/src/renderer/gltf.js +563 -0
  49. package/src/renderer/math.js +161 -0
  50. package/src/renderer/rendering/HistoryBufferManager.js +333 -0
  51. package/src/renderer/rendering/ProbeCapture.js +1495 -0
  52. package/src/renderer/rendering/ReflectionProbeManager.js +352 -0
  53. package/src/renderer/rendering/RenderGraph.js +2258 -0
  54. package/src/renderer/rendering/passes/AOPass.js +308 -0
  55. package/src/renderer/rendering/passes/AmbientCapturePass.js +593 -0
  56. package/src/renderer/rendering/passes/BasePass.js +101 -0
  57. package/src/renderer/rendering/passes/BloomPass.js +420 -0
  58. package/src/renderer/rendering/passes/CRTPass.js +724 -0
  59. package/src/renderer/rendering/passes/FogPass.js +445 -0
  60. package/src/renderer/rendering/passes/GBufferPass.js +730 -0
  61. package/src/renderer/rendering/passes/HiZPass.js +744 -0
  62. package/src/renderer/rendering/passes/LightingPass.js +753 -0
  63. package/src/renderer/rendering/passes/ParticlePass.js +841 -0
  64. package/src/renderer/rendering/passes/PlanarReflectionPass.js +456 -0
  65. package/src/renderer/rendering/passes/PostProcessPass.js +405 -0
  66. package/src/renderer/rendering/passes/ReflectionPass.js +157 -0
  67. package/src/renderer/rendering/passes/RenderPostPass.js +364 -0
  68. package/src/renderer/rendering/passes/SSGIPass.js +266 -0
  69. package/src/renderer/rendering/passes/SSGITilePass.js +305 -0
  70. package/src/renderer/rendering/passes/ShadowPass.js +2072 -0
  71. package/src/renderer/rendering/passes/TransparentPass.js +831 -0
  72. package/src/renderer/rendering/passes/VolumetricFogPass.js +715 -0
  73. package/src/renderer/rendering/shaders/ao.wgsl +182 -0
  74. package/src/renderer/rendering/shaders/bloom.wgsl +97 -0
  75. package/src/renderer/rendering/shaders/bloom_blur.wgsl +80 -0
  76. package/src/renderer/rendering/shaders/crt.wgsl +455 -0
  77. package/src/renderer/rendering/shaders/depth_copy.wgsl +17 -0
  78. package/src/renderer/rendering/shaders/geometry.wgsl +580 -0
  79. package/src/renderer/rendering/shaders/hiz_reduce.wgsl +114 -0
  80. package/src/renderer/rendering/shaders/light_culling.wgsl +204 -0
  81. package/src/renderer/rendering/shaders/lighting.wgsl +932 -0
  82. package/src/renderer/rendering/shaders/lighting_common.wgsl +143 -0
  83. package/src/renderer/rendering/shaders/particle_render.wgsl +672 -0
  84. package/src/renderer/rendering/shaders/particle_simulate.wgsl +440 -0
  85. package/src/renderer/rendering/shaders/postproc.wgsl +293 -0
  86. package/src/renderer/rendering/shaders/render_post.wgsl +289 -0
  87. package/src/renderer/rendering/shaders/shadow.wgsl +117 -0
  88. package/src/renderer/rendering/shaders/ssgi.wgsl +266 -0
  89. package/src/renderer/rendering/shaders/ssgi_accumulate.wgsl +114 -0
  90. package/src/renderer/rendering/shaders/ssgi_propagate.wgsl +132 -0
  91. package/src/renderer/rendering/shaders/volumetric_blur.wgsl +80 -0
  92. package/src/renderer/rendering/shaders/volumetric_composite.wgsl +80 -0
  93. package/src/renderer/rendering/shaders/volumetric_raymarch.wgsl +634 -0
  94. package/src/renderer/utils/BoundingSphere.js +439 -0
  95. package/src/renderer/utils/Frustum.js +281 -0
  96. package/src/renderer/utils/Raycaster.js +761 -0
  97. package/dist/client.d.cts +0 -211
  98. package/dist/client.d.ts +0 -211
  99. package/dist/server.d.cts +0 -120
  100. package/dist/server.d.ts +0 -120
  101. package/dist/terminal.d.cts +0 -64
  102. package/dist/terminal.d.ts +0 -64
  103. package/src/utils.ts +0 -403
@@ -1,64 +0,0 @@
1
- declare function setTitle(title: any): void;
2
- declare function setColor(color: any): void;
3
- declare function resetColor(): void;
4
- declare function clearLine(): void;
5
- declare function eraseEOL(): void;
6
- declare function clear(): void;
7
- declare function cursorPosition(line: any, column: any): void;
8
- declare function cursorUp(lines?: number): void;
9
- declare function cursorDown(lines?: number): void;
10
- declare function cursorForward(columns?: number): void;
11
- declare function cursorBackward(columns?: number): void;
12
- declare function saveCursor(): void;
13
- declare function restoreCursor(): void;
14
- declare const T_RESET: 0;
15
- declare const T_BOLD: 1;
16
- declare const T_DIM: 2;
17
- declare const T_UNDERLINE: 4;
18
- declare const T_BLINK: 5;
19
- declare const T_REVERSE: 7;
20
- declare const T_HIDE: 8;
21
- declare const T_BOLD_OFF: 21;
22
- declare const T_DIM_OFF: 22;
23
- declare const T_UNDERLINE_OFF: 24;
24
- declare const T_BLINK_OFF: 25;
25
- declare const T_REVERSE_OFF: 27;
26
- declare const T_HIDE_OFF: 28;
27
- declare const T_FG_BLACK: 30;
28
- declare const T_FG_RED: 31;
29
- declare const T_FG_GREEN: 32;
30
- declare const T_FG_YELLOW: 33;
31
- declare const T_FG_BLUE: 34;
32
- declare const T_FG_MAGENTA: 35;
33
- declare const T_FG_CYAN: 36;
34
- declare const T_FG_WHITE: 37;
35
- declare const T_FG_DEFAULT: 39;
36
- declare const T_BG_BLACK: 40;
37
- declare const T_BG_RED: 41;
38
- declare const T_BG_GREEN: 42;
39
- declare const T_BG_YELLOW: 43;
40
- declare const T_BG_BLUE: 44;
41
- declare const T_BG_MAGENTA: 45;
42
- declare const T_BG_CYAN: 46;
43
- declare const T_BG_WHITE: 47;
44
- declare const T_BG_DEFAULT: 49;
45
- declare const T_FG_BRIGHT_BLACK: 90;
46
- declare const T_FG_BRIGHT_RED: 91;
47
- declare const T_FG_BRIGHT_GREEN: 92;
48
- declare const T_FG_BRIGHT_YELLOW: 93;
49
- declare const T_FG_BRIGHT_BLUE: 94;
50
- declare const T_FG_BRIGHT_MAGENTA: 95;
51
- declare const T_FG_BRIGHT_CYAN: 96;
52
- declare const T_FG_BRIGHT_WHITE: 97;
53
- declare const T_FG_BRIGHT_DEFAULT: 99;
54
- declare const T_BG_BRIGHT_BLACK: 100;
55
- declare const T_BG_BRIGHT_RED: 101;
56
- declare const T_BG_BRIGHT_GREEN: 102;
57
- declare const T_BG_BRIGHT_YELLOW: 103;
58
- declare const T_BG_BRIGHT_BLUE: 104;
59
- declare const T_BG_BRIGHT_MAGENTA: 105;
60
- declare const T_BG_BRIGHT_CYAN: 106;
61
- declare const T_BG_BRIGHT_WHITE: 107;
62
- declare const T_BG_BRIGHT_DEFAULT: 109;
63
-
64
- export { T_BG_BLACK, T_BG_BLUE, T_BG_BRIGHT_BLACK, T_BG_BRIGHT_BLUE, T_BG_BRIGHT_CYAN, T_BG_BRIGHT_DEFAULT, T_BG_BRIGHT_GREEN, T_BG_BRIGHT_MAGENTA, T_BG_BRIGHT_RED, T_BG_BRIGHT_WHITE, T_BG_BRIGHT_YELLOW, T_BG_CYAN, T_BG_DEFAULT, T_BG_GREEN, T_BG_MAGENTA, T_BG_RED, T_BG_WHITE, T_BG_YELLOW, T_BLINK, T_BLINK_OFF, T_BOLD, T_BOLD_OFF, T_DIM, T_DIM_OFF, T_FG_BLACK, T_FG_BLUE, T_FG_BRIGHT_BLACK, T_FG_BRIGHT_BLUE, T_FG_BRIGHT_CYAN, T_FG_BRIGHT_DEFAULT, T_FG_BRIGHT_GREEN, T_FG_BRIGHT_MAGENTA, T_FG_BRIGHT_RED, T_FG_BRIGHT_WHITE, T_FG_BRIGHT_YELLOW, T_FG_CYAN, T_FG_DEFAULT, T_FG_GREEN, T_FG_MAGENTA, T_FG_RED, T_FG_WHITE, T_FG_YELLOW, T_HIDE, T_HIDE_OFF, T_RESET, T_REVERSE, T_REVERSE_OFF, T_UNDERLINE, T_UNDERLINE_OFF, clear, clearLine, cursorBackward, cursorDown, cursorForward, cursorPosition, cursorUp, eraseEOL, resetColor, restoreCursor, saveCursor, setColor, setTitle };
@@ -1,64 +0,0 @@
1
- declare function setTitle(title: any): void;
2
- declare function setColor(color: any): void;
3
- declare function resetColor(): void;
4
- declare function clearLine(): void;
5
- declare function eraseEOL(): void;
6
- declare function clear(): void;
7
- declare function cursorPosition(line: any, column: any): void;
8
- declare function cursorUp(lines?: number): void;
9
- declare function cursorDown(lines?: number): void;
10
- declare function cursorForward(columns?: number): void;
11
- declare function cursorBackward(columns?: number): void;
12
- declare function saveCursor(): void;
13
- declare function restoreCursor(): void;
14
- declare const T_RESET: 0;
15
- declare const T_BOLD: 1;
16
- declare const T_DIM: 2;
17
- declare const T_UNDERLINE: 4;
18
- declare const T_BLINK: 5;
19
- declare const T_REVERSE: 7;
20
- declare const T_HIDE: 8;
21
- declare const T_BOLD_OFF: 21;
22
- declare const T_DIM_OFF: 22;
23
- declare const T_UNDERLINE_OFF: 24;
24
- declare const T_BLINK_OFF: 25;
25
- declare const T_REVERSE_OFF: 27;
26
- declare const T_HIDE_OFF: 28;
27
- declare const T_FG_BLACK: 30;
28
- declare const T_FG_RED: 31;
29
- declare const T_FG_GREEN: 32;
30
- declare const T_FG_YELLOW: 33;
31
- declare const T_FG_BLUE: 34;
32
- declare const T_FG_MAGENTA: 35;
33
- declare const T_FG_CYAN: 36;
34
- declare const T_FG_WHITE: 37;
35
- declare const T_FG_DEFAULT: 39;
36
- declare const T_BG_BLACK: 40;
37
- declare const T_BG_RED: 41;
38
- declare const T_BG_GREEN: 42;
39
- declare const T_BG_YELLOW: 43;
40
- declare const T_BG_BLUE: 44;
41
- declare const T_BG_MAGENTA: 45;
42
- declare const T_BG_CYAN: 46;
43
- declare const T_BG_WHITE: 47;
44
- declare const T_BG_DEFAULT: 49;
45
- declare const T_FG_BRIGHT_BLACK: 90;
46
- declare const T_FG_BRIGHT_RED: 91;
47
- declare const T_FG_BRIGHT_GREEN: 92;
48
- declare const T_FG_BRIGHT_YELLOW: 93;
49
- declare const T_FG_BRIGHT_BLUE: 94;
50
- declare const T_FG_BRIGHT_MAGENTA: 95;
51
- declare const T_FG_BRIGHT_CYAN: 96;
52
- declare const T_FG_BRIGHT_WHITE: 97;
53
- declare const T_FG_BRIGHT_DEFAULT: 99;
54
- declare const T_BG_BRIGHT_BLACK: 100;
55
- declare const T_BG_BRIGHT_RED: 101;
56
- declare const T_BG_BRIGHT_GREEN: 102;
57
- declare const T_BG_BRIGHT_YELLOW: 103;
58
- declare const T_BG_BRIGHT_BLUE: 104;
59
- declare const T_BG_BRIGHT_MAGENTA: 105;
60
- declare const T_BG_BRIGHT_CYAN: 106;
61
- declare const T_BG_BRIGHT_WHITE: 107;
62
- declare const T_BG_BRIGHT_DEFAULT: 109;
63
-
64
- export { T_BG_BLACK, T_BG_BLUE, T_BG_BRIGHT_BLACK, T_BG_BRIGHT_BLUE, T_BG_BRIGHT_CYAN, T_BG_BRIGHT_DEFAULT, T_BG_BRIGHT_GREEN, T_BG_BRIGHT_MAGENTA, T_BG_BRIGHT_RED, T_BG_BRIGHT_WHITE, T_BG_BRIGHT_YELLOW, T_BG_CYAN, T_BG_DEFAULT, T_BG_GREEN, T_BG_MAGENTA, T_BG_RED, T_BG_WHITE, T_BG_YELLOW, T_BLINK, T_BLINK_OFF, T_BOLD, T_BOLD_OFF, T_DIM, T_DIM_OFF, T_FG_BLACK, T_FG_BLUE, T_FG_BRIGHT_BLACK, T_FG_BRIGHT_BLUE, T_FG_BRIGHT_CYAN, T_FG_BRIGHT_DEFAULT, T_FG_BRIGHT_GREEN, T_FG_BRIGHT_MAGENTA, T_FG_BRIGHT_RED, T_FG_BRIGHT_WHITE, T_FG_BRIGHT_YELLOW, T_FG_CYAN, T_FG_DEFAULT, T_FG_GREEN, T_FG_MAGENTA, T_FG_RED, T_FG_WHITE, T_FG_YELLOW, T_HIDE, T_HIDE_OFF, T_RESET, T_REVERSE, T_REVERSE_OFF, T_UNDERLINE, T_UNDERLINE_OFF, clear, clearLine, cursorBackward, cursorDown, cursorForward, cursorPosition, cursorUp, eraseEOL, resetColor, restoreCursor, saveCursor, setColor, setTitle };
package/src/utils.ts DELETED
@@ -1,403 +0,0 @@
1
- import { Packr } from 'msgpackr';
2
- import { FLOAT32_OPTIONS } from 'msgpackr';
3
- const { ALWAYS } = FLOAT32_OPTIONS;
4
-
5
- let packr = new Packr({
6
- useFloat32: ALWAYS
7
- });
8
-
9
- export function encode(obj: any): Uint8Array {
10
- return packr.pack(obj)
11
- }
12
-
13
- export function decode(data: Uint8Array): any {
14
- return packr.unpack(data)
15
- }
16
-
17
- type ReactiveCallback = (name: string, operation: string, target: any, path: string, value: any) => void;
18
-
19
- export function reactive(name: string, object: any, callback: ReactiveCallback, path: string = '', excludedProperties: Record<string, boolean> | false = false): any {
20
- if (object === null || typeof object !== 'object') {
21
- //console.log('--- Type not object', typeof object)
22
- return object
23
- }
24
-
25
- function isReactive(p: string): boolean {
26
- let r = true
27
- if (p.startsWith('_')) {
28
- r = false
29
- }
30
- if (excludedProperties) {
31
- if (excludedProperties[p]) {
32
- r = false
33
- }
34
- }
35
- if (path == '/entities') {
36
- r = false
37
- }
38
- return r
39
- }
40
-
41
- for (const property in object) {
42
- if (isReactive(property)) {
43
- //console.log(`path '${path}', prop '${property}' is reactive`)
44
- object[property] = reactive(
45
- name,
46
- object[property],
47
- callback,
48
- path + '/' + property,
49
- excludedProperties
50
- )
51
- } else {
52
- //console.log(`--- path '${path}', property '${property}' is NOT reactive`)
53
- }
54
- }
55
- //console.log(`path '${path}' is reactive`)
56
- return new Proxy(object, {
57
- get(target: any, property: string | symbol, receiver: any): any { // ...arguments
58
- return Reflect.get(target, property, receiver)
59
- },
60
- set(target: any, property: string | symbol, value: any): boolean {
61
- let newvalue: any
62
- let pn = path + '/' + String(property)
63
- if (isReactive(String(property))) {
64
- newvalue = reactive(name, value, callback, pn, excludedProperties)
65
- callback(name, 'replace', target, pn, newvalue)
66
- } else {
67
- newvalue = value
68
- }
69
- return Reflect.set(target, property, newvalue)
70
- },
71
- deleteProperty(target: any, property: string | symbol): boolean {
72
- let pn = path + '/' + String(property)
73
- if (isReactive(String(property))) {
74
- callback(name, 'remove', target, pn, null)
75
- }
76
- delete target[property]
77
- return true
78
- },
79
- })
80
-
81
- }
82
-
83
- export function deepGet(obj: any, path: string): any {
84
- //path = path.replace(/^\/+/, '')
85
- let paths = ('' + path).split('/').filter((p) => p)
86
- let len = paths.length
87
- for (let i = 0; i < len; i++) {
88
- if (obj[paths[i]!] == undefined) {
89
- return undefined
90
- } else {
91
- obj = obj[paths[i]!]
92
- }
93
- }
94
- return obj
95
- }
96
-
97
- export function deepSet(obj: any, path: string, value: any): void {
98
- //path = path.replace(/^\/+/, '')
99
- let paths = ('' + path).split('/').filter((p) => p)
100
- let len = paths.length
101
- let i: number
102
- for (i = 0; i < len - 1; i++) {
103
- obj = obj[paths[i]!]
104
- }
105
- obj[paths[i]!] = value
106
- }
107
-
108
- // recursive clone oject, without properties that starts with _ (or __)
109
-
110
- export function clonewo_(obj: any, excludeStart: string | Record<string, boolean> = '_'): any {
111
- if (obj === null || typeof obj !== 'object') {
112
- return obj
113
- }
114
-
115
- function isExcluded(key: string): boolean {
116
- let e = false
117
- if (typeof (excludeStart) == 'string' && key.startsWith(excludeStart)) {
118
- e = true
119
- } else if (typeof(excludeStart) == 'object') {
120
- if (excludeStart[key] || key.startsWith('_')) {
121
- e = true
122
- }
123
- }
124
- return e
125
- }
126
-
127
- if (obj instanceof Map) {
128
- const mapClone = new Map()
129
- Array.from(obj.entries()).forEach(([key, value]) => {
130
- mapClone.set(clonewo_(key, excludeStart), clonewo_(value, excludeStart))
131
- })
132
- return mapClone
133
- }
134
-
135
- let clone: any
136
- if (Array.isArray(obj)) {
137
- clone = []
138
- for (let i = 0; i < obj.length; i++) {
139
- clone[i] = clonewo_(obj[i], excludeStart)
140
- }
141
- } else {
142
- clone = {} as Record<string, any>
143
- for (let key in obj) {
144
- if (obj.hasOwnProperty(key) && !isExcluded(key)) {
145
- if (typeof obj[key] === 'object') {
146
- clone[key] = clonewo_(obj[key], excludeStart)
147
- } else {
148
- clone[key] = obj[key]
149
- }
150
- }
151
- }
152
- }
153
-
154
- return clone
155
- }
156
-
157
- export function limitPrecision(obj: any): any {
158
- if (Array.isArray(obj)) {
159
- return obj.map(limitPrecision)
160
- } else if (obj !== null && typeof obj === 'object') {
161
- const result: Record<string, any> = {}
162
- for (const key in obj) {
163
- result[key] = limitPrecision(obj[key])
164
- }
165
- return result
166
- } else if (typeof obj === 'number') {
167
- if (Number.isInteger(obj)) {
168
- return obj
169
- } else {
170
- // Limit to max 3 decimal digits, not fixed
171
- return parseFloat(obj.toFixed(3))
172
- }
173
- } else {
174
- return obj
175
- }
176
- }
177
-
178
- export function msgop(op: any): any {
179
- let nop: any = {}
180
- if (!op.o) {
181
- nop.op = 'replace'
182
- } else {
183
- nop.op = ({
184
- a: 'add',
185
- r: 'remove',
186
- d: 'delete',
187
- t: 'test',
188
- } as Record<string, string>)[op.o]
189
- }
190
- nop.path = op.p
191
- nop.value = op.v
192
- return nop
193
- }
194
-
195
- export function opmsg(op: string, target: any, path: string, value: any): any {
196
- let c: any = { p: path, v: value }
197
- if (op != 'replace') {
198
- c.o = ({
199
- add: 'a',
200
- remove: 'r',
201
- delete: 'd',
202
- test: 't',
203
- } as Record<string, string>)[op]
204
- }
205
- return c
206
- }
207
-
208
- // a function that converts an int to a hexa string
209
- // (8 characters long)
210
- export function int2hex(int: number): string {
211
- return int.toString(16)
212
- }
213
-
214
- // a function that converts a hexa string to an int
215
- export function hex2int(str: string): bigint {
216
- if (str.length % 2) {
217
- str = '0' + str
218
- }
219
- return BigInt('0x' + str)
220
- }
221
-
222
- // - Fixed point encoding/decoding functions
223
-
224
- // 32-bit unsigned integer encoding
225
- export function encode_uint32(uint: number, byteArray?: Uint8Array, offset: number = 0): Uint8Array {
226
- if (!byteArray) {
227
- byteArray = new Uint8Array(4)
228
- }
229
- let p = offset + 3
230
- byteArray[p--] = uint & 0xff
231
- uint >>= 8
232
- byteArray[p--] = uint & 0xff
233
- uint >>= 8
234
- byteArray[p--] = uint & 0xff
235
- uint >>= 8
236
- byteArray[p] = uint
237
- return byteArray
238
- }
239
- // 32-bit unsigned integer decoding
240
- export function decode_uint32(byteArray: Uint8Array, offset: number = 0): number {
241
- let p = offset
242
- return (
243
- ((byteArray[p++]! & 0x7f) << 24) |
244
- (byteArray[p++]! << 16) |
245
- (byteArray[p++]! << 8) |
246
- byteArray[p]!
247
- )
248
- }
249
-
250
- // 24-bit unsigned integer encoding
251
- export function encode_uint24(uint: number, byteArray?: Uint8Array, offset: number = 0): Uint8Array {
252
- if (!byteArray) {
253
- byteArray = new Uint8Array(3)
254
- }
255
- let p = offset + 2
256
- byteArray[p--] = uint & 0xff
257
- uint >>= 8
258
- byteArray[p--] = uint & 0xff
259
- uint >>= 8
260
- byteArray[p] = uint
261
- return byteArray
262
- }
263
-
264
- // 24-bit unsigned integer decoding
265
- export function decode_uint24(byteArray: Uint8Array, offset: number = 0): number {
266
- let p = offset
267
- return (
268
- (byteArray[p++]! << 16) |
269
- (byteArray[p++]! << 8) |
270
- byteArray[p]!
271
- )
272
- }
273
-
274
- // 16-bit unsigned integer encoding
275
- export function encode_uint16(uint: number, byteArray?: Uint8Array, offset: number = 0): Uint8Array {
276
- if (!byteArray) {
277
- byteArray = new Uint8Array(2)
278
- }
279
- let p = offset + 1
280
- byteArray[p--] = uint & 0xff
281
- uint >>= 8
282
- byteArray[p] = uint
283
- return byteArray
284
- }
285
-
286
- // 16-bit unsigned integer decoding
287
- export function decode_uint16(byteArray: Uint8Array, offset: number = 0): number {
288
- let p = offset
289
- return (byteArray[p++]! << 8) | byteArray[p]!
290
- }
291
-
292
- // 24.8 bit ====================================================================
293
-
294
- // 24.8-bit fixed point encoding
295
- export function encode_fp248(float: number, byteArray?: Uint8Array, offset: number = 0): void {
296
- const fp = Math.round(Math.abs(float) * 256)
297
- encode_uint32(fp, byteArray, offset)
298
- if (float < 0 && byteArray) {
299
- byteArray[offset]! |= 0x80
300
- }
301
- }
302
-
303
- // 24.8-bit fixed point decoding
304
- export function decode_fp248(byteArray: Uint8Array, offset: number = 0): number {
305
- const divider = (byteArray[offset]! & 0x80) === 0x80 ? -256 : 256
306
- byteArray[offset]! &= 0x7f
307
- const fp = decode_uint32(byteArray, offset)
308
- return fp / divider
309
- }
310
-
311
- // 16.8 bit ====================================================================
312
-
313
- // 16.8-bit fixed point encoding (3 bytes)
314
- export function encode_fp168(float: number, byteArray?: Uint8Array, offset: number = 0): void {
315
- const fp = Math.round(Math.abs(float) * 256)
316
- encode_uint24(fp, byteArray, offset)
317
- if (float < 0 && byteArray) {
318
- byteArray[offset]! |= 0x80
319
- }
320
- }
321
-
322
- // 16.8-bit fixed point decoding (3 bytes)
323
- export function decode_fp168(byteArray: Uint8Array, offset: number = 0): number {
324
- const divider = (byteArray[offset]! & 0x80) === 0x80 ? -256 : 256
325
- byteArray[offset]! &= 0x7f
326
- const fp = decode_uint24(byteArray, offset)
327
- return fp / divider
328
- }
329
-
330
- // 16.16 bit ===================================================================
331
-
332
- // 16.16-bit fixed point encoding
333
- export function encode_fp1616(float: number, byteArray?: Uint8Array, offset: number = 0): void {
334
- const fp = Math.round(Math.abs(float) * 65536)
335
- encode_uint32(fp, byteArray, offset)
336
- if (float < 0 && byteArray) {
337
- byteArray[offset]! |= 0x80
338
- }
339
- }
340
-
341
- // 16.16-bit fixed point decoding
342
- export function decode_fp1616(byteArray: Uint8Array, offset: number = 0): number {
343
- const divider = (byteArray[offset]! & 0x80) === 0x80 ? -65536 : 65536
344
- byteArray[offset]! &= 0x7f
345
- const fp = decode_uint32(byteArray, offset)
346
- return fp / divider
347
- }
348
-
349
- // 8.8 bit =====================================================================
350
-
351
- // 8.8-bit fixed point encoding
352
- export function encode_fp88(float: number, byteArray?: Uint8Array, offset: number = 0): void {
353
- const fp = Math.round(Math.abs(float) * 256)
354
- encode_uint16(fp, byteArray, offset)
355
- if (float < 0 && byteArray) {
356
- byteArray[offset]! |= 0x80
357
- }
358
- }
359
-
360
- // 8.8-bit fixed point decoding
361
- export function decode_fp88(byteArray: Uint8Array, offset: number = 0): number {
362
- const divider = (byteArray[offset]! & 0x80) === 0x80 ? -256 : 256
363
- byteArray[offset]! &= 0x7f
364
- const fp = decode_uint16(byteArray, offset)
365
- return fp / divider
366
- }
367
-
368
- // 4.12 bit ====================================================================
369
-
370
- // 4.12-bit fixed point encoding
371
- export function encode_fp412(float: number, byteArray?: Uint8Array, offset: number = 0): void {
372
- const fp = Math.round(Math.abs(float) * 4096)
373
- encode_uint16(fp, byteArray, offset)
374
- if (float < 0 && byteArray) {
375
- byteArray[offset]! |= 0x80
376
- }
377
- }
378
-
379
- // 4.12-bit fixed point decoding
380
- export function decode_fp412(byteArray: Uint8Array, offset: number = 0): number {
381
- const divider = (byteArray[offset]! & 0x80) === 0x80 ? -4096 : 4096
382
- byteArray[offset]! &= 0x7f
383
- const fp = decode_uint16(byteArray, offset)
384
- return fp / divider
385
- }
386
-
387
- // 1.7 bit =====================================================================
388
-
389
- // 1.7-bit fixed point encoding
390
- export function encode_fp17(float: number, byteArray: Uint8Array, offset: number = 0): void {
391
- const fp = Math.round(Math.abs(float) * 128)
392
- byteArray[offset] = fp
393
- if (float < 0) {
394
- byteArray[offset] |= 0x80
395
- }
396
- }
397
-
398
- // 1.7-bit fixed point decoding
399
- export function decode_fp17(byteArray: Uint8Array, offset: number = 0): number {
400
- const divider = (byteArray[offset]! & 0x80) === 0x80 ? -128.0 : 128.0
401
- byteArray[offset]! &= 0x7f
402
- return byteArray[offset]! / divider
403
- }