-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from JediWattson/modularize
Modularize
- Loading branch information
Showing
40 changed files
with
8,303 additions
and
3,229 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
'use client'; | ||
|
||
import { useRef } from "react"; | ||
import { useRender } from "webgpu-fun"; | ||
import example from "../lib/example"; | ||
|
||
function Canvas() { | ||
const canvasRef = useRef<HTMLCanvasElement>(null) | ||
useRender(canvasRef, example()) | ||
return <canvas ref={canvasRef} tabIndex={0} /> | ||
} | ||
|
||
export default Canvas; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
'use client'; | ||
|
||
// each point contains 3 position values, 3 color values | ||
const triangeVert = [ | ||
0.0, 0.0, 0.5, 1.0, 0.0, 0.0, | ||
0.0, -0.5, -0.5, 0.0, 1.0, 0.0, | ||
0.0, 0.5, -0.5, 0.0, 0.0, 1.0 | ||
] | ||
|
||
// each point contains 3 position values, 2 texture pos values | ||
const quadVert = [ | ||
-0.5, 0.5, 0.0, 1.0, 0.0, | ||
-0.5, -0.5, 0.0, 1.0, 1.0, | ||
0.5, -0.5, 0.0, 0.0, 1.0, | ||
|
||
0.5, -0.5, 0.0, 0.0, 1.0, | ||
0.5, 0.5, 0.0, 0.0, 0.0, | ||
-0.5, 0.5, 0.0, 1.0, 0.0, | ||
] | ||
|
||
import { makeAssetBuffer } from 'webgpu-fun' | ||
|
||
const makeTriangle = makeAssetBuffer(triangeVert, 3) | ||
const makeQuad = makeAssetBuffer(quadVert, 6) | ||
export { makeQuad, makeTriangle } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
'use client'; | ||
|
||
import { vec3 } from "wgpu-matrix"; | ||
|
||
import textureShader from "./shaders/texture.wgsl"; | ||
import meshShader from "./shaders/mesh.wgsl"; | ||
|
||
import { makeQuad, makeTriangle } from "./assets"; | ||
import { updateFloor, updateTriangles } from "./utils"; | ||
|
||
const floorCount = 40 | ||
const triangleCount = 40; | ||
|
||
function makeExampleConfig() { | ||
return [ | ||
{ | ||
bufferSize: 64 * (1 + (floorCount*2))**2, | ||
texturePipelineOpts: { | ||
materialUrl: 'floor.jpeg', | ||
}, | ||
renderPipelineOpts: { | ||
shader: textureShader, | ||
vertexBufferLayout: [{ | ||
arrayStride: 20, | ||
attributes: [ | ||
{ | ||
shaderLocation: 0, | ||
format: `float32x3`, | ||
offset: 0 | ||
}, | ||
{ | ||
shaderLocation: 1, | ||
format: `float32x2`, | ||
offset: 12 | ||
} | ||
] | ||
}], | ||
}, | ||
bufferCb: (device: GPUDevice, buffer: GPUBuffer) => { | ||
const floorMesh = makeQuad(device, buffer); | ||
const objects = Array(floorCount**2).fill(0).map( | ||
(_, i) => vec3.create(i%floorCount, Math.floor(i/floorCount), -1) | ||
); | ||
floorMesh.makeObjects(objects); | ||
updateFloor(floorMesh); | ||
return floorMesh | ||
} | ||
}, { | ||
bufferSize: 64 * floorCount, | ||
renderPipelineOpts: { | ||
shader: meshShader, | ||
vertexBufferLayout: [{ | ||
arrayStride: 24, | ||
attributes: [ | ||
{ | ||
shaderLocation: 0, | ||
format: `float32x3`, | ||
offset: 0 | ||
}, | ||
{ | ||
shaderLocation: 1, | ||
format: `float32x3`, | ||
offset: 12 | ||
} | ||
] | ||
}] | ||
}, | ||
bufferCb: (device: GPUDevice, buffer: GPUBuffer) => { | ||
const triangleMesh = makeTriangle(device, buffer); | ||
const objects = Array(triangleCount).fill(0).map((_, i) => vec3.create(2, i, -0.5)); | ||
triangleMesh.makeObjects(objects); | ||
triangleMesh.updateMaterial = updateTriangles; | ||
return triangleMesh | ||
} | ||
} | ||
] | ||
} | ||
|
||
|
||
|
||
export default makeExampleConfig; |
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { mat4 } from "wgpu-matrix"; | ||
|
||
export function updateFloor(floorTexture) { | ||
floorTexture.update(pos => mat4.translation(pos)) | ||
} | ||
|
||
let t = 0.0 | ||
export function updateTriangles(triangleMesh) { | ||
t += 0.01 | ||
if (t > 2.0 * Math.PI) { | ||
t -= 2.0 * Math.PI; | ||
} | ||
|
||
triangleMesh.update(pos => { | ||
const model = mat4.create(); | ||
mat4.translation(pos, model); | ||
return mat4.rotateZ(model, t); | ||
}); | ||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
{ | ||
"name": "webgpu-example", | ||
"version": "0.1.0", | ||
"private": true, | ||
"scripts": { | ||
"dev": "next dev", | ||
"build": "next build", | ||
"start": "next start", | ||
"lint": "next lint" | ||
}, | ||
"dependencies": { | ||
"next": "^14.0.4", | ||
"react": "18.2.0", | ||
"react-dom": "18.2.0", | ||
"webgpu-fun": "^0.2.19" | ||
}, | ||
"devDependencies": { | ||
"@types/node": "20.1.2", | ||
"@types/react": "18.2.6", | ||
"@types/react-dom": "18.2.4", | ||
"eslint": "8.40.0", | ||
"eslint-config-next": "13.4.1", | ||
"raw-loader": "^4.0.2", | ||
"typescript": "^5.3.3" | ||
} | ||
} |
File renamed without changes
File renamed without changes
File renamed without changes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
{ | ||
"compilerOptions": { | ||
"lib": [ | ||
"dom", | ||
"dom.iterable", | ||
"esnext" | ||
], | ||
"types": ["@webgpu/types"], | ||
"allowJs": true, | ||
"skipLibCheck": true, | ||
"strict": false, | ||
"forceConsistentCasingInFileNames": true, | ||
"noEmit": true, | ||
"incremental": true, | ||
"esModuleInterop": true, | ||
"module": "esnext", | ||
"moduleResolution": "node", | ||
"resolveJsonModule": true, | ||
"isolatedModules": true, | ||
"jsx": "preserve", | ||
"plugins": [ | ||
{ | ||
"name": "next" | ||
} | ||
] | ||
}, | ||
"include": [ | ||
"next-env.d.ts", | ||
".next/types/**/*.ts", | ||
"**/*.ts", | ||
"**/*.tsx" | ||
], | ||
"exclude": [ | ||
"node_modules" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
declare module '*.wgsl' { | ||
const shader: string; | ||
export default shader; | ||
} |
Oops, something went wrong.