-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCubeScene.cs
More file actions
73 lines (63 loc) · 1.98 KB
/
Copy pathCubeScene.cs
File metadata and controls
73 lines (63 loc) · 1.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
using System.Numerics;
using Silk.NET.WebGPU;
namespace SilkWebGpuPbr;
public unsafe sealed class CubeScene : IDisposable
{
private readonly PbrInstancedRenderer _renderer;
private readonly GpuMesh _cube;
private readonly PbrMaterial[] _materials;
private readonly MeshInstance[] _instances;
public CubeScene(
WebGPU webGpu,
Device* device,
Queue* queue,
TextureFormat colorFormat,
TextureFormat depthFormat,
float cubeSize = 1.0f)
{
_renderer = new PbrInstancedRenderer(webGpu, device, queue, colorFormat, depthFormat);
MeshData cubeData = PrimitiveMeshes.CreateCube(cubeSize);
_cube = new GpuMesh(webGpu, device, queue, "Cube", cubeData.Vertices, cubeData.Indices);
_materials =
[
new PbrMaterial
{
BaseColorFactor = new Vector4(0.95f, 0.38f, 0.18f, 1.0f),
EmissiveFactor = Vector4.Zero,
MetallicFactor = 0.0f,
RoughnessFactor = 0.48f,
NormalScale = 1.0f,
OcclusionStrength = 1.0f
}
];
_instances = [MeshInstance.Create(Matrix4x4.Identity, 0)];
_renderer.UpdateMaterials(_materials);
_renderer.UpdateInstances(_instances);
}
public void SetCubeTransform(Matrix4x4 model)
{
_instances[0] = MeshInstance.Create(model, 0);
_renderer.UpdateInstances(_instances);
}
public void Draw(
RenderPassEncoder* pass,
Matrix4x4 viewProjection,
Vector3 cameraPosition,
Vector3 lightDirection)
{
_renderer.UpdateScene(GpuSceneData.Create(
viewProjection,
cameraPosition,
lightDirection,
Vector3.One,
4.0f,
new Vector3(0.035f, 0.04f, 0.05f),
1.0f));
_renderer.Draw(pass, _cube);
}
public void Dispose()
{
_cube.Dispose();
_renderer.Dispose();
}
}