shaku
Version:
A simple and effective JavaScript game development framework that knows its place!
94 lines (80 loc) • 2.57 kB
JavaScript
/**
* Implement a basic effect to draw sprites.
*
* |-- copyright and license --|
* @module Shaku
* @file shaku\src\gfx\effects\sprites.js
* @author Ronen Ness (ronenness@gmail.com | http://ronenness.com)
* @copyright (c) 2021 Ronen Ness
* @license MIT
* |-- end copyright and license --|
*
*/
'use strict';
const Effect = require("./effect");
// vertex shader code
const vertexShader = `
attribute vec3 position;
attribute vec2 uv;
attribute vec4 color;
uniform mat4 projection;
uniform mat4 world;
varying vec2 v_texCoord;
varying vec4 v_color;
void main(void) {
gl_Position = projection * world * vec4(position, 1.0);
gl_PointSize = 1.0;
v_texCoord = uv;
v_color = color;
}
`;
// fragment shader code
const fragmentShader = `
precision highp float;
uniform sampler2D mainTexture;
varying vec2 v_texCoord;
varying vec4 v_color;
void main(void) {
gl_FragColor = texture2D(mainTexture, v_texCoord) * v_color;
gl_FragColor.rgb *= gl_FragColor.a;
}
`;
/**
* Default basic effect to draw 2d sprites.
*/
class SpritesEffect extends Effect
{
/** @inheritdoc */
get vertexCode()
{
return vertexShader;
}
/** @inheritdoc */
get fragmentCode()
{
return fragmentShader;
}
/** @inheritdoc */
get uniformTypes()
{
return {
[]: { type: Effect.UniformTypes.Texture, bind: Effect.UniformBinds.MainTexture },
[]: { type: Effect.UniformTypes.Matrix, bind: Effect.UniformBinds.Projection },
[]: { type: Effect.UniformTypes.Matrix, bind: Effect.UniformBinds.World },
[]: { type: Effect.UniformTypes.Matrix, bind: Effect.UniformBinds.View }
};
}
/** @inheritdoc */
get attributeTypes()
{
return {
[]: { size: 3, type: Effect.AttributeTypes.Float, normalize: false, bind: Effect.AttributeBinds.Position },
[]: { size: 2, type: Effect.AttributeTypes.Float, normalize: false, bind: Effect.AttributeBinds.TextureCoords },
[]: { size: 4, type: Effect.AttributeTypes.Float, normalize: false, bind: Effect.AttributeBinds.Colors },
};
}
}
// export the basic shader
module.exports = SpritesEffect;