Skip to content

Instantly share code, notes, and snippets.

View patrickheng's full-sized avatar

Patrick HENG patrickheng

View GitHub Profile
@statico
statico / temp.glsl
Created November 27, 2016 18:14
CSS-style `background-size: cover` for an image texture in a GLSL shader
// An implementation of CSS `background-size: cover`
// using http://stackoverflow.com/a/6565988 and my own crappy math
vec2 s = uScreenSize; // Screen
vec2 i = uBGSize; // Image
float rs = s.x / s.y;
float ri = i.x / i.y;
vec2 new = rs < ri ? vec2(i.x * s.y / i.y, s.y) : vec2(s.x, i.y * s.x / i.x);
vec2 offset = (rs < ri ? vec2((new.x - s.x) / 2.0, 0.0) : vec2(0.0, (new.y - s.y) / 2.0)) / new;
vec2 uv = vTexCoord * s / new + offset;
gl_FragColor = texture2D(uBGTex, uv);
@Anthelmed
Anthelmed / Custom cannon js body
Last active November 9, 2022 00:05
Using v-hacd https://github.com/kmammou/v-hacd to create custom collider for cannon js
import THREE from 'three';
import CANNON from 'cannon';
export const generateThreeVertices = (rawVerts) => {
let verts = [];
for(let v = 0; v < rawVerts.length; v+=3){
verts.push(new THREE.Vector3(rawVerts[v],
rawVerts[v+1],
rawVerts[v+2]));
@patrickheng
patrickheng / Damping
Last active November 19, 2019 11:27
Formula
current -= ( target + current ) * friction
current += ( target - current ) * friction
vec2 rotate(vec2 v, float a) {
float s = sin(a);
float c = cos(a);
mat2 m = mat2(c, s, -s, c);
return m * v;
}
@aaronjanse
aaronjanse / anglediff.js
Created June 20, 2016 21:42
find the smallest distance between two angles
// from http://stackoverflow.com/a/7869457
// converted to js code
// x and y are the angles in radians
function getDifference(x, y) {
var a = x-y;
a = (function(i, j) {return i-Math.floor(i/j)*j})(a+Math.PI, Math.PI*2); // (a+180) % 360; this ensures the correct sign
a -= Math.PI;
return a;
}
@agconti
agconti / index.html
Created May 8, 2016 23:46
Fake Light
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Bonjourno World</title>
</head>
<body>
<script id="vertex-shader" type="text/vertex-shader">
/**
* Multiply each vertex by the model-view matrix
@asus4
asus4 / lut_maker.py
Created April 3, 2016 11:43
Make LUT Texture
#!/usr/bin/env python
# coding: UTF-8
import cv2
import numpy as np
def make_lut256x16(exportPath):
''' 256 x 16 LUT '''
colors = []
@nicoptere
nicoptere / convert.jsx
Last active December 19, 2023 13:39
converts after effects layers' keyframes to JSON and saves file on hard drive.
//JSON object
"object"!=typeof JSON&&(JSON={}),function(){"use strict";function f(t){return 10>t?"0"+t:t}function this_value(){return this.valueOf()}function quote(t){return rx_escapable.lastIndex=0,rx_escapable.test(t)?'"'+t.replace(rx_escapable,function(t){var e=meta[t];return"string"==typeof e?e:"\\u"+("0000"+t.charCodeAt(0).toString(16)).slice(-4)})+'"':'"'+t+'"'}function str(t,e){var r,n,o,u,f,a=gap,i=e[t];switch(i&&"object"==typeof i&&"function"==typeof i.toJSON&&(i=i.toJSON(t)),"function"==typeof rep&&(i=rep.call(e,t,i)),typeof i){case"string":return quote(i);case"number":return isFinite(i)?i+"":"null";case"boolean":case"null":return i+"";case"object":if(!i)return"null";if(gap+=indent,f=[],"[object Array]"===Object.prototype.toString.apply(i)){for(u=i.length,r=0;u>r;r+=1)f[r]=str(r,i)||"null";return o=0===f.length?"[]":gap?"[\n"+gap+f.join(",\n"+gap)+"\n"+a+"]":"["+f.join(",")+"]",gap=a,o}if(rep&&"object"==typeof rep)for(u=rep.length,r=0;u>r;r+=1)"string"==typeof rep[r]&&(n=rep[r],o=str(n,i),o&&f.push
@tabrindle
tabrindle / webp-convert-directory.sh
Last active April 30, 2024 14:03
Convert all files in directory to webp, with default params, or standard cwebp params passed from command
#!/bin/bash
PARAMS=('-m 6 -q 70 -mt -af -progress')
if [ $# -ne 0 ]; then
PARAMS=$@;
fi
cd $(pwd)
@remy
remy / _README.md
Last active January 12, 2024 11:57
requestAnimationFrame helper

raf.js

A simple script with a few niceties that allows for multiple requestAnimationFrame calls, and FPS pinning.

How it works

The script polyfills rAF if required, then overloads requestAnimationFrame and cancelAnimationFrame with a process that allows multiple frames to be queued up for rAF to run.

This is useful if there are multiple animations running on the page, you want all the callbacks to happen at once, and not on multiple rAF calls. This script is meant as a drop-in solution to that problem.