@takram/three-geospatial 0.0.1-alpha.4 → 0.0.1-alpha.6
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/build/index.cjs +1 -43
- package/build/index.cjs.map +1 -1
- package/build/index.js +315 -772
- package/build/index.js.map +1 -1
- package/build/shaders.cjs +276 -0
- package/build/shaders.cjs.map +1 -0
- package/build/shaders.js +286 -0
- package/build/shaders.js.map +1 -0
- package/package.json +7 -3
- package/src/DataLoader.ts +107 -49
- package/src/Texture3DLoader.ts +81 -0
- package/src/TypedArrayLoader.ts +25 -9
- package/src/bufferGeometry.ts +2 -2
- package/src/constants.ts +3 -0
- package/src/index.ts +4 -10
- package/src/math.ts +1 -0
- package/src/r3f/index.ts +1 -0
- package/src/r3f/types.ts +64 -0
- package/src/resolveIncludes.test.ts +21 -0
- package/src/resolveIncludes.ts +22 -0
- package/src/shaders/depth.glsl +3 -1
- package/src/shaders/generators.glsl +9 -0
- package/src/shaders/index.ts +17 -0
- package/src/shaders/math.glsl +52 -0
- package/src/shaders/poissonDisk.glsl +21 -0
- package/src/shaders/raySphereIntersection.glsl +134 -0
- package/src/shaders/turbo.glsl +9 -0
- package/src/typedArray.ts +29 -52
- package/src/typedArrayParsers.ts +77 -0
- package/src/types.ts +5 -51
- package/src/unrollLoops.ts +23 -0
- package/types/DataLoader.d.ts +30 -60
- package/types/Texture3DLoader.d.ts +5 -0
- package/types/TypedArrayLoader.d.ts +10 -10
- package/types/constants.d.ts +3 -0
- package/types/index.d.ts +4 -3
- package/types/math.d.ts +1 -0
- package/types/r3f/index.d.ts +1 -0
- package/types/r3f/types.d.ts +22 -0
- package/types/resolveIncludes.d.ts +5 -0
- package/types/shaders/index.d.ts +8 -0
- package/types/typedArray.d.ts +2 -8
- package/types/typedArrayParsers.d.ts +11 -0
- package/types/types.d.ts +5 -20
- package/types/unrollLoops.d.ts +1 -0
package/src/index.ts
CHANGED
@@ -1,16 +1,7 @@
|
|
1
|
-
/// <reference types="vite-plugin-glsl/ext" />
|
2
|
-
|
3
|
-
import depth from './shaders/depth.glsl'
|
4
|
-
import packing from './shaders/packing.glsl'
|
5
|
-
import transform from './shaders/transform.glsl'
|
6
|
-
|
7
|
-
export const depthShader: string = depth
|
8
|
-
export const packingShader: string = packing
|
9
|
-
export const transformShader: string = transform
|
10
|
-
|
11
1
|
export * from './ArrayBufferLoader'
|
12
2
|
export * from './assertions'
|
13
3
|
export * from './bufferGeometry'
|
4
|
+
export * from './constants'
|
14
5
|
export * from './DataLoader'
|
15
6
|
export * from './Ellipsoid'
|
16
7
|
export * from './EllipsoidGeometry'
|
@@ -18,8 +9,11 @@ export * from './Geodetic'
|
|
18
9
|
export * from './math'
|
19
10
|
export * from './PointOfView'
|
20
11
|
export * from './Rectangle'
|
12
|
+
export * from './resolveIncludes'
|
21
13
|
export * from './TileCoordinate'
|
22
14
|
export * from './TilingScheme'
|
23
15
|
export * from './typedArray'
|
24
16
|
export * from './TypedArrayLoader'
|
17
|
+
export * from './typedArrayParsers'
|
25
18
|
export * from './types'
|
19
|
+
export * from './unrollLoops'
|
package/src/math.ts
CHANGED
@@ -10,6 +10,7 @@ export const isPowerOfTwo = MathUtils.isPowerOfTwo
|
|
10
10
|
export const ceilPowerOfTwo = MathUtils.ceilPowerOfTwo
|
11
11
|
export const floorPowerOfTwo = MathUtils.floorPowerOfTwo
|
12
12
|
export const normalize = MathUtils.normalize
|
13
|
+
export const remap = MathUtils.mapLinear
|
13
14
|
|
14
15
|
// Prefer glsl's argument order which differs from that of MathUtils.
|
15
16
|
export function smoothstep(min: number, max: number, x: number): number {
|
package/src/r3f/index.ts
CHANGED
package/src/r3f/types.ts
ADDED
@@ -0,0 +1,64 @@
|
|
1
|
+
import {
|
2
|
+
type Color,
|
3
|
+
type ExtendedColors,
|
4
|
+
type NodeProps,
|
5
|
+
type Overwrite,
|
6
|
+
type Vector2,
|
7
|
+
type Vector3,
|
8
|
+
type Vector4
|
9
|
+
} from '@react-three/fiber'
|
10
|
+
import {
|
11
|
+
type Vector2 as Vector2Impl,
|
12
|
+
type Vector3 as Vector3Impl,
|
13
|
+
type Vector4 as Vector4Impl
|
14
|
+
} from 'three'
|
15
|
+
import { type WritableKeysOf } from 'type-fest'
|
16
|
+
|
17
|
+
import { type Callable } from '../types'
|
18
|
+
|
19
|
+
// prettier-ignore
|
20
|
+
export type ExtendedVectors<T> = {
|
21
|
+
[K in keyof T]:
|
22
|
+
Vector2Impl extends T[K] ? Vector2 | T[K] :
|
23
|
+
Vector3Impl extends T[K] ? Vector3 | T[K] :
|
24
|
+
Vector4Impl extends T[K] ? Vector4 | T[K] :
|
25
|
+
T[K]
|
26
|
+
}
|
27
|
+
|
28
|
+
export type ExtendedProps<T> = ExtendedColors<ExtendedVectors<T>>
|
29
|
+
|
30
|
+
// @react-three/fiber's NonFunctionKeys cannot exclude partial functions.
|
31
|
+
// This excludes callback properties, which may be undesirable behavior.
|
32
|
+
type NonFunctionKeys<T> = keyof {
|
33
|
+
[K in keyof T as Callable extends T[K] ? never : K]: any
|
34
|
+
}
|
35
|
+
|
36
|
+
// prettier-ignore
|
37
|
+
type WritableNonExtendableKeysOf<T> =
|
38
|
+
| WritableKeysOf<T>
|
39
|
+
| keyof {
|
40
|
+
[K in keyof T as
|
41
|
+
Vector2Impl extends T[K] ? K :
|
42
|
+
Vector3Impl extends T[K] ? K :
|
43
|
+
Vector4Impl extends T[K] ? K :
|
44
|
+
Color extends T[K] ? K :
|
45
|
+
never
|
46
|
+
]: any
|
47
|
+
}
|
48
|
+
|
49
|
+
export type PassThoughInstanceProps<
|
50
|
+
RefType,
|
51
|
+
Args extends readonly any[],
|
52
|
+
Props
|
53
|
+
> = Overwrite<
|
54
|
+
ExtendedProps<{
|
55
|
+
[K in NonFunctionKeys<Props> as K extends WritableNonExtendableKeysOf<Props>
|
56
|
+
? K
|
57
|
+
: never]: Props[K]
|
58
|
+
}>,
|
59
|
+
NodeProps<RefType, Args>
|
60
|
+
>
|
61
|
+
|
62
|
+
export type ExpandNestedProps<T, Prop extends keyof T & string> = {
|
63
|
+
[K in keyof T[Prop] as K extends string ? `${Prop}-${K}` : never]: T[Prop][K]
|
64
|
+
}
|
@@ -0,0 +1,21 @@
|
|
1
|
+
import { resolveIncludes } from './resolveIncludes'
|
2
|
+
|
3
|
+
describe('resolveIncludes', () => {
|
4
|
+
test('delimited paths', () => {
|
5
|
+
expect(
|
6
|
+
resolveIncludes('#include "scope/lib"', {
|
7
|
+
scope: {
|
8
|
+
lib: 'imported'
|
9
|
+
}
|
10
|
+
})
|
11
|
+
).toBe('imported')
|
12
|
+
|
13
|
+
expect(() =>
|
14
|
+
resolveIncludes('#include "scope/lib"', {
|
15
|
+
other: {
|
16
|
+
lib: 'imported'
|
17
|
+
}
|
18
|
+
})
|
19
|
+
).toThrow()
|
20
|
+
})
|
21
|
+
})
|
@@ -0,0 +1,22 @@
|
|
1
|
+
const includePattern = /^[ \t]*#include +"([\w\d./]+)"/gm
|
2
|
+
|
3
|
+
interface Includes {
|
4
|
+
[key: string]: string | Includes
|
5
|
+
}
|
6
|
+
|
7
|
+
export function resolveIncludes(source: string, includes: Includes): string {
|
8
|
+
return source.replace(includePattern, (match, path: string) => {
|
9
|
+
const components = path.split('/')
|
10
|
+
const include = components.reduce<string | Includes | undefined>(
|
11
|
+
(parent, component) =>
|
12
|
+
typeof parent !== 'string' && parent != null
|
13
|
+
? parent[component]
|
14
|
+
: undefined,
|
15
|
+
includes
|
16
|
+
)
|
17
|
+
if (typeof include !== 'string') {
|
18
|
+
throw new Error(`Could not find include for ${path}.`)
|
19
|
+
}
|
20
|
+
return resolveIncludes(include, includes)
|
21
|
+
})
|
22
|
+
}
|
package/src/shaders/depth.glsl
CHANGED
@@ -1,10 +1,12 @@
|
|
1
|
+
// cSpell:words logdepthbuf
|
2
|
+
|
1
3
|
float reverseLogDepth(const float depth, const float near, const float far) {
|
2
4
|
#ifdef USE_LOGDEPTHBUF
|
3
5
|
float d = pow(2.0, depth * log2(far + 1.0)) - 1.0;
|
4
6
|
float a = far / (far - near);
|
5
7
|
float b = far * near / (near - far);
|
6
8
|
return a + b / d;
|
7
|
-
#else
|
9
|
+
#else // USE_LOGDEPTHBUF
|
8
10
|
return depth;
|
9
11
|
#endif // USE_LOGDEPTHBUF
|
10
12
|
}
|
@@ -0,0 +1,17 @@
|
|
1
|
+
import _depth from './depth.glsl?raw'
|
2
|
+
import _generators from './generators.glsl?raw'
|
3
|
+
import _math from './math.glsl?raw'
|
4
|
+
import _packing from './packing.glsl?raw'
|
5
|
+
import _poissonDisk from './poissonDisk.glsl?raw'
|
6
|
+
import _raySphereIntersection from './raySphereIntersection.glsl?raw'
|
7
|
+
import _transform from './transform.glsl?raw'
|
8
|
+
import _turbo from './turbo.glsl?raw'
|
9
|
+
|
10
|
+
export const depth: string = _depth
|
11
|
+
export const generators: string = _generators
|
12
|
+
export const math: string = _math
|
13
|
+
export const packing: string = _packing
|
14
|
+
export const poissonDisk: string = _poissonDisk
|
15
|
+
export const raySphereIntersection: string = _raySphereIntersection
|
16
|
+
export const transform: string = _transform
|
17
|
+
export const turbo: string = _turbo
|
@@ -0,0 +1,52 @@
|
|
1
|
+
#if !defined(saturate)
|
2
|
+
#define saturate(a) clamp(a, 0.0, 1.0)
|
3
|
+
#endif // !defined(saturate)
|
4
|
+
|
5
|
+
float inverseLerp(const float x, const float y, const float a) {
|
6
|
+
return (a - x) / (y - x);
|
7
|
+
}
|
8
|
+
|
9
|
+
vec2 inverseLerp(const vec2 x, const vec2 y, const vec2 a) {
|
10
|
+
return (a - x) / (y - x);
|
11
|
+
}
|
12
|
+
|
13
|
+
vec3 inverseLerp(const vec3 x, const vec3 y, const vec3 a) {
|
14
|
+
return (a - x) / (y - x);
|
15
|
+
}
|
16
|
+
|
17
|
+
vec4 inverseLerp(const vec4 x, const vec4 y, const vec4 a) {
|
18
|
+
return (a - x) / (y - x);
|
19
|
+
}
|
20
|
+
|
21
|
+
float remap(const float x, const float min1, const float max1, const float min2, const float max2) {
|
22
|
+
return min2 + (x - min1) / (max1 - min1) * (max2 - min2);
|
23
|
+
}
|
24
|
+
|
25
|
+
vec2 remap(const vec2 x, const vec2 min1, const vec2 max1, const vec2 min2, const vec2 max2) {
|
26
|
+
return min2 + (x - min1) / (max1 - min1) * (max2 - min2);
|
27
|
+
}
|
28
|
+
|
29
|
+
vec3 remap(const vec3 x, const vec3 min1, const vec3 max1, const vec3 min2, const vec3 max2) {
|
30
|
+
return min2 + (x - min1) / (max1 - min1) * (max2 - min2);
|
31
|
+
}
|
32
|
+
|
33
|
+
vec4 remap(const vec4 x, const vec4 min1, const vec4 max1, const vec4 min2, const vec4 max2) {
|
34
|
+
return min2 + (x - min1) / (max1 - min1) * (max2 - min2);
|
35
|
+
}
|
36
|
+
|
37
|
+
// Implicitly remap to 0 and 1
|
38
|
+
float remap(const float x, const float min1, const float max1) {
|
39
|
+
return (x - min1) / (max1 - min1);
|
40
|
+
}
|
41
|
+
|
42
|
+
vec2 remap(const vec2 x, const vec2 min1, const vec2 max1) {
|
43
|
+
return (x - min1) / (max1 - min1);
|
44
|
+
}
|
45
|
+
|
46
|
+
vec3 remap(const vec3 x, const vec3 min1, const vec3 max1) {
|
47
|
+
return (x - min1) / (max1 - min1);
|
48
|
+
}
|
49
|
+
|
50
|
+
vec4 remap(const vec4 x, const vec4 min1, const vec4 max1) {
|
51
|
+
return (x - min1) / (max1 - min1);
|
52
|
+
}
|
@@ -0,0 +1,21 @@
|
|
1
|
+
// Taken from: https://developer.download.nvidia.com/whitepapers/2008/PCSS_Integration.pdf
|
2
|
+
const vec2 poissonDisk[16] = vec2[16](
|
3
|
+
vec2(-0.94201624, -0.39906216),
|
4
|
+
vec2(0.94558609, -0.76890725),
|
5
|
+
vec2(-0.094184101, -0.9293887),
|
6
|
+
vec2(0.34495938, 0.2938776),
|
7
|
+
vec2(-0.91588581, 0.45771432),
|
8
|
+
vec2(-0.81544232, -0.87912464),
|
9
|
+
vec2(-0.38277543, 0.27676845),
|
10
|
+
vec2(0.97484398, 0.75648379),
|
11
|
+
vec2(0.44323325, -0.97511554),
|
12
|
+
vec2(0.53742981, -0.4737342),
|
13
|
+
vec2(-0.26496911, -0.41893023),
|
14
|
+
vec2(0.79197514, 0.19090188),
|
15
|
+
vec2(-0.2418884, 0.99706507),
|
16
|
+
vec2(-0.81409955, 0.9143759),
|
17
|
+
vec2(0.19984126, 0.78641367),
|
18
|
+
vec2(0.14383161, -0.1410079)
|
19
|
+
);
|
20
|
+
|
21
|
+
#define POISSON_DISK_COUNT (16)
|
@@ -0,0 +1,134 @@
|
|
1
|
+
float raySphereFirstIntersection(
|
2
|
+
const vec3 origin,
|
3
|
+
const vec3 direction,
|
4
|
+
const vec3 center,
|
5
|
+
const float radius
|
6
|
+
) {
|
7
|
+
vec3 a = origin - center;
|
8
|
+
float b = 2.0 * dot(direction, a);
|
9
|
+
float c = dot(a, a) - radius * radius;
|
10
|
+
float discriminant = b * b - 4.0 * c;
|
11
|
+
return discriminant < 0.0
|
12
|
+
? -1.0
|
13
|
+
: (-b - sqrt(discriminant)) * 0.5;
|
14
|
+
}
|
15
|
+
|
16
|
+
float raySphereFirstIntersection(const vec3 origin, const vec3 direction, const float radius) {
|
17
|
+
return raySphereFirstIntersection(origin, direction, vec3(0.0), radius);
|
18
|
+
}
|
19
|
+
|
20
|
+
vec4 raySphereFirstIntersection(
|
21
|
+
const vec3 origin,
|
22
|
+
const vec3 direction,
|
23
|
+
const vec3 center,
|
24
|
+
const vec4 radius
|
25
|
+
) {
|
26
|
+
vec3 a = origin - center;
|
27
|
+
float b = 2.0 * dot(direction, a);
|
28
|
+
vec4 c = dot(a, a) - radius * radius;
|
29
|
+
vec4 discriminant = b * b - 4.0 * c;
|
30
|
+
vec4 mask = step(discriminant, vec4(0.0));
|
31
|
+
return mix((-b - sqrt(max(vec4(0.0), discriminant))) * 0.5, vec4(-1.0), mask);
|
32
|
+
}
|
33
|
+
|
34
|
+
vec4 raySphereFirstIntersection(const vec3 origin, const vec3 direction, const vec4 radius) {
|
35
|
+
return raySphereFirstIntersection(origin, direction, vec3(0.0), radius);
|
36
|
+
}
|
37
|
+
|
38
|
+
float raySphereSecondIntersection(
|
39
|
+
const vec3 origin,
|
40
|
+
const vec3 direction,
|
41
|
+
const vec3 center,
|
42
|
+
const float radius
|
43
|
+
) {
|
44
|
+
vec3 a = origin - center;
|
45
|
+
float b = 2.0 * dot(direction, a);
|
46
|
+
float c = dot(a, a) - radius * radius;
|
47
|
+
float discriminant = b * b - 4.0 * c;
|
48
|
+
return discriminant < 0.0
|
49
|
+
? -1.0
|
50
|
+
: (-b + sqrt(discriminant)) * 0.5;
|
51
|
+
}
|
52
|
+
|
53
|
+
float raySphereSecondIntersection(const vec3 origin, const vec3 direction, const float radius) {
|
54
|
+
return raySphereSecondIntersection(origin, direction, vec3(0.0), radius);
|
55
|
+
}
|
56
|
+
|
57
|
+
vec4 raySphereSecondIntersection(
|
58
|
+
const vec3 origin,
|
59
|
+
const vec3 direction,
|
60
|
+
const vec3 center,
|
61
|
+
const vec4 radius
|
62
|
+
) {
|
63
|
+
vec3 a = origin - center;
|
64
|
+
float b = 2.0 * dot(direction, a);
|
65
|
+
vec4 c = dot(a, a) - radius * radius;
|
66
|
+
vec4 discriminant = b * b - 4.0 * c;
|
67
|
+
vec4 mask = step(discriminant, vec4(0.0));
|
68
|
+
return mix((-b + sqrt(max(vec4(0.0), discriminant))) * 0.5, vec4(-1.0), mask);
|
69
|
+
}
|
70
|
+
|
71
|
+
vec4 raySphereSecondIntersection(const vec3 origin, const vec3 direction, const vec4 radius) {
|
72
|
+
return raySphereSecondIntersection(origin, direction, vec3(0.0), radius);
|
73
|
+
}
|
74
|
+
|
75
|
+
void raySphereIntersections(
|
76
|
+
const vec3 origin,
|
77
|
+
const vec3 direction,
|
78
|
+
const vec3 center,
|
79
|
+
const float radius,
|
80
|
+
out float intersection1,
|
81
|
+
out float intersection2
|
82
|
+
) {
|
83
|
+
vec3 a = origin - center;
|
84
|
+
float b = 2.0 * dot(direction, a);
|
85
|
+
float c = dot(a, a) - radius * radius;
|
86
|
+
float discriminant = b * b - 4.0 * c;
|
87
|
+
if (discriminant < 0.0) {
|
88
|
+
intersection1 = -1.0;
|
89
|
+
intersection2 = -1.0;
|
90
|
+
return;
|
91
|
+
} else {
|
92
|
+
float Q = sqrt(discriminant);
|
93
|
+
intersection1 = (-b - Q) * 0.5;
|
94
|
+
intersection2 = (-b + Q) * 0.5;
|
95
|
+
}
|
96
|
+
}
|
97
|
+
|
98
|
+
void raySphereIntersections(
|
99
|
+
const vec3 origin,
|
100
|
+
const vec3 direction,
|
101
|
+
const float radius,
|
102
|
+
out float intersection1,
|
103
|
+
out float intersection2
|
104
|
+
) {
|
105
|
+
raySphereIntersections(origin, direction, vec3(0.0), radius, intersection1, intersection2);
|
106
|
+
}
|
107
|
+
|
108
|
+
void raySphereIntersections(
|
109
|
+
const vec3 origin,
|
110
|
+
const vec3 direction,
|
111
|
+
const vec3 center,
|
112
|
+
const vec4 radius,
|
113
|
+
out vec4 intersection1,
|
114
|
+
out vec4 intersection2
|
115
|
+
) {
|
116
|
+
vec3 a = origin - center;
|
117
|
+
float b = 2.0 * dot(direction, a);
|
118
|
+
vec4 c = dot(a, a) - radius * radius;
|
119
|
+
vec4 discriminant = b * b - 4.0 * c;
|
120
|
+
vec4 mask = step(discriminant, vec4(0.0));
|
121
|
+
vec4 Q = sqrt(max(vec4(0.0), discriminant));
|
122
|
+
intersection1 = mix((-b - Q) * 0.5, vec4(-1.0), mask);
|
123
|
+
intersection2 = mix((-b + Q) * 0.5, vec4(-1.0), mask);
|
124
|
+
}
|
125
|
+
|
126
|
+
void raySphereIntersections(
|
127
|
+
const vec3 origin,
|
128
|
+
const vec3 direction,
|
129
|
+
const vec4 radius,
|
130
|
+
out vec4 intersection1,
|
131
|
+
out vec4 intersection2
|
132
|
+
) {
|
133
|
+
raySphereIntersections(origin, direction, vec3(0.0), radius, intersection1, intersection2);
|
134
|
+
}
|
@@ -0,0 +1,9 @@
|
|
1
|
+
// A fifth-order polynomial approximation of Turbo color map.
|
2
|
+
// See: https://observablehq.com/@mbostock/turbo
|
3
|
+
// prettier-ignore
|
4
|
+
vec3 turbo(const float x) {
|
5
|
+
float r = 0.1357 + x * (4.5974 - x * (42.3277 - x * (130.5887 - x * (150.5666 - x * 58.1375))));
|
6
|
+
float g = 0.0914 + x * (2.1856 + x * (4.8052 - x * (14.0195 - x * (4.2109 + x * 2.7747))));
|
7
|
+
float b = 0.1067 + x * (12.5925 - x * (60.1097 - x * (109.0745 - x * (88.5066 - x * 26.8183))));
|
8
|
+
return vec3(r, g, b);
|
9
|
+
}
|
package/src/typedArray.ts
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
import invariant from 'tiny-invariant'
|
2
|
+
|
1
3
|
export type TypedArray =
|
2
4
|
| Int8Array
|
3
5
|
| Uint8Array
|
@@ -20,57 +22,32 @@ export type TypedArrayConstructor =
|
|
20
22
|
| Float32ArrayConstructor
|
21
23
|
| Float64ArrayConstructor
|
22
24
|
|
23
|
-
type
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
>(
|
33
|
-
buffer: ArrayBuffer,
|
34
|
-
TypedArray: T,
|
35
|
-
getValue: K,
|
36
|
-
littleEndian?: boolean
|
37
|
-
): InstanceType<T>
|
38
|
-
|
39
|
-
export function parseTypedArray<K extends GetValue>(
|
40
|
-
buffer: ArrayBuffer,
|
41
|
-
TypedArray: TypedArrayConstructor,
|
42
|
-
getValue: K,
|
43
|
-
littleEndian = true
|
44
|
-
): TypedArray {
|
45
|
-
const data = new DataView(buffer)
|
46
|
-
const array = new TypedArray(data.byteLength / TypedArray.BYTES_PER_ELEMENT)
|
47
|
-
for (
|
48
|
-
let index = 0, byteIndex = 0;
|
49
|
-
index < array.length;
|
50
|
-
++index, byteIndex += TypedArray.BYTES_PER_ELEMENT
|
51
|
-
) {
|
52
|
-
array[index] = data[getValue](byteIndex, littleEndian)
|
53
|
-
}
|
54
|
-
return array
|
55
|
-
}
|
56
|
-
|
57
|
-
export function parseInt16Array(
|
58
|
-
buffer: ArrayBuffer,
|
59
|
-
littleEndian?: boolean
|
60
|
-
): Int16Array {
|
61
|
-
return parseTypedArray(buffer, Int16Array, 'getInt16', littleEndian)
|
62
|
-
}
|
63
|
-
|
64
|
-
export function parseUint16Array(
|
65
|
-
buffer: ArrayBuffer,
|
66
|
-
littleEndian?: boolean
|
67
|
-
): Uint16Array {
|
68
|
-
return parseTypedArray(buffer, Uint16Array, 'getUint16', littleEndian)
|
69
|
-
}
|
25
|
+
export type TypedArrayElementType =
|
26
|
+
| 'int8'
|
27
|
+
| 'uint8'
|
28
|
+
| 'int16'
|
29
|
+
| 'uint16'
|
30
|
+
| 'int32'
|
31
|
+
| 'uint32'
|
32
|
+
| 'float32'
|
33
|
+
| 'float64'
|
70
34
|
|
71
|
-
export function
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
35
|
+
export function getTypedArrayElementType(
|
36
|
+
array: TypedArray
|
37
|
+
): TypedArrayElementType {
|
38
|
+
// prettier-ignore
|
39
|
+
const type = (
|
40
|
+
array instanceof Int8Array ? 'int8' :
|
41
|
+
array instanceof Uint8Array ? 'uint8' :
|
42
|
+
array instanceof Uint8ClampedArray ? 'uint8' :
|
43
|
+
array instanceof Int16Array ? 'int16' :
|
44
|
+
array instanceof Uint16Array ? 'uint16' :
|
45
|
+
array instanceof Int32Array ? 'int32' :
|
46
|
+
array instanceof Uint32Array ? 'uint32' :
|
47
|
+
array instanceof Float32Array ? 'float32' :
|
48
|
+
array instanceof Float64Array ? 'float64' :
|
49
|
+
null
|
50
|
+
)
|
51
|
+
invariant(type != null)
|
52
|
+
return type
|
76
53
|
}
|
@@ -0,0 +1,77 @@
|
|
1
|
+
import { type TypedArray, type TypedArrayConstructor } from './typedArray'
|
2
|
+
|
3
|
+
type GetValue = keyof {
|
4
|
+
[K in keyof DataView as DataView[K] extends (byteOffset: number) => number
|
5
|
+
? K
|
6
|
+
: never]: DataView[K]
|
7
|
+
}
|
8
|
+
|
9
|
+
function parseTypedArray<T extends TypedArrayConstructor, K extends GetValue>(
|
10
|
+
buffer: ArrayBuffer,
|
11
|
+
TypedArray: T,
|
12
|
+
getValue: K,
|
13
|
+
littleEndian?: boolean
|
14
|
+
): InstanceType<T>
|
15
|
+
|
16
|
+
function parseTypedArray<K extends GetValue>(
|
17
|
+
buffer: ArrayBuffer,
|
18
|
+
TypedArray: TypedArrayConstructor,
|
19
|
+
getValue: K,
|
20
|
+
littleEndian = true
|
21
|
+
): TypedArray {
|
22
|
+
const data = new DataView(buffer)
|
23
|
+
const array = new TypedArray(data.byteLength / TypedArray.BYTES_PER_ELEMENT)
|
24
|
+
for (
|
25
|
+
let index = 0, byteIndex = 0;
|
26
|
+
index < array.length;
|
27
|
+
++index, byteIndex += TypedArray.BYTES_PER_ELEMENT
|
28
|
+
) {
|
29
|
+
array[index] = data[getValue](byteIndex, littleEndian)
|
30
|
+
}
|
31
|
+
return array
|
32
|
+
}
|
33
|
+
|
34
|
+
export type TypedArrayParser<T extends TypedArray> = (
|
35
|
+
buffer: ArrayBuffer,
|
36
|
+
littleEndian?: boolean
|
37
|
+
) => T
|
38
|
+
|
39
|
+
export const parseUint8Array: TypedArrayParser<Uint8Array> = (
|
40
|
+
buffer,
|
41
|
+
littleEndian
|
42
|
+
) => parseTypedArray(buffer, Uint8Array, 'getUint8', littleEndian)
|
43
|
+
|
44
|
+
export const parseInt8Array: TypedArrayParser<Int8Array> = (
|
45
|
+
buffer,
|
46
|
+
littleEndian
|
47
|
+
) => parseTypedArray(buffer, Int8Array, 'getInt8', littleEndian)
|
48
|
+
|
49
|
+
export const parseUint16Array: TypedArrayParser<Uint16Array> = (
|
50
|
+
buffer,
|
51
|
+
littleEndian
|
52
|
+
) => parseTypedArray(buffer, Uint16Array, 'getUint16', littleEndian)
|
53
|
+
|
54
|
+
export const parseInt16Array: TypedArrayParser<Int16Array> = (
|
55
|
+
buffer,
|
56
|
+
littleEndian
|
57
|
+
) => parseTypedArray(buffer, Int16Array, 'getInt16', littleEndian)
|
58
|
+
|
59
|
+
export const parseInt32Array: TypedArrayParser<Int32Array> = (
|
60
|
+
buffer,
|
61
|
+
littleEndian
|
62
|
+
) => parseTypedArray(buffer, Int32Array, 'getInt32', littleEndian)
|
63
|
+
|
64
|
+
export const parseUint32Array: TypedArrayParser<Uint32Array> = (
|
65
|
+
buffer,
|
66
|
+
littleEndian
|
67
|
+
) => parseTypedArray(buffer, Uint32Array, 'getUint32', littleEndian)
|
68
|
+
|
69
|
+
export const parseFloat32Array: TypedArrayParser<Float32Array> = (
|
70
|
+
buffer,
|
71
|
+
littleEndian
|
72
|
+
) => parseTypedArray(buffer, Float32Array, 'getFloat32', littleEndian)
|
73
|
+
|
74
|
+
export const parseFloat64Array: TypedArrayParser<Float64Array> = (
|
75
|
+
buffer,
|
76
|
+
littleEndian
|
77
|
+
) => parseTypedArray(buffer, Float64Array, 'getFloat64', littleEndian)
|
package/src/types.ts
CHANGED
@@ -1,54 +1,8 @@
|
|
1
|
-
import {
|
2
|
-
type Matrix2,
|
3
|
-
type Matrix3,
|
4
|
-
type Matrix4,
|
5
|
-
type Vector2,
|
6
|
-
type Vector3,
|
7
|
-
type Vector4
|
8
|
-
} from 'three'
|
9
|
-
import { type ReadonlyTuple } from 'type-fest'
|
1
|
+
import { type Uniform } from 'three'
|
10
2
|
|
11
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
12
3
|
export type Callable = (...args: any) => any
|
13
4
|
|
14
|
-
export type
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
// Non-readonly version of ReadonlyTuple. This is not type-safe because mutable
|
19
|
-
// methods still exist in the type.
|
20
|
-
// See https://github.com/sindresorhus/type-fest/blob/main/source/readonly-tuple.d.ts
|
21
|
-
type BuildTupleHelper<
|
22
|
-
Element,
|
23
|
-
Length extends number,
|
24
|
-
Rest extends Element[]
|
25
|
-
> = Rest['length'] extends Length
|
26
|
-
? [...Rest]
|
27
|
-
: BuildTupleHelper<Element, Length, [Element, ...Rest]>
|
28
|
-
|
29
|
-
export type Tuple<T, Length extends number> = number extends Length
|
30
|
-
? readonly T[]
|
31
|
-
: BuildTupleHelper<T, Length, []>
|
32
|
-
|
33
|
-
export type Tuple2<T = number> = BuildTupleHelper<T, 2, []>
|
34
|
-
export type Tuple3<T = number> = BuildTupleHelper<T, 3, []>
|
35
|
-
export type Tuple4<T = number> = BuildTupleHelper<T, 4, []>
|
36
|
-
|
37
|
-
// Suppose return type of the mutable methods of classes like Vector3 is `this`.
|
38
|
-
// TODO: How can we specify `this` as a constraint?
|
39
|
-
type ReadonlyThreeInstance<T> = Readonly<{
|
40
|
-
[K in keyof T as T[K] extends Callable
|
41
|
-
? ReturnType<T[K]> extends T
|
42
|
-
? K extends 'clone'
|
43
|
-
? K
|
44
|
-
: never
|
45
|
-
: K
|
46
|
-
: K]: T[K]
|
47
|
-
}>
|
48
|
-
|
49
|
-
export type ReadonlyVector2 = ReadonlyThreeInstance<Vector2>
|
50
|
-
export type ReadonlyVector3 = ReadonlyThreeInstance<Vector3>
|
51
|
-
export type ReadonlyVector4 = ReadonlyThreeInstance<Vector4>
|
52
|
-
export type ReadonlyMatrix2 = ReadonlyThreeInstance<Matrix2>
|
53
|
-
export type ReadonlyMatrix3 = ReadonlyThreeInstance<Matrix3>
|
54
|
-
export type ReadonlyMatrix4 = ReadonlyThreeInstance<Matrix4>
|
5
|
+
export type UniformMap<T> = Omit<Map<string, Uniform>, 'get'> & {
|
6
|
+
get: <K extends keyof T>(key: K) => T[K]
|
7
|
+
set: <K extends keyof T>(key: K, value: T[K]) => void
|
8
|
+
}
|
@@ -0,0 +1,23 @@
|
|
1
|
+
// Based on: https://github.com/mrdoob/three.js/blob/r170/src/renderers/webgl/WebGLProgram.js#L294
|
2
|
+
|
3
|
+
const unrollLoopPattern =
|
4
|
+
/#pragma unroll_loop_start\s+for\s*\(\s*int\s+i\s*=\s*(\d+)\s*;\s*i\s*<\s*(\d+)\s*;\s*(?:i\s*\+\+|\+\+\s*i)\s*\)\s*{([\s\S]+?)}\s+#pragma unroll_loop_end/g
|
5
|
+
|
6
|
+
function loopReplacer(
|
7
|
+
match: string,
|
8
|
+
start: string,
|
9
|
+
end: string,
|
10
|
+
snippet: string
|
11
|
+
): string {
|
12
|
+
let string = ''
|
13
|
+
for (let i = parseInt(start); i < parseInt(end); ++i) {
|
14
|
+
string += snippet
|
15
|
+
.replace(/\[\s*i\s*\]/g, '[' + i + ']')
|
16
|
+
.replace(/UNROLLED_LOOP_INDEX/g, `${i}`)
|
17
|
+
}
|
18
|
+
return string
|
19
|
+
}
|
20
|
+
|
21
|
+
export function unrollLoops(string: string): string {
|
22
|
+
return string.replace(unrollLoopPattern, loopReplacer)
|
23
|
+
}
|