Skip to content

Instantly share code, notes, and snippets.

View patrickheng's full-sized avatar

Patrick HENG patrickheng

View GitHub Profile
@yiwenl
yiwenl / bezier.glsl
Last active May 31, 2023 18:47
Bezier curve in GLSL
// bezier curve with 2 control points
// A is the starting point, B, C are the control points, D is the destination
// t from 0 ~ 1
vec3 bezier(vec3 A, vec3 B, vec3 C, vec3 D, float t) {
vec3 E = mix(A, B, t);
vec3 F = mix(B, C, t);
vec3 G = mix(C, D, t);
vec3 H = mix(E, F, t);
vec3 I = mix(F, G, t);
@souporserious
souporserious / getJSONFromFigmaFile.js
Created July 13, 2018 21:52
Generates JSON from Figma file
import request from 'request'
const api_endpoint = 'https://api.figma.com/v1'
const personal_access_token = 'FIGMA_ACCESS_TOKEN_HERE' // https://www.figma.com/developers/docs#auth-dev-token
function downloadSvgFromAWS(url) {
return new Promise((resolve, reject) => {
request.get(
url,
{
@ayamflow
ayamflow / gist:a99fd49b773a53bc757df41f77fb369c
Created April 6, 2018 16:54
Visible width/height with threejs perspective camera
// https://discourse.threejs.org/t/functions-to-calculate-the-visible-width-height-at-a-given-z-depth-from-a-perspective-camera/269
function visibleHeightAtDepth(depth, camera) {
// compensate for cameras not positioned at z=0
const cameraOffset = camera.position.z;
if ( depth < cameraOffset ) depth -= cameraOffset;
else depth += cameraOffset;
// vertical fov in radians
const vFOV = camera.fov * Math.PI / 180;
// Math.abs to ensure the result is always positive
@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)
@mattdesl
mattdesl / about.md
Last active December 24, 2017 14:47

shader-reload with budo

Say you want to use shader-reload on your current budo/ThreeJS application, but you don't want to use the glsl-server since you have some specific transforms or options you need.

You can write a new dev.js script (see below) and include any additional transforms you may want (babel, brfs, etc).

Now you can replace the budo command with node dev.js, and pass all other flags as usual.

Note: The utility handles live reloading for you (JS, HTML, CSS, GLSL), so you don't need to pass the --live or -l flags.

@cdata
cdata / three-clone-gltf.js
Created November 8, 2017 23:26
A quick hack to clone a Three.js GLTF scene without re-loading or re-parsing the source.
const cloneGltf = (gltf) => {
const clone = {
animations: gltf.animations,
scene: gltf.scene.clone(true)
};
const skinnedMeshes = {};
gltf.scene.traverse(node => {
if (node.isSkinnedMesh) {
@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;
}
@kchapelier
kchapelier / merge-three-buffer-geometries.js
Last active September 11, 2018 22:04
Merge indexed and non-indexed THREE.BufferGeometry into a new indexed THREE.BufferGeometry. All missing uniforms are set to 0.
/**
* Combine indexed and non-indexed BufferGeometry into a new indexed BufferGeometry. All missing uniforms are set to 0.
* @param {array} geometries Array of THREE.BufferGeometry instances
* @returns {BufferGeometry}
*/
function mergeBufferGeometries (geometries) {
var indexLength = 0,
verticesLength = 0,
attributesInfos = {},
geometriesInfos = [],
@ayamflow
ayamflow / gist:96a1f554c3f88eef2f9d0024fc42940f
Last active March 20, 2024 02:48
Threejs Fit plane to screen
var cameraZ = camera.position.z;
var planeZ = 5;
var distance = cameraZ - planeZ;
var aspect = viewWidth / viewHeight;
var vFov = camera.fov * Math.PI / 180;
var planeHeightAtDistance = 2 * Math.tan(vFov / 2) * distance;
var planeWidthAtDistance = planeHeightAtDistance * aspect;
// or

Common Blender shortcuts

Camera

  • Shift + F - FPS mode

Outliner

  • Ctrl + Left Click - Select parent and its children
  • Shift + Left Click - Hide parent and its children