Skip to content

Instantly share code, notes, and snippets.

View sketchpunk's full-sized avatar
🏠
Working from home

Pedro sketchpunk

🏠
Working from home
View GitHub Profile
@sketchpunk
sketchpunk / DynamicPoints.js
Last active January 25, 2019 15:37
Draw Dynamic Points in Three.js
/**
* Display Points on the scene, each with its own size and color.
* @example <caption>Simple How to use</caption>
* let dPnt = new DynamicPoints( 200 );
* Scene.add( dPnt.mesh );
*/
class DynamicPoints{
constructor( maxLen = 100 ){
/**
* How many vertices to draw.
@sketchpunk
sketchpunk / callback.js
Last active September 10, 2019 16:42
Event manager and Callback Manager
class Callbacks{
constructor(){
this.cb = {};
}
on( eName, func ){
if( !this.cb[ eName ] ) this.cb[ eName ] = new Array();
this.cb[ eName ].push( func );
return this;
}
@sketchpunk
sketchpunk / FlatVectorArray.js
Last active April 4, 2019 14:49
Flat Vector Array
class FlatVectorArray{
constructor( elmCnt=1, compLen=3, type=null ){
this.elmCnt = elmCnt;
this.compLen = compLen;
this.aryLen = elmCnt * compLen;
this.count = 0;
if( !type ){
this.aryType = Float32Array;
this.data = new Float32Array( this.aryLen );
@sketchpunk
sketchpunk / _CURVES_IN_JS.md
Last active November 25, 2024 08:48
Curves in Javascript

Curves in Javascript

Collection of algorithms related to using curves written in javascript.

Curve Types

  • Cubic Bezier Splines : inc. derivative, 2nd derivative, normals, easing
  • Catmull Rom
  • Kochanek Bartels ( TCB Splines ) : inc. derivative
  • Lemniscate of Bernoulli and Gerono : inc. derivative
  • Watt's Curve
  • Torus Knot : inc. derivative, 2nd derivative
@sketchpunk
sketchpunk / TimelineAnimation.js
Last active March 15, 2019 15:14
Timeline Animation - Track and Key Frames
/*
This is a basic framework for Timeline Animations. This example only animates a single float value, but you may build the frame
class to hold any value type, maybe have different Frame objects based on Type, for example quaternions would need an extra step
for interpolation by normalizing the final value where a vector would not need that.
If buidling this in rust or other languages, Maybe generics would be a good use for creating keyframes of different types.
Or you can build a general purpose keyframe that interpolates multiple values at once. Sky's the limit.
*/
// TRACK - COLLECTION OF ORDERED KEYFRAMES
@sketchpunk
sketchpunk / triangleWaveInt.js
Created March 20, 2019 13:41
triangleWaveInt
// Bounce back forth between 0 and Max
function triangleWaveInt( i, max ){
let max2 = max * 2;
i = i - Math.floor( i / max2 ) * max2; // sawtooth wave, but using a max value double
i = Math.max( 0, Math.min( max2, i )); // clamp
return max - Math.abs( i - max ); // triangle Wave
}
@sketchpunk
sketchpunk / circle_and_arcs.txt
Last active March 21, 2019 15:05
Programming Math Notes
Radius = ArcLength / Angle
ArcLength = Radius * Angle
Arc / Segment Height = Radius * ( 1 - cos(angle / 2) )
circleCenterPoint_fromCurve = Radius - Segment Height
chord length = 2 * Radius * sin( angle / 2 )
circumference = PI * diameter
circumference = PI * radius * 2;
let GamepadList = new Array();
class GamepadTracker{
constructor( profile ){
this.gpIndex = -1;
this.profile = profile;
this.timestamp = 0;
this.hasChanged = false;
this.onConnectBind = this.onConnect.bind( this );
@sketchpunk
sketchpunk / ObjectPool.js
Created March 29, 2019 19:20
ObjectPool - Very bare bones for maximum performance
/* NOTES
When i create 5 Float32Arrays at 10 million iterations, It takes about 3 seconds to complete.
But if create a Pool of 5 Float arrays and use Get/Rtn at the same interactions is only 0.17s.
So by using ObjectPool, using Float32Arrays for Vector3 data is just as fast as an Object Vector
but uses up less memory and affects garbage collection much less.
let pool = new ObjectPool( THREE.Vector3, 10, 5 );
let a = pool.get(),
b = pool.get();
pool.rtn( a ).rtn( b );
@sketchpunk
sketchpunk / limb_ik.js
Created March 31, 2019 15:34
Limb Inverse Kinematic Solver
// https://www.mathsisfun.com/algebra/trig-solving-sss-triangles.html
static lawcos_sss( aLen, bLen, cLen ){
// Law of Cosines - SSS : cos(C) = (a^2 + b^2 - c^2) / 2ab
// The Angle between A and B with C being the opposite length of the angle.
return Math.acos( (aLen*aLen + bLen*bLen - cLen*cLen) / (2 * aLen * bLen) );
}
function limb( chain, target, pose, wt ){
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Using law of cos SSS, so need the length of all sides of the triangle