Skip to content

Instantly share code, notes, and snippets.

/*
** Copyright (c) 2012, Romain Dura [email protected]
**
** Permission to use, copy, modify, and/or distribute this software for any
** purpose with or without fee is hereby granted, provided that the above
** copyright notice and this permission notice appear in all copies.
**
** THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
** WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
** MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
@Samsy
Samsy / GLSL-Noise.md
Created July 6, 2018 10:33 — forked from patriciogonzalezvivo/GLSL-Noise.md
GLSL Noise Algorithms

Generic 1,2,3 Noise

float rand(float n){return fract(sin(n) * 43758.5453123);}

float noise(float p){
	float fl = floor(p);
  float fc = fract(p);
	return mix(rand(fl), rand(fl + 1.0), fc);
}
@Samsy
Samsy / gltf-anim.js
Created April 15, 2018 15:59 — forked from ayamflow/gltf-anim.js
threejs GLTF animation
// There's always some fiddling because the GLTF exports a whole scene
// with root bone and skinned mesh already appended to it.
let gltf = loadedGltf;
let skinned;
gltf.scene.traverse(function(object) {
if (object instanceof THREE.SkinnedMesh) {
skinned = object;
}
})
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
@Samsy
Samsy / gist:6408928a88eee2ff271d96e1a2be17b4
Created April 7, 2018 17:11 — forked from ayamflow/gist:a99fd49b773a53bc757df41f77fb369c
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
@Samsy
Samsy / three-clone-gltf.js
Created February 13, 2018 18:00 — forked from cdata/three-clone-gltf.js
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) {
function webgl_support() {
try{
var canvas = document.createElement( 'canvas' );
return !! window.WebGLRenderingContext && (
canvas.getContext( 'webgl' ) || canvas.getContext( 'experimental-webgl' ) );
}catch( e ) { return false; }
};
@Samsy
Samsy / billboard.glsl
Created January 19, 2017 10:53 — forked from mattdesl/billboard.glsl
billboarding in GLSL
attribute vec3 position;
uniform mat4 modelViewMatrix;
uniform mat4 modelMatrix;
uniform mat4 projectionMatrix;
uniform float scale;
uniform float size;
uniform float aspect;
varying vec2 vUv;
@Samsy
Samsy / gist:f8c2a62e41519a404ee8617f88234a7b
Last active April 20, 2021 19:48
Orbit control with smoother
import {
EventDispatcher,
Vector2,
Vector3,
MOUSE,
Quaternion,
Spherical,
PerspectiveCamera,
OrthographicCamera,
} from 'three';
var fs = require('fs');
var path = require('path');
var http = require('http');
var request = require('request');