Skip to content

Instantly share code, notes, and snippets.

View kishalmi's full-sized avatar
🤘
ready to rock

LMG kishalmi

🤘
ready to rock
View GitHub Profile
@kishalmi
kishalmi / gist:5480983
Created April 29, 2013 11:08
support for shadowMapCullFace 'None' and 'Both'
--- build/three.js 2013-04-29 12:12:34.237944000 +0200
+++ /home/lmg/js/three.js 2013-04-29 13:00:24.276532000 +0200
@@ -35281,18 +35281,14 @@
_gl.clearColor( 1, 1, 1, 1 );
_gl.disable( _gl.BLEND );
- _gl.enable( _gl.CULL_FACE );
- _gl.frontFace( _gl.CCW );
-
- if ( _renderer.shadowMapCullFace === THREE.CullFaceFront ) {
@kishalmi
kishalmi / gist:5608286
Created May 19, 2013 17:08
invert UV.x of three.js geometry (if for example watching a textured sphere from the inside)
// invert U coordinate
for( var li in geometry.faceVertexUvs )
for( var fi in geometry.faceVertexUvs[li] )
for( var vi in geometry.faceVertexUvs[li][fi] )
geometry.faceVertexUvs[li][fi][vi].x = 1.0 - geometry.faceVertexUvs[li][fi][vi].x;
@kishalmi
kishalmi / gist:5638574
Last active December 17, 2015 16:28
three.js util - load shaders from files requires jquery for $.ajax
// object with shader paths (optinally nested, for better structure)
// the paths get replaced with the actual file content.
var shaders = {
atmoVert: 'glsl/atmo.vert.c',
car: {
vert: 'glsl/car.vert.c',
frag: 'glsl/car.frag.c'
}
};
@kishalmi
kishalmi / kishalmi.js
Last active December 23, 2015 15:59
simple require using jquery
/**
* cheap require (using jquery)
* loads a script (or scripts, if given an array) and executes it
* callbacks for progress and success
* won't load a script twice if already loaded (or failed)
* @param files
* @param callbackProgress
* @param callbackSuccess
*/
function require(files, callbackProgress, callbackSuccess) {
@kishalmi
kishalmi / main_rods.js
Created October 4, 2013 20:30
three.js traverse scenegraph and trigger event from all objects found
function render() {
var t = new Date().getTime() / 1000; // unixtime
var dt = clock.getDelta();
dispatchSceneGraphEvent(scene, {type: 'preRender', t: t, dt: dt});
renderer.clear(true, true, true);
renderer.render(scene, camera);
}
@kishalmi
kishalmi / gist:6951786
Created October 12, 2013 16:14
get ImageData from a WebGL renderer
var img = document.createElement('img');
img.src = renderer.domElement.toDataURL('image/png');
img.onload = function() {
var canvas = document.createElement('canvas');
canvas.width = renderer.domElement.width;
canvas.height = renderer.domElement.height;
var ctx = canvas.getContext('2d');
ctx.drawImage(img, 0, 0, img.naturalWidth, img.naturalHeight, 0, 0, canvas.width, canvas.height);
var imageData = ctx.getImageData(0, 0, canvas.width, canvas.height);
}
@kishalmi
kishalmi / main.js
Created October 23, 2013 07:53
create a geometry suitable for a THREE.Line of type THREE.LineStrip from a sphere (not displaying triangle-edges of a sphere wireframe)
/**
* create a geometry suitable for a THREE.Line of type THREE.LineStrip from a sphere
* thereby not displaying triangle-edges of a sphere wireframe.
* @param {THREE.SphereGeometry} geoSphere
* @returns {THREE.Geometry}
*/
function lineStripGeometryFromSphereGeometry(geoSphere) {
var geoLine = new THREE.Geometry(),
w = geoSphere.widthSegments, h = geoSphere.heightSegments,
x, y;
@kishalmi
kishalmi / main.js
Last active December 26, 2015 21:58
auto updating uniform float time
uniforms.time = { type: 'f', get value() { return performance.now()/1000 } };
@kishalmi
kishalmi / HexGeometry.js
Created December 9, 2013 12:22
THREE hexagon geometry - evenly subdivided
/**
* hexagon geometry - evenly subdivided
* @param radius
* @param {int=} segments - subdivisions
* @param {Number=} theta - default "pointy top" (Math.PI/2 for "flat top")
* @constructor
* @author lmg / https://github.com/kishalmi
*/
THREE.HexGeometry = function (radius, segments, theta) {
@kishalmi
kishalmi / gist:8452247
Created January 16, 2014 09:42
three.js - split a (loaded) model into mesh parts by material index
/**
* split a (loaded) model into mesh parts by material index
* @param {THREE.Geometry} geometry
* @param {Array<THREE.Material>} materials
* @returns {Array<THREE.Mesh>}
*/
var splitByMaterial = function(geometry,materials) {
var parts = [],
geo, vMap, iMat,
addPart = function() {