generated from BarthPaleologue/babylonjs-template
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Compute shaders work for the planet terrain - Fixed character walking on sphere - added custom url parameter to enable webgpu (it will always default to webgl for stability) - formatting
- Loading branch information
1 parent
ed76a78
commit 7b97bcf
Showing
21 changed files
with
397 additions
and
211 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
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,85 @@ | ||
import heightMapComputeSource from "./vertexData.wgsl"; | ||
import { VertexData } from "@babylonjs/core/Meshes/mesh.vertexData"; | ||
import { Engine } from "@babylonjs/core/Engines/engine"; | ||
import { ComputeShader } from "@babylonjs/core/Compute/computeShader"; | ||
import { StorageBuffer } from "@babylonjs/core/Buffers/storageBuffer"; | ||
import { UniformBuffer } from "@babylonjs/core/Materials/uniformBuffer"; | ||
import { Matrix, Quaternion, Vector3 } from "@babylonjs/core/Maths/math.vector"; | ||
|
||
export async function computeVertexData(nbVerticesPerRow: number, position: Vector3, rotation: Quaternion, size: number, engine: Engine): Promise<VertexData> { | ||
const computeShader = new ComputeShader( | ||
"heightMap", | ||
engine, | ||
{ computeSource: heightMapComputeSource }, | ||
{ | ||
bindingsMapping: { | ||
positions: { group: 0, binding: 0 }, | ||
normals: { group: 0, binding: 1 }, | ||
indices: { group: 0, binding: 2 }, | ||
params: { group: 0, binding: 3 } | ||
} | ||
} | ||
); | ||
|
||
const positions = new Float32Array(nbVerticesPerRow * nbVerticesPerRow * 3); | ||
const normals = new Float32Array(nbVerticesPerRow * nbVerticesPerRow * 3); | ||
const indices = new Uint32Array((nbVerticesPerRow - 1) * (nbVerticesPerRow - 1) * 6); | ||
|
||
const positionsBuffer = new StorageBuffer(engine, positions.byteLength); | ||
positionsBuffer.update(positions); | ||
computeShader.setStorageBuffer("positions", positionsBuffer); | ||
|
||
const normalsBuffer = new StorageBuffer(engine, normals.byteLength); | ||
normalsBuffer.update(normals); | ||
computeShader.setStorageBuffer("normals", normalsBuffer); | ||
|
||
const indicesBuffer = new StorageBuffer(engine, indices.byteLength); | ||
indicesBuffer.update(indices); | ||
computeShader.setStorageBuffer("indices", indicesBuffer); | ||
|
||
const paramsBuffer = new UniformBuffer(engine); | ||
|
||
paramsBuffer.addUniform("nbVerticesPerRow", 1); | ||
paramsBuffer.addUniform("size", 1); | ||
paramsBuffer.addUniform("position", 3); | ||
paramsBuffer.addUniform("rotationMatrix", 16); | ||
|
||
paramsBuffer.updateUInt("nbVerticesPerRow", nbVerticesPerRow); | ||
paramsBuffer.updateFloat("size", size); | ||
paramsBuffer.updateVector3("position", position); | ||
paramsBuffer.updateMatrix("rotationMatrix", rotation.toRotationMatrix(new Matrix())); | ||
paramsBuffer.update(); | ||
|
||
computeShader.setUniformBuffer("params", paramsBuffer); | ||
|
||
return new Promise((resolve, reject) => { | ||
computeShader | ||
.dispatchWhenReady(nbVerticesPerRow, nbVerticesPerRow, 1) | ||
.then(async () => { | ||
try { | ||
const [positionsBufferView, normalsBufferView, indicesBufferView] = await Promise.all([positionsBuffer.read(), normalsBuffer.read(), indicesBuffer.read()]); | ||
|
||
const positions = new Float32Array(positionsBufferView.buffer); | ||
positionsBuffer.dispose(); | ||
|
||
const normals = new Float32Array(normalsBufferView.buffer); | ||
normalsBuffer.dispose(); | ||
|
||
const indices = new Uint32Array(indicesBufferView.buffer); | ||
indicesBuffer.dispose(); | ||
|
||
const vertexData = new VertexData(); | ||
vertexData.positions = positions; | ||
vertexData.indices = indices; | ||
vertexData.normals = normals; | ||
|
||
resolve(vertexData); | ||
} catch (error) { | ||
reject(new Error("Error: " + error)); | ||
} | ||
}) | ||
.catch((error) => { | ||
reject(new Error("Error: " + error)); | ||
}); | ||
}); | ||
} |
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,57 @@ | ||
struct Params { | ||
nbVerticesPerRow : u32, | ||
size : f32, | ||
position : vec3<f32>, | ||
rotationMatrix : mat4x4<f32>, | ||
}; | ||
|
||
struct FloatArray { | ||
elements : array<f32>, | ||
}; | ||
struct UIntArray { | ||
elements : array<atomic<u32>>, | ||
}; | ||
|
||
@group(0) @binding(0) var<storage, read_write> positions : FloatArray; | ||
@group(0) @binding(1) var<storage, read_write> normals : FloatArray; | ||
@group(0) @binding(2) var<storage, read_write> indices : UIntArray; | ||
@group(0) @binding(3) var<uniform> params : Params; | ||
|
||
@compute @workgroup_size(1,1,1) | ||
fn main(@builtin(global_invocation_id) id: vec3<u32>) | ||
{ | ||
let x : f32 = f32(id.x); | ||
let y : f32 = f32(id.y); | ||
|
||
let index: u32 = id.x + id.y * u32(params.nbVerticesPerRow); | ||
|
||
let vertex_position = vec3<f32>(params.size * x / f32(params.nbVerticesPerRow - 1) - params.size / 2.0, params.size * y / f32(params.nbVerticesPerRow - 1) - params.size / 2.0, 0.0); | ||
var vertex_position_world: vec3<f32> = vertex_position + vec3(0.0, 0.0, -params.size / 2.0); | ||
vertex_position_world = (params.rotationMatrix * vec4<f32>(vertex_position_world, 1.0)).xyz; | ||
|
||
let normal: vec3<f32> = normalize(vertex_position_world); | ||
|
||
vertex_position_world = normal * params.size / 2.0; | ||
|
||
//vertex_position_world = vertex_position_world - params.position; | ||
|
||
positions.elements[index * 3 + 0] = vertex_position_world.x; | ||
positions.elements[index * 3 + 1] = vertex_position_world.y; | ||
positions.elements[index * 3 + 2] = vertex_position_world.z; | ||
|
||
normals.elements[index * 3 + 0] = normal.x; | ||
normals.elements[index * 3 + 1] = normal.y; | ||
normals.elements[index * 3 + 2] = normal.z; | ||
|
||
if(x > 0 && y > 0) { | ||
let indexIndex = ((id.x - 1) + (id.y - 1) * (params.nbVerticesPerRow - 1)) * 6; | ||
|
||
atomicStore(&indices.elements[indexIndex + 0], index - 1); | ||
atomicStore(&indices.elements[indexIndex + 1], index - params.nbVerticesPerRow - 1); | ||
atomicStore(&indices.elements[indexIndex + 2], index); | ||
|
||
atomicStore(&indices.elements[indexIndex + 3], index); | ||
atomicStore(&indices.elements[indexIndex + 4], index - params.nbVerticesPerRow - 1); | ||
atomicStore(&indices.elements[indexIndex + 5], index - params.nbVerticesPerRow); | ||
} | ||
} |
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
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
Oops, something went wrong.