made with requirebin
Last active
October 22, 2015 05:28
-
-
Save tmpvar/9dd793b61f6cdcb4cac1 to your computer and use it in GitHub Desktop.
requirebin sketch
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var fc = require('fc') | |
var center = require('ctx-translate-center'); | |
var circle = require('ctx-circle'); | |
var ndarray = require('ndarray') | |
var vec2 = require('gl-vec2'); | |
var createRay = require('ray-aabb'); | |
function sign(a) { | |
return typeof a === 'number' ? a ? a < 0 ? -1 : 1 : a === a ? 0 : 0 : 0 | |
} | |
var ro = [0, 0]; | |
var rd = [0, 0]; | |
var ray = createRay(ro, rd); | |
var modelWidth = 8; | |
var modelHalfWidth = modelWidth/2; | |
var blockSize = 30 | |
var model = ndarray( | |
new Uint8Array(modelWidth*modelWidth), | |
[modelWidth, modelWidth] | |
); | |
var normal = [0, 0]; | |
var isect = [0, 0]; | |
model.set(modelHalfWidth, modelHalfWidth, 255) | |
model.set(modelHalfWidth, modelHalfWidth-1, 255) | |
model.set(modelHalfWidth, modelHalfWidth-2, 255) | |
model.set(modelHalfWidth-1, modelHalfWidth, 255) | |
model.set(modelHalfWidth-1, modelHalfWidth+1, 255) | |
model.set(modelHalfWidth-2, modelHalfWidth-2, 255) | |
model.set(modelHalfWidth-2, modelHalfWidth-3, 255) | |
model.set(modelHalfWidth-3, modelHalfWidth-3, 255) | |
model.set(modelHalfWidth-3, modelHalfWidth-4, 255) | |
model.set(modelHalfWidth-3, modelHalfWidth-2, 255) | |
model.set(modelHalfWidth+3, modelHalfWidth+2, 255) | |
model.set(modelHalfWidth+2, modelHalfWidth+1, 255) | |
model.set(modelHalfWidth+2, modelHalfWidth, 255) | |
model.set(modelHalfWidth+2, modelHalfWidth-1, 255) | |
model.set(modelHalfWidth+2, modelHalfWidth-2, 255) | |
model.set(modelHalfWidth+3, modelHalfWidth-3, 255) | |
var modelBounds = [ | |
[-modelHalfWidth*blockSize, -modelHalfWidth*blockSize], | |
[modelHalfWidth*blockSize, modelHalfWidth*blockSize] | |
]; | |
console.log(modelBounds) | |
var TAU = Math.PI*2; | |
var pos = 53.14696599999997 | |
var ctx = fc(function render(dt) { | |
console.clear(); | |
// pos+=dt/5000; | |
console.log('POS', pos); | |
// pos = .2562352000000014; | |
// console.log(pos); | |
ctx.clear(); | |
center(ctx); | |
ctx.scale(1, -1); | |
ctx.stroke(); | |
ctx.strokeStyle = "rgba(40, 221, 0, .25)"; | |
ctx.fillStyle = "rgb(149, 153, 152)" | |
for (var mx=modelBounds[0][0]; mx<modelBounds[1][0]; mx+=blockSize) { | |
for (var my=modelBounds[0][1]; my<modelBounds[1][1]; my+=blockSize) { | |
var mmx = (mx-modelBounds[0][0])/blockSize; | |
var mmy = (my-modelBounds[0][1])/blockSize; | |
ctx.strokeRect(mx, my, blockSize, blockSize) | |
if (model.get(mmx, mmy)) { | |
ctx.fillRect(mx-1, my-1, blockSize+2, blockSize+2) | |
} | |
} | |
} | |
ctx.strokeStyle = 'rgb(65, 176, 221)'; | |
ctx.strokeRect( | |
modelBounds[0][0], | |
modelBounds[0][1], | |
modelWidth * blockSize, | |
modelWidth * blockSize | |
); | |
ctx.beginPath(); | |
ctx.strokeStyle = "rgba(221, 87, 103, .75)" | |
var cx = Math.sin(pos)*modelWidth*blockSize | |
var cy = Math.cos(pos)*modelWidth*blockSize | |
circle(ctx, cx, cy, 5); | |
ctx.moveTo(cx, cy); | |
ctx.lineTo(0, 0); | |
ctx.stroke(); | |
// cast that ray! | |
ro[0] = cx; | |
ro[1] = cy; | |
rd[0] = -cx; | |
rd[1] = -cy; | |
vec2.normalize(rd, rd); | |
ray.update(ro, rd); | |
var d = ray.intersects(modelBounds, normal) | |
if (d !== false) { | |
vec2.add(isect, ro, vec2.scale(isect, rd, d)) | |
ctx.fillStyle = "orange"; | |
ctx.beginPath() | |
circle(ctx, isect[0], isect[1], 3); | |
ctx.fill(); | |
// this is pivotal to ensure the test is done inside the block | |
isect[0] += rd[0]; | |
isect[1] += rd[1]; | |
var block = computeBlock(rd, isect, normal); | |
// ctx.fillStyle = "purple"; | |
// ctx.fillRect( | |
// Math.floor(block[0]/blockSize)*blockSize + 2, | |
// Math.floor(block[1]/blockSize)*blockSize + 2, | |
// blockSize - 4, | |
// blockSize - 4 | |
// ); | |
// convert back into the block world | |
block[0] = Math.floor((block[0] - modelBounds[0][0]) / blockSize); | |
block[1] = Math.floor((block[1] - modelBounds[0][1]) / blockSize); | |
// find the next occupied | |
ctx.strokeStyle = "yellow"; | |
console.log('rd', rd, ro) | |
var res = calcStraightLine2d( | |
[[0, 0], [modelWidth, modelWidth]], | |
[(isect[0] - modelBounds[0][0])/blockSize, (isect[1] - modelBounds[0][1])/blockSize], | |
block, | |
rd, | |
normal, | |
model, | |
function (x, y) { | |
console.log('visit', x, y) | |
ctx.strokeRect( | |
modelBounds[0][0] + x*blockSize + 4, | |
modelBounds[0][1] + y*blockSize + 4, | |
blockSize - 8, | |
blockSize - 8 | |
); | |
}); | |
if (res) { | |
console.log('RES') | |
ctx.fillStyle = "#f0f" | |
ctx.fillRect( | |
modelBounds[0][0] + res[0]*blockSize + 2, | |
modelBounds[0][1] + res[1]*blockSize + 2, | |
blockSize - 4, | |
blockSize - 4 | |
); | |
} | |
} | |
}, 1) | |
function computeBlock(rd, isect, normal, out) { | |
out = out || [0, 0]; | |
var l = rd.length; | |
for (var i=0; i<l; i++) { | |
out[i] = Math.floor(isect[i]); | |
} | |
return out; | |
} | |
var paused = false; | |
window.addEventListener('mousedown', function() { | |
paused = !paused; | |
if (paused) { | |
ctx.stop(); | |
} else { | |
ctx.start(); | |
} | |
}) | |
window.addEventListener('mousewheel', function(ev) { | |
pos += ev.wheelDelta / 500; | |
ev.preventDefault(); | |
}) | |
function frac(x) { | |
return x - Math.floor(x); | |
} | |
function calcStraightLine2d(aabb, isect, origin, direction, isectNormal, pixels, visit) { | |
var dx = direction[0] | |
var dy = direction[1] | |
var max, min; | |
if (Math.abs(dx) > Math.abs(dy)) { | |
max = dx; | |
min = dy; | |
} else { | |
max = dy; | |
min = dx | |
} | |
var step = Math.abs((1/max) * min); | |
var ix = isect[0] + direction[0] * -step; | |
var iy = isect[1] + direction[1] * -step; | |
var lx = ix|0; | |
var ly = iy|0; | |
var sentnel = 10000; | |
while (sentnel--) { | |
var rx = ix|0; | |
var ry = iy|0; | |
ix += dx*step; | |
iy += dy*step; | |
if (sign(rx - lx) || sign(ry - ly)) { | |
if (lx >= 0 && ly >= 0 && lx < aabb[1][0] && ly < aabb[1][1]) { | |
visit(lx, ly); | |
if (pixels.get(lx, ly)) { | |
console.log('holy shit', 10000-sentnel); | |
return [lx, ly]; | |
} | |
} | |
lx = rx; | |
ly = ry; | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require=function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s}({fc:[function(require,module,exports){(function(){var performance=window.performance||{};var performanceNow=performance.now||performance.now||performance.mozNow||performance.msNow||performance.oNow||performance.webkitNow||function(){return(new Date).getTime()};performanceNow=performanceNow.bind(performance);function fc(fn,autorun,dimensions){document.body.style.margin="0px";document.body.style.padding="0px";var canvas=document.createElement("canvas");document.body.appendChild(canvas);canvas.style.position="absolute";canvas.style.left="0px";canvas.style.top="0px";var ctx;dimensions=dimensions||2;if(dimensions===2){ctx=canvas.getContext("2d")}else if(dimensions===3){ctx=canvas.getContext("webgl")||canvas.getContext("experimental-webgl")}if(!ctx){return}var last=performanceNow(),request;function requestFrame(){if(request===null){request=requestAnimationFrame(tick)}}function tick(){request=null;var time=performanceNow();var delta=time-last;last=time;ctx.reset();dimensions===2&&ctx.save();fn&&fn.call(ctx,delta);dimensions===2&&ctx.restore();if(autorun){requestFrame()}}if(dimensions===2){ctx.reset=function fc_reset(){canvas.width=0;canvas.width=window.innerWidth;canvas.height=window.innerHeight};ctx.clear=function fc_clear(color){var orig=ctx.fillStyle;ctx.fillStyle=color||"#223";ctx.fillRect(0,0,canvas.width,canvas.height);ctx.fillStyle=orig}}else{ctx.reset=function fc_reset(){if(canvas.width!==window.innerWidth){canvas.width=window.innerWidth}if(canvas.height!==window.innerHeight){canvas.height=window.innerHeight}}}setTimeout(tick,0);ctx.dirty=function fc_dirty(){last=performanceNow();requestFrame()};ctx.stop=function fc_stop(){autorun=false;request&&cancelAnimationFrame(request);request=null};ctx.start=function fc_start(){autorun=true;requestFrame()};(window.attachEvent||window.addEventListener)("resize",ctx.dirty);ctx.reset();ctx.canvas=canvas;return ctx}if(typeof module!=="undefined"&&typeof module.exports!=="undefined"){module.exports=fc}if(typeof window!=="undefined"){window.fc=window.fc||fc}})()},{}]},{},[]);require=function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s}({"ctx-translate-center":[function(require,module,exports){module.exports=center;function center(ctx){var w=ctx.canvas.width/2|0;var h=ctx.canvas.height/2|0;ctx.translate(w,h)}},{}]},{},[]);require=function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s}({"ctx-circle":[function(require,module,exports){var TAU=Math.PI*2;module.exports=circle;function circle(ctx,x,y,r,reverse){ctx.moveTo(x+r,y);ctx.arc(x,y,r,0,TAU,!!reverse)}},{}]},{},[]);require=function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s}({1:[function(require,module,exports){"use strict";function iota(n){var result=new Array(n);for(var i=0;i<n;++i){result[i]=i}return result}module.exports=iota},{}],2:[function(require,module,exports){module.exports=function(obj){return!!(obj!=null&&(obj._isBuffer||obj.constructor&&typeof obj.constructor.isBuffer==="function"&&obj.constructor.isBuffer(obj)))}},{}],ndarray:[function(require,module,exports){var iota=require("iota-array");var isBuffer=require("is-buffer");var hasTypedArrays=typeof Float64Array!=="undefined";function compare1st(a,b){return a[0]-b[0]}function order(){var stride=this.stride;var terms=new Array(stride.length);var i;for(i=0;i<terms.length;++i){terms[i]=[Math.abs(stride[i]),i]}terms.sort(compare1st);var result=new Array(terms.length);for(i=0;i<result.length;++i){result[i]=terms[i][1]}return result}function compileConstructor(dtype,dimension){var className=["View",dimension,"d",dtype].join("");if(dimension<0){className="View_Nil"+dtype}var useGetters=dtype==="generic";if(dimension===-1){var code="function "+className+"(a){this.data=a;};var proto="+className+".prototype;proto.dtype='"+dtype+"';proto.index=function(){return -1};proto.size=0;proto.dimension=-1;proto.shape=proto.stride=proto.order=[];proto.lo=proto.hi=proto.transpose=proto.step=function(){return new "+className+"(this.data);};proto.get=proto.set=function(){};proto.pick=function(){return null};return function construct_"+className+"(a){return new "+className+"(a);}";var procedure=new Function(code);return procedure()}else if(dimension===0){var code="function "+className+"(a,d) {this.data = a;this.offset = d};var proto="+className+".prototype;proto.dtype='"+dtype+"';proto.index=function(){return this.offset};proto.dimension=0;proto.size=1;proto.shape=proto.stride=proto.order=[];proto.lo=proto.hi=proto.transpose=proto.step=function "+className+"_copy() {return new "+className+"(this.data,this.offset)};proto.pick=function "+className+"_pick(){return TrivialArray(this.data);};proto.valueOf=proto.get=function "+className+"_get(){return "+(useGetters?"this.data.get(this.offset)":"this.data[this.offset]")+"};proto.set=function "+className+"_set(v){return "+(useGetters?"this.data.set(this.offset,v)":"this.data[this.offset]=v")+"};return function construct_"+className+"(a,b,c,d){return new "+className+"(a,d)}";var procedure=new Function("TrivialArray",code);return procedure(CACHED_CONSTRUCTORS[dtype][0])}var code=["'use strict'"];var indices=iota(dimension);var args=indices.map(function(i){return"i"+i});var index_str="this.offset+"+indices.map(function(i){return"this.stride["+i+"]*i"+i}).join("+");var shapeArg=indices.map(function(i){return"b"+i}).join(",");var strideArg=indices.map(function(i){return"c"+i}).join(",");code.push("function "+className+"(a,"+shapeArg+","+strideArg+",d){this.data=a","this.shape=["+shapeArg+"]","this.stride=["+strideArg+"]","this.offset=d|0}","var proto="+className+".prototype","proto.dtype='"+dtype+"'","proto.dimension="+dimension);code.push("Object.defineProperty(proto,'size',{get:function "+className+"_size(){return "+indices.map(function(i){return"this.shape["+i+"]"}).join("*"),"}})");if(dimension===1){code.push("proto.order=[0]")}else{code.push("Object.defineProperty(proto,'order',{get:");if(dimension<4){code.push("function "+className+"_order(){");if(dimension===2){code.push("return (Math.abs(this.stride[0])>Math.abs(this.stride[1]))?[1,0]:[0,1]}})")}else if(dimension===3){code.push("var s0=Math.abs(this.stride[0]),s1=Math.abs(this.stride[1]),s2=Math.abs(this.stride[2]);if(s0>s1){if(s1>s2){return [2,1,0];}else if(s0>s2){return [1,2,0];}else{return [1,0,2];}}else if(s0>s2){return [2,0,1];}else if(s2>s1){return [0,1,2];}else{return [0,2,1];}}})")}}else{code.push("ORDER})")}}code.push("proto.set=function "+className+"_set("+args.join(",")+",v){");if(useGetters){code.push("return this.data.set("+index_str+",v)}")}else{code.push("return this.data["+index_str+"]=v}")}code.push("proto.get=function "+className+"_get("+args.join(",")+"){");if(useGetters){code.push("return this.data.get("+index_str+")}")}else{code.push("return this.data["+index_str+"]}")}code.push("proto.index=function "+className+"_index(",args.join(),"){return "+index_str+"}");code.push("proto.hi=function "+className+"_hi("+args.join(",")+"){return new "+className+"(this.data,"+indices.map(function(i){return["(typeof i",i,"!=='number'||i",i,"<0)?this.shape[",i,"]:i",i,"|0"].join("")}).join(",")+","+indices.map(function(i){return"this.stride["+i+"]"}).join(",")+",this.offset)}");var a_vars=indices.map(function(i){return"a"+i+"=this.shape["+i+"]"});var c_vars=indices.map(function(i){return"c"+i+"=this.stride["+i+"]"});code.push("proto.lo=function "+className+"_lo("+args.join(",")+"){var b=this.offset,d=0,"+a_vars.join(",")+","+c_vars.join(","));for(var i=0;i<dimension;++i){code.push("if(typeof i"+i+"==='number'&&i"+i+">=0){d=i"+i+"|0;b+=c"+i+"*d;a"+i+"-=d}")}code.push("return new "+className+"(this.data,"+indices.map(function(i){return"a"+i}).join(",")+","+indices.map(function(i){return"c"+i}).join(",")+",b)}");code.push("proto.step=function "+className+"_step("+args.join(",")+"){var "+indices.map(function(i){return"a"+i+"=this.shape["+i+"]"}).join(",")+","+indices.map(function(i){return"b"+i+"=this.stride["+i+"]"}).join(",")+",c=this.offset,d=0,ceil=Math.ceil");for(var i=0;i<dimension;++i){code.push("if(typeof i"+i+"==='number'){d=i"+i+"|0;if(d<0){c+=b"+i+"*(a"+i+"-1);a"+i+"=ceil(-a"+i+"/d)}else{a"+i+"=ceil(a"+i+"/d)}b"+i+"*=d}")}code.push("return new "+className+"(this.data,"+indices.map(function(i){return"a"+i}).join(",")+","+indices.map(function(i){return"b"+i}).join(",")+",c)}");var tShape=new Array(dimension);var tStride=new Array(dimension);for(var i=0;i<dimension;++i){tShape[i]="a[i"+i+"]";tStride[i]="b[i"+i+"]"}code.push("proto.transpose=function "+className+"_transpose("+args+"){"+args.map(function(n,idx){return n+"=("+n+"===undefined?"+idx+":"+n+"|0)"}).join(";"),"var a=this.shape,b=this.stride;return new "+className+"(this.data,"+tShape.join(",")+","+tStride.join(",")+",this.offset)}");code.push("proto.pick=function "+className+"_pick("+args+"){var a=[],b=[],c=this.offset");for(var i=0;i<dimension;++i){code.push("if(typeof i"+i+"==='number'&&i"+i+">=0){c=(c+this.stride["+i+"]*i"+i+")|0}else{a.push(this.shape["+i+"]);b.push(this.stride["+i+"])}")}code.push("var ctor=CTOR_LIST[a.length+1];return ctor(this.data,a,b,c)}");code.push("return function construct_"+className+"(data,shape,stride,offset){return new "+className+"(data,"+indices.map(function(i){return"shape["+i+"]"}).join(",")+","+indices.map(function(i){return"stride["+i+"]"}).join(",")+",offset)}");var procedure=new Function("CTOR_LIST","ORDER",code.join("\n"));return procedure(CACHED_CONSTRUCTORS[dtype],order)}function arrayDType(data){if(isBuffer(data)){return"buffer"}if(hasTypedArrays){switch(Object.prototype.toString.call(data)){case"[object Float64Array]":return"float64";case"[object Float32Array]":return"float32";case"[object Int8Array]":return"int8";case"[object Int16Array]":return"int16";case"[object Int32Array]":return"int32";case"[object Uint8Array]":return"uint8";case"[object Uint16Array]":return"uint16";case"[object Uint32Array]":return"uint32";case"[object Uint8ClampedArray]":return"uint8_clamped"}}if(Array.isArray(data)){return"array"}return"generic"}var CACHED_CONSTRUCTORS={float32:[],float64:[],int8:[],int16:[],int32:[],uint8:[],uint16:[],uint32:[],array:[],uint8_clamped:[],buffer:[],generic:[]};(function(){for(var id in CACHED_CONSTRUCTORS){CACHED_CONSTRUCTORS[id].push(compileConstructor(id,-1))}});function wrappedNDArrayCtor(data,shape,stride,offset){if(data===undefined){var ctor=CACHED_CONSTRUCTORS.array[0];return ctor([])}else if(typeof data==="number"){data=[data]}if(shape===undefined){shape=[data.length]}var d=shape.length;if(stride===undefined){stride=new Array(d);for(var i=d-1,sz=1;i>=0;--i){stride[i]=sz;sz*=shape[i]}}if(offset===undefined){offset=0;for(var i=0;i<d;++i){if(stride[i]<0){offset-=(shape[i]-1)*stride[i]}}}var dtype=arrayDType(data);var ctor_list=CACHED_CONSTRUCTORS[dtype];while(ctor_list.length<=d+1){ctor_list.push(compileConstructor(dtype,ctor_list.length-1))}var ctor=ctor_list[d+1];return ctor(data,shape,stride,offset)}module.exports=wrappedNDArrayCtor},{"iota-array":1,"is-buffer":2}]},{},[]);require=function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s}({1:[function(require,module,exports){module.exports=add;function add(out,a,b){out[0]=a[0]+b[0];out[1]=a[1]+b[1];return out}},{}],2:[function(require,module,exports){module.exports=clone;function clone(a){var out=new Float32Array(2);out[0]=a[0];out[1]=a[1];return out}},{}],3:[function(require,module,exports){module.exports=copy;function copy(out,a){out[0]=a[0];out[1]=a[1];return out}},{}],4:[function(require,module,exports){module.exports=create;function create(){var out=new Float32Array(2);out[0]=0;out[1]=0;return out}},{}],5:[function(require,module,exports){module.exports=cross;function cross(out,a,b){var z=a[0]*b[1]-a[1]*b[0];out[0]=out[1]=0;out[2]=z;return out}},{}],6:[function(require,module,exports){module.exports=distance;function distance(a,b){var x=b[0]-a[0],y=b[1]-a[1];return Math.sqrt(x*x+y*y)}},{}],7:[function(require,module,exports){module.exports=divide;function divide(out,a,b){out[0]=a[0]/b[0];out[1]=a[1]/b[1];return out}},{}],8:[function(require,module,exports){module.exports=dot;function dot(a,b){return a[0]*b[0]+a[1]*b[1]}},{}],9:[function(require,module,exports){module.exports=forEach;var vec=require("./create")();function forEach(a,stride,offset,count,fn,arg){var i,l;if(!stride){stride=2}if(!offset){offset=0}if(count){l=Math.min(count*stride+offset,a.length)}else{l=a.length}for(i=offset;i<l;i+=stride){vec[0]=a[i];vec[1]=a[i+1];fn(vec,vec,arg);a[i]=vec[0];a[i+1]=vec[1]}return a}},{"./create":4}],10:[function(require,module,exports){module.exports=fromValues;function fromValues(x,y){var out=new Float32Array(2);out[0]=x;out[1]=y;return out}},{}],11:[function(require,module,exports){module.exports=length;function length(a){var x=a[0],y=a[1];return Math.sqrt(x*x+y*y)}},{}],12:[function(require,module,exports){module.exports=lerp;function lerp(out,a,b,t){var ax=a[0],ay=a[1];out[0]=ax+t*(b[0]-ax);out[1]=ay+t*(b[1]-ay);return out}},{}],13:[function(require,module,exports){module.exports=max;function max(out,a,b){out[0]=Math.max(a[0],b[0]);out[1]=Math.max(a[1],b[1]);return out}},{}],14:[function(require,module,exports){module.exports=min;function min(out,a,b){out[0]=Math.min(a[0],b[0]);out[1]=Math.min(a[1],b[1]);return out}},{}],15:[function(require,module,exports){module.exports=multiply;function multiply(out,a,b){out[0]=a[0]*b[0];out[1]=a[1]*b[1];return out}},{}],16:[function(require,module,exports){module.exports=negate;function negate(out,a){out[0]=-a[0];out[1]=-a[1];return out}},{}],17:[function(require,module,exports){module.exports=normalize;function normalize(out,a){var x=a[0],y=a[1];var len=x*x+y*y;if(len>0){len=1/Math.sqrt(len);out[0]=a[0]*len;out[1]=a[1]*len}return out}},{}],18:[function(require,module,exports){module.exports=random;function random(out,scale){scale=scale||1;var r=Math.random()*2*Math.PI;out[0]=Math.cos(r)*scale;out[1]=Math.sin(r)*scale;return out}},{}],19:[function(require,module,exports){module.exports=scale;function scale(out,a,b){out[0]=a[0]*b;out[1]=a[1]*b;return out}},{}],20:[function(require,module,exports){module.exports=scaleAndAdd;function scaleAndAdd(out,a,b,scale){out[0]=a[0]+b[0]*scale;out[1]=a[1]+b[1]*scale;return out}},{}],21:[function(require,module,exports){module.exports=set;function set(out,x,y){out[0]=x;out[1]=y;return out}},{}],22:[function(require,module,exports){module.exports=squaredDistance;function squaredDistance(a,b){var x=b[0]-a[0],y=b[1]-a[1];return x*x+y*y}},{}],23:[function(require,module,exports){module.exports=squaredLength;function squaredLength(a){var x=a[0],y=a[1];return x*x+y*y}},{}],24:[function(require,module,exports){module.exports=subtract;function subtract(out,a,b){out[0]=a[0]-b[0];out[1]=a[1]-b[1];return out}},{}],25:[function(require,module,exports){module.exports=transformMat2;function transformMat2(out,a,m){var x=a[0],y=a[1];out[0]=m[0]*x+m[2]*y;out[1]=m[1]*x+m[3]*y;return out}},{}],26:[function(require,module,exports){module.exports=transformMat2d;function transformMat2d(out,a,m){var x=a[0],y=a[1];out[0]=m[0]*x+m[2]*y+m[4];out[1]=m[1]*x+m[3]*y+m[5];return out}},{}],27:[function(require,module,exports){module.exports=transformMat3;function transformMat3(out,a,m){var x=a[0],y=a[1];out[0]=m[0]*x+m[3]*y+m[6];out[1]=m[1]*x+m[4]*y+m[7];return out}},{}],28:[function(require,module,exports){module.exports=transformMat4;function transformMat4(out,a,m){var x=a[0],y=a[1];out[0]=m[0]*x+m[4]*y+m[12];out[1]=m[1]*x+m[5]*y+m[13];return out}},{}],"gl-vec2":[function(require,module,exports){module.exports={create:require("./create"),clone:require("./clone"),fromValues:require("./fromValues"),copy:require("./copy"),set:require("./set"),add:require("./add"),subtract:require("./subtract"),multiply:require("./multiply"),divide:require("./divide"),min:require("./min"),max:require("./max"),scale:require("./scale"),scaleAndAdd:require("./scaleAndAdd"),distance:require("./distance"),squaredDistance:require("./squaredDistance"),length:require("./length"),squaredLength:require("./squaredLength"),negate:require("./negate"),normalize:require("./normalize"),dot:require("./dot"),cross:require("./cross"),lerp:require("./lerp"),random:require("./random"),transformMat2:require("./transformMat2"),transformMat2d:require("./transformMat2d"),transformMat3:require("./transformMat3"),transformMat4:require("./transformMat4"),forEach:require("./forEach")}},{"./add":1,"./clone":2,"./copy":3,"./create":4,"./cross":5,"./distance":6,"./divide":7,"./dot":8,"./forEach":9,"./fromValues":10,"./length":11,"./lerp":12,"./max":13,"./min":14,"./multiply":15,"./negate":16,"./normalize":17,"./random":18,"./scale":19,"./scaleAndAdd":20,"./set":21,"./squaredDistance":22,"./squaredLength":23,"./subtract":24,"./transformMat2":25,"./transformMat2d":26,"./transformMat3":27,"./transformMat4":28}]},{},[]);require=function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s}({1:[function(require,module,exports){module.exports=classify;function sign(a){return typeof a==="number"?a?a<0?-1:1:a===a?0:0:0}function classify(i,j,k){i=sign(i);j=sign(j);k=sign(k);return i>>>-1<<5|(i&1)<<4|j>>>-1<<3|(j&1)<<2|k>>>-1<<1|k&1}classify.MMM=63;classify.MMP=61;classify.MPM=55;classify.MPP=53;classify.PMM=31;classify.PMP=29;classify.PPM=23;classify.PPP=21;classify.POO=16;classify.MOO=48;classify.OPO=4;classify.OMO=12;classify.OOP=1;classify.OOM=3;classify.OMM=15;classify.OMP=13;classify.OPM=7;classify.OPP=5;classify.MOM=51;classify.MOP=49;classify.POM=19;classify.POP=17;classify.MMO=60;classify.MPO=52;classify.PMO=28;classify.PPO=20},{}],"ray-aabb":[function(require,module,exports){var classify=require("ray-direction-classify");module.exports=createRay;var tests={};var max=Math.max;var abs=Math.abs;tests[classify.MMM]=function testMMM(ray,lb,ub){var ro=ray.ro;return!(ro[0]<lb[0]||ro[1]<lb[1]||ro[2]<lb[2]||ray.jbyi*lb[0]-ub[1]+ray.cxy>0||ray.ibyj*lb[1]-ub[0]+ray.cyx>0||ray.jbyk*lb[2]-ub[1]+ray.czy>0||ray.kbyj*lb[1]-ub[2]+ray.cyz>0||ray.kbyi*lb[0]-ub[2]+ray.cxz>0||ray.ibyk*lb[2]-ub[0]+ray.czx>0)};tests[classify.MMP]=function testMMP(ray,lb,ub){var ro=ray.ro;return!(ro[0]<lb[0]||ro[1]<lb[1]||ro[2]>ub[2]||ray.jbyi*lb[0]-ub[1]+ray.cxy>0||ray.ibyj*lb[1]-ub[0]+ray.cyx>0||ray.jbyk*ub[2]-ub[1]+ray.czy>0||ray.kbyj*lb[1]-lb[2]+ray.cyz<0||ray.kbyi*lb[0]-lb[2]+ray.cxz<0||ray.ibyk*ub[2]-ub[0]+ray.czx>0)};tests[classify.MPM]=function testMPM(ray,lb,ub){var ro=ray.ro;return!(ro[0]<lb[0]||ro[1]>ub[1]||ro[2]<lb[2]||ray.jbyi*lb[0]-lb[1]+ray.cxy<0||ray.ibyj*ub[1]-ub[0]+ray.cyx>0||ray.jbyk*lb[2]-lb[1]+ray.czy<0||ray.kbyj*ub[1]-ub[2]+ray.cyz>0||ray.kbyi*lb[0]-ub[2]+ray.cxz>0||ray.ibyk*lb[2]-ub[0]+ray.czx>0)};tests[classify.MPP]=function testMPP(ray,lb,ub){var ro=ray.ro;return!(ro[0]<lb[0]||ro[1]>ub[1]||ro[2]>ub[2]||ray.jbyi*lb[0]-lb[1]+ray.cxy<0||ray.ibyj*ub[1]-ub[0]+ray.cyx>0||ray.jbyk*ub[2]-lb[1]+ray.czy<0||ray.kbyj*ub[1]-lb[2]+ray.cyz<0||ray.kbyi*lb[0]-lb[2]+ray.cxz<0||ray.ibyk*ub[2]-ub[0]+ray.czx>0)};tests[classify.PMM]=function testPMM(ray,lb,ub){var ro=ray.ro;return!(ro[0]>ub[0]||ro[1]<lb[1]||ro[2]<lb[2]||ray.jbyi*ub[0]-ub[1]+ray.cxy>0||ray.ibyj*lb[1]-lb[0]+ray.cyx<0||ray.jbyk*lb[2]-ub[1]+ray.czy>0||ray.kbyj*lb[1]-ub[2]+ray.cyz>0||ray.kbyi*ub[0]-ub[2]+ray.cxz>0||ray.ibyk*lb[2]-lb[0]+ray.czx<0)};tests[classify.PMP]=function testPMP(ray,lb,ub){var ro=ray.ro;return!(ro[0]>ub[0]||ro[1]<lb[1]||ro[2]>ub[2]||ray.jbyi*ub[0]-ub[1]+ray.cxy>0||ray.ibyj*lb[1]-lb[0]+ray.cyx<0||ray.jbyk*ub[2]-ub[1]+ray.czy>0||ray.kbyj*lb[1]-lb[2]+ray.cyz<0||ray.kbyi*ub[0]-lb[2]+ray.cxz<0||ray.ibyk*ub[2]-lb[0]+ray.czx<0)};tests[classify.PPM]=function testPPM(ray,lb,ub){var ro=ray.ro;return!(ro[0]>ub[0]||ro[1]>ub[1]||ro[2]<lb[2]||ray.jbyi*ub[0]-lb[1]+ray.cxy<0||ray.ibyj*ub[1]-lb[0]+ray.cyx<0||ray.jbyk*lb[2]-lb[1]+ray.czy<0||ray.kbyj*ub[1]-ub[2]+ray.cyz>0||ray.kbyi*ub[0]-ub[2]+ray.cxz>0||ray.ibyk*lb[2]-lb[0]+ray.czx<0)};tests[classify.PPP]=function testPPP(ray,lb,ub){var ro=ray.ro;return!(ro[0]>ub[0]||ro[1]>ub[1]||ro[2]>ub[2]||ray.jbyi*ub[0]-lb[1]+ray.cxy<0||ray.ibyj*ub[1]-lb[0]+ray.cyx<0||ray.jbyk*ub[2]-lb[1]+ray.czy<0||ray.kbyj*ub[1]-lb[2]+ray.cyz<0||ray.kbyi*ub[0]-lb[2]+ray.cxz<0||ray.ibyk*ub[2]-lb[0]+ray.czx<0)};tests[classify.POO]=function testPOO(ray,lb,ub){var ro=ray.ro;var ro1=ro[1];var ro2=ro[2];return!(ro[0]>ub[0]||ro1<lb[1]||ro1>ub[1]||ro2<lb[2]||ro2>ub[2])};tests[classify.MOO]=function testMOO(ray,lb,ub){var ro=ray.ro;return!(ro[0]<lb[0]||ro[1]<lb[1]||ro[1]>ub[1]||ro[2]<lb[2]||ro[2]>ub[2])};tests[classify.OPO]=function testOPO(ray,lb,ub){var ro=ray.ro;return!(ro[1]>ub[1]||ro[0]<lb[0]||ro[0]>ub[0]||ro[2]<lb[2]||ro[2]>ub[2])};tests[classify.OMO]=function testOMO(ray,lb,ub){var ro=ray.ro;return!(ro[1]<lb[1]||ro[0]<lb[0]||ro[0]>ub[0]||ro[2]<lb[2]||ro[2]>ub[2])};tests[classify.OOP]=function testOOP(ray,lb,ub){var ro=ray.ro;return!(ro[2]>ub[2]||ro[0]<lb[0]||ro[0]>ub[0]||ro[1]<lb[1]||ro[1]>ub[1])};tests[classify.OOM]=function testOOM(ray,lb,ub){var ro=ray.ro;return!(ro[2]<lb[2]||ro[0]<lb[0]||ro[0]>ub[0]||ro[1]<lb[1]||ro[1]>ub[1])};tests[classify.OMM]=function testOMM(ray,lb,ub){var ro=ray.ro;return!(ro[0]<lb[0]||ro[0]>ub[0]||ro[1]<lb[1]||ro[2]<lb[2]||ray.jbyk*lb[2]-ub[1]+ray.czy>0||ray.kbyj*lb[1]-ub[2]+ray.cyz>0)};tests[classify.OMP]=function testOMP(ray,lb,ub){var ro=ray.ro;return!(ro[0]<lb[0]||ro[0]>ub[0]||ro[1]<lb[1]||ro[2]>ub[2]||ray.jbyk*ub[2]-ub[1]+ray.czy>0||ray.kbyj*lb[1]-lb[2]+ray.cyz<0)};tests[classify.OPM]=function testOPM(ray,lb,ub){var ro=ray.ro;return!(ro[0]<lb[0]||ro[0]>ub[0]||ro[1]>ub[1]||ro[2]<lb[2]||ray.jbyk*lb[2]-lb[1]+ray.czy<0||ray.kbyj*ub[1]-ub[2]+ray.cyz>0)};tests[classify.OPP]=function testOPP(ray,lb,ub){var ro=ray.ro;return!(ro[0]<lb[0]||ro[0]>ub[0]||ro[1]>ub[1]||ro[2]>ub[2]||ray.jbyk*ub[2]-lb[1]+ray.czy<0||ray.kbyj*ub[1]-lb[2]+ray.cyz<0)};tests[classify.MOM]=function testMOM(ray,lb,ub){var ro=ray.ro;return!(ro[1]<lb[1]||ro[1]>ub[1]||ro[0]<lb[0]||ro[2]<lb[2]||ray.kbyi*lb[0]-ub[2]+ray.cxz>0||ray.ibyk*lb[2]-ub[0]+ray.czx>0)};tests[classify.MOP]=function testMOP(ray,lb,ub){var ro=ray.ro;return!(ro[1]<lb[1]||ro[1]>ub[1]||ro[0]<lb[0]||ro[2]>ub[2]||ray.kbyi*lb[0]-lb[2]+ray.cxz<0||ray.ibyk*ub[2]-ub[0]+ray.czx>0)};tests[classify.POM]=function testPOM(ray,lb,ub){var ro=ray.ro;return!(ro[1]<lb[1]||ro[1]>ub[1]||ro[0]>ub[0]||ro[2]<lb[2]||ray.kbyi*ub[0]-ub[2]+ray.cxz>0||ray.ibyk*lb[2]-lb[0]+ray.czx<0)};tests[classify.POP]=function testPOP(ray,lb,ub){var ro=ray.ro;return!(ro[1]<lb[1]||ro[1]>ub[1]||ro[0]>ub[0]||ro[2]>ub[2]||ray.kbyi*ub[0]-lb[2]+ray.cxz<0||ray.ibyk*ub[2]-lb[0]+ray.czx<0)};tests[classify.MMO]=function testMMO(ray,lb,ub){var ro=ray.ro;return!(ro[2]<lb[2]||ro[2]>ub[2]||ro[0]<lb[0]||ro[1]<lb[1]||ray.jbyi*lb[0]-ub[1]+ray.cxy>0||ray.ibyj*lb[1]-ub[0]+ray.cyx>0)};tests[classify.MPO]=function testMPO(ray,lb,ub){var ro=ray.ro;return!(ro[2]<lb[2]||ro[2]>ub[2]||ro[0]<lb[0]||ro[1]>ub[1]||ray.jbyi*lb[0]-lb[1]+ray.cxy<0||ray.ibyj*ub[1]-ub[0]+ray.cyx>0)};tests[classify.PMO]=function testPMO(ray,lb,ub){var ro=ray.ro;return!(ro[2]<lb[2]||ro[2]>ub[2]||ro[0]>ub[0]||ro[1]<lb[1]||ray.jbyi*ub[0]-ub[1]+ray.cxy>0||ray.ibyj*lb[1]-lb[0]+ray.cyx<0)};tests[classify.PPO]=function testPPO(ray,lb,ub){var ro=ray.ro;return!(ro[2]<lb[2]||ro[2]>ub[2]||ro[0]>ub[0]||ro[1]>ub[1]||ray.jbyi*ub[0]-lb[1]+ray.cxy<0||ray.ibyj*ub[1]-lb[0]+ray.cyx<0)};var lerps={};lerps[classify.MMM]=function lerpMMM(ray,aabb,norm){var ro=ray.ro;var ub=aabb[1];var a=(ub[0]-ro[0])*ray.ii;var b=(ub[1]-ro[1])*ray.ij;var c=(ub[2]-ro[2])*ray.ik;norm[0]=+(a>=b&&a>=c);norm[1]=+(b>=c&&b>=a);norm[2]=+(c>=a&&c>=b);return max(a,b,c)};lerps[classify.MMP]=function lerpMMP(ray,aabb,norm){var ro=ray.ro;var ub=aabb[1];var lb=aabb[0];var a=(ub[0]-ro[0])*ray.ii;var b=(ub[1]-ro[1])*ray.ij;var c=(lb[2]-ro[2])*ray.ik;norm[0]=+(a>=b&&a>=c);norm[1]=+(b>=c&&b>=a);norm[2]=-(c>=a&&c>=b);return max(a,b,c)};lerps[classify.MPM]=function lerpMPM(ray,aabb,norm){var ro=ray.ro;var ub=aabb[1];var lb=aabb[0];var a=(ub[0]-ro[0])*ray.ii;var b=(lb[1]-ro[1])*ray.ij;var c=(ub[2]-ro[2])*ray.ik;norm[0]=+(a>=b&&a>=c);norm[1]=-(b>=c&&b>=a);norm[2]=+(c>=a&&c>=b);return max(a,b,c)};lerps[classify.MPP]=function lerpMPP(ray,aabb,norm){var ro=ray.ro;var ub=aabb[1];var lb=aabb[0];var a=(ub[0]-ro[0])*ray.ii;var b=(lb[1]-ro[1])*ray.ij;var c=(lb[2]-ro[2])*ray.ik;norm[0]=+(a>=b&&a>=c);norm[1]=-(b>=c&&b>=a);norm[2]=-(c>=a&&c>=b);return max(a,b,c)};lerps[classify.PMM]=function lerpPMM(ray,aabb,norm){var ro=ray.ro;var ub=aabb[1];var lb=aabb[0];var a=(lb[0]-ro[0])*ray.ii;var b=(ub[1]-ro[1])*ray.ij;var c=(ub[2]-ro[2])*ray.ik;norm[0]=-(a>=b&&a>=c);norm[1]=+(b>=c&&b>=a);norm[2]=+(c>=a&&c>=b);return max(a,b,c)};lerps[classify.PMP]=function lerpPMP(ray,aabb,norm){var ro=ray.ro;var ub=aabb[1];var lb=aabb[0];var a=(lb[0]-ro[0])*ray.ii;var b=(ub[1]-ro[1])*ray.ij;var c=(lb[2]-ro[2])*ray.ik;norm[0]=-(a>=b&&a>=c);norm[1]=+(b>=c&&b>=a);norm[2]=-(c>=a&&c>=b);return max(a,b,c)};lerps[classify.PPM]=function lerpPPM(ray,aabb,norm){var ro=ray.ro;var lb=aabb[0];var ub=aabb[1];var a=(lb[0]-ro[0])*ray.ii;var b=(lb[1]-ro[1])*ray.ij;var c=(ub[2]-ro[2])*ray.ik;norm[0]=-(a>=b&&a>=c);norm[1]=-(b>=c&&b>=a);norm[2]=+(c>=a&&c>=b);return max(a,b,c)};lerps[classify.PPP]=function lerpPPP(ray,aabb,norm){var ro=ray.ro;var lb=aabb[0];var a=(lb[0]-ro[0])*ray.ii;var b=(lb[1]-ro[1])*ray.ij;var c=(lb[2]-ro[2])*ray.ik;norm[0]=-(a>=b&&a>=c);norm[1]=-(b>=c&&b>=a);norm[2]=-(c>=a&&c>=b);return max(a,b,c)};lerps[classify.OMM]=function lerpOMM(ray,aabb,norm){var ro=ray.ro;var ub=aabb[1];var a=(ub[1]-ro[1])*ray.ij;var b=(ub[2]-ro[2])*ray.ik;norm[0]=0;norm[1]=+(a>=b);norm[2]=+(b>=a);return max(a,b)};lerps[classify.OMP]=function lerpOMP(ray,aabb,norm){var ro=ray.ro;var a=(aabb[1][1]-ro[1])*ray.ij;var b=(aabb[0][2]-ro[2])*ray.ik;norm[0]=0;norm[1]=+(a>=b);norm[2]=-(b>=a);return max(a,b)};lerps[classify.OPM]=function lerpOPM(ray,aabb,norm){var ro=ray.ro;var a=(aabb[0][1]-ro[1])*ray.ij;var b=(aabb[1][2]-ro[2])*ray.ik;norm[0]=0;norm[1]=-(a>=b);norm[2]=+(b>=a);return max(a,b)};lerps[classify.OPP]=function lerpOPP(ray,aabb,norm){var ro=ray.ro;var lb=aabb[0];var a=(lb[1]-ro[1])*ray.ij;var b=(lb[2]-ro[2])*ray.ik;norm[0]=0;norm[1]=-(a>=b);norm[2]=-(b>=a);return max(a,b)};lerps[classify.MOM]=function lerpMOM(ray,aabb,norm){var ro=ray.ro;var ub=aabb[1];var a=(ub[0]-ro[0])*ray.ii;var b=(ub[2]-ro[2])*ray.ik;norm[0]=+(a>=b);norm[1]=0;norm[2]=+(b>=a);return max(a,b)};lerps[classify.MOP]=function lerpMOP(ray,aabb,norm){var ro=ray.ro;var a=(aabb[1][0]-ro[0])*ray.ii;var b=(aabb[0][2]-ro[2])*ray.ik;norm[0]=+(a>=b);norm[1]=0;norm[2]=-(b>=a);return max(a,b)};lerps[classify.POM]=function lerpPOM(ray,aabb,norm){var ro=ray.ro;var a=(aabb[0][0]-ray.ro[0])*ray.ii;var b=(aabb[1][2]-ray.ro[2])*ray.ik;norm[0]=-(a>=b);norm[1]=0;norm[2]=+(b>=a);return max(a,b)};lerps[classify.POP]=function lerpPOP(ray,aabb,norm){var ro=ray.ro;var lb=aabb[0];var a=(lb[0]-ro[0])*ray.ii;var b=(lb[2]-ro[2])*ray.ik;norm[0]=-(a>=b);norm[1]=0;norm[2]=-(b>=a);return max(a,b)};lerps[classify.MMO]=function lerpMMO(ray,aabb,norm){var ro=ray.ro;var ub=aabb[1];var a=(ub[0]-ro[0])*ray.ii;var b=(ub[1]-ro[1])*ray.ij;norm[0]=+(a>=b);norm[1]=+(b>=a);norm[2]=0;return max(a,b)};lerps[classify.MPO]=function lerpMPO(ray,aabb,norm){var ro=ray.ro;var a=(aabb[1][0]-ro[0])*ray.ii;var b=(aabb[0][1]-ro[1])*ray.ij;norm[0]=+(a>=b);norm[1]=-(b>=a);norm[2]=0;return max(a,b)};lerps[classify.PMO]=function lerpPMO(ray,aabb,norm){var ro=ray.ro;var a=(aabb[0][0]-ro[0])*ray.ii;var b=(aabb[1][1]-ro[1])*ray.ij;norm[0]=-(a>=b);norm[1]=+(b>=a);norm[2]=0;return max(a,b)};lerps[classify.PPO]=function lerpPPO(ray,aabb,norm){var ro=ray.ro;var lb=aabb[0];var a=(lb[0]-ro[0])*ray.ii;var b=(lb[1]-ro[1])*ray.ij;norm[0]=-(a>=b);norm[1]=-(b>=a);norm[2]=0;return max(a,b)};lerps[classify.MOO]=function lerpMOO(ray,aabb,norm){norm[0]=1;norm[1]=norm[2]=0;return(aabb[1][0]-ray.ro[0])*ray.ii};lerps[classify.POO]=function lerpPOO(ray,aabb,norm){norm[0]=-1;norm[1]=norm[2]=0;return(aabb[0][0]-ray.ro[0])*ray.ii};lerps[classify.OMO]=function lerpOMO(ray,aabb,norm){norm[0]=0;norm[1]=1;norm[2]=0;return(aabb[1][1]-ray.ro[1])*ray.ij};lerps[classify.OPO]=function lerpOPO(ray,aabb,norm){norm[0]=0;norm[1]=-1;norm[2]=0;return(aabb[0][1]-ray.ro[1])*ray.ij};lerps[classify.OOM]=function lerpOOM(ray,aabb,norm){norm[0]=norm[1]=0;norm[2]=1;return(aabb[1][2]-ray.ro[2])*ray.ik};lerps[classify.OOP]=function lerpOOP(ray,aabb,norm){norm[0]=norm[1]=0;norm[2]=-1;return(aabb[0][2]-ray.ro[2])*ray.ik};function Ray(ro,rd){this.ro=[0,0,0];this.rd=[0,0,0];this.update(ro,rd)}Ray.prototype.ii=0;Ray.prototype.ij=0;Ray.prototype.ik=0;Ray.prototype.ibyj=0;Ray.prototype.jbyi=0;Ray.prototype.jbyk=0;Ray.prototype.kbyj=0;Ray.prototype.ibyk=0;Ray.prototype.kbyi=0;Ray.prototype.cxy=0;Ray.prototype.cxz=0;Ray.prototype.cyx=0;Ray.prototype.cyz=0;Ray.prototype.czx=0;Ray.prototype.czy=0;Ray.prototype.classification=0;Ray.prototype.result=null;var scratchNormal=[0,0,0];Ray.prototype.intersects=function rayIntersectsAABB(aabb,computeDistance){var classification=this.classification;var t=tests[classification];if(t&&t(this,aabb[0],aabb[1])){if(!computeDistance){return true}var lerp=lerps[classification];var normal=Array.isArray(computeDistance)?computeDistance:scratchNormal;return lerp&&lerp(this,aabb,normal)}return false};Ray.prototype.update=function updateRay(ro,rd){var r=this;r.ro=ro;r.rd=rd;var i=rd[0],j=rd[1],k=rd[2];var x=ro[0],y=ro[1],z=ro[2];r.ii=i?1/i:0;r.ij=j?1/j:0;r.ik=k?1/k:0;r.ibyj=i*r.ij;r.jbyi=j*r.ii;r.jbyk=j*r.ik;r.kbyj=k*r.ij;r.ibyk=i*r.ik;r.kbyi=k*r.ii;r.cxy=y-r.jbyi*x;r.cxz=z-r.kbyi*x;r.cyx=x-r.ibyj*y;r.cyz=z-r.kbyj*y;r.czx=x-r.ibyk*z;r.czy=y-r.jbyk*z;r.classification=classify(i,j,k);return r};function createRay(rayOrigin,rayDirection){return new Ray(rayOrigin,rayDirection)}},{"ray-direction-classify":1}]},{},[]);var fc=require("fc");var center=require("ctx-translate-center");var circle=require("ctx-circle"); | |
var ndarray=require("ndarray");var vec2=require("gl-vec2");var createRay=require("ray-aabb");function sign(a){return typeof a==="number"?a?a<0?-1:1:a===a?0:0:0}var ro=[0,0];var rd=[0,0];var ray=createRay(ro,rd);var modelWidth=8;var modelHalfWidth=modelWidth/2;var blockSize=30;var model=ndarray(new Uint8Array(modelWidth*modelWidth),[modelWidth,modelWidth]);var normal=[0,0];var isect=[0,0];model.set(modelHalfWidth,modelHalfWidth,255);model.set(modelHalfWidth,modelHalfWidth-1,255);model.set(modelHalfWidth,modelHalfWidth-2,255);model.set(modelHalfWidth-1,modelHalfWidth,255);model.set(modelHalfWidth-1,modelHalfWidth+1,255);model.set(modelHalfWidth-2,modelHalfWidth-2,255);model.set(modelHalfWidth-2,modelHalfWidth-3,255);model.set(modelHalfWidth-3,modelHalfWidth-3,255);model.set(modelHalfWidth-3,modelHalfWidth-4,255);model.set(modelHalfWidth-3,modelHalfWidth-2,255);model.set(modelHalfWidth+3,modelHalfWidth+2,255);model.set(modelHalfWidth+2,modelHalfWidth+1,255);model.set(modelHalfWidth+2,modelHalfWidth,255);model.set(modelHalfWidth+2,modelHalfWidth-1,255);model.set(modelHalfWidth+2,modelHalfWidth-2,255);model.set(modelHalfWidth+3,modelHalfWidth-3,255);var modelBounds=[[-modelHalfWidth*blockSize,-modelHalfWidth*blockSize],[modelHalfWidth*blockSize,modelHalfWidth*blockSize]];console.log(modelBounds);var TAU=Math.PI*2;var pos=53.14696599999997;var ctx=fc(function render(dt){console.clear();console.log("POS",pos);ctx.clear();center(ctx);ctx.scale(1,-1);ctx.stroke();ctx.strokeStyle="rgba(40, 221, 0, .25)";ctx.fillStyle="rgb(149, 153, 152)";for(var mx=modelBounds[0][0];mx<modelBounds[1][0];mx+=blockSize){for(var my=modelBounds[0][1];my<modelBounds[1][1];my+=blockSize){var mmx=(mx-modelBounds[0][0])/blockSize;var mmy=(my-modelBounds[0][1])/blockSize;ctx.strokeRect(mx,my,blockSize,blockSize);if(model.get(mmx,mmy)){ctx.fillRect(mx-1,my-1,blockSize+2,blockSize+2)}}}ctx.strokeStyle="rgb(65, 176, 221)";ctx.strokeRect(modelBounds[0][0],modelBounds[0][1],modelWidth*blockSize,modelWidth*blockSize);ctx.beginPath();ctx.strokeStyle="rgba(221, 87, 103, .75)";var cx=Math.sin(pos)*modelWidth*blockSize;var cy=Math.cos(pos)*modelWidth*blockSize;circle(ctx,cx,cy,5);ctx.moveTo(cx,cy);ctx.lineTo(0,0);ctx.stroke();ro[0]=cx;ro[1]=cy;rd[0]=-cx;rd[1]=-cy;vec2.normalize(rd,rd);ray.update(ro,rd);var d=ray.intersects(modelBounds,normal);if(d!==false){vec2.add(isect,ro,vec2.scale(isect,rd,d));ctx.fillStyle="orange";ctx.beginPath();circle(ctx,isect[0],isect[1],3);ctx.fill();isect[0]+=rd[0];isect[1]+=rd[1];var block=computeBlock(rd,isect,normal);block[0]=Math.floor((block[0]-modelBounds[0][0])/blockSize);block[1]=Math.floor((block[1]-modelBounds[0][1])/blockSize);ctx.strokeStyle="yellow";console.log("rd",rd,ro);var res=calcStraightLine2d([[0,0],[modelWidth,modelWidth]],[(isect[0]-modelBounds[0][0])/blockSize,(isect[1]-modelBounds[0][1])/blockSize],block,rd,normal,model,function(x,y){console.log("visit",x,y);ctx.strokeRect(modelBounds[0][0]+x*blockSize+4,modelBounds[0][1]+y*blockSize+4,blockSize-8,blockSize-8)});if(res){console.log("RES");ctx.fillStyle="#f0f";ctx.fillRect(modelBounds[0][0]+res[0]*blockSize+2,modelBounds[0][1]+res[1]*blockSize+2,blockSize-4,blockSize-4)}}},1);function computeBlock(rd,isect,normal,out){out=out||[0,0];var l=rd.length;for(var i=0;i<l;i++){out[i]=Math.floor(isect[i])}return out}var paused=false;window.addEventListener("mousedown",function(){paused=!paused;if(paused){ctx.stop()}else{ctx.start()}});window.addEventListener("mousewheel",function(ev){pos+=ev.wheelDelta/500;ev.preventDefault()});function frac(x){return x-Math.floor(x)}function calcStraightLine2d(aabb,isect,origin,direction,isectNormal,pixels,visit){var dx=direction[0];var dy=direction[1];var max,min;if(Math.abs(dx)>Math.abs(dy)){max=dx;min=dy}else{max=dy;min=dx}var step=Math.abs(1/max*min);var ix=isect[0]+direction[0]*-step;var iy=isect[1]+direction[1]*-step;var lx=ix|0;var ly=iy|0;var sentnel=1e4;while(sentnel--){var rx=ix|0;var ry=iy|0;ix+=dx*step;iy+=dy*step;if(sign(rx-lx)||sign(ry-ly)){if(lx>=0&&ly>=0&&lx<aabb[1][0]&&ly<aabb[1][1]){visit(lx,ly);if(pixels.get(lx,ly)){console.log("holy shit",1e4-sentnel);return[lx,ly]}}lx=rx;ly=ry}}} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "requirebin-sketch", | |
"version": "1.0.0", | |
"dependencies": { | |
"fc": "1.4.3", | |
"ctx-translate-center": "1.0.0", | |
"ctx-circle": "1.0.0", | |
"ndarray": "1.0.18", | |
"gl-vec2": "1.0.0", | |
"ray-aabb": "3.0.1" | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- contents of this file will be placed inside the <body> --> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- contents of this file will be placed inside the <head> --> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment