Skip to content

Commit eee755e

Browse files
committed
merge feat/sampler into master
1 parent c2aca26 commit eee755e

20 files changed

+2459
-331
lines changed

controller/fps-camera.controller.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ export class FpsCameraController implements CameraControllerInterface {
2323
constructor(
2424
private canvas: HTMLCanvasElement,
2525
public readonly camera: Camera,
26-
private speed = 1.0e2,
26+
private speed = 1.0e1,
2727
public sensibility = 1.0e-2
2828
) {
2929
this.canvas = canvas;

graphics/gizmo-renderer.ts

-37
This file was deleted.

graphics/gizmos.ts

+199
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,199 @@
1+
import { loadObj } from '../loader';
2+
import { Geometry, Gpu, Mesh, ShaderProgram } from './gpu';
3+
import { Transform } from './transform';
4+
import { mat4, vec3, vec4 } from 'gl-matrix';
5+
6+
import OBJ from '../objects/shapes';
7+
import { createGrid } from './mesh';
8+
import { Camera } from './camera';
9+
import { vs, fs } from './programs/mesh';
10+
11+
export class Gizmos {
12+
private readonly sphereGeometry: Geometry;
13+
private readonly gridGeometry: Geometry;
14+
private readonly patchGeometry: Geometry;
15+
private readonly transform = new Transform();
16+
private readonly meshShader: ShaderProgram;
17+
18+
constructor(private readonly gpu: Gpu) {
19+
this.meshShader = this.gpu.createShaderProgram(vs, fs);
20+
this.gridGeometry = this.gpu.createGeometry(
21+
createGrid(5.0),
22+
WebGL2RenderingContext.LINES
23+
);
24+
this.patchGeometry = this.gpu.createGeometry(
25+
createPatch(4),
26+
WebGL2RenderingContext.LINES
27+
);
28+
const obj = loadObj(OBJ);
29+
this.sphereGeometry = this.gpu.createGeometry(obj['sphere']);
30+
}
31+
32+
drawGrid(camera: Camera) {
33+
this.gpu.setProgram(this.meshShader);
34+
this.gpu.setProgramVariable(
35+
this.meshShader,
36+
'worldMat',
37+
'mat4',
38+
mat4.create()
39+
);
40+
this.gpu.setProgramVariable(
41+
this.meshShader,
42+
'viewMat',
43+
'mat4',
44+
camera.view
45+
);
46+
this.gpu.setProgramVariable(
47+
this.meshShader,
48+
'projMat',
49+
'mat4',
50+
camera.projection
51+
);
52+
this.gpu.setProgramVariable(
53+
this.meshShader,
54+
'pos',
55+
'vec3',
56+
camera.position
57+
);
58+
this.gpu.setProgramVariable(
59+
this.meshShader,
60+
'albedo',
61+
'vec4',
62+
vec4.fromValues(1.0, 1.0, 1.0, 0.0)
63+
);
64+
65+
this.gpu.drawGeometry(this.gridGeometry);
66+
}
67+
68+
drawPointSample(
69+
camera: Camera,
70+
position: vec3,
71+
color: vec4 = vec4.fromValues(1.0, 0.25, 0.25, 1.0)
72+
): void {
73+
this.transform.reset();
74+
this.transform.position = position;
75+
this.transform.scale = vec3.fromValues(0.2, 0.2, 0.2);
76+
77+
this.gpu.setProgram(this.meshShader);
78+
this.gpu.setProgramVariable(
79+
this.meshShader,
80+
'worldMat',
81+
'mat4',
82+
this.transform.transform
83+
);
84+
this.gpu.setProgramVariable(
85+
this.meshShader,
86+
'viewMat',
87+
'mat4',
88+
camera.view
89+
);
90+
this.gpu.setProgramVariable(
91+
this.meshShader,
92+
'projMat',
93+
'mat4',
94+
camera.projection
95+
);
96+
this.gpu.setProgramVariable(
97+
this.meshShader,
98+
'pos',
99+
'vec3',
100+
camera.position
101+
);
102+
this.gpu.setProgramVariable(this.meshShader, 'albedo', 'vec4', color);
103+
this.gpu.drawGeometry(this.sphereGeometry);
104+
}
105+
106+
drawPatchSample(camera: Camera, patch: vec3[], yOffset: number = 0.1): void {
107+
this.transform.reset();
108+
this.transform.position = vec3.fromValues(0.0, yOffset, 0.0);
109+
110+
const buffer = Float32Array.from(patch.map((e) => [...e]).flat());
111+
this.gpu.updateGeometry(this.patchGeometry, buffer);
112+
this.gpu.setProgram(this.meshShader);
113+
this.gpu.setProgramVariable(
114+
this.meshShader,
115+
'worldMat',
116+
'mat4',
117+
this.transform.transform
118+
);
119+
this.gpu.setProgramVariable(
120+
this.meshShader,
121+
'viewMat',
122+
'mat4',
123+
camera.view
124+
);
125+
this.gpu.setProgramVariable(
126+
this.meshShader,
127+
'projMat',
128+
'mat4',
129+
camera.projection
130+
);
131+
this.gpu.setProgramVariable(
132+
this.meshShader,
133+
'pos',
134+
'vec3',
135+
camera.position
136+
);
137+
this.gpu.setProgramVariable(
138+
this.meshShader,
139+
'albedo',
140+
'vec4',
141+
vec4.fromValues(0.25, 1.0, 0.25, 0.0)
142+
);
143+
this.gpu.drawGeometry(this.patchGeometry);
144+
}
145+
}
146+
147+
const createPatch = (resolution: number): Mesh => {
148+
const indices: number[] = [];
149+
const points: vec3[] = [];
150+
const colors: vec3[] = [];
151+
152+
for (let i = 0; i < resolution; i++) {
153+
for (let j = 0; j < resolution; j++) {
154+
if (i < resolution - 1 && j < resolution - 1) {
155+
const i0 = i * resolution + j;
156+
const i1 = i * resolution + j + 1;
157+
const i2 = (i + 1) * resolution + j + 1;
158+
const i3 = (i + 1) * resolution + j;
159+
indices.push(i0, i1, i1, i2, i2, i3, i3, i0);
160+
}
161+
162+
points.push(vec3.create());
163+
if (i === 0 || j === 0 || i === resolution - 1 || j === resolution - 1) {
164+
colors.push(vec3.fromValues(1.0, 1.0, 0.0));
165+
} else {
166+
colors.push(vec3.fromValues(1.0, 0.5, 0.0));
167+
}
168+
}
169+
}
170+
171+
return {
172+
vertexFormat: [
173+
{
174+
semantics: 'position',
175+
size: 3,
176+
type: WebGL2RenderingContext.FLOAT,
177+
slot: 0,
178+
offset: 0,
179+
stride: 12,
180+
},
181+
{
182+
semantics: 'color',
183+
size: 3,
184+
type: WebGL2RenderingContext.FLOAT,
185+
slot: 2,
186+
offset: 12 * resolution ** 2,
187+
stride: 12,
188+
},
189+
],
190+
191+
vertexData: Float32Array.from(
192+
points
193+
.concat(colors)
194+
.map((v) => [...v])
195+
.flat()
196+
),
197+
indexData: Uint32Array.from(indices),
198+
};
199+
};

0 commit comments

Comments
 (0)