react-native-wgpu 0.1.4 → 0.1.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/README.md +43 -42
- package/android/cpp/AndroidPlatformContext.h +2 -1
- package/cpp/jsi/RNFJSIConverter.h +10 -0
- package/cpp/rnwgpu/SurfaceRegistry.h +1 -2
- package/cpp/rnwgpu/api/Canvas.h +3 -3
- package/cpp/rnwgpu/api/GPUBuffer.h +1 -1
- package/ios/WebGPUView.mm +14 -1
- package/lib/commonjs/Canvas.js.map +1 -1
- package/lib/commonjs/utils.js +2 -2
- package/lib/commonjs/utils.js.map +1 -1
- package/lib/module/Canvas.js.map +1 -1
- package/lib/module/utils.js +2 -2
- package/lib/module/utils.js.map +1 -1
- package/lib/typescript/lib/commonjs/utils.d.ts +1 -1
- package/lib/typescript/lib/commonjs/utils.d.ts.map +1 -1
- package/lib/typescript/lib/module/utils.d.ts +1 -1
- package/lib/typescript/lib/module/utils.d.ts.map +1 -1
- package/lib/typescript/src/Canvas.d.ts +3 -2
- package/lib/typescript/src/Canvas.d.ts.map +1 -1
- package/lib/typescript/src/__tests__/ArrayBuffer.spec.d.ts +2 -0
- package/lib/typescript/src/__tests__/ArrayBuffer.spec.d.ts.map +1 -0
- package/lib/typescript/src/__tests__/Buffer.spec.d.ts +2 -0
- package/lib/typescript/src/__tests__/Buffer.spec.d.ts.map +1 -0
- package/lib/typescript/src/__tests__/ComputeShader.spec.d.ts +2 -0
- package/lib/typescript/src/__tests__/ComputeShader.spec.d.ts.map +1 -0
- package/lib/typescript/src/__tests__/Constants.spec.d.ts +2 -0
- package/lib/typescript/src/__tests__/Constants.spec.d.ts.map +1 -0
- package/lib/typescript/src/__tests__/Device.spec.d.ts +2 -0
- package/lib/typescript/src/__tests__/Device.spec.d.ts.map +1 -0
- package/lib/typescript/src/__tests__/ExternalTexture.spec.d.ts +2 -0
- package/lib/typescript/src/__tests__/ExternalTexture.spec.d.ts.map +1 -0
- package/lib/typescript/src/__tests__/GPU.spec.d.ts +2 -0
- package/lib/typescript/src/__tests__/GPU.spec.d.ts.map +1 -0
- package/lib/typescript/src/__tests__/ImageData.spec.d.ts +2 -0
- package/lib/typescript/src/__tests__/ImageData.spec.d.ts.map +1 -0
- package/lib/typescript/src/__tests__/Shaders.spec.d.ts +2 -0
- package/lib/typescript/src/__tests__/Shaders.spec.d.ts.map +1 -0
- package/lib/typescript/src/__tests__/Texture.spec.d.ts +2 -0
- package/lib/typescript/src/__tests__/Texture.spec.d.ts.map +1 -0
- package/lib/typescript/src/__tests__/components/DrawingContext.d.ts +12 -0
- package/lib/typescript/src/__tests__/components/DrawingContext.d.ts.map +1 -0
- package/lib/typescript/src/__tests__/components/Wireframe/Shaders.d.ts +3 -0
- package/lib/typescript/src/__tests__/components/Wireframe/Shaders.d.ts.map +1 -0
- package/lib/typescript/src/__tests__/components/Wireframe/models.d.ts +29 -0
- package/lib/typescript/src/__tests__/components/Wireframe/models.d.ts.map +1 -0
- package/lib/typescript/src/__tests__/components/Wireframe/utils.d.ts +5 -0
- package/lib/typescript/src/__tests__/components/Wireframe/utils.d.ts.map +1 -0
- package/lib/typescript/src/__tests__/components/cube.d.ts +7 -0
- package/lib/typescript/src/__tests__/components/cube.d.ts.map +1 -0
- package/lib/typescript/src/__tests__/components/meshes/mesh.d.ts +22 -0
- package/lib/typescript/src/__tests__/components/meshes/mesh.d.ts.map +1 -0
- package/lib/typescript/src/__tests__/components/meshes/sphere.d.ts +12 -0
- package/lib/typescript/src/__tests__/components/meshes/sphere.d.ts.map +1 -0
- package/lib/typescript/src/__tests__/components/meshes/stanfordDragon.d.ts +7 -0
- package/lib/typescript/src/__tests__/components/meshes/stanfordDragon.d.ts.map +1 -0
- package/lib/typescript/src/__tests__/components/meshes/stanfordDragonData.d.ts +6 -0
- package/lib/typescript/src/__tests__/components/meshes/stanfordDragonData.d.ts.map +1 -0
- package/lib/typescript/src/__tests__/components/meshes/teapot.d.ts +6 -0
- package/lib/typescript/src/__tests__/components/meshes/teapot.d.ts.map +1 -0
- package/lib/typescript/src/__tests__/components/meshes/utils.d.ts +10 -0
- package/lib/typescript/src/__tests__/components/meshes/utils.d.ts.map +1 -0
- package/lib/typescript/src/__tests__/components/triangle.d.ts +3 -0
- package/lib/typescript/src/__tests__/components/triangle.d.ts.map +1 -0
- package/lib/typescript/src/__tests__/config.d.ts +3 -0
- package/lib/typescript/src/__tests__/config.d.ts.map +1 -0
- package/lib/typescript/src/__tests__/demos/ABuffer.spec.d.ts +2 -0
- package/lib/typescript/src/__tests__/demos/ABuffer.spec.d.ts.map +1 -0
- package/lib/typescript/src/__tests__/demos/Blur.spec.d.ts +2 -0
- package/lib/typescript/src/__tests__/demos/Blur.spec.d.ts.map +1 -0
- package/lib/typescript/src/__tests__/demos/Cube.spec.d.ts +2 -0
- package/lib/typescript/src/__tests__/demos/Cube.spec.d.ts.map +1 -0
- package/lib/typescript/src/__tests__/demos/FractalCube.spec.d.ts +2 -0
- package/lib/typescript/src/__tests__/demos/FractalCube.spec.d.ts.map +1 -0
- package/lib/typescript/src/__tests__/demos/OcclusionQuery.spec.d.ts +2 -0
- package/lib/typescript/src/__tests__/demos/OcclusionQuery.spec.d.ts.map +1 -0
- package/lib/typescript/src/__tests__/demos/RenderBundles.spec.d.ts +2 -0
- package/lib/typescript/src/__tests__/demos/RenderBundles.spec.d.ts.map +1 -0
- package/lib/typescript/src/__tests__/demos/Triangle.spec.d.ts +2 -0
- package/lib/typescript/src/__tests__/demos/Triangle.spec.d.ts.map +1 -0
- package/lib/typescript/src/__tests__/demos/Wireframe.spec.d.ts +2 -0
- package/lib/typescript/src/__tests__/demos/Wireframe.spec.d.ts.map +1 -0
- package/lib/typescript/src/__tests__/globalSetup.d.ts +10 -0
- package/lib/typescript/src/__tests__/globalSetup.d.ts.map +1 -0
- package/lib/typescript/src/__tests__/globalTeardown.d.ts +3 -0
- package/lib/typescript/src/__tests__/globalTeardown.d.ts.map +1 -0
- package/lib/typescript/src/__tests__/setup.d.ts +62 -0
- package/lib/typescript/src/__tests__/setup.d.ts.map +1 -0
- package/lib/typescript/src/utils.d.ts +2 -1
- package/lib/typescript/src/utils.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/{ios/libwebgpu_dawn.xcframework/ios-arm64 → apple/arm64_iphoneos}/libwebgpu_dawn.a +0 -0
- package/libs/{ios → apple}/arm64_iphonesimulator/libwebgpu_dawn.a +0 -0
- package/libs/{ios/libwebgpu_dawn.xcframework/xros-arm64 → apple/arm64_xros}/libwebgpu_dawn.a +0 -0
- package/libs/{ios → apple}/arm64_xrsimulator/libwebgpu_dawn.a +0 -0
- package/libs/{ios → apple}/libwebgpu_dawn.a +0 -0
- package/libs/apple/libwebgpu_dawn.xcframework/Info.plist +44 -0
- package/libs/{ios/arm64_iphoneos → apple/libwebgpu_dawn.xcframework/ios-arm64}/libwebgpu_dawn.a +0 -0
- package/libs/{ios → apple}/libwebgpu_dawn.xcframework/ios-arm64_x86_64-simulator/libwebgpu_dawn.a +0 -0
- package/libs/apple/libwebgpu_dawn_macosx.xcframework/Info.plist +28 -0
- package/libs/apple/libwebgpu_dawn_macosx.xcframework/macos-arm64_x86_64/libwebgpu_dawn.a +0 -0
- package/libs/{ios/libwebgpu_dawn.xcframework/xros-arm64_x86_64-simulator → apple}/libwebgpu_dawn_visionos.a +0 -0
- package/libs/{ios/libwebgpu_dawn.xcframework → apple/libwebgpu_dawn_visionos.xcframework}/Info.plist +0 -31
- package/libs/{ios/arm64_xros → apple/libwebgpu_dawn_visionos.xcframework/xros-arm64}/libwebgpu_dawn.a +0 -0
- package/libs/{ios → apple/libwebgpu_dawn_visionos.xcframework/xros-arm64_x86_64-simulator}/libwebgpu_dawn_visionos.a +0 -0
- package/libs/apple/universal_macosx/libwebgpu_dawn.a +0 -0
- package/libs/{ios → apple}/x86_64_iphonesimulator/libwebgpu_dawn.a +0 -0
- package/libs/{ios → apple}/x86_64_xrsimulator/libwebgpu_dawn.a +0 -0
- package/package.json +24 -24
- package/src/Canvas.tsx +4 -2
- package/src/__tests__/ArrayBuffer.spec.ts +76 -0
- package/src/__tests__/Buffer.spec.ts +357 -0
- package/src/__tests__/ComputeShader.spec.ts +375 -0
- package/src/__tests__/Constants.spec.ts +91 -0
- package/src/__tests__/Device.spec.ts +35 -0
- package/src/__tests__/ExternalTexture.spec.ts +284 -0
- package/src/__tests__/GPU.spec.ts +229 -0
- package/src/__tests__/ImageData.spec.ts +26 -0
- package/src/__tests__/Shaders.spec.ts +232 -0
- package/src/__tests__/Texture.spec.ts +191 -0
- package/src/__tests__/assets/Di-3d.png +0 -0
- package/src/__tests__/components/DrawingContext.ts +11 -0
- package/src/__tests__/components/Wireframe/Shaders.ts +138 -0
- package/src/__tests__/components/Wireframe/models.ts +113 -0
- package/src/__tests__/components/Wireframe/utils.ts +22 -0
- package/src/__tests__/components/cube.ts +51 -0
- package/src/__tests__/components/meshes/mesh.ts +96 -0
- package/src/__tests__/components/meshes/sphere.ts +103 -0
- package/src/__tests__/components/meshes/stanfordDragon.ts +44 -0
- package/src/__tests__/components/meshes/stanfordDragonData.ts +5 -0
- package/src/__tests__/components/meshes/teapot.ts +13 -0
- package/src/__tests__/components/meshes/utils.ts +235 -0
- package/src/__tests__/components/triangle.ts +17 -0
- package/src/__tests__/config.ts +2 -0
- package/src/__tests__/demos/ABuffer.spec.ts +885 -0
- package/src/__tests__/demos/Blur.spec.ts +397 -0
- package/src/__tests__/demos/Cube.spec.ts +925 -0
- package/src/__tests__/demos/FractalCube.spec.ts +239 -0
- package/src/__tests__/demos/OcclusionQuery.spec.ts +376 -0
- package/src/__tests__/demos/RenderBundles.spec.ts +579 -0
- package/src/__tests__/demos/Triangle.spec.ts +248 -0
- package/src/__tests__/demos/Wireframe.spec.ts +190 -0
- package/src/__tests__/globalSetup.ts +54 -0
- package/src/__tests__/globalTeardown.ts +11 -0
- package/src/__tests__/setup.ts +409 -0
- 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/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
- package/src/utils.ts +3 -1
- package/android/gradle.properties +0 -5
- package/react-native-wgpu.podspec +0 -53
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
export const cubeVertexSize = 4 * 10; // Byte size of one cube vertex.
|
|
2
|
+
export const cubePositionOffset = 0;
|
|
3
|
+
export const cubeColorOffset = 4 * 4; // Byte offset of cube vertex color attribute.
|
|
4
|
+
export const cubeUVOffset = 4 * 8;
|
|
5
|
+
export const cubeVertexCount = 36;
|
|
6
|
+
|
|
7
|
+
// prettier-ignore
|
|
8
|
+
export const cubeVertexArray = new Float32Array([
|
|
9
|
+
// float4 position, float4 color, float2 uv,
|
|
10
|
+
1, -1, 1, 1, 1, 0, 1, 1, 0, 1,
|
|
11
|
+
-1, -1, 1, 1, 0, 0, 1, 1, 1, 1,
|
|
12
|
+
-1, -1, -1, 1, 0, 0, 0, 1, 1, 0,
|
|
13
|
+
1, -1, -1, 1, 1, 0, 0, 1, 0, 0,
|
|
14
|
+
1, -1, 1, 1, 1, 0, 1, 1, 0, 1,
|
|
15
|
+
-1, -1, -1, 1, 0, 0, 0, 1, 1, 0,
|
|
16
|
+
|
|
17
|
+
1, 1, 1, 1, 1, 1, 1, 1, 0, 1,
|
|
18
|
+
1, -1, 1, 1, 1, 0, 1, 1, 1, 1,
|
|
19
|
+
1, -1, -1, 1, 1, 0, 0, 1, 1, 0,
|
|
20
|
+
1, 1, -1, 1, 1, 1, 0, 1, 0, 0,
|
|
21
|
+
1, 1, 1, 1, 1, 1, 1, 1, 0, 1,
|
|
22
|
+
1, -1, -1, 1, 1, 0, 0, 1, 1, 0,
|
|
23
|
+
|
|
24
|
+
-1, 1, 1, 1, 0, 1, 1, 1, 0, 1,
|
|
25
|
+
1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
|
|
26
|
+
1, 1, -1, 1, 1, 1, 0, 1, 1, 0,
|
|
27
|
+
-1, 1, -1, 1, 0, 1, 0, 1, 0, 0,
|
|
28
|
+
-1, 1, 1, 1, 0, 1, 1, 1, 0, 1,
|
|
29
|
+
1, 1, -1, 1, 1, 1, 0, 1, 1, 0,
|
|
30
|
+
|
|
31
|
+
-1, -1, 1, 1, 0, 0, 1, 1, 0, 1,
|
|
32
|
+
-1, 1, 1, 1, 0, 1, 1, 1, 1, 1,
|
|
33
|
+
-1, 1, -1, 1, 0, 1, 0, 1, 1, 0,
|
|
34
|
+
-1, -1, -1, 1, 0, 0, 0, 1, 0, 0,
|
|
35
|
+
-1, -1, 1, 1, 0, 0, 1, 1, 0, 1,
|
|
36
|
+
-1, 1, -1, 1, 0, 1, 0, 1, 1, 0,
|
|
37
|
+
|
|
38
|
+
1, 1, 1, 1, 1, 1, 1, 1, 0, 1,
|
|
39
|
+
-1, 1, 1, 1, 0, 1, 1, 1, 1, 1,
|
|
40
|
+
-1, -1, 1, 1, 0, 0, 1, 1, 1, 0,
|
|
41
|
+
-1, -1, 1, 1, 0, 0, 1, 1, 1, 0,
|
|
42
|
+
1, -1, 1, 1, 1, 0, 1, 1, 0, 0,
|
|
43
|
+
1, 1, 1, 1, 1, 1, 1, 1, 0, 1,
|
|
44
|
+
|
|
45
|
+
1, -1, -1, 1, 1, 0, 0, 1, 0, 1,
|
|
46
|
+
-1, -1, -1, 1, 0, 0, 0, 1, 1, 1,
|
|
47
|
+
-1, 1, -1, 1, 0, 1, 0, 1, 1, 0,
|
|
48
|
+
1, 1, -1, 1, 1, 1, 0, 1, 0, 0,
|
|
49
|
+
1, -1, -1, 1, 1, 0, 0, 1, 0, 1,
|
|
50
|
+
-1, 1, -1, 1, 0, 1, 0, 1, 1, 0,
|
|
51
|
+
]);
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import { vec3, vec2 } from "wgpu-matrix";
|
|
2
|
+
|
|
3
|
+
// Defines what to pass to pipeline to render mesh
|
|
4
|
+
export interface Renderable {
|
|
5
|
+
vertexBuffer: GPUBuffer;
|
|
6
|
+
indexBuffer: GPUBuffer;
|
|
7
|
+
indexCount: number;
|
|
8
|
+
bindGroup?: GPUBindGroup;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export interface Mesh {
|
|
12
|
+
vertices: Float32Array;
|
|
13
|
+
indices: Uint16Array | Uint32Array;
|
|
14
|
+
vertexStride: number;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* @param {GPUDevice} device - A valid GPUDevice.
|
|
19
|
+
* @param {Mesh} mesh - An indexed triangle-list mesh, containing its vertices, indices, and vertexStride (number of elements per vertex).
|
|
20
|
+
* @param {boolean} storeVertices - A boolean flag indicating whether the vertexBuffer should be available to use as a storage buffer.
|
|
21
|
+
* @returns {boolean} An object containing an array of bindGroups and the bindGroupLayout they implement.
|
|
22
|
+
*/
|
|
23
|
+
export const createMeshRenderable = (
|
|
24
|
+
device: GPUDevice,
|
|
25
|
+
mesh: Mesh,
|
|
26
|
+
storeVertices = false,
|
|
27
|
+
storeIndices = false,
|
|
28
|
+
): Renderable => {
|
|
29
|
+
// Define buffer usage
|
|
30
|
+
const vertexBufferUsage = storeVertices
|
|
31
|
+
? GPUBufferUsage.VERTEX | GPUBufferUsage.STORAGE
|
|
32
|
+
: GPUBufferUsage.VERTEX;
|
|
33
|
+
const indexBufferUsage = storeIndices
|
|
34
|
+
? GPUBufferUsage.INDEX | GPUBufferUsage.STORAGE
|
|
35
|
+
: GPUBufferUsage.INDEX;
|
|
36
|
+
|
|
37
|
+
// Create vertex and index buffers
|
|
38
|
+
const vertexBuffer = device.createBuffer({
|
|
39
|
+
size: mesh.vertices.byteLength,
|
|
40
|
+
usage: vertexBufferUsage,
|
|
41
|
+
mappedAtCreation: true,
|
|
42
|
+
});
|
|
43
|
+
new Float32Array(vertexBuffer.getMappedRange()).set(mesh.vertices);
|
|
44
|
+
vertexBuffer.unmap();
|
|
45
|
+
|
|
46
|
+
const indexBuffer = device.createBuffer({
|
|
47
|
+
size: mesh.indices.byteLength,
|
|
48
|
+
usage: indexBufferUsage,
|
|
49
|
+
mappedAtCreation: true,
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
// Determine whether index buffer is indices are in uint16 or uint32 format
|
|
53
|
+
if (
|
|
54
|
+
mesh.indices.byteLength ===
|
|
55
|
+
mesh.indices.length * Uint16Array.BYTES_PER_ELEMENT
|
|
56
|
+
) {
|
|
57
|
+
new Uint16Array(indexBuffer.getMappedRange()).set(mesh.indices);
|
|
58
|
+
} else {
|
|
59
|
+
new Uint32Array(indexBuffer.getMappedRange()).set(mesh.indices);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
indexBuffer.unmap();
|
|
63
|
+
|
|
64
|
+
return {
|
|
65
|
+
vertexBuffer,
|
|
66
|
+
indexBuffer,
|
|
67
|
+
indexCount: mesh.indices.length,
|
|
68
|
+
};
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
export const getMeshPosAtIndex = (mesh: Mesh, index: number) => {
|
|
72
|
+
const arr = new Float32Array(
|
|
73
|
+
mesh.vertices.buffer,
|
|
74
|
+
index * mesh.vertexStride + 0,
|
|
75
|
+
3,
|
|
76
|
+
);
|
|
77
|
+
return vec3.fromValues(arr[0], arr[1], arr[2]);
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
export const getMeshNormalAtIndex = (mesh: Mesh, index: number) => {
|
|
81
|
+
const arr = new Float32Array(
|
|
82
|
+
mesh.vertices.buffer,
|
|
83
|
+
index * mesh.vertexStride + 3 * Float32Array.BYTES_PER_ELEMENT,
|
|
84
|
+
3,
|
|
85
|
+
);
|
|
86
|
+
return vec3.fromValues(arr[0], arr[1], arr[2]);
|
|
87
|
+
};
|
|
88
|
+
|
|
89
|
+
export const getMeshUVAtIndex = (mesh: Mesh, index: number) => {
|
|
90
|
+
const arr = new Float32Array(
|
|
91
|
+
mesh.vertices.buffer,
|
|
92
|
+
index * mesh.vertexStride + 6 * Float32Array.BYTES_PER_ELEMENT,
|
|
93
|
+
2,
|
|
94
|
+
);
|
|
95
|
+
return vec2.fromValues(arr[0], arr[1]);
|
|
96
|
+
};
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
import { vec3 } from "wgpu-matrix";
|
|
2
|
+
|
|
3
|
+
export interface SphereMesh {
|
|
4
|
+
vertices: Float32Array;
|
|
5
|
+
indices: Uint16Array;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
export const SphereLayout = {
|
|
9
|
+
vertexStride: 8 * 4,
|
|
10
|
+
positionsOffset: 0,
|
|
11
|
+
normalOffset: 3 * 4,
|
|
12
|
+
uvOffset: 6 * 4,
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
// Borrowed and simplified from https://github.com/mrdoob/three.js/blob/master/src/geometries/SphereGeometry.js
|
|
16
|
+
export function createSphereMesh(
|
|
17
|
+
radius: number,
|
|
18
|
+
widthSegments = 32,
|
|
19
|
+
heightSegments = 16,
|
|
20
|
+
randomness = 0,
|
|
21
|
+
): SphereMesh {
|
|
22
|
+
const vertices = [];
|
|
23
|
+
const indices = [];
|
|
24
|
+
|
|
25
|
+
widthSegments = Math.max(3, Math.floor(widthSegments));
|
|
26
|
+
heightSegments = Math.max(2, Math.floor(heightSegments));
|
|
27
|
+
|
|
28
|
+
const firstVertex = vec3.create();
|
|
29
|
+
const vertex = vec3.create();
|
|
30
|
+
const normal = vec3.create();
|
|
31
|
+
|
|
32
|
+
let index = 0;
|
|
33
|
+
const grid = [];
|
|
34
|
+
|
|
35
|
+
// generate vertices, normals and uvs
|
|
36
|
+
for (let iy = 0; iy <= heightSegments; iy++) {
|
|
37
|
+
const verticesRow = [];
|
|
38
|
+
const v = iy / heightSegments;
|
|
39
|
+
|
|
40
|
+
// special case for the poles
|
|
41
|
+
let uOffset = 0;
|
|
42
|
+
if (iy === 0) {
|
|
43
|
+
uOffset = 0.5 / widthSegments;
|
|
44
|
+
} else if (iy === heightSegments) {
|
|
45
|
+
uOffset = -0.5 / widthSegments;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
for (let ix = 0; ix <= widthSegments; ix++) {
|
|
49
|
+
const u = ix / widthSegments;
|
|
50
|
+
|
|
51
|
+
// Poles should just use the same position all the way around.
|
|
52
|
+
if (ix == widthSegments) {
|
|
53
|
+
vec3.copy(firstVertex, vertex);
|
|
54
|
+
} else if (ix == 0 || (iy != 0 && iy !== heightSegments)) {
|
|
55
|
+
const rr = radius + (Math.random() - 0.5) * 2 * randomness * radius;
|
|
56
|
+
|
|
57
|
+
// vertex
|
|
58
|
+
vertex[0] = -rr * Math.cos(u * Math.PI * 2) * Math.sin(v * Math.PI);
|
|
59
|
+
vertex[1] = rr * Math.cos(v * Math.PI);
|
|
60
|
+
vertex[2] = rr * Math.sin(u * Math.PI * 2) * Math.sin(v * Math.PI);
|
|
61
|
+
|
|
62
|
+
if (ix == 0) {
|
|
63
|
+
vec3.copy(vertex, firstVertex);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
vertices.push(...vertex);
|
|
68
|
+
|
|
69
|
+
// normal
|
|
70
|
+
vec3.copy(vertex, normal);
|
|
71
|
+
vec3.normalize(normal, normal);
|
|
72
|
+
vertices.push(...normal);
|
|
73
|
+
|
|
74
|
+
// uv
|
|
75
|
+
vertices.push(u + uOffset, 1 - v);
|
|
76
|
+
verticesRow.push(index++);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
grid.push(verticesRow);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
// indices
|
|
83
|
+
for (let iy = 0; iy < heightSegments; iy++) {
|
|
84
|
+
for (let ix = 0; ix < widthSegments; ix++) {
|
|
85
|
+
const a = grid[iy][ix + 1];
|
|
86
|
+
const b = grid[iy][ix];
|
|
87
|
+
const c = grid[iy + 1][ix];
|
|
88
|
+
const d = grid[iy + 1][ix + 1];
|
|
89
|
+
|
|
90
|
+
if (iy !== 0) {
|
|
91
|
+
indices.push(a, b, d);
|
|
92
|
+
}
|
|
93
|
+
if (iy !== heightSegments - 1) {
|
|
94
|
+
indices.push(b, c, d);
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
return {
|
|
100
|
+
vertices: new Float32Array(vertices),
|
|
101
|
+
indices: new Uint16Array(indices),
|
|
102
|
+
};
|
|
103
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import dragonRawData from './stanfordDragonData';
|
|
2
|
+
import { computeProjectedPlaneUVs, generateNormals } from './utils';
|
|
3
|
+
|
|
4
|
+
const { positions, normals, triangles } = generateNormals(
|
|
5
|
+
Math.PI,
|
|
6
|
+
dragonRawData.positions as [number, number, number][],
|
|
7
|
+
dragonRawData.cells as [number, number, number][]
|
|
8
|
+
);
|
|
9
|
+
|
|
10
|
+
const uvs = computeProjectedPlaneUVs(positions, 'xy');
|
|
11
|
+
|
|
12
|
+
// Push indices for an additional ground plane
|
|
13
|
+
triangles.push(
|
|
14
|
+
[positions.length, positions.length + 2, positions.length + 1],
|
|
15
|
+
[positions.length, positions.length + 1, positions.length + 3]
|
|
16
|
+
);
|
|
17
|
+
|
|
18
|
+
// Push vertex attributes for an additional ground plane
|
|
19
|
+
// prettier-ignore
|
|
20
|
+
positions.push(
|
|
21
|
+
[-100, 20, -100], //
|
|
22
|
+
[ 100, 20, 100], //
|
|
23
|
+
[-100, 20, 100], //
|
|
24
|
+
[ 100, 20, -100]
|
|
25
|
+
);
|
|
26
|
+
normals.push(
|
|
27
|
+
[0, 1, 0], //
|
|
28
|
+
[0, 1, 0], //
|
|
29
|
+
[0, 1, 0], //
|
|
30
|
+
[0, 1, 0]
|
|
31
|
+
);
|
|
32
|
+
uvs.push(
|
|
33
|
+
[0, 0], //
|
|
34
|
+
[1, 1], //
|
|
35
|
+
[0, 1], //
|
|
36
|
+
[1, 0]
|
|
37
|
+
);
|
|
38
|
+
|
|
39
|
+
export const mesh = {
|
|
40
|
+
positions,
|
|
41
|
+
triangles,
|
|
42
|
+
normals,
|
|
43
|
+
uvs,
|
|
44
|
+
};
|