mirror of
https://github.com/pissang/claygl.git
synced 2026-01-18 16:22:29 +00:00
78 lines
1.8 KiB
TypeScript
78 lines
1.8 KiB
TypeScript
import { GLRenderer, GeometryBase, Shader, glsl, setCanvasSize, Texture2DArray } from 'claygl';
|
|
|
|
const { uniform, attribute, varying } = Shader;
|
|
|
|
const canvas = document.getElementById('main') as HTMLCanvasElement;
|
|
setCanvasSize(canvas, 400, 400);
|
|
const gl = canvas.getContext('webgl2')!;
|
|
gl.viewport(0, 0, 400, 400);
|
|
const renderer = new GLRenderer(gl);
|
|
|
|
const geometry = new GeometryBase();
|
|
geometry.createAttribute('position', 'float', 3, 'POSITION');
|
|
geometry.createAttribute('textureIndex', 'int', 2);
|
|
geometry.attributes.position.fromArray([
|
|
[-0.5, -0.5, 0],
|
|
[0.5, -0.5, 0],
|
|
[0, 0.5, 0]
|
|
]);
|
|
// It's for testing ivec2
|
|
geometry.attributes.textureIndex.fromArray([
|
|
[0, 0],
|
|
[1, 0],
|
|
[0, 1]
|
|
]);
|
|
|
|
const vs = new Shader.Vertex({
|
|
attributes: {
|
|
position: attribute('vec3', 'POSITION'),
|
|
textureIndex: attribute('ivec2')
|
|
},
|
|
varyings: {
|
|
v_TextureIndex: varying('vec2')
|
|
},
|
|
main: glsl`
|
|
void main() {
|
|
gl_Position = vec4(position, 1.0);
|
|
v_TextureIndex = vec2(textureIndex);
|
|
}`
|
|
});
|
|
const fs = new Shader.Fragment({
|
|
uniforms: {
|
|
colorTex: uniform('sampler2DArray')
|
|
},
|
|
main: glsl`
|
|
void main() {
|
|
vec3 coord = vec3(0.0, 0.0, v_TextureIndex.x);
|
|
out_color = vec4(texture(colorTex, coord).rgb, 1.0);
|
|
}`
|
|
});
|
|
|
|
const shader = new Shader(vs, fs);
|
|
const uniforms = shader.createUniforms();
|
|
|
|
function createCanvas(color: string) {
|
|
const canvas = document.createElement('canvas');
|
|
canvas.width = 1;
|
|
canvas.height = 1;
|
|
const ctx = canvas.getContext('2d')!;
|
|
ctx.fillStyle = color;
|
|
ctx.fillRect(0, 0, 1, 1);
|
|
return canvas;
|
|
}
|
|
|
|
uniforms.colorTex.value = new Texture2DArray({
|
|
image: [createCanvas('red'), createCanvas('green')]
|
|
});
|
|
|
|
function render() {
|
|
renderer.render([
|
|
{
|
|
geometry,
|
|
material: { shader, uniforms }
|
|
}
|
|
]);
|
|
}
|
|
|
|
render();
|