Skip to content

Instantly share code, notes, and snippets.

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

Gianmarco gsimone

🏠
Working from home
View GitHub Profile
@gsimone
gsimone / NewFogClass.js
Last active June 6, 2022 09:19
Three fog rework
/**
* Abstract example, a real one will be better
**/
class MyFog extends Fog {
constructor( fogEnvMap ) {
super()
// initialize whatever you need
// keep some of the shaderChunk shaders and override others
@gsimone
gsimone / Clock.js
Created March 27, 2022 08:52
Time provider defined at construction
class Clock {
constructor( autoStart = true ) {
this.autoStart = autoStart;
this.startTime = 0;
this.oldTime = 0;
this.elapsedTime = 0;
const hash = require('object-hash')
const sum = require('hash-sum')
const a = hash({ color: "red" })
const b = hash({ color: "blue" })
console.assert(a !== b)
const c = hash({ color: "red", opacity: 1 }, { unorderedObjects: true })
const d = hash({ opacity: 1, color: "red" }, { unorderedObjects: true })
@gsimone
gsimone / gist:023133e31da92c02ba0f2445437dc727
Created February 2, 2022 12:41
Spheriphy box three.js
function createGeometry() {
const r = 2
const geometry = new THREE.BoxGeometry(2, 2, 2, 32, 32, 32)
const positionAttribute = geometry.attributes.position
for (let i = 0; i < positionAttribute.count; i++) {
const x = positionAttribute.getX(i)
const y = positionAttribute.getY(i)
const z = positionAttribute.getZ(i)
// world position
vPositionW = vec3( vec4( position, 1.0 ) * modelMatrix);
// world normal
vNormalW = normalize( vec3( vec4( normal, 0.0 ) * modelMatrix ) );
// world camera direction
vec3 viewDirectionW = normalize(cameraPosition - vPositionW);
// fresnel, calc with dot mult between view and normal
// https://en.m.wikipedia.org/wiki/Rodrigues%27_rotation_formula
vec3 erot(vec3 p, vec3 ax, float ro) {
return mix(dot(p,ax)*ax,p,cos(ro))+sin(ro)*cross(ax,p);
}
@gsimone
gsimone / fbm.glsl
Last active August 18, 2020 08:46
Shader Stuff
float mod289(float x){return x - floor(x * (1.0 / 289.0)) * 289.0;}
vec4 mod289(vec4 x){return x - floor(x * (1.0 / 289.0)) * 289.0;}
vec4 perm(vec4 x){return mod289(((x * 34.0) + 1.0) * x);}
float noise(vec3 p){
vec3 a = floor(p);
vec3 d = p - a;
d = d * d * (3.0 - 2.0 * d);
vec4 b = a.xxyy + vec4(0.0, 1.0, 0.0, 1.0);
@gsimone
gsimone / absolute-imports-in-create-react-app.md
Last active December 31, 2018 11:10
Easily add absolute imports in create react app

Easy absolute imports in VSC + Create React Apps

create-react-apps supports absolute imports via .env

.env

    NODE_PATH=src

this will make webpack look for absolute imports (imports without .. or /) in the src directory of our project, meaning we can do something like this

@gsimone
gsimone / validate.js
Created May 26, 2018 10:40
Better Redux Form Error handling with Yup
import yup from 'yup'
import yupErroToReduxFormError from './yupErroToReduxFormError'
// yup schema
const schema = yup.object().shape(...)
// this would be our sync Redux Form validation function
// yup can also validate asynchronously with the yup.validate method
export default (values) => {
@gsimone
gsimone / index.js
Created May 4, 2018 11:56
Wait for all images
const images = ['http://picsum.photos/300/300?r&a=1', 'http://picsum.photos/300/300?r&a=2', 'http://picsum.photos/300/300?r&a=3']
const imageLoadPromise = (src) => new Promise((resolve, reject) => {
const img = new Image()
img.onload = resolve
img.src = src
})
Promise.all(images.map(imageLoadPromise))