|
| 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