Skip to content

Instantly share code, notes, and snippets.

@ndugger
Last active July 6, 2019 21:15
Show Gist options
  • Save ndugger/1528851c33228ca8fa3ab0599c3a4d44 to your computer and use it in GitHub Desktop.
Save ndugger/1528851c33228ca8fa3ab0599c3a4d44 to your computer and use it in GitHub Desktop.
interface StandardAttributes {
texture: ImageData;
}
interface StandardInputs {
i_Points: Float32Array;
i_Sample: Float32Array;
}
interface StandardUniforms {
u_Texture: ImageBitmapSource;
}
class StandardMaterial extends Bifrost.GFX.Material<StandardAttributes, StandardInputs, StandardUniforms> {
public shaders(): Bifrost.GFX.Material.Shaders {
return {
vertex: `
in vec2 i_Points;
in vec2 i_Sample;
uniform vec2 engine_Resolution;
out vec2 o_Sample;
void main () {
vec2 one = i_Points / engine_Resolution;
vec2 two = one * 2.0;
vec2 clip = two - 1.0;
vec2 flip = vec2(1.0, -1.0);
gl_Position = vec4(clip * flip, 0.0, 1.0);
o_Sample = i_Sample;
}
`,
fragment: `
precision mediump float;
in vec2 o_Sample;
uniform sampler2D u_Texture;
out vec4 o_Color;
void main () {
o_Color = texture(u_Texture, o_Sample);
}
`
};
}
public paint(): Bifrost.GFX.Material.Outputs<Inputs, Uniforms> {
return {
inputs: {
i_Points: new Float32Array([
0, 0,
this.attributes.texture.width, 0,
0, this.attributes.texture.height,
0, this.attributes.texture.height,
this.attributes.texture.width, 0,
this.attributes.texture.width, this.attributes.texture.height
]),
i_Sample: new Float32Array([
0, 0,
1, 0,
0, 1,
0, 1,
1, 0,
1, 1
])
},
uniforms: {
u_Texture: this.attributes.texture
}
};
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment