Skip to content

Instantly share code, notes, and snippets.

/* springFactory
*
* Generate a physically realistic easing curve for a damped mass-spring system.
*
* Required:
* damping (zeta): [0, 1)
* halfcycles: 0...inf
*
* Optional:
* initial_position: -1..1, default 1
@AlainBarrios
AlainBarrios / viewport-size.js
Created June 20, 2020 10:25
Get the viewport size to give the plane full size
const getViewSize = () => {
const fov = THREE.Math.degToRad(camera.fov);
const dist = camera.position.z - plane.position.z;
const height = 2 * Math.tan(fov / 2) * dist;
const width = height * camera.aspect;
return [width, height];
};
/* this.bufferGeometry.setAttribute(
"uv",
new THREE.BufferAttribute(uvArray, 2)
);
*/
createUV(mesh) {
const box = new THREE.Box3().setFromObject(mesh);
const size = new THREE.Vector3();
box.getSize(size);
@AlainBarrios
AlainBarrios / onBeforeCompile.js
Last active April 13, 2020 22:10
Modify the shaders before compiling
this.material.onBeforeCompile = (shader) => {
shader.uniforms.time = { value: 0 };
shader.uniforms.texture = { value: texture };
/******************** Vertex Shader ********************/
shader.vertexShader = `
#define TAU 6.28318530718
uniform float time;
@AlainBarrios
AlainBarrios / grid.js
Last active April 3, 2020 20:28
Distribute the points in the form of a grid like this https://codepen.io/AlainBarrios/pen/mQwMPd
for (let i = 0; i < gridLength; i++) {
for (let j = 0; j < gridLength; j++) {
const x = c.width / (gridLength + 1) * (j + 1);
const y = c.height / (gridLength + 1) * (i + 1);
}
}
@AlainBarrios
AlainBarrios / isEmpty.js
Created March 27, 2020 11:53
Know if the alpha is transparent
const isEmpty = (texture) => {
const canvas = document.createElement("canvas");
const ctx = canvas.getContext("2d");
ctx.drawImage(texture.image, 0, 0);
const data = ctx.getImageData(0, 0, texture.image.width, texture.image.height);
const lengthData = data.data.length
let numVisible = 0
float fog(vec2 coord, float multiplier){
vec2 newCoord = coord * multiplier;
float motion = fbm(newCoord + vec2(uTime * -.5, uTime * -.5) );
return fbm(newCoord + motion);
}
@AlainBarrios
AlainBarrios / fbm.frag
Created September 22, 2019 16:54
Fractal Brownian Motion
float rand (vec2 st) {
return fract(sin(dot(st.xy,
vec2(12.9898,78.233)))*
43758.5453123);
}
float noise(vec2 p){
vec2 ip = floor(p);
vec2 u = fract(p);
u = u*u*(3.0-2.0*u);
@AlainBarrios
AlainBarrios / map.js
Last active September 24, 2019 10:55
const map = (value, inMin, inMax, outMin, outMax) => {
return ( (value - inMin) / ( inMax - inMin ) * ( outMax - outMin ) ) + outMin;
}
vec2 uv = vTextureCoord;
vec2 m = uMouse / uReso;
vec3 lightAmbient = vec3(.2, .2, .2 );
vec4 lightColor = vec4(1., 1., 1., 1.3);
vec3 lightPos = vec3(uv - m, .1);
vec3 color = texture2D(texture0, uv).xyz;
vec3 normalMap = texture2D(map, uv).xyz - .5;