Skip to content

Instantly share code, notes, and snippets.

View patrickheng's full-sized avatar

Patrick HENG patrickheng

View GitHub Profile
@aslakhellesoy
aslakhellesoy / rounding.java
Created August 9, 2011 16:18
Rounding up and down to nearest multiple
/** round n down to nearest multiple of m */
long roundDown(long n, long m) {
return n >= 0 ? (n / m) * m : ((n - m + 1) / m) * m;
}
/** round n up to nearest multiple of m */
long roundUp(long n, long m) {
return n >= 0 ? ((n + m - 1) / m) * m : (n / m) * m;
}
@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]));
@patriciogonzalezvivo
patriciogonzalezvivo / GLSL-Lights.md
Last active November 16, 2022 01:46
GLSL Shaders Lights Funtions
@mattdesl
mattdesl / upload-texture.js
Created November 6, 2014 16:49
immediate texture upload for threejs
/*
Uploads to GPU immediately when the image is ready, then fires callback.
//takes a string path or image/canvas/video/ImageData
uploadTexture(renderer, pathOrImage, function(err, texture) {
if (err) console.error(err)
//do something with the ThreeJS 'texture' result
})
*/
var THREE = require('three');
@jonathantneal
jonathantneal / README.md
Last active July 24, 2023 08:35
Fluid Aspect: A Sass mixin for creating intrinsic ratios

Fluid Aspect

fluid-aspect is a Sass mixin for creating intrinsic ratios in CSS. Intrinsic ratios allow elements to fill the width of their containing block and resize on the fly while maintaining their aspect ratio.

@include fluid-aspect($ratio, [$target]);
  • $ratio: An aspect ratio represented as two numbers separated by a space. Defaults to 1:1
  • $target: A selector targeting the element to be made fluid. Defaults to "> :first-child"
@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);
@yomotsu
yomotsu / gist:d845f21e2e1eb49f647f
Last active September 29, 2023 02:13
Intersection AABB - Plane ,Triangle - AABB, sphere - AABB, sphere - sphere, sphere - triangle, in three.js
var collision = {};
// aabb: <THREE.Box3>
// Plane: <THREE.Plane>
collision.isIntersectionAABBPlane = function ( aabb, Plane ) {
var center = new THREE.Vector3().addVectors( aabb.max, aabb.min ).multiplyScalar( 0.5 ),
extents = new THREE.Vector3().subVectors( aabb.max, center );
@finlaybob
finlaybob / normalmap.frag
Created March 10, 2013 00:37
Normal Map Fragment Shader
#version 400
layout( location = 0 ) out vec4 frag_main;
struct PointLight{
vec3 position;
vec3 ambient;
vec3 diffuse;
vec3 specular;
float shininess;
float intensity;
};
@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