react-native-wgpu 0.2.10 → 0.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +155 -69
- package/android/CMakeLists.txt +5 -7
- package/android/build.gradle +7 -18
- package/android/src/main/java/com/webgpu/WebGPUViewPackage.java +34 -10
- package/apple/MetalView.mm +0 -19
- package/apple/WebGPUModule.h +1 -9
- package/apple/WebGPUModule.mm +0 -3
- package/apple/WebGPUView.h +0 -3
- package/apple/WebGPUView.mm +0 -2
- package/cpp/WGPULogger.h +10 -0
- package/cpp/dawn/dawn_proc_table.h +1 -1
- package/cpp/dawn/webgpu.h +4855 -0
- package/cpp/dawn/webgpu_cpp.h +10168 -0
- package/cpp/dawn/wire/client/webgpu.h +354 -0
- package/cpp/dawn/wire/client/webgpu_cpp.h +10343 -0
- package/cpp/dawn/wire/client/webgpu_cpp_print.h +2715 -0
- package/cpp/jsi/RNFHybridObject.cpp +8 -4
- package/cpp/jsi/RNFHybridObject.h +7 -2
- package/cpp/jsi/RNFJSIConverter.h +13 -102
- package/cpp/jsi/RNFJSIHelper.h +5 -3
- package/cpp/jsi/RNFRuntimeState.cpp +18 -0
- package/cpp/jsi/RNFRuntimeState.h +106 -0
- package/cpp/rnwgpu/RNWebGPUManager.cpp +1 -9
- package/cpp/rnwgpu/api/GPU.cpp +51 -26
- package/cpp/rnwgpu/api/GPU.h +5 -18
- package/cpp/rnwgpu/api/GPUAdapter.cpp +79 -54
- package/cpp/rnwgpu/api/GPUAdapter.h +6 -6
- package/cpp/rnwgpu/api/GPUAdapterInfo.h +0 -1
- package/cpp/rnwgpu/api/GPUBindGroup.h +1 -3
- package/cpp/rnwgpu/api/GPUBindGroupLayout.h +1 -3
- package/cpp/rnwgpu/api/GPUBuffer.cpp +35 -32
- package/cpp/rnwgpu/api/GPUBuffer.h +9 -7
- package/cpp/rnwgpu/api/GPUCanvasContext.cpp +5 -1
- package/cpp/rnwgpu/api/GPUCanvasContext.h +0 -2
- package/cpp/rnwgpu/api/GPUCommandBuffer.h +1 -3
- package/cpp/rnwgpu/api/GPUCommandEncoder.h +1 -3
- package/cpp/rnwgpu/api/GPUCompilationInfo.h +0 -2
- package/cpp/rnwgpu/api/GPUCompilationMessage.h +1 -3
- package/cpp/rnwgpu/api/GPUComputePassEncoder.h +1 -3
- package/cpp/rnwgpu/api/GPUComputePipeline.h +1 -3
- package/cpp/rnwgpu/api/GPUDevice.cpp +183 -128
- package/cpp/rnwgpu/api/GPUDevice.h +22 -21
- package/cpp/rnwgpu/api/GPUDeviceLostInfo.h +1 -3
- package/cpp/rnwgpu/api/GPUExternalTexture.h +1 -3
- package/cpp/rnwgpu/api/GPUPipelineLayout.h +1 -3
- package/cpp/rnwgpu/api/GPUQuerySet.h +1 -3
- package/cpp/rnwgpu/api/GPUQueue.cpp +19 -8
- package/cpp/rnwgpu/api/GPUQueue.h +7 -6
- package/cpp/rnwgpu/api/GPURenderBundle.h +1 -3
- package/cpp/rnwgpu/api/GPURenderBundleEncoder.h +1 -3
- package/cpp/rnwgpu/api/GPURenderPassEncoder.h +1 -3
- package/cpp/rnwgpu/api/GPURenderPipeline.h +1 -3
- package/cpp/rnwgpu/api/GPUSampler.h +1 -3
- package/cpp/rnwgpu/api/GPUShaderModule.cpp +42 -28
- package/cpp/rnwgpu/api/GPUShaderModule.h +6 -6
- package/cpp/rnwgpu/api/GPUSupportedLimits.h +1 -3
- package/cpp/rnwgpu/api/GPUTexture.h +1 -3
- package/cpp/rnwgpu/api/GPUTextureView.h +1 -3
- package/cpp/rnwgpu/api/RNWebGPU.h +1 -7
- package/cpp/rnwgpu/async/AsyncDispatcher.h +28 -0
- package/cpp/rnwgpu/async/AsyncRunner.cpp +215 -0
- package/cpp/rnwgpu/async/AsyncRunner.h +53 -0
- package/cpp/rnwgpu/async/AsyncTaskHandle.cpp +181 -0
- package/cpp/rnwgpu/async/AsyncTaskHandle.h +55 -0
- package/cpp/rnwgpu/async/JSIMicrotaskDispatcher.cpp +23 -0
- package/cpp/rnwgpu/async/JSIMicrotaskDispatcher.h +22 -0
- package/cpp/webgpu/webgpu.h +5 -4827
- package/cpp/webgpu/webgpu_cpp.h +5 -10140
- package/cpp/{dawn/native/WebGPUBackend.h → webgpu/webgpu_cpp_print.h} +4 -20
- package/lib/commonjs/Canvas.js +6 -66
- package/lib/commonjs/Canvas.js.map +1 -1
- package/lib/commonjs/hooks.js +6 -42
- package/lib/commonjs/hooks.js.map +1 -1
- package/lib/module/Canvas.js +7 -67
- package/lib/module/Canvas.js.map +1 -1
- package/lib/module/hooks.js +5 -40
- package/lib/module/hooks.js.map +1 -1
- package/lib/typescript/lib/commonjs/hooks.d.ts +1 -5
- package/lib/typescript/lib/commonjs/hooks.d.ts.map +1 -1
- package/lib/typescript/lib/module/Canvas.d.ts.map +1 -1
- package/lib/typescript/lib/module/hooks.d.ts +1 -5
- package/lib/typescript/lib/module/hooks.d.ts.map +1 -1
- package/lib/typescript/src/Canvas.d.ts +0 -1
- package/lib/typescript/src/Canvas.d.ts.map +1 -1
- package/lib/typescript/src/hooks.d.ts +2 -7
- package/lib/typescript/src/hooks.d.ts.map +1 -1
- package/libs/android/arm64-v8a/libwebgpu_dawn.so +0 -0
- package/libs/android/armeabi-v7a/libwebgpu_dawn.so +0 -0
- package/libs/android/x86/libwebgpu_dawn.so +0 -0
- package/libs/android/x86_64/libwebgpu_dawn.so +0 -0
- package/libs/apple/libwebgpu_dawn.xcframework/Info.plist +5 -35
- package/libs/apple/libwebgpu_dawn.xcframework/ios-arm64/libwebgpu_dawn.a +0 -0
- package/libs/apple/libwebgpu_dawn.xcframework/ios-arm64_x86_64-simulator/libwebgpu_dawn.a +0 -0
- package/libs/apple/libwebgpu_dawn.xcframework/macos-arm64_x86_64/libwebgpu_dawn.a +0 -0
- package/package.json +4 -3
- package/react-native-wgpu.podspec +12 -16
- package/src/Canvas.tsx +8 -69
- package/src/hooks.tsx +14 -48
- package/android/cpp/platform/ThreadUtils.cpp +0 -41
- package/android/src/oldarch/com/webgpu/NativeWebGPUModuleSpec.java +0 -23
- package/android/src/oldarch/com/webgpu/WebGPUViewManagerSpec.java +0 -12
- package/apple/WebGPUViewManager.mm +0 -24
- package/apple/platform/ThreadUtils.cpp +0 -34
- package/cpp/dawn/dawn_proc.h +0 -50
- package/cpp/dawn/dawn_thread_dispatch_proc.h +0 -47
- package/cpp/dawn/native/D3D11Backend.h +0 -77
- package/cpp/dawn/native/D3D12Backend.h +0 -68
- package/cpp/dawn/native/D3DBackend.h +0 -56
- package/cpp/dawn/native/MetalBackend.h +0 -56
- package/cpp/dawn/platform/DawnPlatform.h +0 -167
- package/cpp/dawn/platform/dawn_platform_export.h +0 -49
- package/cpp/jsi/RNFRuntimeCache.cpp +0 -57
- package/cpp/jsi/RNFRuntimeCache.h +0 -79
- package/cpp/jsi/RNFWorkletRuntimeCollector.h +0 -43
- package/cpp/jsi/RNFWorkletRuntimeRegistry.cpp +0 -12
- package/cpp/jsi/RNFWorkletRuntimeRegistry.h +0 -44
- package/cpp/platform/ThreadUtils.h +0 -30
- package/cpp/rnwgpu/api/AsyncRunner.h +0 -30
- package/cpp/threading/CallInvokerDispatcher.h +0 -37
- package/cpp/threading/Dispatcher.cpp +0 -55
- package/cpp/threading/Dispatcher.h +0 -93
- package/cpp/threading/ThreadPool.cpp +0 -88
- package/cpp/threading/ThreadPool.h +0 -53
- package/cpp/webgpu/webgpu_glfw.h +0 -88
- package/lib/typescript/src/__tests__/Alpha.spec.d.ts +0 -2
- package/lib/typescript/src/__tests__/Alpha.spec.d.ts.map +0 -1
- package/lib/typescript/src/__tests__/ArrayBuffer.spec.d.ts +0 -2
- package/lib/typescript/src/__tests__/ArrayBuffer.spec.d.ts.map +0 -1
- package/lib/typescript/src/__tests__/Buffer.spec.d.ts +0 -2
- package/lib/typescript/src/__tests__/Buffer.spec.d.ts.map +0 -1
- package/lib/typescript/src/__tests__/ComputeShader.spec.d.ts +0 -2
- package/lib/typescript/src/__tests__/ComputeShader.spec.d.ts.map +0 -1
- package/lib/typescript/src/__tests__/Constants.spec.d.ts +0 -2
- package/lib/typescript/src/__tests__/Constants.spec.d.ts.map +0 -1
- package/lib/typescript/src/__tests__/Device.spec.d.ts +0 -2
- package/lib/typescript/src/__tests__/Device.spec.d.ts.map +0 -1
- package/lib/typescript/src/__tests__/ErrorScope.spec.d.ts +0 -2
- package/lib/typescript/src/__tests__/ErrorScope.spec.d.ts.map +0 -1
- package/lib/typescript/src/__tests__/ExternalTexture.spec.d.ts +0 -2
- package/lib/typescript/src/__tests__/ExternalTexture.spec.d.ts.map +0 -1
- package/lib/typescript/src/__tests__/GPU.spec.d.ts +0 -2
- package/lib/typescript/src/__tests__/GPU.spec.d.ts.map +0 -1
- package/lib/typescript/src/__tests__/ImageData.spec.d.ts +0 -2
- package/lib/typescript/src/__tests__/ImageData.spec.d.ts.map +0 -1
- package/lib/typescript/src/__tests__/Shaders.spec.d.ts +0 -2
- package/lib/typescript/src/__tests__/Shaders.spec.d.ts.map +0 -1
- package/lib/typescript/src/__tests__/Texture.spec.d.ts +0 -2
- package/lib/typescript/src/__tests__/Texture.spec.d.ts.map +0 -1
- package/lib/typescript/src/__tests__/components/Wireframe/Shaders.d.ts +0 -3
- package/lib/typescript/src/__tests__/components/Wireframe/Shaders.d.ts.map +0 -1
- package/lib/typescript/src/__tests__/components/Wireframe/models.d.ts +0 -29
- package/lib/typescript/src/__tests__/components/Wireframe/models.d.ts.map +0 -1
- package/lib/typescript/src/__tests__/components/Wireframe/utils.d.ts +0 -5
- package/lib/typescript/src/__tests__/components/Wireframe/utils.d.ts.map +0 -1
- package/lib/typescript/src/__tests__/components/cube.d.ts +0 -7
- package/lib/typescript/src/__tests__/components/cube.d.ts.map +0 -1
- package/lib/typescript/src/__tests__/components/meshes/mesh.d.ts +0 -22
- package/lib/typescript/src/__tests__/components/meshes/mesh.d.ts.map +0 -1
- package/lib/typescript/src/__tests__/components/meshes/sphere.d.ts +0 -12
- package/lib/typescript/src/__tests__/components/meshes/sphere.d.ts.map +0 -1
- package/lib/typescript/src/__tests__/components/meshes/stanfordDragon.d.ts +0 -7
- package/lib/typescript/src/__tests__/components/meshes/stanfordDragon.d.ts.map +0 -1
- package/lib/typescript/src/__tests__/components/meshes/stanfordDragonData.d.ts +0 -6
- package/lib/typescript/src/__tests__/components/meshes/stanfordDragonData.d.ts.map +0 -1
- package/lib/typescript/src/__tests__/components/meshes/teapot.d.ts +0 -6
- package/lib/typescript/src/__tests__/components/meshes/teapot.d.ts.map +0 -1
- package/lib/typescript/src/__tests__/components/meshes/utils.d.ts +0 -10
- package/lib/typescript/src/__tests__/components/meshes/utils.d.ts.map +0 -1
- package/lib/typescript/src/__tests__/components/triangle.d.ts +0 -3
- package/lib/typescript/src/__tests__/components/triangle.d.ts.map +0 -1
- package/lib/typescript/src/__tests__/config.d.ts +0 -3
- package/lib/typescript/src/__tests__/config.d.ts.map +0 -1
- package/lib/typescript/src/__tests__/demos/ABuffer.spec.d.ts +0 -2
- package/lib/typescript/src/__tests__/demos/ABuffer.spec.d.ts.map +0 -1
- package/lib/typescript/src/__tests__/demos/Blur.spec.d.ts +0 -2
- package/lib/typescript/src/__tests__/demos/Blur.spec.d.ts.map +0 -1
- package/lib/typescript/src/__tests__/demos/Cube.spec.d.ts +0 -2
- package/lib/typescript/src/__tests__/demos/Cube.spec.d.ts.map +0 -1
- package/lib/typescript/src/__tests__/demos/FractalCube.spec.d.ts +0 -2
- package/lib/typescript/src/__tests__/demos/FractalCube.spec.d.ts.map +0 -1
- package/lib/typescript/src/__tests__/demos/OcclusionQuery.spec.d.ts +0 -2
- package/lib/typescript/src/__tests__/demos/OcclusionQuery.spec.d.ts.map +0 -1
- package/lib/typescript/src/__tests__/demos/RenderBundles.spec.d.ts +0 -2
- package/lib/typescript/src/__tests__/demos/RenderBundles.spec.d.ts.map +0 -1
- package/lib/typescript/src/__tests__/demos/Triangle.spec.d.ts +0 -2
- package/lib/typescript/src/__tests__/demos/Triangle.spec.d.ts.map +0 -1
- package/lib/typescript/src/__tests__/demos/Wireframe.spec.d.ts +0 -2
- package/lib/typescript/src/__tests__/demos/Wireframe.spec.d.ts.map +0 -1
- package/lib/typescript/src/__tests__/globalSetup.d.ts +0 -3
- package/lib/typescript/src/__tests__/globalSetup.d.ts.map +0 -1
- package/lib/typescript/src/__tests__/globalTeardown.d.ts +0 -3
- package/lib/typescript/src/__tests__/globalTeardown.d.ts.map +0 -1
- package/lib/typescript/src/__tests__/setup.d.ts +0 -63
- package/lib/typescript/src/__tests__/setup.d.ts.map +0 -1
- package/libs/apple/arm64_iphoneos/libwebgpu_dawn.a +0 -0
- package/libs/apple/arm64_iphonesimulator/libwebgpu_dawn.a +0 -0
- package/libs/apple/arm64_xros/libwebgpu_dawn.a +0 -0
- package/libs/apple/arm64_xrsimulator/libwebgpu_dawn.a +0 -0
- package/libs/apple/iphonesimulator/libwebgpu_dawn.a +0 -0
- package/libs/apple/libwebgpu_dawn.xcframework/xros-arm64/libwebgpu_dawn.a +0 -0
- package/libs/apple/libwebgpu_dawn.xcframework/xros-arm64-simulator/libwebgpu_dawn.a +0 -0
- package/libs/apple/universal_macosx/libwebgpu_dawn.a +0 -0
- package/libs/apple/x86_64_iphonesimulator/libwebgpu_dawn.a +0 -0
- package/libs/dawn.json +0 -4670
- package/src/__tests__/Alpha.spec.ts +0 -28
- package/src/__tests__/ArrayBuffer.spec.ts +0 -76
- package/src/__tests__/Buffer.spec.ts +0 -357
- package/src/__tests__/ComputeShader.spec.ts +0 -375
- package/src/__tests__/Constants.spec.ts +0 -89
- package/src/__tests__/Device.spec.ts +0 -84
- package/src/__tests__/ErrorScope.spec.ts +0 -92
- package/src/__tests__/ExternalTexture.spec.ts +0 -284
- package/src/__tests__/GPU.spec.ts +0 -272
- package/src/__tests__/ImageData.spec.ts +0 -26
- package/src/__tests__/Shaders.spec.ts +0 -232
- package/src/__tests__/Texture.spec.ts +0 -197
- package/src/__tests__/assets/Di-3d.png +0 -0
- package/src/__tests__/components/Wireframe/Shaders.ts +0 -138
- package/src/__tests__/components/Wireframe/models.ts +0 -113
- package/src/__tests__/components/Wireframe/utils.ts +0 -22
- package/src/__tests__/components/cube.ts +0 -51
- package/src/__tests__/components/meshes/mesh.ts +0 -96
- package/src/__tests__/components/meshes/sphere.ts +0 -103
- package/src/__tests__/components/meshes/stanfordDragon.ts +0 -44
- package/src/__tests__/components/meshes/stanfordDragonData.ts +0 -5
- package/src/__tests__/components/meshes/teapot.ts +0 -13
- package/src/__tests__/components/meshes/utils.ts +0 -235
- package/src/__tests__/components/triangle.ts +0 -17
- package/src/__tests__/config.ts +0 -2
- package/src/__tests__/demos/ABuffer.spec.ts +0 -890
- package/src/__tests__/demos/Blur.spec.ts +0 -398
- package/src/__tests__/demos/Cube.spec.ts +0 -929
- package/src/__tests__/demos/FractalCube.spec.ts +0 -240
- package/src/__tests__/demos/OcclusionQuery.spec.ts +0 -376
- package/src/__tests__/demos/RenderBundles.spec.ts +0 -580
- package/src/__tests__/demos/Triangle.spec.ts +0 -266
- package/src/__tests__/demos/Wireframe.spec.ts +0 -188
- package/src/__tests__/globalSetup.ts +0 -45
- package/src/__tests__/globalTeardown.ts +0 -11
- package/src/__tests__/setup.ts +0 -423
- package/src/__tests__/snapshots/abuffer.png +0 -0
- package/src/__tests__/snapshots/asteroid.png +0 -0
- package/src/__tests__/snapshots/blur.png +0 -0
- package/src/__tests__/snapshots/buffer.png +0 -0
- package/src/__tests__/snapshots/constant-triangle.png +0 -0
- package/src/__tests__/snapshots/cube.png +0 -0
- package/src/__tests__/snapshots/f.png +0 -0
- package/src/__tests__/snapshots/f2.png +0 -0
- package/src/__tests__/snapshots/fractal-cubes.png +0 -0
- package/src/__tests__/snapshots/instanced-cubes.png +0 -0
- package/src/__tests__/snapshots/occlusion-query.png +0 -0
- package/src/__tests__/snapshots/ref.png +0 -0
- package/src/__tests__/snapshots/semi-opaque-cyan.png +0 -0
- package/src/__tests__/snapshots/texture.png +0 -0
- package/src/__tests__/snapshots/textured-cube.png +0 -0
- package/src/__tests__/snapshots/triangle-msaa.png +0 -0
- package/src/__tests__/snapshots/triangle.png +0 -0
- package/src/__tests__/snapshots/two-cube.png +0 -0
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
// @ts-expect-error
|
|
2
|
-
import teapotData from "teapot";
|
|
3
|
-
|
|
4
|
-
import { computeSurfaceNormals } from "./utils";
|
|
5
|
-
|
|
6
|
-
export const mesh = {
|
|
7
|
-
positions: teapotData.positions as [number, number, number][],
|
|
8
|
-
triangles: teapotData.cells as [number, number, number][],
|
|
9
|
-
normals: [] as [number, number, number][],
|
|
10
|
-
};
|
|
11
|
-
|
|
12
|
-
// Compute surface normals
|
|
13
|
-
mesh.normals = computeSurfaceNormals(mesh.positions, mesh.triangles);
|
|
@@ -1,235 +0,0 @@
|
|
|
1
|
-
import type { Vec3 } from "wgpu-matrix";
|
|
2
|
-
import { vec3 } from "wgpu-matrix";
|
|
3
|
-
|
|
4
|
-
export function computeSurfaceNormals(
|
|
5
|
-
positions: [number, number, number][],
|
|
6
|
-
triangles: [number, number, number][],
|
|
7
|
-
): [number, number, number][] {
|
|
8
|
-
const normals: [number, number, number][] = positions.map(() => {
|
|
9
|
-
// Initialize to zero.
|
|
10
|
-
return [0, 0, 0];
|
|
11
|
-
});
|
|
12
|
-
triangles.forEach(([i0, i1, i2]) => {
|
|
13
|
-
const p0 = positions[i0];
|
|
14
|
-
const p1 = positions[i1];
|
|
15
|
-
const p2 = positions[i2];
|
|
16
|
-
|
|
17
|
-
const v0 = vec3.subtract(p1, p0);
|
|
18
|
-
const v1 = vec3.subtract(p2, p0);
|
|
19
|
-
|
|
20
|
-
vec3.normalize(v0, v0);
|
|
21
|
-
vec3.normalize(v1, v1);
|
|
22
|
-
const norm = vec3.cross(v0, v1);
|
|
23
|
-
|
|
24
|
-
// Accumulate the normals.
|
|
25
|
-
vec3.add(normals[i0], norm, normals[i0]);
|
|
26
|
-
vec3.add(normals[i1], norm, normals[i1]);
|
|
27
|
-
vec3.add(normals[i2], norm, normals[i2]);
|
|
28
|
-
});
|
|
29
|
-
normals.forEach((n) => {
|
|
30
|
-
// Normalize accumulated normals.
|
|
31
|
-
vec3.normalize(n, n);
|
|
32
|
-
});
|
|
33
|
-
|
|
34
|
-
return normals;
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
function makeTriangleIndicesFn(triangles: [number, number, number][]) {
|
|
38
|
-
let triNdx = 0;
|
|
39
|
-
let vNdx = 0;
|
|
40
|
-
const fn = function () {
|
|
41
|
-
const ndx = triangles[triNdx][vNdx++];
|
|
42
|
-
if (vNdx === 3) {
|
|
43
|
-
vNdx = 0;
|
|
44
|
-
++triNdx;
|
|
45
|
-
}
|
|
46
|
-
return ndx;
|
|
47
|
-
};
|
|
48
|
-
fn.reset = function () {
|
|
49
|
-
triNdx = 0;
|
|
50
|
-
vNdx = 0;
|
|
51
|
-
};
|
|
52
|
-
fn.numElements = triangles.length * 3;
|
|
53
|
-
return fn;
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
// adapted from: https://webglfundamentals.org/webgl/lessons/webgl-3d-geometry-lathe.htmls
|
|
57
|
-
export function generateNormals(
|
|
58
|
-
maxAngle: number,
|
|
59
|
-
positions: [number, number, number][],
|
|
60
|
-
triangles: [number, number, number][],
|
|
61
|
-
) {
|
|
62
|
-
// first compute the normal of each face
|
|
63
|
-
const getNextIndex = makeTriangleIndicesFn(triangles);
|
|
64
|
-
const numFaceVerts = getNextIndex.numElements;
|
|
65
|
-
const numVerts = positions.length;
|
|
66
|
-
const numFaces = numFaceVerts / 3;
|
|
67
|
-
const faceNormals: Vec3[] = [];
|
|
68
|
-
|
|
69
|
-
// Compute the normal for every face.
|
|
70
|
-
// While doing that, create a new vertex for every face vertex
|
|
71
|
-
for (let i = 0; i < numFaces; ++i) {
|
|
72
|
-
const n1 = getNextIndex();
|
|
73
|
-
const n2 = getNextIndex();
|
|
74
|
-
const n3 = getNextIndex();
|
|
75
|
-
|
|
76
|
-
const v1 = positions[n1];
|
|
77
|
-
const v2 = positions[n2];
|
|
78
|
-
const v3 = positions[n3];
|
|
79
|
-
|
|
80
|
-
faceNormals.push(
|
|
81
|
-
vec3.normalize(vec3.cross(vec3.subtract(v2, v1), vec3.subtract(v3, v1))),
|
|
82
|
-
);
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
let tempVerts: any = {};
|
|
86
|
-
let tempVertNdx = 0;
|
|
87
|
-
|
|
88
|
-
// this assumes vertex positions are an exact match
|
|
89
|
-
|
|
90
|
-
function getVertIndex(vert: [number, number, number]): number {
|
|
91
|
-
const vertId = JSON.stringify(vert);
|
|
92
|
-
const ndx = tempVerts[vertId];
|
|
93
|
-
if (ndx !== undefined) {
|
|
94
|
-
return ndx;
|
|
95
|
-
}
|
|
96
|
-
const newNdx = tempVertNdx++;
|
|
97
|
-
tempVerts[vertId] = newNdx;
|
|
98
|
-
return newNdx;
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
// We need to figure out the shared vertices.
|
|
102
|
-
// It's not as simple as looking at the faces (triangles)
|
|
103
|
-
// because for example if we have a standard cylinder
|
|
104
|
-
//
|
|
105
|
-
//
|
|
106
|
-
// 3-4
|
|
107
|
-
// / \
|
|
108
|
-
// 2 5 Looking down a cylinder starting at S
|
|
109
|
-
// | | and going around to E, E and S are not
|
|
110
|
-
// 1 6 the same vertex in the data we have
|
|
111
|
-
// \ / as they don't share UV coords.
|
|
112
|
-
// S/E
|
|
113
|
-
//
|
|
114
|
-
// the vertices at the start and end do not share vertices
|
|
115
|
-
// since they have different UVs but if you don't consider
|
|
116
|
-
// them to share vertices they will get the wrong normals
|
|
117
|
-
|
|
118
|
-
const vertIndices: number[] = [];
|
|
119
|
-
for (let i = 0; i < numVerts; ++i) {
|
|
120
|
-
const vert = positions[i];
|
|
121
|
-
vertIndices.push(getVertIndex(vert));
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
// go through every vertex and record which faces it's on
|
|
125
|
-
const vertFaces: number[][] = [];
|
|
126
|
-
getNextIndex.reset();
|
|
127
|
-
for (let i = 0; i < numFaces; ++i) {
|
|
128
|
-
for (let j = 0; j < 3; ++j) {
|
|
129
|
-
const ndx = getNextIndex();
|
|
130
|
-
const sharedNdx = vertIndices[ndx];
|
|
131
|
-
let faces = vertFaces[sharedNdx];
|
|
132
|
-
if (!faces) {
|
|
133
|
-
faces = [];
|
|
134
|
-
vertFaces[sharedNdx] = faces;
|
|
135
|
-
}
|
|
136
|
-
faces.push(i);
|
|
137
|
-
}
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
// now go through every face and compute the normals for each
|
|
141
|
-
// vertex of the face. Only include faces that aren't more than
|
|
142
|
-
// maxAngle different. Add the result to arrays of newPositions,
|
|
143
|
-
// newTexcoords and newNormals, discarding any vertices that
|
|
144
|
-
// are the same.
|
|
145
|
-
tempVerts = {};
|
|
146
|
-
tempVertNdx = 0;
|
|
147
|
-
const newPositions: [number, number, number][] = [];
|
|
148
|
-
const newNormals: [number, number, number][] = [];
|
|
149
|
-
|
|
150
|
-
function getNewVertIndex(
|
|
151
|
-
position: [number, number, number],
|
|
152
|
-
normal: [number, number, number],
|
|
153
|
-
) {
|
|
154
|
-
const vertId = JSON.stringify({ position, normal });
|
|
155
|
-
const ndx = tempVerts[vertId];
|
|
156
|
-
if (ndx !== undefined) {
|
|
157
|
-
return ndx;
|
|
158
|
-
}
|
|
159
|
-
const newNdx = tempVertNdx++;
|
|
160
|
-
tempVerts[vertId] = newNdx;
|
|
161
|
-
newPositions.push(position);
|
|
162
|
-
newNormals.push(normal);
|
|
163
|
-
return newNdx;
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
const newTriangles: [number, number, number][] = [];
|
|
167
|
-
getNextIndex.reset();
|
|
168
|
-
const maxAngleCos = Math.cos(maxAngle);
|
|
169
|
-
// for each face
|
|
170
|
-
for (let i = 0; i < numFaces; ++i) {
|
|
171
|
-
// get the normal for this face
|
|
172
|
-
const thisFaceNormal = faceNormals[i];
|
|
173
|
-
// for each vertex on the face
|
|
174
|
-
const newTriangle: number[] = [];
|
|
175
|
-
for (let j = 0; j < 3; ++j) {
|
|
176
|
-
const ndx = getNextIndex();
|
|
177
|
-
const sharedNdx = vertIndices[ndx];
|
|
178
|
-
const faces = vertFaces[sharedNdx];
|
|
179
|
-
const norm = [0, 0, 0] as [number, number, number];
|
|
180
|
-
faces.forEach((faceNdx: number) => {
|
|
181
|
-
// is this face facing the same way
|
|
182
|
-
const otherFaceNormal = faceNormals[faceNdx];
|
|
183
|
-
const dot = vec3.dot(thisFaceNormal, otherFaceNormal);
|
|
184
|
-
if (dot > maxAngleCos) {
|
|
185
|
-
vec3.add(norm, otherFaceNormal, norm);
|
|
186
|
-
}
|
|
187
|
-
});
|
|
188
|
-
vec3.normalize(norm, norm);
|
|
189
|
-
newTriangle.push(getNewVertIndex(positions[ndx], norm));
|
|
190
|
-
}
|
|
191
|
-
newTriangles.push(newTriangle as [number, number, number]);
|
|
192
|
-
}
|
|
193
|
-
|
|
194
|
-
return {
|
|
195
|
-
positions: newPositions,
|
|
196
|
-
normals: newNormals,
|
|
197
|
-
triangles: newTriangles,
|
|
198
|
-
};
|
|
199
|
-
}
|
|
200
|
-
|
|
201
|
-
type ProjectedPlane = "xy" | "xz" | "yz";
|
|
202
|
-
|
|
203
|
-
const projectedPlane2Ids: { [key in ProjectedPlane]: [number, number] } = {
|
|
204
|
-
xy: [0, 1],
|
|
205
|
-
xz: [0, 2],
|
|
206
|
-
yz: [1, 2],
|
|
207
|
-
};
|
|
208
|
-
|
|
209
|
-
export function computeProjectedPlaneUVs(
|
|
210
|
-
positions: [number, number, number][],
|
|
211
|
-
projectedPlane: ProjectedPlane = "xy",
|
|
212
|
-
): [number, number][] {
|
|
213
|
-
const idxs = projectedPlane2Ids[projectedPlane];
|
|
214
|
-
const uvs: [number, number][] = positions.map(() => {
|
|
215
|
-
// Initialize to zero.
|
|
216
|
-
return [0, 0];
|
|
217
|
-
});
|
|
218
|
-
const extentMin = [Infinity, Infinity];
|
|
219
|
-
const extentMax = [-Infinity, -Infinity];
|
|
220
|
-
positions.forEach((pos, i) => {
|
|
221
|
-
// Simply project to the selected plane
|
|
222
|
-
uvs[i][0] = pos[idxs[0]];
|
|
223
|
-
uvs[i][1] = pos[idxs[1]];
|
|
224
|
-
|
|
225
|
-
extentMin[0] = Math.min(pos[idxs[0]], extentMin[0]);
|
|
226
|
-
extentMin[1] = Math.min(pos[idxs[1]], extentMin[1]);
|
|
227
|
-
extentMax[0] = Math.max(pos[idxs[0]], extentMax[0]);
|
|
228
|
-
extentMax[1] = Math.max(pos[idxs[1]], extentMax[1]);
|
|
229
|
-
});
|
|
230
|
-
uvs.forEach((uv) => {
|
|
231
|
-
uv[0] = (uv[0] - extentMin[0]) / (extentMax[0] - extentMin[0]);
|
|
232
|
-
uv[1] = (uv[1] - extentMin[1]) / (extentMax[1] - extentMin[1]);
|
|
233
|
-
});
|
|
234
|
-
return uvs;
|
|
235
|
-
}
|
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
export const triangleVertWGSL = `@vertex
|
|
2
|
-
fn main(
|
|
3
|
-
@builtin(vertex_index) VertexIndex : u32
|
|
4
|
-
) -> @builtin(position) vec4f {
|
|
5
|
-
var pos = array<vec2f, 3>(
|
|
6
|
-
vec2(0.0, 0.5),
|
|
7
|
-
vec2(-0.5, -0.5),
|
|
8
|
-
vec2(0.5, -0.5)
|
|
9
|
-
);
|
|
10
|
-
|
|
11
|
-
return vec4f(pos[VertexIndex], 0.0, 1.0);
|
|
12
|
-
}`;
|
|
13
|
-
|
|
14
|
-
export const redFragWGSL = `@fragment
|
|
15
|
-
fn main() -> @location(0) vec4f {
|
|
16
|
-
return vec4(1.0, 0.0, 0.0, 1.0);
|
|
17
|
-
}`;
|
package/src/__tests__/config.ts
DELETED