react-native-wgpu 0.2.9 → 0.3.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/README.md +155 -69
- package/android/CMakeLists.txt +4 -5
- 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/RNFJSIConverter.h +17 -59
- 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 +75 -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/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,232 +0,0 @@
|
|
|
1
|
-
import { client } from "./setup";
|
|
2
|
-
|
|
3
|
-
describe("Triangle", () => {
|
|
4
|
-
it("creates a shader module (1)", async () => {
|
|
5
|
-
const result = await client.eval(
|
|
6
|
-
({ device, shaders: { triangleVertWGSL } }) => {
|
|
7
|
-
const mod = device.createShaderModule({
|
|
8
|
-
code: triangleVertWGSL,
|
|
9
|
-
label: "triangleVertWGSL",
|
|
10
|
-
});
|
|
11
|
-
return mod.label;
|
|
12
|
-
},
|
|
13
|
-
);
|
|
14
|
-
expect(result).toBe("triangleVertWGSL");
|
|
15
|
-
});
|
|
16
|
-
it("creates a shader module (2)", async () => {
|
|
17
|
-
const result = await client.eval(({ device, shaders: { redFragWGSL } }) => {
|
|
18
|
-
const mod = device.createShaderModule({
|
|
19
|
-
code: redFragWGSL,
|
|
20
|
-
label: "redFragWGSL",
|
|
21
|
-
});
|
|
22
|
-
return mod.label;
|
|
23
|
-
});
|
|
24
|
-
expect(result).toBe("redFragWGSL");
|
|
25
|
-
});
|
|
26
|
-
it("creates a shader module (3)", async () => {
|
|
27
|
-
const result = await client.eval(({ device, shaders: { redFragWGSL } }) => {
|
|
28
|
-
const mod = device.createShaderModule({
|
|
29
|
-
code: redFragWGSL,
|
|
30
|
-
label: "redFragWGSL",
|
|
31
|
-
});
|
|
32
|
-
return mod.getCompilationInfo().then((info) => info.messages.length);
|
|
33
|
-
});
|
|
34
|
-
// On Chrome the message property is undefined if no errors are present
|
|
35
|
-
expect(result).toBe(0);
|
|
36
|
-
});
|
|
37
|
-
// it("creates a shader module (4)", async () => {
|
|
38
|
-
// const result = await client.eval(({ device, shaders: { redFragWGSL } }) => {
|
|
39
|
-
// const mod = device.createShaderModule({
|
|
40
|
-
// code: redFragWGSL + " ++++ ",
|
|
41
|
-
// label: "redFragWGSL",
|
|
42
|
-
// });
|
|
43
|
-
// return mod.getCompilationInfo().then((info) => {
|
|
44
|
-
// const msg1 = info.messages[0];
|
|
45
|
-
// const msg2 = info.messages[1];
|
|
46
|
-
// return {
|
|
47
|
-
// messages: [
|
|
48
|
-
// { type: msg1.type, message: msg1.message },
|
|
49
|
-
// { type: msg2.type, message: msg2.message },
|
|
50
|
-
// ],
|
|
51
|
-
// };
|
|
52
|
-
// });
|
|
53
|
-
// });
|
|
54
|
-
// expect(result).toEqual({
|
|
55
|
-
// messages: [
|
|
56
|
-
// { type: "error", message: "unexpected token" },
|
|
57
|
-
// { type: "error", message: "unexpected token" },
|
|
58
|
-
// ],
|
|
59
|
-
// });
|
|
60
|
-
// });
|
|
61
|
-
it("create the pipeline", async () => {
|
|
62
|
-
const result = await client.eval(
|
|
63
|
-
({ device, shaders: { triangleVertWGSL, redFragWGSL } }) => {
|
|
64
|
-
const pipeline = device.createRenderPipeline({
|
|
65
|
-
layout: "auto",
|
|
66
|
-
vertex: {
|
|
67
|
-
module: device.createShaderModule({
|
|
68
|
-
code: triangleVertWGSL,
|
|
69
|
-
}),
|
|
70
|
-
},
|
|
71
|
-
fragment: {
|
|
72
|
-
module: device.createShaderModule({
|
|
73
|
-
code: redFragWGSL,
|
|
74
|
-
}),
|
|
75
|
-
targets: [
|
|
76
|
-
{
|
|
77
|
-
format: navigator.gpu.getPreferredCanvasFormat(),
|
|
78
|
-
},
|
|
79
|
-
],
|
|
80
|
-
},
|
|
81
|
-
primitive: {
|
|
82
|
-
topology: "triangle-list",
|
|
83
|
-
},
|
|
84
|
-
label: "trianglePipeline",
|
|
85
|
-
});
|
|
86
|
-
return pipeline.label;
|
|
87
|
-
},
|
|
88
|
-
);
|
|
89
|
-
expect(result).toBe("trianglePipeline");
|
|
90
|
-
});
|
|
91
|
-
it("create the pipeline and sample it", async () => {
|
|
92
|
-
const result = await client.eval(
|
|
93
|
-
({ device, shaders: { triangleVertWGSL, redFragWGSL }, gpu }) => {
|
|
94
|
-
// Create a texture to render to
|
|
95
|
-
const textureSize = 512;
|
|
96
|
-
const texture = device.createTexture({
|
|
97
|
-
size: [textureSize, textureSize],
|
|
98
|
-
format: gpu.getPreferredCanvasFormat(),
|
|
99
|
-
usage:
|
|
100
|
-
GPUTextureUsage.RENDER_ATTACHMENT | GPUTextureUsage.TEXTURE_BINDING,
|
|
101
|
-
});
|
|
102
|
-
|
|
103
|
-
// Create a pipeline for rendering to the texture
|
|
104
|
-
const texturePipeline = device.createRenderPipeline({
|
|
105
|
-
layout: "auto",
|
|
106
|
-
vertex: {
|
|
107
|
-
module: device.createShaderModule({
|
|
108
|
-
code: triangleVertWGSL,
|
|
109
|
-
}),
|
|
110
|
-
entryPoint: "main",
|
|
111
|
-
},
|
|
112
|
-
fragment: {
|
|
113
|
-
module: device.createShaderModule({
|
|
114
|
-
code: redFragWGSL,
|
|
115
|
-
}),
|
|
116
|
-
entryPoint: "main",
|
|
117
|
-
targets: [{ format: gpu.getPreferredCanvasFormat() }],
|
|
118
|
-
},
|
|
119
|
-
primitive: {
|
|
120
|
-
topology: "triangle-list",
|
|
121
|
-
},
|
|
122
|
-
});
|
|
123
|
-
|
|
124
|
-
// Create a pipeline for displaying the texture
|
|
125
|
-
const displayPipeline = device.createRenderPipeline({
|
|
126
|
-
layout: "auto",
|
|
127
|
-
vertex: {
|
|
128
|
-
module: device.createShaderModule({
|
|
129
|
-
code: `struct VertexOutput {
|
|
130
|
-
@builtin(position) position: vec4<f32>,
|
|
131
|
-
@location(0) texCoord: vec2<f32>,
|
|
132
|
-
};
|
|
133
|
-
|
|
134
|
-
@vertex
|
|
135
|
-
fn main(@builtin(vertex_index) vertexIndex: u32) -> VertexOutput {
|
|
136
|
-
var output: VertexOutput;
|
|
137
|
-
let x = f32(vertexIndex & 1u) * 2.0 - 1.0;
|
|
138
|
-
let y = f32(vertexIndex & 2u) - 1.0;
|
|
139
|
-
output.position = vec4<f32>(x, y, 0.0, 1.0);
|
|
140
|
-
output.texCoord = vec2<f32>((x + 1.0) * 0.5, (1.0 - y) * 0.5);
|
|
141
|
-
return output;
|
|
142
|
-
}`,
|
|
143
|
-
}),
|
|
144
|
-
entryPoint: "main",
|
|
145
|
-
},
|
|
146
|
-
fragment: {
|
|
147
|
-
module: device.createShaderModule({
|
|
148
|
-
code: `@group(0) @binding(0) var texSampler: sampler;
|
|
149
|
-
@group(0) @binding(1) var tex: texture_2d<f32>;
|
|
150
|
-
|
|
151
|
-
@fragment
|
|
152
|
-
fn main(@location(0) texCoord: vec2<f32>) -> @location(0) vec4<f32> {
|
|
153
|
-
return textureSample(tex, texSampler, texCoord);
|
|
154
|
-
}`,
|
|
155
|
-
}),
|
|
156
|
-
entryPoint: "main",
|
|
157
|
-
targets: [{ format: gpu.getPreferredCanvasFormat() }],
|
|
158
|
-
},
|
|
159
|
-
primitive: {
|
|
160
|
-
topology: "triangle-list",
|
|
161
|
-
},
|
|
162
|
-
});
|
|
163
|
-
// Create a bind group for the texture
|
|
164
|
-
const sampler = device.createSampler({
|
|
165
|
-
magFilter: "linear",
|
|
166
|
-
minFilter: "linear",
|
|
167
|
-
});
|
|
168
|
-
|
|
169
|
-
const bindGroup = device.createBindGroup({
|
|
170
|
-
layout: displayPipeline.getBindGroupLayout(0),
|
|
171
|
-
entries: [
|
|
172
|
-
{
|
|
173
|
-
binding: 0,
|
|
174
|
-
resource: sampler,
|
|
175
|
-
},
|
|
176
|
-
{
|
|
177
|
-
binding: 1,
|
|
178
|
-
resource: texture.createView(),
|
|
179
|
-
},
|
|
180
|
-
],
|
|
181
|
-
});
|
|
182
|
-
|
|
183
|
-
// // Render the triangle to the texture
|
|
184
|
-
const commandEncoder = device.createCommandEncoder();
|
|
185
|
-
const textureView = texture.createView();
|
|
186
|
-
|
|
187
|
-
const renderPassDescriptor: GPURenderPassDescriptor = {
|
|
188
|
-
colorAttachments: [
|
|
189
|
-
{
|
|
190
|
-
view: textureView,
|
|
191
|
-
clearValue: [0, 0, 0, 1],
|
|
192
|
-
loadOp: "clear",
|
|
193
|
-
storeOp: "store",
|
|
194
|
-
},
|
|
195
|
-
],
|
|
196
|
-
};
|
|
197
|
-
|
|
198
|
-
const passEncoder =
|
|
199
|
-
commandEncoder.beginRenderPass(renderPassDescriptor);
|
|
200
|
-
passEncoder.setPipeline(texturePipeline);
|
|
201
|
-
passEncoder.draw(3);
|
|
202
|
-
passEncoder.end();
|
|
203
|
-
|
|
204
|
-
device.queue.submit([commandEncoder.finish()]);
|
|
205
|
-
|
|
206
|
-
// // // Function to display the texture on the screen
|
|
207
|
-
const cmdEncoder = device.createCommandEncoder();
|
|
208
|
-
|
|
209
|
-
const renderPD: GPURenderPassDescriptor = {
|
|
210
|
-
colorAttachments: [
|
|
211
|
-
{
|
|
212
|
-
view: textureView,
|
|
213
|
-
clearValue: [0.5, 0.5, 0.5, 1],
|
|
214
|
-
loadOp: "clear",
|
|
215
|
-
storeOp: "store",
|
|
216
|
-
},
|
|
217
|
-
],
|
|
218
|
-
};
|
|
219
|
-
|
|
220
|
-
const encoder = cmdEncoder.beginRenderPass(renderPD);
|
|
221
|
-
encoder.setPipeline(displayPipeline);
|
|
222
|
-
encoder.setBindGroup(0, bindGroup);
|
|
223
|
-
encoder.draw(6);
|
|
224
|
-
encoder.end();
|
|
225
|
-
|
|
226
|
-
//device.queue.submit([cmdEncoder.finish()]);
|
|
227
|
-
return true;
|
|
228
|
-
},
|
|
229
|
-
);
|
|
230
|
-
expect(result).toBe(true);
|
|
231
|
-
});
|
|
232
|
-
});
|
|
@@ -1,197 +0,0 @@
|
|
|
1
|
-
import { checkImage, client, encodeImage } from "./setup";
|
|
2
|
-
|
|
3
|
-
describe("Texture", () => {
|
|
4
|
-
it("Check usage", async () => {
|
|
5
|
-
const result = await client.eval(() => {
|
|
6
|
-
return (
|
|
7
|
-
GPUTextureUsage.RENDER_ATTACHMENT | GPUTextureUsage.TEXTURE_BINDING
|
|
8
|
-
);
|
|
9
|
-
});
|
|
10
|
-
expect(result).toBe(20);
|
|
11
|
-
});
|
|
12
|
-
it("create texture (1)", async () => {
|
|
13
|
-
const result = await client.eval(({ device }) => {
|
|
14
|
-
const textureSize = 512;
|
|
15
|
-
const texture = device.createTexture({
|
|
16
|
-
size: [textureSize, textureSize],
|
|
17
|
-
format: "rgba8unorm",
|
|
18
|
-
usage:
|
|
19
|
-
GPUTextureUsage.RENDER_ATTACHMENT | GPUTextureUsage.TEXTURE_BINDING,
|
|
20
|
-
label: "texture",
|
|
21
|
-
});
|
|
22
|
-
return [
|
|
23
|
-
texture.label,
|
|
24
|
-
texture.width,
|
|
25
|
-
texture.height,
|
|
26
|
-
texture.depthOrArrayLayers,
|
|
27
|
-
texture.mipLevelCount,
|
|
28
|
-
texture.sampleCount,
|
|
29
|
-
texture.dimension,
|
|
30
|
-
texture.format,
|
|
31
|
-
texture.usage,
|
|
32
|
-
];
|
|
33
|
-
});
|
|
34
|
-
expect(result).toEqual([
|
|
35
|
-
"texture",
|
|
36
|
-
512,
|
|
37
|
-
512,
|
|
38
|
-
1,
|
|
39
|
-
1,
|
|
40
|
-
1,
|
|
41
|
-
"2d",
|
|
42
|
-
"rgba8unorm",
|
|
43
|
-
20,
|
|
44
|
-
]);
|
|
45
|
-
});
|
|
46
|
-
it("Write to texture", async () => {
|
|
47
|
-
const result = await client.eval(
|
|
48
|
-
({ device, shaders: { triangleVertWGSL, redFragWGSL } }) => {
|
|
49
|
-
// Define the size of the off-screen texture
|
|
50
|
-
const textureWidth = 800;
|
|
51
|
-
const textureHeight = 600;
|
|
52
|
-
|
|
53
|
-
// Calculate bytesPerRow to be a multiple of 256
|
|
54
|
-
const bytesPerRow = Math.ceil((textureWidth * 4) / 256) * 256;
|
|
55
|
-
|
|
56
|
-
// Calculate the required buffer size
|
|
57
|
-
const bufferSize = bytesPerRow * textureHeight;
|
|
58
|
-
|
|
59
|
-
// Create the off-screen texture
|
|
60
|
-
const texture = device.createTexture({
|
|
61
|
-
size: [textureWidth, textureHeight],
|
|
62
|
-
format: "rgba8unorm",
|
|
63
|
-
usage: GPUTextureUsage.RENDER_ATTACHMENT | GPUTextureUsage.COPY_SRC,
|
|
64
|
-
});
|
|
65
|
-
|
|
66
|
-
const pipeline = device.createRenderPipeline({
|
|
67
|
-
layout: "auto",
|
|
68
|
-
vertex: {
|
|
69
|
-
module: device.createShaderModule({
|
|
70
|
-
code: triangleVertWGSL,
|
|
71
|
-
}),
|
|
72
|
-
entryPoint: "main",
|
|
73
|
-
},
|
|
74
|
-
fragment: {
|
|
75
|
-
module: device.createShaderModule({
|
|
76
|
-
code: redFragWGSL,
|
|
77
|
-
}),
|
|
78
|
-
entryPoint: "main",
|
|
79
|
-
targets: [
|
|
80
|
-
{
|
|
81
|
-
format: "rgba8unorm",
|
|
82
|
-
},
|
|
83
|
-
],
|
|
84
|
-
},
|
|
85
|
-
primitive: {
|
|
86
|
-
topology: "triangle-list",
|
|
87
|
-
},
|
|
88
|
-
});
|
|
89
|
-
|
|
90
|
-
// Create a buffer to store the rendered image
|
|
91
|
-
const outputBuffer = device.createBuffer({
|
|
92
|
-
size: bufferSize,
|
|
93
|
-
usage: GPUBufferUsage.COPY_DST | GPUBufferUsage.MAP_READ,
|
|
94
|
-
});
|
|
95
|
-
|
|
96
|
-
const commandEncoder = device.createCommandEncoder();
|
|
97
|
-
const textureView = texture.createView();
|
|
98
|
-
|
|
99
|
-
const renderPassDescriptor: GPURenderPassDescriptor = {
|
|
100
|
-
colorAttachments: [
|
|
101
|
-
{
|
|
102
|
-
view: textureView,
|
|
103
|
-
clearValue: [0, 0, 0, 1],
|
|
104
|
-
loadOp: "clear",
|
|
105
|
-
storeOp: "store",
|
|
106
|
-
},
|
|
107
|
-
],
|
|
108
|
-
};
|
|
109
|
-
|
|
110
|
-
const passEncoder =
|
|
111
|
-
commandEncoder.beginRenderPass(renderPassDescriptor);
|
|
112
|
-
passEncoder.setPipeline(pipeline);
|
|
113
|
-
passEncoder.draw(3);
|
|
114
|
-
passEncoder.end();
|
|
115
|
-
|
|
116
|
-
// Copy the rendered texture to the output buffer
|
|
117
|
-
commandEncoder.copyTextureToBuffer(
|
|
118
|
-
{ texture },
|
|
119
|
-
{ buffer: outputBuffer, bytesPerRow: bytesPerRow },
|
|
120
|
-
[textureWidth, textureHeight],
|
|
121
|
-
);
|
|
122
|
-
|
|
123
|
-
device.queue.submit([commandEncoder.finish()]);
|
|
124
|
-
return [
|
|
125
|
-
texture.label,
|
|
126
|
-
texture.width,
|
|
127
|
-
texture.height,
|
|
128
|
-
texture.depthOrArrayLayers,
|
|
129
|
-
texture.mipLevelCount,
|
|
130
|
-
texture.sampleCount,
|
|
131
|
-
texture.dimension,
|
|
132
|
-
texture.format,
|
|
133
|
-
texture.usage,
|
|
134
|
-
];
|
|
135
|
-
},
|
|
136
|
-
);
|
|
137
|
-
expect(result).toEqual(["", 800, 600, 1, 1, 1, "2d", "rgba8unorm", 17]);
|
|
138
|
-
});
|
|
139
|
-
it("Create texture and reads it", async () => {
|
|
140
|
-
const result = await client.eval(
|
|
141
|
-
({
|
|
142
|
-
device,
|
|
143
|
-
shaders: { triangleVertWGSL, redFragWGSL },
|
|
144
|
-
gpu,
|
|
145
|
-
ctx,
|
|
146
|
-
canvas,
|
|
147
|
-
}) => {
|
|
148
|
-
const pipeline = device.createRenderPipeline({
|
|
149
|
-
layout: "auto",
|
|
150
|
-
vertex: {
|
|
151
|
-
module: device.createShaderModule({
|
|
152
|
-
code: triangleVertWGSL,
|
|
153
|
-
}),
|
|
154
|
-
},
|
|
155
|
-
fragment: {
|
|
156
|
-
module: device.createShaderModule({
|
|
157
|
-
code: redFragWGSL,
|
|
158
|
-
}),
|
|
159
|
-
targets: [
|
|
160
|
-
{
|
|
161
|
-
format: gpu.getPreferredCanvasFormat(),
|
|
162
|
-
},
|
|
163
|
-
],
|
|
164
|
-
},
|
|
165
|
-
primitive: {
|
|
166
|
-
topology: "triangle-list",
|
|
167
|
-
},
|
|
168
|
-
});
|
|
169
|
-
|
|
170
|
-
const commandEncoder = device.createCommandEncoder();
|
|
171
|
-
const textureView = ctx.getCurrentTexture().createView();
|
|
172
|
-
|
|
173
|
-
const renderPassDescriptor: GPURenderPassDescriptor = {
|
|
174
|
-
colorAttachments: [
|
|
175
|
-
{
|
|
176
|
-
view: textureView,
|
|
177
|
-
clearValue: [0, 0, 0, 0],
|
|
178
|
-
loadOp: "clear",
|
|
179
|
-
storeOp: "store",
|
|
180
|
-
},
|
|
181
|
-
],
|
|
182
|
-
};
|
|
183
|
-
|
|
184
|
-
const passEncoder =
|
|
185
|
-
commandEncoder.beginRenderPass(renderPassDescriptor);
|
|
186
|
-
passEncoder.setPipeline(pipeline);
|
|
187
|
-
passEncoder.draw(3);
|
|
188
|
-
passEncoder.end();
|
|
189
|
-
|
|
190
|
-
device.queue.submit([commandEncoder.finish()]);
|
|
191
|
-
return canvas.getImageData();
|
|
192
|
-
},
|
|
193
|
-
);
|
|
194
|
-
const image = encodeImage(result);
|
|
195
|
-
checkImage(image, "snapshots/texture.png");
|
|
196
|
-
});
|
|
197
|
-
});
|
|
Binary file
|
|
@@ -1,138 +0,0 @@
|
|
|
1
|
-
export const solidColorLitWGSL = /*wgsl*/ `struct Uniforms {
|
|
2
|
-
worldViewProjectionMatrix: mat4x4f,
|
|
3
|
-
worldMatrix: mat4x4f,
|
|
4
|
-
color: vec4f,
|
|
5
|
-
};
|
|
6
|
-
|
|
7
|
-
struct Vertex {
|
|
8
|
-
@location(0) position: vec4f,
|
|
9
|
-
@location(1) normal: vec3f,
|
|
10
|
-
};
|
|
11
|
-
|
|
12
|
-
struct VSOut {
|
|
13
|
-
@builtin(position) position: vec4f,
|
|
14
|
-
@location(0) normal: vec3f,
|
|
15
|
-
};
|
|
16
|
-
|
|
17
|
-
@group(0) @binding(0) var<uniform> uni: Uniforms;
|
|
18
|
-
|
|
19
|
-
@vertex fn vs(vin: Vertex) -> VSOut {
|
|
20
|
-
var vOut: VSOut;
|
|
21
|
-
vOut.position = uni.worldViewProjectionMatrix * vin.position;
|
|
22
|
-
vOut.normal = (uni.worldMatrix * vec4f(vin.normal, 0)).xyz;
|
|
23
|
-
return vOut;
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
@fragment fn fs(vin: VSOut) -> @location(0) vec4f {
|
|
27
|
-
let lightDirection = normalize(vec3f(4, 10, 6));
|
|
28
|
-
let light = dot(normalize(vin.normal), lightDirection) * 0.5 + 0.5;
|
|
29
|
-
return vec4f(uni.color.rgb * light, uni.color.a);
|
|
30
|
-
}`;
|
|
31
|
-
|
|
32
|
-
export const wireframeWGSL = /*wgsl*/ `struct Uniforms {
|
|
33
|
-
worldViewProjectionMatrix: mat4x4f,
|
|
34
|
-
worldMatrix: mat4x4f,
|
|
35
|
-
color: vec4f,
|
|
36
|
-
};
|
|
37
|
-
|
|
38
|
-
struct LineUniforms {
|
|
39
|
-
stride: u32,
|
|
40
|
-
thickness: f32,
|
|
41
|
-
alphaThreshold: f32,
|
|
42
|
-
};
|
|
43
|
-
|
|
44
|
-
struct VSOut {
|
|
45
|
-
@builtin(position) position: vec4f,
|
|
46
|
-
};
|
|
47
|
-
|
|
48
|
-
@group(0) @binding(0) var<uniform> uni: Uniforms;
|
|
49
|
-
@group(0) @binding(1) var<storage, read> positions: array<f32>;
|
|
50
|
-
@group(0) @binding(2) var<storage, read> indices: array<u32>;
|
|
51
|
-
@group(0) @binding(3) var<uniform> line: LineUniforms;
|
|
52
|
-
|
|
53
|
-
@vertex fn vsIndexedU32(@builtin(vertex_index) vNdx: u32) -> VSOut {
|
|
54
|
-
// indices make a triangle so for every 3 indices we need to output
|
|
55
|
-
// 6 values
|
|
56
|
-
let triNdx = vNdx / 6;
|
|
57
|
-
// 0 1 0 1 0 1 0 1 0 1 0 1 vNdx % 2
|
|
58
|
-
// 0 0 1 1 2 2 3 3 4 4 5 5 vNdx / 2
|
|
59
|
-
// 0 1 1 2 2 3 3 4 4 5 5 6 vNdx % 2 + vNdx / 2
|
|
60
|
-
// 0 1 1 2 2 0 0 1 1 2 2 0 (vNdx % 2 + vNdx / 2) % 3
|
|
61
|
-
let vertNdx = (vNdx % 2 + vNdx / 2) % 3;
|
|
62
|
-
let index = indices[triNdx * 3 + vertNdx];
|
|
63
|
-
|
|
64
|
-
// note:
|
|
65
|
-
//
|
|
66
|
-
// * if your indices are U16 you could use this
|
|
67
|
-
//
|
|
68
|
-
// let indexNdx = triNdx * 3 + vertNdx;
|
|
69
|
-
// let twoIndices = indices[indexNdx / 2]; // indices is u32 but we want u16
|
|
70
|
-
// let index = (twoIndices >> ((indexNdx & 1) * 16)) & 0xFFFF;
|
|
71
|
-
//
|
|
72
|
-
// * if you're not using indices you could use this
|
|
73
|
-
//
|
|
74
|
-
// let index = triNdx * 3 + vertNdx;
|
|
75
|
-
|
|
76
|
-
let pNdx = index * line.stride;
|
|
77
|
-
let position = vec4f(positions[pNdx], positions[pNdx + 1], positions[pNdx + 2], 1);
|
|
78
|
-
|
|
79
|
-
var vOut: VSOut;
|
|
80
|
-
vOut.position = uni.worldViewProjectionMatrix * position;
|
|
81
|
-
return vOut;
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
@fragment fn fs() -> @location(0) vec4f {
|
|
85
|
-
return uni.color + vec4f(0.5);
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
struct BarycentricCoordinateBasedVSOutput {
|
|
89
|
-
@builtin(position) position: vec4f,
|
|
90
|
-
@location(0) barycenticCoord: vec3f,
|
|
91
|
-
};
|
|
92
|
-
|
|
93
|
-
@vertex fn vsIndexedU32BarycentricCoordinateBasedLines(
|
|
94
|
-
@builtin(vertex_index) vNdx: u32
|
|
95
|
-
) -> BarycentricCoordinateBasedVSOutput {
|
|
96
|
-
let vertNdx = vNdx % 3;
|
|
97
|
-
let index = indices[vNdx];
|
|
98
|
-
|
|
99
|
-
// note:
|
|
100
|
-
//
|
|
101
|
-
// * if your indices are U16 you could use this
|
|
102
|
-
//
|
|
103
|
-
// let twoIndices = indices[vNdx / 2]; // indices is u32 but we want u16
|
|
104
|
-
// let index = (twoIndices >> ((vNdx & 1) * 16)) & 0xFFFF;
|
|
105
|
-
//
|
|
106
|
-
// * if you're not using indices you could use this
|
|
107
|
-
//
|
|
108
|
-
// let index = vNdx;
|
|
109
|
-
|
|
110
|
-
let pNdx = index * line.stride;
|
|
111
|
-
let position = vec4f(positions[pNdx], positions[pNdx + 1], positions[pNdx + 2], 1);
|
|
112
|
-
|
|
113
|
-
var vsOut: BarycentricCoordinateBasedVSOutput;
|
|
114
|
-
vsOut.position = uni.worldViewProjectionMatrix * position;
|
|
115
|
-
|
|
116
|
-
// emit a barycentric coordinate
|
|
117
|
-
vsOut.barycenticCoord = vec3f(0);
|
|
118
|
-
vsOut.barycenticCoord[vertNdx] = 1.0;
|
|
119
|
-
return vsOut;
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
fn edgeFactor(bary: vec3f) -> f32 {
|
|
123
|
-
let d = fwidth(bary);
|
|
124
|
-
let a3 = smoothstep(vec3f(0.0), d * line.thickness, bary);
|
|
125
|
-
return min(min(a3.x, a3.y), a3.z);
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
@fragment fn fsBarycentricCoordinateBasedLines(
|
|
129
|
-
v: BarycentricCoordinateBasedVSOutput
|
|
130
|
-
) -> @location(0) vec4f {
|
|
131
|
-
let a = 1.0 - edgeFactor(v.barycenticCoord);
|
|
132
|
-
if (a < line.alphaThreshold) {
|
|
133
|
-
discard;
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
return vec4((uni.color.rgb + 0.5) * a, a);
|
|
137
|
-
}
|
|
138
|
-
`;
|
|
@@ -1,113 +0,0 @@
|
|
|
1
|
-
// Ideally all the models would be the same format
|
|
2
|
-
// and we'd determine that format at build time or before
|
|
3
|
-
// but, we want to reuse the model data in this repo
|
|
4
|
-
// so we'll normalize it here
|
|
5
|
-
|
|
6
|
-
import { vec3 } from "wgpu-matrix";
|
|
7
|
-
|
|
8
|
-
import { mesh as teapot } from "../meshes/teapot";
|
|
9
|
-
import { createSphereMesh } from "../meshes/sphere";
|
|
10
|
-
|
|
11
|
-
type Mesh = {
|
|
12
|
-
positions: [number, number, number][];
|
|
13
|
-
triangles: [number, number, number][];
|
|
14
|
-
normals: [number, number, number][];
|
|
15
|
-
};
|
|
16
|
-
|
|
17
|
-
export function convertMeshToTypedArrays(
|
|
18
|
-
mesh: Mesh,
|
|
19
|
-
scale: number,
|
|
20
|
-
offset = [0, 0, 0],
|
|
21
|
-
) {
|
|
22
|
-
const { positions, normals, triangles } = mesh;
|
|
23
|
-
const scaledPositions = positions.map((p) =>
|
|
24
|
-
p.map((v, i) => v * scale + offset[i % 3]),
|
|
25
|
-
);
|
|
26
|
-
const vertices = new Float32Array(scaledPositions.length * 6);
|
|
27
|
-
for (let i = 0; i < scaledPositions.length; ++i) {
|
|
28
|
-
vertices.set(scaledPositions[i], 6 * i);
|
|
29
|
-
vertices.set(normals[i], 6 * i + 3);
|
|
30
|
-
}
|
|
31
|
-
const indices = new Uint32Array(triangles.length * 3);
|
|
32
|
-
for (let i = 0; i < triangles.length; ++i) {
|
|
33
|
-
indices.set(triangles[i], 3 * i);
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
return {
|
|
37
|
-
vertices,
|
|
38
|
-
indices,
|
|
39
|
-
};
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
function createSphereTypedArrays(
|
|
43
|
-
radius: number,
|
|
44
|
-
widthSegments = 32,
|
|
45
|
-
heightSegments = 16,
|
|
46
|
-
randomness = 0,
|
|
47
|
-
) {
|
|
48
|
-
const { vertices: verticesWithUVs, indices } = createSphereMesh(
|
|
49
|
-
radius,
|
|
50
|
-
widthSegments,
|
|
51
|
-
heightSegments,
|
|
52
|
-
randomness,
|
|
53
|
-
);
|
|
54
|
-
const numVertices = verticesWithUVs.length / 8;
|
|
55
|
-
const vertices = new Float32Array(numVertices * 6);
|
|
56
|
-
for (let i = 0; i < numVertices; ++i) {
|
|
57
|
-
const srcNdx = i * 8;
|
|
58
|
-
const dstNdx = i * 6;
|
|
59
|
-
vertices.set(verticesWithUVs.subarray(srcNdx, srcNdx + 6), dstNdx);
|
|
60
|
-
}
|
|
61
|
-
return {
|
|
62
|
-
vertices,
|
|
63
|
-
indices: new Uint32Array(indices),
|
|
64
|
-
};
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
function flattenNormals({
|
|
68
|
-
vertices,
|
|
69
|
-
indices,
|
|
70
|
-
}: {
|
|
71
|
-
vertices: Float32Array;
|
|
72
|
-
indices: Uint32Array;
|
|
73
|
-
}) {
|
|
74
|
-
const newVertices = new Float32Array(indices.length * 6);
|
|
75
|
-
const newIndices = new Uint32Array(indices.length);
|
|
76
|
-
for (let i = 0; i < indices.length; i += 3) {
|
|
77
|
-
const positions = [];
|
|
78
|
-
for (let j = 0; j < 3; ++j) {
|
|
79
|
-
const ndx = indices[i + j];
|
|
80
|
-
const srcNdx = ndx * 6;
|
|
81
|
-
const dstNdx = (i + j) * 6;
|
|
82
|
-
// copy position
|
|
83
|
-
const pos = vertices.subarray(srcNdx, srcNdx + 3);
|
|
84
|
-
newVertices.set(pos, dstNdx);
|
|
85
|
-
positions.push(pos);
|
|
86
|
-
newIndices[i + j] = i + j;
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
const normal = vec3.normalize(
|
|
90
|
-
vec3.cross(
|
|
91
|
-
vec3.normalize(vec3.subtract(positions[1], positions[0])),
|
|
92
|
-
vec3.normalize(vec3.subtract(positions[2], positions[1])),
|
|
93
|
-
),
|
|
94
|
-
);
|
|
95
|
-
|
|
96
|
-
for (let j = 0; j < 3; ++j) {
|
|
97
|
-
const dstNdx = (i + j) * 6;
|
|
98
|
-
newVertices.set(normal, dstNdx + 3);
|
|
99
|
-
}
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
return {
|
|
103
|
-
vertices: newVertices,
|
|
104
|
-
indices: newIndices,
|
|
105
|
-
};
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
export const modelData = {
|
|
109
|
-
teapot: convertMeshToTypedArrays(teapot, 1.5),
|
|
110
|
-
sphere: createSphereTypedArrays(20),
|
|
111
|
-
jewel: flattenNormals(createSphereTypedArrays(20, 5, 3)),
|
|
112
|
-
rock: flattenNormals(createSphereTypedArrays(20, 32, 16, 0.1)),
|
|
113
|
-
};
|
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
export function rand(min?: number, max?: number) {
|
|
2
|
-
if (min === undefined) {
|
|
3
|
-
max = 1;
|
|
4
|
-
min = 0;
|
|
5
|
-
} else if (max === undefined) {
|
|
6
|
-
max = min;
|
|
7
|
-
min = 0;
|
|
8
|
-
}
|
|
9
|
-
return Math.random() * (max - min) + min;
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
export function randInt(min: number, max?: number) {
|
|
13
|
-
return Math.floor(rand(min, max));
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
export function randColor() {
|
|
17
|
-
return [rand(), rand(), rand(), 1];
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
export function randElement<T>(arr: T[]): T {
|
|
21
|
-
return arr[randInt(arr.length)];
|
|
22
|
-
}
|