Skip to content

Instantly share code, notes, and snippets.

vec2 rotate(vec2 v, float a) {
float s = sin(a);
float c = cos(a);
mat2 m = mat2(c, -s, s, c);
return m * v;
}
@ayamflow
ayamflow / texture-atlas.js
Last active November 21, 2023 01:22
threejs Texture Atlas (TexturePacker spritesheet)
import _ from 'underscore';
export default class TextureAtlas {
constructor(json, image) {
this._textures = {};
let texture = new THREE.Texture(image);
texture.needsUpdate = true;
let frames = json.frames;
_.keys(frames).forEach(function(key, i) {
@ayamflow
ayamflow / shader.vert
Created September 7, 2017 00:55
GLSL World space
vec3 vWorldPosition = (modelViewMatrix * position).xyz;'
@ayamflow
ayamflow / num-loop.js
Created September 11, 2017 18:04
Loop numbers between min/max using the modulo operator
function loop(val, min, max) {
return ((val - min) % (max - min + 1) + (max - min + 1)) % (max - min + 1) + min;
}
@ayamflow
ayamflow / multiply.js
Last active September 20, 2017 18:12
Canvas multiply (IE)
// Also see https://github.com/Phrogz/context-blender
function multiply(R, G, B) {
var imgData = ctx.getImageData(0, 0, canvas.width, canvas.height);
var data = imgData.data;
for (var i = 0; i < data.length; i += 4) {
data[i ] = R * data[i ] / 255;
data[i + 1] = G * data[i + 1] / 255;
data[i + 2] = B * data[i + 2] / 255;
@ayamflow
ayamflow / performance-monitor.js
Created December 8, 2017 00:31
Performance monitor
// Check FPS estimate over an interval of time and trigger callback if it becomes too slow.
// Ideally callback turn quality settings down (canvas dpr, post process, number of particles, ...)
// See also
// https://mrnmnm.com/
// http://findinglove.activetheory.net
const PAST_FRAMES = 60; // Memorize 1s of render time
const BAD_FRAMES = 30; // Max number of slow frame before downgrade
export default class PerformanceMonitor {
@ayamflow
ayamflow / rotate-uv.glsl
Created January 16, 2018 23:24
Rotate UV in GLSL
vec2 rotateUV(vec2 uv, float rotation)
{
float mid = 0.5;
return vec2(
cos(rotation) * (uv.x - mid) + sin(rotation) * (uv.y - mid) + mid,
cos(rotation) * (uv.y - mid) - sin(rotation) * (uv.x - mid) + mid
);
}
vec2 rotateUV(vec2 uv, float rotation, vec2 mid)
@ayamflow
ayamflow / trim-decimals.js
Created January 29, 2018 20:21
Trim decimals for 3D JSON models
var fs = require('fs');
var argv = require('minimist')(process.argv.slice(2));
let data = JSON.parse(fs.readFileSync('./' + argv.input, 'utf8'));
Object.keys(data).forEach(function(key) {
if (data[key] instanceof Array) {
data[key].forEach(function(value, i) {
data[key][i] = parseFloat(parseFloat(value).toFixed(3 || argv.cs));
});
}
@ayamflow
ayamflow / layer.glsl
Created February 8, 2018 00:24
Layer 2 textures on top of each other in GLSL
// https://stackoverflow.com/questions/24480901/libgdx-overlay-texture-above-another-texture-using-shader
vec4 layer(vec4 foreground, vec4 background) {
return foreground * foreground.a + background * (1.0 - foreground.a);
}
#pragma glslify: export(layer);
@ayamflow
ayamflow / numbers.css
Created February 15, 2018 18:06
Prevent animated counters numbers to shift
font-feature-settings: "tnum";
font-variant-numeric: tabular-nums;