Skip to content

Commit 04ed713

Browse files
committed
refine examples and minor tweaks
1 parent 1023cc5 commit 04ed713

19 files changed

+910
-173
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
language: node_js
1+
language: Node.js CI
22
node_js:
33
- '14'
44
install:

demos/cubescape/cubescape.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import { CubeGeometry } from './cubegeometry';
2727
// tslint:disable:max-classes-per-file
2828

2929

30-
const _gEye = vec3.fromValues(1.0, -0.5, -1.0);
30+
const _gEye = vec3.fromValues(1.5, -0.2, 1.5); //vec3.fromValues(1.0, -0.5, -1.0);
3131
const _gCenter = vec3.fromValues(0.0, -1.0, 0.0);
3232
const _gUp = vec3.fromValues(0.0, 1.0, 0.0);
3333

@@ -43,7 +43,7 @@ class CubescapeRenderer extends Renderer {
4343
protected _program: Program;
4444
protected _uViewProjection: WebGLUniformLocation;
4545
protected _aVertex: GLuint;
46-
protected _numCubes = 256;
46+
protected _numCubes = 16;
4747

4848
protected _patches: Texture2D;
4949
protected _terrain: Texture2D;
@@ -76,13 +76,13 @@ class CubescapeRenderer extends Renderer {
7676

7777
// bind FBO
7878
this._defaultFBO.bind();
79-
this._defaultFBO.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT, false, false);
79+
this._defaultFBO.clear(gl.DEPTH_BUFFER_BIT, true, true);
8080

8181
gl.viewport(0, 0, this._frameSize[0], this._frameSize[1]);
8282

83-
gl.enable(gl.CULL_FACE);
84-
gl.cullFace(gl.BACK);
85-
gl.enable(gl.DEPTH_TEST);
83+
// gl.enable(gl.CULL_FACE);
84+
// gl.cullFace(gl.BACK);
85+
// gl.enable(gl.DEPTH_TEST);
8686

8787
this._program.bind();
8888
gl.uniformMatrix4fv(this._uViewProjection, false, this._camera.viewProjection);
@@ -99,8 +99,8 @@ class CubescapeRenderer extends Renderer {
9999

100100
this._program.unbind();
101101

102-
gl.cullFace(gl.BACK);
103-
gl.disable(gl.CULL_FACE);
102+
// gl.cullFace(gl.BACK);
103+
// gl.disable(gl.CULL_FACE);
104104
}
105105

106106
protected onSwap(): void {

demos/point-cloud/benchmark.ts

Lines changed: 69 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1,103 +1,103 @@
11

2-
interface FramePrepareCallback {
3-
(frame: number, framesForWarmup: number,
4-
framesPerRun: number, cycle: number): void;
5-
}
6-
interface RunsFinishedCallback {
7-
(cycles: number, framesForWarmup: number,
8-
framesPerRun: number, results: Array<number>): void;
9-
}
2+
// interface FramePrepareCallback {
3+
// (frame: number, framesForWarmup: number,
4+
// framesPerRun: number, cycle: number): void;
5+
// }
6+
// interface RunsFinishedCallback {
7+
// (cycles: number, framesForWarmup: number,
8+
// framesPerRun: number, results: Array<number>): void;
9+
// }
1010

1111

12-
export class Benchmark {
12+
// export class Benchmark {
1313

14-
protected _running = false;
14+
// protected _running = false;
1515

16-
protected _results: Array<number> = new Array<number>();
16+
// protected _results: Array<number> = new Array<number>();
1717

18-
protected _frames: number;
18+
// protected _frames: number;
1919

20-
protected _runs: number;
21-
protected _framesPerCycle: number;
22-
protected _framesForWarmup: number;
20+
// protected _runs: number;
21+
// protected _framesPerCycle: number;
22+
// protected _framesForWarmup: number;
2323

24-
protected _framePrepare: FramePrepareCallback | undefined = undefined;
25-
protected _runsFinished: RunsFinishedCallback | undefined = undefined;
24+
// protected _framePrepare: FramePrepareCallback | undefined = undefined;
25+
// protected _runsFinished: RunsFinishedCallback | undefined = undefined;
2626

2727

28-
initialize(cycles: number, framesForWarmup: number, framesPerRun: number,
29-
framePrepare: FramePrepareCallback, runsFinished: RunsFinishedCallback): void {
28+
// initialize(cycles: number, framesForWarmup: number, framesPerRun: number,
29+
// framePrepare: FramePrepareCallback, runsFinished: RunsFinishedCallback): void {
3030

31-
if (this._running) {
32-
console.log('benchmark already in progress');
33-
return;
34-
}
31+
// if (this._running) {
32+
// console.log('benchmark already in progress');
33+
// return;
34+
// }
3535

36-
this._framePrepare = framePrepare;
37-
this._runsFinished = runsFinished;
36+
// this._framePrepare = framePrepare;
37+
// this._runsFinished = runsFinished;
3838

3939

40-
this._running = true;
40+
// this._running = true;
4141

42-
this._frames = 0;
42+
// this._frames = 0;
4343

44-
this._runs = Math.max(0, cycles);
45-
this._framesForWarmup = Math.max(0, framesForWarmup);
46-
this._framesPerCycle = Math.max(1, framesPerRun);
44+
// this._runs = Math.max(0, cycles);
45+
// this._framesForWarmup = Math.max(0, framesForWarmup);
46+
// this._framesPerCycle = Math.max(1, framesPerRun);
4747

48-
this._results.length = this._runs;
49-
this._results.fill(0.0);
50-
}
48+
// this._results.length = this._runs;
49+
// this._results.fill(0.0);
50+
// }
5151

52-
frame(): void {
52+
// frame(): void {
5353

54-
if (this._running === false) {
55-
return;
56-
}
57-
++this._frames;
54+
// if (this._running === false) {
55+
// return;
56+
// }
57+
// ++this._frames;
5858

59-
const frames: number = this._frames - this._framesForWarmup;
60-
const frame: number = frames < 0 ? frames : frames % this._framesPerCycle;
59+
// const frames: number = this._frames - this._framesForWarmup;
60+
// const frame: number = frames < 0 ? frames : frames % this._framesPerCycle;
6161

62-
const cycle: number = frames >= 0 ? Math.floor(frames / this._framesPerCycle) : -1;
62+
// const cycle: number = frames >= 0 ? Math.floor(frames / this._framesPerCycle) : -1;
6363

64-
if (frames === 1 - this._framesForWarmup) {
65-
console.log('---- benchmark warmup ------');
66-
}
67-
if (frames === 0) {
68-
console.log('---- benchmark started -----');
69-
}
64+
// if (frames === 1 - this._framesForWarmup) {
65+
// console.log('---- benchmark warmup ------');
66+
// }
67+
// if (frames === 0) {
68+
// console.log('---- benchmark started -----');
69+
// }
7070

71-
if ((frames % this._framesPerCycle) === 0 && cycle > 0) {
72-
this._results[cycle - 1] = (performance.now() - this._results[cycle - 1]) / this._framesPerCycle;
73-
console.log(' -- cycle: ' + cycle.toString().padStart(2, '0') +
74-
', fps: ' + this._results[cycle - 1].toFixed(4).padStart(9, '0'));
75-
}
71+
// if ((frames % this._framesPerCycle) === 0 && cycle > 0) {
72+
// this._results[cycle - 1] = (performance.now() - this._results[cycle - 1]) / this._framesPerCycle;
73+
// console.log(' -- cycle: ' + cycle.toString().padStart(2, '0') +
74+
// ', fps: ' + this._results[cycle - 1].toFixed(4).padStart(9, '0'));
75+
// }
7676

77-
if ((frames % this._framesPerCycle) === 0 && cycle >= 0 && cycle < this._runs) {
78-
this._results[cycle] = performance.now();
79-
}
77+
// if ((frames % this._framesPerCycle) === 0 && cycle >= 0 && cycle < this._runs) {
78+
// this._results[cycle] = performance.now();
79+
// }
8080

8181

82-
if (cycle >= this._runs) {
82+
// if (cycle >= this._runs) {
8383

84-
this._running = false;
85-
console.log('---- benchmark stopped -----');
84+
// this._running = false;
85+
// console.log('---- benchmark stopped -----');
8686

87-
this._framePrepare = undefined;
87+
// this._framePrepare = undefined;
8888

89-
this._runsFinished!(this._runs, this._framesForWarmup, this._framesPerCycle, this._results);
90-
this._runsFinished = undefined;
89+
// this._runsFinished!(this._runs, this._framesForWarmup, this._framesPerCycle, this._results);
90+
// this._runsFinished = undefined;
9191

92-
} else {
92+
// } else {
9393

94-
this._framePrepare!(frame, this._framesForWarmup, this._framesPerCycle, cycle);
94+
// this._framePrepare!(frame, this._framesForWarmup, this._framesPerCycle, cycle);
9595

96-
}
97-
}
96+
// }
97+
// }
9898

99-
get running(): boolean {
100-
return this._running;
101-
}
99+
// get running(): boolean {
100+
// return this._running;
101+
// }
102102

103-
}
103+
// }

examples/data/instanced-zpre.frag

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
2+
precision highp float;
3+
precision highp int;
4+
5+
@import ../../source/shaders/facade.frag;
6+
7+
#if __VERSION__ == 100
8+
#define fragColor gl_FragColor
9+
#else
10+
layout(location = 0) out vec4 fragColor;
11+
#endif
12+
13+
14+
uniform int u_drawMode;
15+
16+
uniform bool u_textured;
17+
18+
uniform sampler2D u_texture;
19+
uniform vec4 u_clearColor;
20+
21+
// uniform float u_seed;
22+
23+
24+
varying vec4 v_vertex;
25+
varying vec2 v_uv;
26+
27+
varying float v_attenuation;
28+
29+
30+
vec2 rand2(in vec2 uv);
31+
32+
vec4 simpleLoad();
33+
vec4 heavyLoad();
34+
35+
36+
vec4 attenuate(vec4 diffuse) {
37+
if(u_drawMode >= 3) { // indicate prez
38+
diffuse.rgb = mix(v_vertex.xyz * 0.5 + 0.5, diffuse.rgb, 0.9);
39+
}
40+
return mix(diffuse, u_clearColor, v_attenuation);
41+
}
42+
43+
void main(void)
44+
{
45+
if(u_drawMode == 0) {
46+
fragColor = vec4(1.0);
47+
return;
48+
}
49+
50+
if(!u_textured) { // ignore draw mode, just color the cube
51+
fragColor = attenuate(vec4(v_vertex.xyz * 0.5 + 0.5, 1.0));
52+
return;
53+
}
54+
55+
// simple load
56+
if(u_drawMode == 1 || u_drawMode == 3) {
57+
fragColor = attenuate(simpleLoad());
58+
return;
59+
}
60+
// heavy load
61+
fragColor = attenuate(heavyLoad());
62+
}
63+
64+
65+
vec4 simpleLoad() {
66+
return texture(u_texture, v_uv);
67+
}
68+
69+
const int samples = 1024;
70+
vec4 heavyLoad() {
71+
vec4 diffuse;
72+
for(int i = 0; i < samples; ++i) {
73+
diffuse += texture(u_texture, v_uv + rand2(v_uv) * 0.001 - 0.0005);
74+
}
75+
return diffuse / float(samples);
76+
}
77+
78+
vec2 rand2(in vec2 uv) {
79+
vec2 v = vec2(dot(uv, vec2(127.1, 311.7)), dot(uv, vec2(269.5, 183.3)));
80+
return clamp(normalize(-1.0 + 2.0 * fract(sin(/* u_seed + */ v) * 43758.5453123)), -1.0, 1.0);
81+
}

examples/data/instanced-zpre.vert

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
2+
precision highp float;
3+
precision highp int;
4+
5+
@import ../../source/shaders/facade.vert;
6+
7+
#if __VERSION__ == 100
8+
attribute vec3 a_vertex;
9+
attribute vec2 a_texCoord;
10+
#else
11+
layout(location = 0) in vec3 a_vertex;
12+
layout(location = 1) in vec2 a_texCoord;
13+
#endif
14+
15+
uniform float u_seed;
16+
17+
uniform mat4 u_model;
18+
uniform mat4 u_viewProjection;
19+
20+
uniform int u_numInstances;
21+
uniform int u_drawMode;
22+
23+
24+
varying vec4 v_vertex;
25+
varying vec2 v_uv;
26+
// varying vec3 v_normal;
27+
28+
varying float v_attenuation;
29+
30+
31+
float rand(in vec2 uv) {
32+
// return mod(uv.x * 12213.231 + uv.y * 32132.567, 1.0);
33+
return fract(sin(u_seed + dot(uv, vec2(12.9898, 78.233))) * 4375.5453123);
34+
}
35+
36+
void main()
37+
{
38+
int n = u_numInstances;
39+
float n05 = float(u_numInstances) * 0.5;
40+
41+
vec3 offset = vec3(float(gl_InstanceID % n), float((gl_InstanceID / n) % n), float(gl_InstanceID / (n * n)));
42+
offset += 0.5 - n05 + 0.5 * vec3(rand(offset.yz), rand(offset.zx), rand(offset.xy)) - 0.25;
43+
44+
vec4 vertex = u_model * vec4(a_vertex, 1.0);
45+
46+
if(u_drawMode == 0) {
47+
48+
} else {
49+
v_attenuation = min(1.0, pow(length(offset) / n05, 2.0));
50+
v_uv = a_texCoord;
51+
// v_normal = normalize(a_vertex);
52+
v_vertex = vertex;
53+
}
54+
55+
offset.xyz *= 0.50;
56+
vertex.xyz *= 0.125;
57+
58+
vec4 position = u_viewProjection * (vertex + vec4(offset, 1.0));
59+
gl_Position = position;
60+
}

examples/data/instanced.frag

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ uniform vec4 u_clearColor;
1313

1414
varying vec2 v_uv;
1515
varying float v_attenuation;
16+
varying vec3 v_position;
1617

1718

1819
void main(void)

examples/data/instanced.vert

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ precision highp float;
1515

1616
varying vec2 v_uv;
1717
varying float v_attenuation;
18+
varying vec3 v_position;
1819

1920
uniform mat4 u_viewProjection;
2021

@@ -25,5 +26,6 @@ void main(void)
2526
vec3 offset = vec3(float(gl_InstanceID % 32 - 16), float((gl_InstanceID / 32) % 32 - 16), float(gl_InstanceID / 1024 - 16));
2627
v_attenuation = min(1.0, length(offset) * 0.1);
2728
vec3 position = a_position.xyz;
28-
gl_Position = u_viewProjection * vec4(position * 0.03125 + offset * 0.25, 1.0);
29+
v_position = position * 0.6 + 0.5;
30+
gl_Position = u_viewProjection * vec4(position * 0.1 + offset * 0.25, 1.0);
2931
}

0 commit comments

Comments
 (0)