Skip to content

Commit 8523b22

Browse files
committed
changes to test projects
1 parent 8c6319f commit 8523b22

File tree

5 files changed

+144
-2
lines changed

5 files changed

+144
-2
lines changed

test/Blazor.Extensions.Canvas.Test.ClientSide/Pages/Index.razor

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
<h1>Canvas demo!!!</h1>
55

6-
<a href="/webgl">View WebGL page</a><br>
6+
<a href="/webgl2">View WebGL2 page</a><br>
77

88
Welcome to your new app.
99

test/Blazor.Extensions.Canvas.Test.ClientSide/Pages/WebGLComponent.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Diagnostics;
23
using System.Threading.Tasks;
34
using Blazor.Extensions.Canvas.WebGL;
45
using Microsoft.AspNetCore.Components;
@@ -63,7 +64,9 @@ protected override async Task OnAfterRenderAsync(bool firstRender)
6364
private async Task<WebGLProgram> InitProgramAsync(WebGLContext gl, string vsSource, string fsSource)
6465
{
6566
var vertexShader = await this.LoadShaderAsync(gl, ShaderType.VERTEX_SHADER, vsSource);
67+
Debug.WriteLine(vertexShader.Id);
6668
var fragmentShader = await this.LoadShaderAsync(gl, ShaderType.FRAGMENT_SHADER, fsSource);
69+
Debug.WriteLine(fragmentShader.Id);
6770

6871
var program = await gl.CreateProgramAsync();
6972
await gl.AttachShaderAsync(program, vertexShader);
@@ -86,6 +89,9 @@ private async Task<WebGLShader> LoadShaderAsync(WebGLContext gl, ShaderType type
8689
{
8790
var shader = await gl.CreateShaderAsync(type);
8891

92+
Console.WriteLine(shader.Id);
93+
Debug.WriteLine(shader.Id);
94+
8995
await gl.ShaderSourceAsync(shader, source);
9096
await gl.CompileShaderAsync(shader);
9197

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"version": "1.0",
3+
"defaultProvider": "cdnjs",
4+
"libraries": []
5+
}

test/Blazor.Extensions.Canvas.Test.ServerSide/Pages/Index.razor

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
<h1>Canvas demo!!!</h1>
55

6-
<a href="/webgl">View WebGL page</a><br>
6+
<a href="/webgl2">View WebGL2 page</a><br>
77

88
Welcome to your new app.
99

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
@page "/webgl2"
2+
@using Blazor.Extensions.Canvas.WebGL
3+
@implements IAsyncDisposable
4+
5+
<h1>Canvas demo!!!</h1>
6+
7+
<a href="/">View Canvas2d page</a><br>
8+
9+
Welcome to your new app.
10+
11+
<BECanvas Width="800" Height="600" @ref="canvasReference" ></BECanvas>
12+
13+
@code {
14+
private BECanvasComponent canvasReference;
15+
private WebGL2Context context;
16+
private WebGLProgram program;
17+
private WebGLBuffer vertexBuffer;
18+
private WebGLBuffer elementBuffer;
19+
private WebGLBuffer instanceBuffer;
20+
private WebGLVertexArrayObject vertexArray;
21+
22+
private Task BuildAsync(WebGLProgram program, string vs, string fs)
23+
{
24+
async Task compile(ShaderType type, string source, Func<Task> action)
25+
{
26+
var shader = await context.CreateShaderAsync(type);
27+
try
28+
{
29+
await context.ShaderSourceAsync(shader, source);
30+
await context.CompileShaderAsync(shader);
31+
var error = await context.GetShaderInfoLogAsync(shader);
32+
if (error != string.Empty) throw new Exception(error);
33+
await context.AttachShaderAsync(program, shader);
34+
try
35+
{
36+
await action();
37+
}
38+
finally
39+
{
40+
await context.DetachShaderAsync(program, shader);
41+
}
42+
}
43+
finally
44+
{
45+
await context.DeleteShaderAsync(shader);
46+
}
47+
}
48+
return compile(ShaderType.VERTEX_SHADER, vs, () => compile(ShaderType.FRAGMENT_SHADER, fs, async () =>
49+
{
50+
await context.LinkProgramAsync(program);
51+
var error = await context.GetProgramInfoLogAsync(program);
52+
if (error != string.Empty) throw new Exception(error);
53+
}));
54+
}
55+
56+
public async ValueTask DisposeAsync()
57+
{
58+
if (context == null) return;
59+
await context.DeleteProgramAsync(program);
60+
await context.DeleteBufferAsync(vertexBuffer);
61+
await context.DeleteBufferAsync(elementBuffer);
62+
await context.DeleteBufferAsync(instanceBuffer);
63+
await context.DeleteVertexArrayAsync(vertexArray);
64+
context.Dispose();
65+
}
66+
protected override async Task OnAfterRenderAsync(bool firstRender)
67+
{
68+
if (firstRender)
69+
{
70+
context = await canvasReference.CreateWebGL2Async();
71+
program = await context.CreateProgramAsync();
72+
vertexBuffer = await context.CreateBufferAsync();
73+
elementBuffer = await context.CreateBufferAsync();
74+
instanceBuffer = await context.CreateBufferAsync();
75+
vertexArray = await context.CreateVertexArrayAsync();
76+
await BuildAsync(program,
77+
@"#version 300 es
78+
layout(location = 0) in vec3 aPos;
79+
layout(location = 1) in vec3 aColor;
80+
layout(location = 2) in vec3 aOffset;
81+
out vec3 vColor;
82+
83+
void main() {
84+
gl_Position = vec4(aPos + aOffset, 1.0);
85+
vColor = aColor;
86+
}",
87+
@"#version 300 es
88+
precision mediump float;
89+
in vec3 vColor;
90+
out vec4 fColor;
91+
92+
void main() {
93+
fColor = vec4(vColor, 1.0);
94+
}"
95+
);
96+
await context.BindVertexArrayAsync(vertexArray);
97+
await context.BindBufferAsync(BufferType.ARRAY_BUFFER, vertexBuffer);
98+
await context.BufferDataAsync(BufferType.ARRAY_BUFFER, new[]
99+
{
100+
-0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 0.0f,
101+
0.5f, -0.5f, 0.0f, 0.0f, 1.0f, 0.0f,
102+
0.0f, 0.5f, 0.0f, 0.0f, 0.0f, 1.0f
103+
}, BufferUsageHint.STATIC_DRAW);
104+
await context.VertexAttribPointerAsync(0, 3, DataType.FLOAT, false, 6 * sizeof(float), 0);
105+
await context.EnableVertexAttribArrayAsync(0);
106+
await context.VertexAttribPointerAsync(1, 3, DataType.FLOAT, false, 6 * sizeof(float), 3 * sizeof(float));
107+
await context.EnableVertexAttribArrayAsync(1);
108+
await context.BindBufferAsync(BufferType.ELEMENT_ARRAY_BUFFER, elementBuffer);
109+
await context.BufferDataAsync(BufferType.ELEMENT_ARRAY_BUFFER, new byte[]
110+
{
111+
0, 1, 2
112+
}, BufferUsageHint.STATIC_DRAW);
113+
await context.BindBufferAsync(BufferType.ARRAY_BUFFER, instanceBuffer);
114+
await context.BufferDataAsync(BufferType.ARRAY_BUFFER, new[]
115+
{
116+
-0.5f, 0.0f, 0.0f, 0.5f, 0.0f, 0.0f
117+
}, BufferUsageHint.STATIC_DRAW);
118+
await context.VertexAttribPointerAsync(2, 3, DataType.FLOAT, false, 3 * sizeof(float), 0);
119+
await context.VertexAttribDivisorAsync(2, 1);
120+
await context.EnableVertexAttribArrayAsync(2);
121+
await context.BindVertexArrayAsync(default);
122+
}
123+
await context.ClearColorAsync(0, 0, 0, 1);
124+
await context.ClearAsync(BufferBits.COLOR_BUFFER_BIT);
125+
await context.UseProgramAsync(program);
126+
await context.BindVertexArrayAsync(vertexArray);
127+
await context.DrawElementsInstancedAsync(Primitive.TRIANGLES, 3, DataType.UNSIGNED_BYTE, 0, 2);
128+
await context.UseProgramAsync(default);
129+
await context.BindVertexArrayAsync(default);
130+
}
131+
}

0 commit comments

Comments
 (0)