├── choc3d ├── utils │ ├── externs │ │ ├── choc3d.js │ │ └── common.js │ ├── compiler │ │ ├── compiler.jar │ │ ├── README │ │ └── COPYING │ ├── build.sh │ ├── includes │ │ └── choc3d.json │ └── build.py ├── src │ ├── Choc3D.js │ ├── objects │ │ ├── Object.js │ │ ├── Ball.js │ │ └── Plane.js │ ├── apps │ │ └── App.js │ ├── scenes │ │ └── Choc3DScene.js │ └── physics │ │ └── Physics.js └── build │ ├── choc3d.min.js │ └── choc3d.js ├── .gitignore ├── README.md ├── LICENSE ├── js ├── shaders │ └── SkyShader.js ├── libs │ ├── stats.min.js │ ├── tween.min.js │ └── dat.gui.min.js ├── controls │ └── OrbitControls.js ├── runs │ └── Run.js └── Sparks.js ├── css └── reset.css └── index.html /choc3d/utils/externs/choc3d.js: -------------------------------------------------------------------------------- 1 | var Choc3D; -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Mac 2 | .DS_Store 3 | ._.DS_Store -------------------------------------------------------------------------------- /choc3d/utils/externs/common.js: -------------------------------------------------------------------------------- 1 | var console; 2 | var JSON; 3 | var THREE; -------------------------------------------------------------------------------- /choc3d/src/Choc3D.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @author Mathieu Ledru 3 | */ 4 | 5 | var Choc3D = Choc3D || { VERSION: '1.0' }; -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Choc3d 2 | ====== 3 | 4 | More informations at [https://apps.darkwood.fr/choc3d](https://apps.darkwood.fr/choc3d) 5 | -------------------------------------------------------------------------------- /choc3d/utils/compiler/compiler.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/darkwood-com/choc3d/master/choc3d/utils/compiler/compiler.jar -------------------------------------------------------------------------------- /choc3d/utils/build.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | python build.py --include choc3d --output ../build/choc3d.js 4 | python build.py --include choc3d --minify --output ../build/choc3d.min.js 5 | -------------------------------------------------------------------------------- /choc3d/utils/includes/choc3d.json: -------------------------------------------------------------------------------- 1 | [ 2 | "../src/Choc3D.js", 3 | "../src/apps/App.js", 4 | "../src/objects/Object.js", 5 | "../src/objects/Ball.js", 6 | "../src/objects/Plane.js", 7 | "../src/physics/Physics.js", 8 | "../src/scenes/Choc3DScene.js" 9 | ] 10 | -------------------------------------------------------------------------------- /choc3d/src/objects/Object.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @author Mathieu Ledru 3 | */ 4 | 5 | Choc3D.Object = function ( object3D ) { 6 | 7 | THREE.Object3D.call( this ); 8 | 9 | this.weight = 0; 10 | this.velocity = new THREE.Vector3( 0, 0, 0 ); 11 | 12 | this.add( object3D ); 13 | 14 | }; 15 | 16 | Choc3D.Object.prototype = Object.create( THREE.Object3D.prototype ); 17 | -------------------------------------------------------------------------------- /choc3d/src/objects/Ball.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @author Mathieu Ledru 3 | */ 4 | 5 | Choc3D.Ball = function ( geometry, material ) { 6 | 7 | geometry = geometry || new THREE.SphereGeometry(0.5, 16, 16); 8 | material = material || new THREE.MeshPhongMaterial( { color: Math.random() * 0xffffff } ); 9 | 10 | Choc3D.Object.call( this, new THREE.Mesh( geometry, material ) ); 11 | 12 | this.radius = 0.5; 13 | 14 | }; 15 | 16 | Choc3D.Ball.prototype = Object.create( Choc3D.Object.prototype ); 17 | -------------------------------------------------------------------------------- /choc3d/src/objects/Plane.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @author Mathieu Ledru 3 | */ 4 | 5 | Choc3D.Plane = function ( geometry, material ) { 6 | 7 | geometry = geometry || new THREE.PlaneGeometry(1, 1); 8 | material = material || new THREE.MeshPhongMaterial( { color: Math.random() * 0xffffff } ); 9 | 10 | Choc3D.Object.call( this, new THREE.Mesh( geometry, material ) ); 11 | 12 | this.normal = this.up.clone(); 13 | 14 | }; 15 | 16 | Choc3D.Plane.prototype = Object.create( Choc3D.Object.prototype ); 17 | 18 | Choc3D.Plane.prototype.lookAt = function ( vector ) { 19 | 20 | Choc3D.Object.prototype.lookAt.call( this, vector ); 21 | 22 | this.normal.sub( vector, this.position ).normalize(); 23 | 24 | }; -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License 2 | 3 | Copyright (c) 2013 choc3d Mathieu Ledru (http://www.darkwood.fr) 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /choc3d/src/apps/App.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @author Mathieu Ledru 3 | */ 4 | 5 | Choc3D.App = function() { 6 | 7 | this.renderer = new THREE.WebGLRenderer(); 8 | this.scene = new Choc3D.Choc3DScene(); 9 | this.camera = new THREE.PerspectiveCamera(); 10 | this.light = new THREE.PointLight( 0xFFFFFF ); 11 | 12 | // init 13 | 14 | this.camera.position.x = 2; 15 | this.camera.position.y = 1; 16 | this.camera.position.z = 2; 17 | 18 | this.scene.add(this.camera); 19 | 20 | this.light.position.copy(this.camera.position); 21 | this.scene.add(this.light); 22 | 23 | }; 24 | 25 | Choc3D.App.prototype = { 26 | 27 | constructor: Choc3D.App, 28 | 29 | reshape: function(width, height) 30 | { 31 | var view_angle = 45, 32 | aspect = width / height, 33 | near = 0.1, 34 | far = 10000; 35 | 36 | this.camera.projectionMatrix.makePerspective(view_angle, aspect, near, far); 37 | this.renderer.setSize(width, height); 38 | }, 39 | 40 | render: function() 41 | { 42 | this.renderer.render(this.scene, this.camera); 43 | }, 44 | 45 | update: function(dt) 46 | { 47 | this.scene.update(dt); 48 | } 49 | 50 | }; -------------------------------------------------------------------------------- /js/shaders/SkyShader.js: -------------------------------------------------------------------------------- 1 | THREE.SkyShader = { 2 | 3 | uniforms: { 4 | 5 | topColor: { type: "c", value: new THREE.Color( 0x0077ff ) }, 6 | bottomColor: { type: "c", value: new THREE.Color( 0xffffff ) }, 7 | offset: { type: "f", value: 400 }, 8 | exponent: { type: "f", value: 0.6 } 9 | 10 | }, 11 | 12 | vertexShader: [ 13 | 14 | "varying vec3 vWorldPosition;", 15 | 16 | "void main() {", 17 | 18 | "vec4 worldPosition = modelMatrix * vec4( position, 1.0 );", 19 | "vWorldPosition = worldPosition.xyz;", 20 | 21 | "gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );", 22 | 23 | "}" 24 | 25 | ].join("\n"), 26 | 27 | fragmentShader: [ 28 | 29 | "uniform vec3 topColor;", 30 | "uniform vec3 bottomColor;", 31 | "uniform float offset;", 32 | "uniform float exponent;", 33 | 34 | "varying vec3 vWorldPosition;", 35 | 36 | "void main() {", 37 | 38 | "float h = normalize( vWorldPosition + offset ).y;", 39 | "gl_FragColor = vec4( mix( bottomColor, topColor, max( pow( h, exponent ), 0.0 ) ), 1.0 );", 40 | 41 | "}" 42 | 43 | ].join("\n") 44 | 45 | }; 46 | -------------------------------------------------------------------------------- /css/reset.css: -------------------------------------------------------------------------------- 1 | /* http://meyerweb.com/eric/tools/css/reset/ 2 | v2.0 | 20110126 3 | License: none (public domain) 4 | */ 5 | 6 | html, body, div, span, applet, object, iframe, 7 | h1, h2, h3, h4, h5, h6, p, blockquote, pre, 8 | a, abbr, acronym, address, big, cite, code, 9 | del, dfn, em, img, ins, kbd, q, s, samp, 10 | small, strike, strong, sub, sup, tt, var, 11 | b, u, i, center, 12 | dl, dt, dd, ol, ul, li, 13 | fieldset, form, label, legend, 14 | table, caption, tbody, tfoot, thead, tr, th, td, 15 | article, aside, canvas, details, embed, 16 | figure, figcaption, footer, header, hgroup, 17 | menu, nav, output, ruby, section, summary, 18 | time, mark, audio, video { 19 | margin: 0; 20 | padding: 0; 21 | border: 0; 22 | font-size: 100%; 23 | font: inherit; 24 | vertical-align: baseline; 25 | } 26 | /* HTML5 display-role reset for older browsers */ 27 | article, aside, details, figcaption, figure, 28 | footer, header, hgroup, menu, nav, section { 29 | display: block; 30 | } 31 | body { 32 | line-height: 1; 33 | } 34 | ol, ul { 35 | list-style: none; 36 | } 37 | blockquote, q { 38 | quotes: none; 39 | } 40 | blockquote:before, blockquote:after, 41 | q:before, q:after { 42 | content: ''; 43 | content: none; 44 | } 45 | table { 46 | border-collapse: collapse; 47 | border-spacing: 0; 48 | } -------------------------------------------------------------------------------- /choc3d/utils/build.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | import argparse 4 | import json 5 | import os 6 | import shutil 7 | import sys 8 | import tempfile 9 | 10 | 11 | def main(argv=None): 12 | 13 | parser = argparse.ArgumentParser() 14 | parser.add_argument('--include', action='append', required=True) 15 | parser.add_argument('--externs', action='append', default=['externs/common.js']) 16 | parser.add_argument('--minify', action='store_true', default=False) 17 | parser.add_argument('--output', default='../build/three.js') 18 | 19 | args = parser.parse_args() 20 | 21 | output = args.output 22 | 23 | # merge 24 | 25 | print(' * Building ' + output) 26 | 27 | fd, path = tempfile.mkstemp() 28 | tmp = open(path, 'w') 29 | 30 | for include in args.include: 31 | with open('includes/' + include + '.json','r') as f: files = json.load(f) 32 | for filename in files: 33 | with open(filename, 'r') as f: tmp.write(f.read()) 34 | 35 | tmp.close() 36 | 37 | # save 38 | 39 | if args.minify is False: 40 | 41 | shutil.copy(path, output) 42 | os.chmod(output, 0o664); # temp files would usually get 0600 43 | 44 | else: 45 | 46 | externs = ' --externs '.join(args.externs) 47 | os.system('java -jar compiler/compiler.jar --warning_level=VERBOSE --jscomp_off=globalThis --externs %s --jscomp_off=checkTypes --language_in=ECMASCRIPT5_STRICT --js %s --js_output_file %s' % (externs, path, output)) 48 | 49 | # header 50 | 51 | with open(output,'r') as f: text = f.read() 52 | with open(output,'w') as f: f.write(("// %s - Mathieu Ledru\n" % os.path.basename(output)) + text) 53 | 54 | os.close(fd) 55 | os.remove(path) 56 | 57 | 58 | if __name__ == "__main__": 59 | main() 60 | -------------------------------------------------------------------------------- /index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Choc 3D 6 | 7 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 60 | 61 | -------------------------------------------------------------------------------- /js/libs/stats.min.js: -------------------------------------------------------------------------------- 1 | // stats.js - http://github.com/mrdoob/stats.js 2 | var Stats=function(){var l=Date.now(),m=l,g=0,n=Infinity,o=0,h=0,p=Infinity,q=0,r=0,s=0,f=document.createElement("div");f.id="stats";f.addEventListener("mousedown",function(b){b.preventDefault();t(++s%2)},!1);f.style.cssText="width:80px;opacity:0.9;cursor:pointer";var a=document.createElement("div");a.id="fps";a.style.cssText="padding:0 0 3px 3px;text-align:left;background-color:#002";f.appendChild(a);var i=document.createElement("div");i.id="fpsText";i.style.cssText="color:#0ff;font-family:Helvetica,Arial,sans-serif;font-size:9px;font-weight:bold;line-height:15px"; 3 | i.innerHTML="FPS";a.appendChild(i);var c=document.createElement("div");c.id="fpsGraph";c.style.cssText="position:relative;width:74px;height:30px;background-color:#0ff";for(a.appendChild(c);74>c.children.length;){var j=document.createElement("span");j.style.cssText="width:1px;height:30px;float:left;background-color:#113";c.appendChild(j)}var d=document.createElement("div");d.id="ms";d.style.cssText="padding:0 0 3px 3px;text-align:left;background-color:#020;display:none";f.appendChild(d);var k=document.createElement("div"); 4 | k.id="msText";k.style.cssText="color:#0f0;font-family:Helvetica,Arial,sans-serif;font-size:9px;font-weight:bold;line-height:15px";k.innerHTML="MS";d.appendChild(k);var e=document.createElement("div");e.id="msGraph";e.style.cssText="position:relative;width:74px;height:30px;background-color:#0f0";for(d.appendChild(e);74>e.children.length;)j=document.createElement("span"),j.style.cssText="width:1px;height:30px;float:left;background-color:#131",e.appendChild(j);var t=function(b){s=b;switch(s){case 0:a.style.display= 5 | "block";d.style.display="none";break;case 1:a.style.display="none",d.style.display="block"}};return{REVISION:11,domElement:f,setMode:t,begin:function(){l=Date.now()},end:function(){var b=Date.now();g=b-l;n=Math.min(n,g);o=Math.max(o,g);k.textContent=g+" MS ("+n+"-"+o+")";var a=Math.min(30,30-30*(g/200));e.appendChild(e.firstChild).style.height=a+"px";r++;b>m+1E3&&(h=Math.round(1E3*r/(b-m)),p=Math.min(p,h),q=Math.max(q,h),i.textContent=h+" FPS ("+p+"-"+q+")",a=Math.min(30,30-30*(h/100)),c.appendChild(c.firstChild).style.height= 6 | a+"px",m=b,r=0);return b},update:function(){l=this.end()}}}; 7 | -------------------------------------------------------------------------------- /choc3d/src/scenes/Choc3DScene.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @author Mathieu Ledru 3 | */ 4 | 5 | Choc3D.Choc3DScene = function ( ) { 6 | 7 | THREE.Scene.call( this ); 8 | 9 | this.physics = new Choc3D.Physics(); 10 | 11 | var scope = this; 12 | 13 | //create cube 14 | function addPlane( u, v , depth ) { 15 | var w; 16 | 17 | if ( ( u === 'x' && v === 'y' ) || ( u === 'y' && v === 'x' ) ) { 18 | 19 | w = 'z'; 20 | 21 | } else if ( ( u === 'x' && v === 'z' ) || ( u === 'z' && v === 'x' ) ) { 22 | 23 | w = 'y'; 24 | 25 | } else if ( ( u === 'z' && v === 'y' ) || ( u === 'y' && v === 'z' ) ) { 26 | 27 | w = 'x'; 28 | 29 | } 30 | 31 | var plane = new Choc3D.Plane(); 32 | plane.position[ u ] = 0; 33 | plane.position[ v ] = 0; 34 | plane.position[ w ] = 0.5 * depth; 35 | 36 | plane.lookAt(new THREE.Vector3( 0, 0, 0 )); 37 | 38 | scope.add(plane); 39 | } 40 | 41 | addPlane( 'z', 'y' , 1 ); 42 | addPlane( 'z', 'y' , - 1 ); 43 | addPlane( 'x', 'z' , 1 ); 44 | addPlane( 'x', 'z' , - 1 ); 45 | addPlane( 'x', 'y' , 1 ); 46 | addPlane( 'x', 'y' , - 1 ); 47 | 48 | //add balls 49 | for(var i = 0; i < 10; ++i) { 50 | this.addBall(); 51 | } 52 | }; 53 | 54 | Choc3D.Choc3DScene.prototype = Object.create( THREE.Scene.prototype ); 55 | 56 | Choc3D.Choc3DScene.prototype.addBall = function() { 57 | 58 | var ballVolume = function( radius ) { 59 | return (4 * Math.PI * Math.pow( radius, 3) / 3) 60 | }; 61 | 62 | var ballCanBePlaced = function( ball ) { 63 | 64 | var vector = new THREE.Vector3(); 65 | var unit = new THREE.Vector3(1, 1, 1); 66 | 67 | //check if ball is contained into Scene cube 68 | var cubeBox = new THREE.Box3(); 69 | cubeBox.setFromCenterAndSize(vector, unit); 70 | var sphereBox = new THREE.Box3(); 71 | sphereBox.setFromCenterAndSize(ball.position, unit.clone().multiplyScalar(2 * ball.radius)); 72 | if( !cubeBox.containsBox(sphereBox) ) { 73 | return false; 74 | } 75 | 76 | //check if ball do not collide with Scene balls 77 | for ( var i = 0, l = this.children.length; i < l; i ++ ) { 78 | 79 | var child = this.children[i]; 80 | if ( child instanceof Choc3D.Ball ) { 81 | 82 | vector.sub(ball.position, child.position); 83 | 84 | //if distance between positions is shorter than radius sum, then there is collision 85 | if(vector.lengthSq() <= Math.pow(ball.radius + child.radius, 2) + this.physics.ZERO) return false; 86 | 87 | } 88 | 89 | } 90 | 91 | return true; 92 | }; 93 | 94 | var ball = new Choc3D.Ball(); 95 | var radius = ball.radius; 96 | 97 | do { 98 | ball.position.x = Math.random() - 0.5; 99 | ball.position.y = Math.random() - 0.5; 100 | ball.position.z = Math.random() - 0.5; 101 | ball.velocity.x = (Math.random() - 0.5) / 1000; 102 | ball.velocity.y = (Math.random() - 0.5) / 1000; 103 | ball.velocity.z = (Math.random() - 0.5) / 1000; 104 | ball.radius = Math.random() / 25 + 0.05; 105 | 106 | //weight is proportional to the ball volume 107 | ball.weight = 2 * ballVolume.call( this, ball.radius ); 108 | 109 | } while( !ballCanBePlaced.call( this, ball ) ); 110 | 111 | ball.scale.multiplyScalar( ball.radius / radius ); 112 | 113 | this.add(ball); 114 | 115 | }; 116 | 117 | Choc3D.Choc3DScene.prototype.add = function ( object ) { 118 | 119 | THREE.Scene.prototype.add.call( this, object ); 120 | 121 | this.physics.add( object ); 122 | 123 | }; 124 | 125 | Choc3D.Choc3DScene.prototype.remove = function ( object ) { 126 | 127 | this.physics.remove( object ); 128 | 129 | THREE.Scene.prototype.remove.call( this, object ); 130 | 131 | }; 132 | 133 | Choc3D.Choc3DScene.prototype.update = function( dt ) { 134 | 135 | this.physics.update( dt ); 136 | 137 | }; 138 | -------------------------------------------------------------------------------- /js/libs/tween.min.js: -------------------------------------------------------------------------------- 1 | // tween.js - http://github.com/sole/tween.js 2 | 'use strict';var TWEEN=TWEEN||function(){var a=[];return{REVISION:"7",getAll:function(){return a},removeAll:function(){a=[]},add:function(c){a.push(c)},remove:function(c){c=a.indexOf(c);-1!==c&&a.splice(c,1)},update:function(c){if(0===a.length)return!1;for(var b=0,d=a.length,c=void 0!==c?c:Date.now();b(a*=2)?0.5*a*a:-0.5*(--a*(a-2)-1)}},Cubic:{In:function(a){return a*a*a},Out:function(a){return--a*a*a+1},InOut:function(a){return 1>(a*=2)?0.5*a*a*a:0.5*((a-=2)*a*a+2)}},Quartic:{In:function(a){return a*a*a*a},Out:function(a){return 1- --a*a*a*a},InOut:function(a){return 1>(a*=2)?0.5*a*a*a*a:-0.5*((a-=2)*a*a*a-2)}},Quintic:{In:function(a){return a*a*a* 7 | a*a},Out:function(a){return--a*a*a*a*a+1},InOut:function(a){return 1>(a*=2)?0.5*a*a*a*a*a:0.5*((a-=2)*a*a*a*a+2)}},Sinusoidal:{In:function(a){return 1-Math.cos(a*Math.PI/2)},Out:function(a){return Math.sin(a*Math.PI/2)},InOut:function(a){return 0.5*(1-Math.cos(Math.PI*a))}},Exponential:{In:function(a){return 0===a?0:Math.pow(1024,a-1)},Out:function(a){return 1===a?1:1-Math.pow(2,-10*a)},InOut:function(a){return 0===a?0:1===a?1:1>(a*=2)?0.5*Math.pow(1024,a-1):0.5*(-Math.pow(2,-10*(a-1))+2)}},Circular:{In:function(a){return 1- 8 | Math.sqrt(1-a*a)},Out:function(a){return Math.sqrt(1- --a*a)},InOut:function(a){return 1>(a*=2)?-0.5*(Math.sqrt(1-a*a)-1):0.5*(Math.sqrt(1-(a-=2)*a)+1)}},Elastic:{In:function(a){var c,b=0.1;if(0===a)return 0;if(1===a)return 1;!b||1>b?(b=1,c=0.1):c=0.4*Math.asin(1/b)/(2*Math.PI);return-(b*Math.pow(2,10*(a-=1))*Math.sin((a-c)*2*Math.PI/0.4))},Out:function(a){var c,b=0.1;if(0===a)return 0;if(1===a)return 1;!b||1>b?(b=1,c=0.1):c=0.4*Math.asin(1/b)/(2*Math.PI);return b*Math.pow(2,-10*a)*Math.sin((a-c)* 9 | 2*Math.PI/0.4)+1},InOut:function(a){var c,b=0.1;if(0===a)return 0;if(1===a)return 1;!b||1>b?(b=1,c=0.1):c=0.4*Math.asin(1/b)/(2*Math.PI);return 1>(a*=2)?-0.5*b*Math.pow(2,10*(a-=1))*Math.sin((a-c)*2*Math.PI/0.4):0.5*b*Math.pow(2,-10*(a-=1))*Math.sin((a-c)*2*Math.PI/0.4)+1}},Back:{In:function(a){return a*a*(2.70158*a-1.70158)},Out:function(a){return--a*a*(2.70158*a+1.70158)+1},InOut:function(a){return 1>(a*=2)?0.5*a*a*(3.5949095*a-2.5949095):0.5*((a-=2)*a*(3.5949095*a+2.5949095)+2)}},Bounce:{In:function(a){return 1- 10 | TWEEN.Easing.Bounce.Out(1-a)},Out:function(a){return a<1/2.75?7.5625*a*a:a<2/2.75?7.5625*(a-=1.5/2.75)*a+0.75:a<2.5/2.75?7.5625*(a-=2.25/2.75)*a+0.9375:7.5625*(a-=2.625/2.75)*a+0.984375},InOut:function(a){return 0.5>a?0.5*TWEEN.Easing.Bounce.In(2*a):0.5*TWEEN.Easing.Bounce.Out(2*a-1)+0.5}}}; 11 | TWEEN.Interpolation={Linear:function(a,c){var b=a.length-1,d=b*c,e=Math.floor(d),f=TWEEN.Interpolation.Utils.Linear;return 0>c?f(a[0],a[1],d):1b?b:e+1],d-e)},Bezier:function(a,c){var b=0,d=a.length-1,e=Math.pow,f=TWEEN.Interpolation.Utils.Bernstein,h;for(h=0;h<=d;h++)b+=e(1-c,d-h)*e(c,h)*a[h]*f(d,h);return b},CatmullRom:function(a,c){var b=a.length-1,d=b*c,e=Math.floor(d),f=TWEEN.Interpolation.Utils.CatmullRom;return a[0]===a[b]?(0>c&&(e=Math.floor(d=b*(1+c))),f(a[(e- 12 | 1+b)%b],a[e],a[(e+1)%b],a[(e+2)%b],d-e)):0>c?a[0]-(f(a[0],a[0],a[1],a[1],-d)-a[0]):1e)&&(e=Math.sqrt(e),d=-(f.x+ 9 | f.y+f.z),c=void 0,f=d+e,0<=f&&(c=f),e=d-e,0<=e&&c>e&&(c=e),void 0!==c))return c/=g,c-=c*this.ZERO},timeCollisionBallPlane:function(a,b){var c=a.velocity.dot(b.normal);if(!(-this.ZEROc;++c)this.addBall()}; 12 | Choc3D.Choc3DScene.prototype=Object.create(THREE.Scene.prototype); 13 | Choc3D.Choc3DScene.prototype.addBall=function(){var a=function(a){var b=new THREE.Vector3,c=new THREE.Vector3(1,1,1),e=new THREE.Box3;e.setFromCenterAndSize(b,c);var h=new THREE.Box3;h.setFromCenterAndSize(a.position,c.clone().multiplyScalar(2*a.radius));if(!e.containsBox(h))return!1;c=0;for(e=this.children.length;c 0 ) { 181 | 182 | this.dispatchEvent( changeEvent ); 183 | 184 | lastPosition.copy( this.object.position ); 185 | 186 | } 187 | 188 | }; 189 | 190 | 191 | function getAutoRotationAngle() { 192 | 193 | return 2 * Math.PI / 60 / 60 * scope.autoRotateSpeed; 194 | 195 | } 196 | 197 | function getZoomScale() { 198 | 199 | return Math.pow( 0.95, scope.userZoomSpeed ); 200 | 201 | } 202 | 203 | function onMouseDown( event ) { 204 | 205 | if ( !scope.userRotate ) return; 206 | 207 | event.preventDefault(); 208 | 209 | if ( event.button === 0 || event.button === 2 ) { 210 | 211 | state = STATE.ROTATE; 212 | 213 | rotateStart.set( event.clientX, event.clientY ); 214 | 215 | } else if ( event.button === 1 ) { 216 | 217 | state = STATE.ZOOM; 218 | 219 | zoomStart.set( event.clientX, event.clientY ); 220 | 221 | } 222 | 223 | document.addEventListener( 'mousemove', onMouseMove, false ); 224 | document.addEventListener( 'mouseup', onMouseUp, false ); 225 | 226 | } 227 | 228 | function onMouseMove( event ) { 229 | 230 | event.preventDefault(); 231 | 232 | if ( state === STATE.ROTATE ) { 233 | 234 | rotateEnd.set( event.clientX, event.clientY ); 235 | rotateDelta.sub( rotateEnd, rotateStart ); 236 | 237 | scope.rotateLeft( 2 * Math.PI * rotateDelta.x / PIXELS_PER_ROUND * scope.userRotateSpeed ); 238 | scope.rotateUp( 2 * Math.PI * rotateDelta.y / PIXELS_PER_ROUND * scope.userRotateSpeed ); 239 | 240 | rotateStart.copy( rotateEnd ); 241 | 242 | } else if ( state === STATE.ZOOM ) { 243 | 244 | zoomEnd.set( event.clientX, event.clientY ); 245 | zoomDelta.sub( zoomEnd, zoomStart ); 246 | 247 | if ( zoomDelta.y > 0 ) { 248 | 249 | scope.zoomIn(); 250 | 251 | } else { 252 | 253 | scope.zoomOut(); 254 | 255 | } 256 | 257 | zoomStart.copy( zoomEnd ); 258 | 259 | } 260 | 261 | } 262 | 263 | function onMouseUp( event ) { 264 | 265 | if ( ! scope.userRotate ) return; 266 | 267 | document.removeEventListener( 'mousemove', onMouseMove, false ); 268 | document.removeEventListener( 'mouseup', onMouseUp, false ); 269 | 270 | state = STATE.NONE; 271 | 272 | } 273 | 274 | function onMouseWheel( event ) { 275 | 276 | if ( ! scope.userZoom ) return; 277 | 278 | if ( event.wheelDelta > 0 ) { 279 | 280 | scope.zoomOut(); 281 | 282 | } else { 283 | 284 | scope.zoomIn(); 285 | 286 | } 287 | 288 | } 289 | 290 | this.domElement.addEventListener( 'contextmenu', function ( event ) { event.preventDefault(); }, false ); 291 | this.domElement.addEventListener( 'mousedown', onMouseDown, false ); 292 | this.domElement.addEventListener( 'mousewheel', onMouseWheel, false ); 293 | 294 | }; 295 | -------------------------------------------------------------------------------- /choc3d/src/physics/Physics.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @author Mathieu Ledru 3 | */ 4 | 5 | Choc3D.Physics = function () { 6 | 7 | this.ZERO = Math.pow(2, -32); //adjust imprecision 8 | 9 | this.objects = []; 10 | 11 | }; 12 | 13 | Choc3D.Physics.prototype = { 14 | 15 | constructor: Choc3D.Physics, 16 | 17 | add: function( object ) { 18 | 19 | if ( object instanceof Choc3D.Object ) { 20 | 21 | this.objects.push( object ); 22 | 23 | } 24 | 25 | }, 26 | 27 | remove: function ( object ) { 28 | 29 | var index = this.objects.indexOf( object ); 30 | 31 | if ( index !== - 1 ) { 32 | 33 | this.objects.splice( index, 1 ); 34 | 35 | } 36 | 37 | }, 38 | 39 | /** 40 | * Update objects between dt 41 | * Possible collisions are : 42 | * - Ball vs Ball 43 | * - Ball vs Plan 44 | * 45 | * @param dt 46 | */ 47 | update: function ( dt ) { 48 | 49 | var l = this.objects.length, i, j, objI, objJ, time; 50 | 51 | while ( dt > 0 ) { 52 | 53 | //search min collision time between objects 54 | var collision = { 55 | time: dt, 56 | objects: null 57 | }; 58 | 59 | for ( i = 0; i < l; i ++ ) { 60 | 61 | for ( j = i + 1; j < l; j ++ ) { 62 | 63 | objI = this.objects[ i ]; 64 | objJ = this.objects[ j ]; 65 | 66 | time = undefined; 67 | 68 | if ( objI instanceof Choc3D.Ball && objJ instanceof Choc3D.Ball ) { 69 | 70 | time = this.timeCollisionBallBall(objI, objJ); 71 | 72 | } else if ( objI instanceof Choc3D.Ball && objJ instanceof Choc3D.Plane 73 | || objI instanceof Choc3D.Plane && objJ instanceof Choc3D.Ball ) { 74 | 75 | time = this.timeCollisionBallPlane( 76 | objI instanceof Choc3D.Ball ? objI : objJ, 77 | objI instanceof Choc3D.Plane ? objI : objJ 78 | ); 79 | 80 | } 81 | 82 | if ( time !== undefined && time >= 0 && time < collision.time ) { 83 | 84 | collision = { 85 | time: time, 86 | objects: { i: i, j: j } 87 | }; 88 | 89 | } 90 | 91 | } 92 | 93 | } 94 | 95 | //apply time 96 | for ( i = 0; i < l; i ++ ) { 97 | 98 | var object = this.objects[ i ]; 99 | var v = object.velocity.clone().multiplyScalar( collision.time ); 100 | 101 | object.position.addSelf( v ); 102 | 103 | } 104 | 105 | //apply collision if found between the two objects 106 | if( collision.objects ) { 107 | 108 | objI = this.objects[ collision.objects.i ]; 109 | objJ = this.objects[ collision.objects.j ]; 110 | 111 | if ( objI instanceof Choc3D.Ball && objJ instanceof Choc3D.Ball ) { 112 | 113 | this.collideBallBall(objI, objJ); 114 | 115 | } else if ( objI instanceof Choc3D.Ball && objJ instanceof Choc3D.Plane 116 | || objI instanceof Choc3D.Plane && objJ instanceof Choc3D.Ball ) { 117 | 118 | this.collideBallPlane( 119 | objI instanceof Choc3D.Ball ? objI : objJ, 120 | objI instanceof Choc3D.Plane ? objI : objJ 121 | ); 122 | 123 | } 124 | 125 | } 126 | 127 | dt -= collision.time; 128 | 129 | } 130 | 131 | }, 132 | 133 | timeCollisionBallBall: function( ballI, ballJ ) { 134 | 135 | //relative position 136 | var p = new THREE.Vector3(); 137 | p.sub(ballJ.position, ballI.position); 138 | 139 | //relative velocity 140 | var v = new THREE.Vector3(); 141 | v.sub(ballJ.velocity, ballI.velocity); 142 | 143 | //relative square velocity 144 | var v2 = v.clone().multiplySelf(v); 145 | 146 | //kinematic viscosity 147 | var kv = p.clone().multiplySelf(v); 148 | 149 | //minimal distance between the two balls 150 | var d = ballI.radius + ballJ.radius; 151 | 152 | var c = v2.x + v2.y + v2.z; 153 | var b = 2*(kv.x*kv.y + kv.x*kv.z + kv.y*kv.z) + c * Math.pow(d, 2); 154 | b -= Math.pow(p.x, 2)*(v2.y + v2.z); 155 | b -= Math.pow(p.y, 2)*(v2.x + v2.z); 156 | b -= Math.pow(p.z, 2)*(v2.x + v2.y); 157 | 158 | //check if there is a collision (b >= 0) 159 | if(b < 0) return undefined; 160 | 161 | //then we can calculate b^(1/2) 162 | b = Math.sqrt(b); 163 | 164 | //we also can calculate a 165 | var a = -(kv.x + kv.y + kv.z); 166 | 167 | //there is a collision, we seek the min collision time >= 0 168 | //t1 and t2 are two possible time collision 169 | var time = undefined, t1, t2; 170 | 171 | t1 = a + b; 172 | if(t1 >= 0) time = t1; 173 | 174 | t2 = a - b; 175 | if(t2 >= 0 && time > t2) time = t2; 176 | 177 | //if time is undefined, then the collision is in the opposite direction 178 | if(time === undefined) return undefined; 179 | 180 | //divide time by c 181 | time /= c; 182 | 183 | //warning : fix imprecision in time calculation due to computation 184 | time -= time * this.ZERO; 185 | 186 | return time; 187 | 188 | }, 189 | 190 | timeCollisionBallPlane: function( ball, plane ) { 191 | 192 | //dot product between the normal of the plan and the ball direction 193 | var dot = ball.velocity.dot( plane.normal ); 194 | 195 | /* 196 | * if dot is near ZERO, we consider it value as undefined 197 | * because time collision calculation would be a to high number 198 | * 199 | * if dot > ZERO, it means the ball go to the opposite direction of the plan, there is no collision 200 | */ 201 | 202 | if ( -this.ZERO < dot ) return undefined; 203 | 204 | //here, we calculate time that is the collision between the surface of the ball and the plan 205 | //we calculate the point on the ball surface that is most near the plan 206 | var surfacePosition = new THREE.Vector3(); 207 | surfacePosition.sub(ball.position, plane.normal.clone().multiplyScalar(ball.radius)); 208 | 209 | //then we calculate the vector to the collision 210 | var deltaPos = new THREE.Vector3(); 211 | deltaPos.sub(plane.position, surfacePosition); 212 | 213 | //seek collision time between the ball and the plan 214 | var time = ( plane.normal.dot( deltaPos ) ) / dot; 215 | 216 | return time; 217 | 218 | }, 219 | 220 | collideBallBall: function( ballI, ballJ ) { 221 | 222 | //relative velocity 223 | var v = new THREE.Vector3(); 224 | v.sub(ballI.velocity, ballJ.velocity); 225 | 226 | //normal direction vector 227 | var k = new THREE.Vector3(); 228 | k.sub(ballI.position, ballJ.position).normalize(); 229 | 230 | //var factor 231 | var a = 2 / (1 / ballI.weight + 1 / ballJ.weight) * k.dot(v); 232 | 233 | //apply new velocities 234 | ballI.velocity.sub(ballI.velocity, k.clone().multiplyScalar(a/ballI.weight)); 235 | ballJ.velocity.add(ballJ.velocity, k.clone().multiplyScalar(a/ballJ.weight)); 236 | 237 | }, 238 | 239 | collideBallPlane: function( ball, plane ) { 240 | 241 | var dot = 2 * ball.velocity.dot( plane.normal ); 242 | var k = plane.normal.clone(); 243 | ball.velocity.sub( ball.velocity, k.multiplyScalar( dot ) ); 244 | 245 | } 246 | }; 247 | -------------------------------------------------------------------------------- /choc3d/utils/compiler/README: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2009 The Closure Compiler Authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | // 18 | // Contents 19 | // 20 | 21 | The Closure Compiler performs checking, instrumentation, and 22 | optimizations on JavaScript code. The purpose of this README is to 23 | explain how to build and run the Closure Compiler. 24 | 25 | The Closure Compiler requires Java 6 or higher. 26 | http://www.java.com/ 27 | 28 | 29 | // 30 | // Building The Closure Compiler 31 | // 32 | 33 | There are three ways to get a Closure Compiler executable. 34 | 35 | 1) Use one we built for you. 36 | 37 | Pre-built Closure binaries can be found at 38 | http://code.google.com/p/closure-compiler/downloads/list 39 | 40 | 41 | 2) Check out the source and build it with Apache Ant. 42 | 43 | First, check out the full source tree of the Closure Compiler. There 44 | are instructions on how to do this at the project site. 45 | http://code.google.com/p/closure-compiler/source/checkout 46 | 47 | Apache Ant is a cross-platform build tool. 48 | http://ant.apache.org/ 49 | 50 | At the root of the source tree, there is an Ant file named 51 | build.xml. To use it, navigate to the same directory and type the 52 | command 53 | 54 | ant jar 55 | 56 | This will produce a jar file called "build/compiler.jar". 57 | 58 | 59 | 3) Check out the source and build it with Eclipse. 60 | 61 | Eclipse is a cross-platform IDE. 62 | http://www.eclipse.org/ 63 | 64 | Under Eclipse's File menu, click "New > Project ..." and create a 65 | "Java Project." You will see an options screen. Give the project a 66 | name, select "Create project from existing source," and choose the 67 | root of the checked-out source tree as the existing directory. Verify 68 | that you are using JRE version 6 or higher. 69 | 70 | Eclipse can use the build.xml file to discover rules. When you 71 | navigate to the build.xml file, you will see all the build rules in 72 | the "Outline" pane. Run the "jar" rule to build the compiler in 73 | build/compiler.jar. 74 | 75 | 76 | // 77 | // Running The Closure Compiler 78 | // 79 | 80 | Once you have the jar binary, running the Closure Compiler is straightforward. 81 | 82 | On the command line, type 83 | 84 | java -jar compiler.jar 85 | 86 | This starts the compiler in interactive mode. Type 87 | 88 | var x = 17 + 25; 89 | 90 | then hit "Enter", then hit "Ctrl-Z" (on Windows) or "Ctrl-D" (on Mac or Linux) 91 | and "Enter" again. The Compiler will respond: 92 | 93 | var x=42; 94 | 95 | The Closure Compiler has many options for reading input from a file, 96 | writing output to a file, checking your code, and running 97 | optimizations. To learn more, type 98 | 99 | java -jar compiler.jar --help 100 | 101 | You can read more detailed documentation about the many flags at 102 | http://code.google.com/closure/compiler/docs/gettingstarted_app.html 103 | 104 | 105 | // 106 | // Compiling Multiple Scripts 107 | // 108 | 109 | If you have multiple scripts, you should compile them all together with 110 | one compile command. 111 | 112 | java -jar compiler.jar --js=in1.js --js=in2.js ... --js_output_file=out.js 113 | 114 | The Closure Compiler will concatenate the files in the order they're 115 | passed at the command line. 116 | 117 | If you need to compile many, many scripts together, you may start to 118 | run into problems with managing dependencies between scripts. You 119 | should check out the Closure Library. It contains functions for 120 | enforcing dependencies between scripts, and a tool called calcdeps.py 121 | that knows how to give scripts to the Closure Compiler in the right 122 | order. 123 | 124 | http://code.google.com/p/closure-library/ 125 | 126 | // 127 | // Licensing 128 | // 129 | 130 | Unless otherwise stated, all source files are licensed under 131 | the Apache License, Version 2.0. 132 | 133 | 134 | ----- 135 | Code under: 136 | src/com/google/javascript/rhino 137 | test/com/google/javascript/rhino 138 | 139 | URL: http://www.mozilla.org/rhino 140 | Version: 1.5R3, with heavy modifications 141 | License: Netscape Public License and MPL / GPL dual license 142 | 143 | Description: A partial copy of Mozilla Rhino. Mozilla Rhino is an 144 | implementation of JavaScript for the JVM. The JavaScript parser and 145 | the parse tree data structures were extracted and modified 146 | significantly for use by Google's JavaScript compiler. 147 | 148 | Local Modifications: The packages have been renamespaced. All code not 149 | relevant to parsing has been removed. A JsDoc parser and static typing 150 | system have been added. 151 | 152 | 153 | ----- 154 | Code in: 155 | lib/rhino 156 | 157 | Rhino 158 | URL: http://www.mozilla.org/rhino 159 | Version: Trunk 160 | License: Netscape Public License and MPL / GPL dual license 161 | 162 | Description: Mozilla Rhino is an implementation of JavaScript for the JVM. 163 | 164 | Local Modifications: Minor changes to parsing JSDoc that usually get pushed 165 | up-stream to Rhino trunk. 166 | 167 | 168 | ----- 169 | Code in: 170 | lib/args4j.jar 171 | 172 | Args4j 173 | URL: https://args4j.dev.java.net/ 174 | Version: 2.0.12 175 | License: MIT 176 | 177 | Description: 178 | args4j is a small Java class library that makes it easy to parse command line 179 | options/arguments in your CUI application. 180 | 181 | Local Modifications: None. 182 | 183 | 184 | ----- 185 | Code in: 186 | lib/guava.jar 187 | 188 | Guava Libraries 189 | URL: http://code.google.com/p/guava-libraries/ 190 | Version: r08 191 | License: Apache License 2.0 192 | 193 | Description: Google's core Java libraries. 194 | 195 | Local Modifications: None. 196 | 197 | 198 | ----- 199 | Code in: 200 | lib/jsr305.jar 201 | 202 | Annotations for software defect detection 203 | URL: http://code.google.com/p/jsr-305/ 204 | Version: svn revision 47 205 | License: BSD License 206 | 207 | Description: Annotations for software defect detection. 208 | 209 | Local Modifications: None. 210 | 211 | 212 | ----- 213 | Code in: 214 | lib/jarjar.jar 215 | 216 | Jar Jar Links 217 | URL: http://jarjar.googlecode.com/ 218 | Version: 1.1 219 | License: Apache License 2.0 220 | 221 | Description: 222 | A utility for repackaging Java libraries. 223 | 224 | Local Modifications: None. 225 | 226 | 227 | ---- 228 | Code in: 229 | lib/junit.jar 230 | 231 | JUnit 232 | URL: http://sourceforge.net/projects/junit/ 233 | Version: 4.8.2 234 | License: Common Public License 1.0 235 | 236 | Description: A framework for writing and running automated tests in Java. 237 | 238 | Local Modifications: None. 239 | 240 | 241 | --- 242 | Code in: 243 | lib/protobuf-java.jar 244 | 245 | Protocol Buffers 246 | URL: http://code.google.com/p/protobuf/ 247 | Version: 2.3.0 248 | License: New BSD License 249 | 250 | Description: Supporting libraries for protocol buffers, 251 | an encoding of structured data. 252 | 253 | Local Modifications: None 254 | 255 | 256 | --- 257 | Code in: 258 | lib/ant.jar 259 | lib/ant-launcher.jar 260 | 261 | URL: http://ant.apache.org/bindownload.cgi 262 | Version: 1.8.1 263 | License: Apache License 2.0 264 | Description: 265 | Ant is a Java based build tool. In theory it is kind of like "make" 266 | without make's wrinkles and with the full portability of pure java code. 267 | 268 | Local Modifications: None 269 | 270 | 271 | --- 272 | Code in: 273 | lib/json.jar 274 | URL: http://json.org/java/index.html 275 | Version: JSON version 20090211 276 | License: MIT license 277 | Description: 278 | JSON is a set of java files for use in transmitting data in JSON format. 279 | 280 | Local Modifications: None 281 | 282 | --- 283 | Code in: 284 | tools/maven-ant-tasks-2.1.1.jar 285 | URL: http://maven.apache.org 286 | Version 2.1.1 287 | License: Apache License 2.0 288 | Description: 289 | Maven Ant tasks are used to manage dependencies and to install/deploy to 290 | maven repositories. 291 | 292 | Local Modifications: None 293 | -------------------------------------------------------------------------------- /choc3d/utils/compiler/COPYING: -------------------------------------------------------------------------------- 1 | 2 | Apache License 3 | Version 2.0, January 2004 4 | http://www.apache.org/licenses/ 5 | 6 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 7 | 8 | 1. Definitions. 9 | 10 | "License" shall mean the terms and conditions for use, reproduction, 11 | and distribution as defined by Sections 1 through 9 of this document. 12 | 13 | "Licensor" shall mean the copyright owner or entity authorized by 14 | the copyright owner that is granting the License. 15 | 16 | "Legal Entity" shall mean the union of the acting entity and all 17 | other entities that control, are controlled by, or are under common 18 | control with that entity. For the purposes of this definition, 19 | "control" means (i) the power, direct or indirect, to cause the 20 | direction or management of such entity, whether by contract or 21 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 22 | outstanding shares, or (iii) beneficial ownership of such entity. 23 | 24 | "You" (or "Your") shall mean an individual or Legal Entity 25 | exercising permissions granted by this License. 26 | 27 | "Source" form shall mean the preferred form for making modifications, 28 | including but not limited to software source code, documentation 29 | source, and configuration files. 30 | 31 | "Object" form shall mean any form resulting from mechanical 32 | transformation or translation of a Source form, including but 33 | not limited to compiled object code, generated documentation, 34 | and conversions to other media types. 35 | 36 | "Work" shall mean the work of authorship, whether in Source or 37 | Object form, made available under the License, as indicated by a 38 | copyright notice that is included in or attached to the work 39 | (an example is provided in the Appendix below). 40 | 41 | "Derivative Works" shall mean any work, whether in Source or Object 42 | form, that is based on (or derived from) the Work and for which the 43 | editorial revisions, annotations, elaborations, or other modifications 44 | represent, as a whole, an original work of authorship. For the purposes 45 | of this License, Derivative Works shall not include works that remain 46 | separable from, or merely link (or bind by name) to the interfaces of, 47 | the Work and Derivative Works thereof. 48 | 49 | "Contribution" shall mean any work of authorship, including 50 | the original version of the Work and any modifications or additions 51 | to that Work or Derivative Works thereof, that is intentionally 52 | submitted to Licensor for inclusion in the Work by the copyright owner 53 | or by an individual or Legal Entity authorized to submit on behalf of 54 | the copyright owner. For the purposes of this definition, "submitted" 55 | means any form of electronic, verbal, or written communication sent 56 | to the Licensor or its representatives, including but not limited to 57 | communication on electronic mailing lists, source code control systems, 58 | and issue tracking systems that are managed by, or on behalf of, the 59 | Licensor for the purpose of discussing and improving the Work, but 60 | excluding communication that is conspicuously marked or otherwise 61 | designated in writing by the copyright owner as "Not a Contribution." 62 | 63 | "Contributor" shall mean Licensor and any individual or Legal Entity 64 | on behalf of whom a Contribution has been received by Licensor and 65 | subsequently incorporated within the Work. 66 | 67 | 2. Grant of Copyright License. Subject to the terms and conditions of 68 | this License, each Contributor hereby grants to You a perpetual, 69 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 70 | copyright license to reproduce, prepare Derivative Works of, 71 | publicly display, publicly perform, sublicense, and distribute the 72 | Work and such Derivative Works in Source or Object form. 73 | 74 | 3. Grant of Patent License. Subject to the terms and conditions of 75 | this License, each Contributor hereby grants to You a perpetual, 76 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 77 | (except as stated in this section) patent license to make, have made, 78 | use, offer to sell, sell, import, and otherwise transfer the Work, 79 | where such license applies only to those patent claims licensable 80 | by such Contributor that are necessarily infringed by their 81 | Contribution(s) alone or by combination of their Contribution(s) 82 | with the Work to which such Contribution(s) was submitted. If You 83 | institute patent litigation against any entity (including a 84 | cross-claim or counterclaim in a lawsuit) alleging that the Work 85 | or a Contribution incorporated within the Work constitutes direct 86 | or contributory patent infringement, then any patent licenses 87 | granted to You under this License for that Work shall terminate 88 | as of the date such litigation is filed. 89 | 90 | 4. Redistribution. You may reproduce and distribute copies of the 91 | Work or Derivative Works thereof in any medium, with or without 92 | modifications, and in Source or Object form, provided that You 93 | meet the following conditions: 94 | 95 | (a) You must give any other recipients of the Work or 96 | Derivative Works a copy of this License; and 97 | 98 | (b) You must cause any modified files to carry prominent notices 99 | stating that You changed the files; and 100 | 101 | (c) You must retain, in the Source form of any Derivative Works 102 | that You distribute, all copyright, patent, trademark, and 103 | attribution notices from the Source form of the Work, 104 | excluding those notices that do not pertain to any part of 105 | the Derivative Works; and 106 | 107 | (d) If the Work includes a "NOTICE" text file as part of its 108 | distribution, then any Derivative Works that You distribute must 109 | include a readable copy of the attribution notices contained 110 | within such NOTICE file, excluding those notices that do not 111 | pertain to any part of the Derivative Works, in at least one 112 | of the following places: within a NOTICE text file distributed 113 | as part of the Derivative Works; within the Source form or 114 | documentation, if provided along with the Derivative Works; or, 115 | within a display generated by the Derivative Works, if and 116 | wherever such third-party notices normally appear. The contents 117 | of the NOTICE file are for informational purposes only and 118 | do not modify the License. You may add Your own attribution 119 | notices within Derivative Works that You distribute, alongside 120 | or as an addendum to the NOTICE text from the Work, provided 121 | that such additional attribution notices cannot be construed 122 | as modifying the License. 123 | 124 | You may add Your own copyright statement to Your modifications and 125 | may provide additional or different license terms and conditions 126 | for use, reproduction, or distribution of Your modifications, or 127 | for any such Derivative Works as a whole, provided Your use, 128 | reproduction, and distribution of the Work otherwise complies with 129 | the conditions stated in this License. 130 | 131 | 5. Submission of Contributions. Unless You explicitly state otherwise, 132 | any Contribution intentionally submitted for inclusion in the Work 133 | by You to the Licensor shall be under the terms and conditions of 134 | this License, without any additional terms or conditions. 135 | Notwithstanding the above, nothing herein shall supersede or modify 136 | the terms of any separate license agreement you may have executed 137 | with Licensor regarding such Contributions. 138 | 139 | 6. Trademarks. This License does not grant permission to use the trade 140 | names, trademarks, service marks, or product names of the Licensor, 141 | except as required for reasonable and customary use in describing the 142 | origin of the Work and reproducing the content of the NOTICE file. 143 | 144 | 7. Disclaimer of Warranty. Unless required by applicable law or 145 | agreed to in writing, Licensor provides the Work (and each 146 | Contributor provides its Contributions) on an "AS IS" BASIS, 147 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 148 | implied, including, without limitation, any warranties or conditions 149 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 150 | PARTICULAR PURPOSE. You are solely responsible for determining the 151 | appropriateness of using or redistributing the Work and assume any 152 | risks associated with Your exercise of permissions under this License. 153 | 154 | 8. Limitation of Liability. In no event and under no legal theory, 155 | whether in tort (including negligence), contract, or otherwise, 156 | unless required by applicable law (such as deliberate and grossly 157 | negligent acts) or agreed to in writing, shall any Contributor be 158 | liable to You for damages, including any direct, indirect, special, 159 | incidental, or consequential damages of any character arising as a 160 | result of this License or out of the use or inability to use the 161 | Work (including but not limited to damages for loss of goodwill, 162 | work stoppage, computer failure or malfunction, or any and all 163 | other commercial damages or losses), even if such Contributor 164 | has been advised of the possibility of such damages. 165 | 166 | 9. Accepting Warranty or Additional Liability. While redistributing 167 | the Work or Derivative Works thereof, You may choose to offer, 168 | and charge a fee for, acceptance of support, warranty, indemnity, 169 | or other liability obligations and/or rights consistent with this 170 | License. However, in accepting such obligations, You may act only 171 | on Your own behalf and on Your sole responsibility, not on behalf 172 | of any other Contributor, and only if You agree to indemnify, 173 | defend, and hold each Contributor harmless for any liability 174 | incurred by, or claims asserted against, such Contributor by reason 175 | of your accepting any such warranty or additional liability. 176 | 177 | END OF TERMS AND CONDITIONS 178 | 179 | APPENDIX: How to apply the Apache License to your work. 180 | 181 | To apply the Apache License to your work, attach the following 182 | boilerplate notice, with the fields enclosed by brackets "[]" 183 | replaced with your own identifying information. (Don't include 184 | the brackets!) The text should be enclosed in the appropriate 185 | comment syntax for the file format. We also recommend that a 186 | file or class name and description of purpose be included on the 187 | same "printed page" as the copyright notice for easier 188 | identification within third-party archives. 189 | 190 | Copyright [yyyy] [name of copyright owner] 191 | 192 | Licensed under the Apache License, Version 2.0 (the "License"); 193 | you may not use this file except in compliance with the License. 194 | You may obtain a copy of the License at 195 | 196 | http://www.apache.org/licenses/LICENSE-2.0 197 | 198 | Unless required by applicable law or agreed to in writing, software 199 | distributed under the License is distributed on an "AS IS" BASIS, 200 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 201 | See the License for the specific language governing permissions and 202 | limitations under the License. 203 | -------------------------------------------------------------------------------- /choc3d/build/choc3d.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @author Mathieu Ledru 3 | */ 4 | 5 | var Choc3D = Choc3D || { VERSION: '1.0' };/** 6 | * @author Mathieu Ledru 7 | */ 8 | 9 | Choc3D.App = function() { 10 | 11 | this.renderer = new THREE.WebGLRenderer(); 12 | this.scene = new Choc3D.Choc3DScene(); 13 | this.camera = new THREE.PerspectiveCamera(); 14 | this.light = new THREE.PointLight( 0xFFFFFF ); 15 | 16 | // init 17 | 18 | this.camera.position.x = 2; 19 | this.camera.position.y = 1; 20 | this.camera.position.z = 2; 21 | 22 | this.scene.add(this.camera); 23 | 24 | this.light.position.copy(this.camera.position); 25 | this.scene.add(this.light); 26 | 27 | }; 28 | 29 | Choc3D.App.prototype = { 30 | 31 | constructor: Choc3D.App, 32 | 33 | reshape: function(width, height) 34 | { 35 | var view_angle = 45, 36 | aspect = width / height, 37 | near = 0.1, 38 | far = 10000; 39 | 40 | this.camera.projectionMatrix.makePerspective(view_angle, aspect, near, far); 41 | this.renderer.setSize(width, height); 42 | }, 43 | 44 | render: function() 45 | { 46 | this.renderer.render(this.scene, this.camera); 47 | }, 48 | 49 | update: function(dt) 50 | { 51 | this.scene.update(dt); 52 | } 53 | 54 | };/** 55 | * @author Mathieu Ledru 56 | */ 57 | 58 | Choc3D.Object = function ( object3D ) { 59 | 60 | THREE.Object3D.call( this ); 61 | 62 | this.weight = 0; 63 | this.velocity = new THREE.Vector3( 0, 0, 0 ); 64 | 65 | this.add( object3D ); 66 | 67 | }; 68 | 69 | Choc3D.Object.prototype = Object.create( THREE.Object3D.prototype ); 70 | /** 71 | * @author Mathieu Ledru 72 | */ 73 | 74 | Choc3D.Ball = function ( geometry, material ) { 75 | 76 | geometry = geometry || new THREE.SphereGeometry(0.5, 16, 16); 77 | material = material || new THREE.MeshPhongMaterial( { color: Math.random() * 0xffffff } ); 78 | 79 | Choc3D.Object.call( this, new THREE.Mesh( geometry, material ) ); 80 | 81 | this.radius = 0.5; 82 | 83 | }; 84 | 85 | Choc3D.Ball.prototype = Object.create( Choc3D.Object.prototype ); 86 | /** 87 | * @author Mathieu Ledru 88 | */ 89 | 90 | Choc3D.Plane = function ( geometry, material ) { 91 | 92 | geometry = geometry || new THREE.PlaneGeometry(1, 1); 93 | material = material || new THREE.MeshPhongMaterial( { color: Math.random() * 0xffffff } ); 94 | 95 | Choc3D.Object.call( this, new THREE.Mesh( geometry, material ) ); 96 | 97 | this.normal = this.up.clone(); 98 | 99 | }; 100 | 101 | Choc3D.Plane.prototype = Object.create( Choc3D.Object.prototype ); 102 | 103 | Choc3D.Plane.prototype.lookAt = function ( vector ) { 104 | 105 | Choc3D.Object.prototype.lookAt.call( this, vector ); 106 | 107 | this.normal.sub( vector, this.position ).normalize(); 108 | 109 | };/** 110 | * @author Mathieu Ledru 111 | */ 112 | 113 | Choc3D.Physics = function () { 114 | 115 | this.ZERO = Math.pow(2, -32); //adjust imprecision 116 | 117 | this.objects = []; 118 | 119 | }; 120 | 121 | Choc3D.Physics.prototype = { 122 | 123 | constructor: Choc3D.Physics, 124 | 125 | add: function( object ) { 126 | 127 | if ( object instanceof Choc3D.Object ) { 128 | 129 | this.objects.push( object ); 130 | 131 | } 132 | 133 | }, 134 | 135 | remove: function ( object ) { 136 | 137 | var index = this.objects.indexOf( object ); 138 | 139 | if ( index !== - 1 ) { 140 | 141 | this.objects.splice( index, 1 ); 142 | 143 | } 144 | 145 | }, 146 | 147 | /** 148 | * Update objects between dt 149 | * Possible collisions are : 150 | * - Ball vs Ball 151 | * - Ball vs Plan 152 | * 153 | * @param dt 154 | */ 155 | update: function ( dt ) { 156 | 157 | var l = this.objects.length, i, j, objI, objJ, time; 158 | 159 | while ( dt > 0 ) { 160 | 161 | //search min collision time between objects 162 | var collision = { 163 | time: dt, 164 | objects: null 165 | }; 166 | 167 | for ( i = 0; i < l; i ++ ) { 168 | 169 | for ( j = i + 1; j < l; j ++ ) { 170 | 171 | objI = this.objects[ i ]; 172 | objJ = this.objects[ j ]; 173 | 174 | time = undefined; 175 | 176 | if ( objI instanceof Choc3D.Ball && objJ instanceof Choc3D.Ball ) { 177 | 178 | time = this.timeCollisionBallBall(objI, objJ); 179 | 180 | } else if ( objI instanceof Choc3D.Ball && objJ instanceof Choc3D.Plane 181 | || objI instanceof Choc3D.Plane && objJ instanceof Choc3D.Ball ) { 182 | 183 | time = this.timeCollisionBallPlane( 184 | objI instanceof Choc3D.Ball ? objI : objJ, 185 | objI instanceof Choc3D.Plane ? objI : objJ 186 | ); 187 | 188 | } 189 | 190 | if ( time !== undefined && time >= 0 && time < collision.time ) { 191 | 192 | collision = { 193 | time: time, 194 | objects: { i: i, j: j } 195 | }; 196 | 197 | } 198 | 199 | } 200 | 201 | } 202 | 203 | //apply time 204 | for ( i = 0; i < l; i ++ ) { 205 | 206 | var object = this.objects[ i ]; 207 | var v = object.velocity.clone().multiplyScalar( collision.time ); 208 | 209 | object.position.addSelf( v ); 210 | 211 | } 212 | 213 | //apply collision if found between the two objects 214 | if( collision.objects ) { 215 | 216 | objI = this.objects[ collision.objects.i ]; 217 | objJ = this.objects[ collision.objects.j ]; 218 | 219 | if ( objI instanceof Choc3D.Ball && objJ instanceof Choc3D.Ball ) { 220 | 221 | this.collideBallBall(objI, objJ); 222 | 223 | } else if ( objI instanceof Choc3D.Ball && objJ instanceof Choc3D.Plane 224 | || objI instanceof Choc3D.Plane && objJ instanceof Choc3D.Ball ) { 225 | 226 | this.collideBallPlane( 227 | objI instanceof Choc3D.Ball ? objI : objJ, 228 | objI instanceof Choc3D.Plane ? objI : objJ 229 | ); 230 | 231 | } 232 | 233 | } 234 | 235 | dt -= collision.time; 236 | 237 | } 238 | 239 | }, 240 | 241 | timeCollisionBallBall: function( ballI, ballJ ) { 242 | 243 | //relative position 244 | var p = new THREE.Vector3(); 245 | p.sub(ballJ.position, ballI.position); 246 | 247 | //relative velocity 248 | var v = new THREE.Vector3(); 249 | v.sub(ballJ.velocity, ballI.velocity); 250 | 251 | //relative square velocity 252 | var v2 = v.clone().multiplySelf(v); 253 | 254 | //kinematic viscosity 255 | var kv = p.clone().multiplySelf(v); 256 | 257 | //minimal distance between the two balls 258 | var d = ballI.radius + ballJ.radius; 259 | 260 | var c = v2.x + v2.y + v2.z; 261 | var b = 2*(kv.x*kv.y + kv.x*kv.z + kv.y*kv.z) + c * Math.pow(d, 2); 262 | b -= Math.pow(p.x, 2)*(v2.y + v2.z); 263 | b -= Math.pow(p.y, 2)*(v2.x + v2.z); 264 | b -= Math.pow(p.z, 2)*(v2.x + v2.y); 265 | 266 | //check if there is a collision (b >= 0) 267 | if(b < 0) return undefined; 268 | 269 | //then we can calculate b^(1/2) 270 | b = Math.sqrt(b); 271 | 272 | //we also can calculate a 273 | var a = -(kv.x + kv.y + kv.z); 274 | 275 | //there is a collision, we seek the min collision time >= 0 276 | //t1 and t2 are two possible time collision 277 | var time = undefined, t1, t2; 278 | 279 | t1 = a + b; 280 | if(t1 >= 0) time = t1; 281 | 282 | t2 = a - b; 283 | if(t2 >= 0 && time > t2) time = t2; 284 | 285 | //if time is undefined, then the collision is in the opposite direction 286 | if(time === undefined) return undefined; 287 | 288 | //divide time by c 289 | time /= c; 290 | 291 | //warning : fix imprecision in time calculation due to computation 292 | time -= time * this.ZERO; 293 | 294 | return time; 295 | 296 | }, 297 | 298 | timeCollisionBallPlane: function( ball, plane ) { 299 | 300 | //dot product between the normal of the plan and the ball direction 301 | var dot = ball.velocity.dot( plane.normal ); 302 | 303 | /* 304 | * if dot is near ZERO, we consider it value as undefined 305 | * because time collision calculation would be a to high number 306 | * 307 | * if dot > ZERO, it means the ball go to the opposite direction of the plan, there is no collision 308 | */ 309 | 310 | if ( -this.ZERO < dot ) return undefined; 311 | 312 | //here, we calculate time that is the collision between the surface of the ball and the plan 313 | //we calculate the point on the ball surface that is most near the plan 314 | var surfacePosition = new THREE.Vector3(); 315 | surfacePosition.sub(ball.position, plane.normal.clone().multiplyScalar(ball.radius)); 316 | 317 | //then we calculate the vector to the collision 318 | var deltaPos = new THREE.Vector3(); 319 | deltaPos.sub(plane.position, surfacePosition); 320 | 321 | //seek collision time between the ball and the plan 322 | var time = ( plane.normal.dot( deltaPos ) ) / dot; 323 | 324 | return time; 325 | 326 | }, 327 | 328 | collideBallBall: function( ballI, ballJ ) { 329 | 330 | //relative velocity 331 | var v = new THREE.Vector3(); 332 | v.sub(ballI.velocity, ballJ.velocity); 333 | 334 | //normal direction vector 335 | var k = new THREE.Vector3(); 336 | k.sub(ballI.position, ballJ.position).normalize(); 337 | 338 | //var factor 339 | var a = 2 / (1 / ballI.weight + 1 / ballJ.weight) * k.dot(v); 340 | 341 | //apply new velocities 342 | ballI.velocity.sub(ballI.velocity, k.clone().multiplyScalar(a/ballI.weight)); 343 | ballJ.velocity.add(ballJ.velocity, k.clone().multiplyScalar(a/ballJ.weight)); 344 | 345 | }, 346 | 347 | collideBallPlane: function( ball, plane ) { 348 | 349 | var dot = 2 * ball.velocity.dot( plane.normal ); 350 | var k = plane.normal.clone(); 351 | ball.velocity.sub( ball.velocity, k.multiplyScalar( dot ) ); 352 | 353 | } 354 | }; 355 | /** 356 | * @author Mathieu Ledru 357 | */ 358 | 359 | Choc3D.Choc3DScene = function ( ) { 360 | 361 | THREE.Scene.call( this ); 362 | 363 | this.physics = new Choc3D.Physics(); 364 | 365 | var scope = this; 366 | 367 | //create cube 368 | function addPlane( u, v , depth ) { 369 | var w; 370 | 371 | if ( ( u === 'x' && v === 'y' ) || ( u === 'y' && v === 'x' ) ) { 372 | 373 | w = 'z'; 374 | 375 | } else if ( ( u === 'x' && v === 'z' ) || ( u === 'z' && v === 'x' ) ) { 376 | 377 | w = 'y'; 378 | 379 | } else if ( ( u === 'z' && v === 'y' ) || ( u === 'y' && v === 'z' ) ) { 380 | 381 | w = 'x'; 382 | 383 | } 384 | 385 | var plane = new Choc3D.Plane(); 386 | plane.position[ u ] = 0; 387 | plane.position[ v ] = 0; 388 | plane.position[ w ] = 0.5 * depth; 389 | 390 | plane.lookAt(new THREE.Vector3( 0, 0, 0 )); 391 | 392 | scope.add(plane); 393 | } 394 | 395 | addPlane( 'z', 'y' , 1 ); 396 | addPlane( 'z', 'y' , - 1 ); 397 | addPlane( 'x', 'z' , 1 ); 398 | addPlane( 'x', 'z' , - 1 ); 399 | addPlane( 'x', 'y' , 1 ); 400 | addPlane( 'x', 'y' , - 1 ); 401 | 402 | //add balls 403 | for(var i = 0; i < 10; ++i) { 404 | this.addBall(); 405 | } 406 | }; 407 | 408 | Choc3D.Choc3DScene.prototype = Object.create( THREE.Scene.prototype ); 409 | 410 | Choc3D.Choc3DScene.prototype.addBall = function() { 411 | 412 | var ballVolume = function( radius ) { 413 | return (4 * Math.PI * Math.pow( radius, 3) / 3) 414 | }; 415 | 416 | var ballCanBePlaced = function( ball ) { 417 | 418 | var vector = new THREE.Vector3(); 419 | var unit = new THREE.Vector3(1, 1, 1); 420 | 421 | //check if ball is contained into Scene cube 422 | var cubeBox = new THREE.Box3(); 423 | cubeBox.setFromCenterAndSize(vector, unit); 424 | var sphereBox = new THREE.Box3(); 425 | sphereBox.setFromCenterAndSize(ball.position, unit.clone().multiplyScalar(2 * ball.radius)); 426 | if( !cubeBox.containsBox(sphereBox) ) { 427 | return false; 428 | } 429 | 430 | //check if ball do not collide with Scene balls 431 | for ( var i = 0, l = this.children.length; i < l; i ++ ) { 432 | 433 | var child = this.children[i]; 434 | if ( child instanceof Choc3D.Ball ) { 435 | 436 | vector.sub(ball.position, child.position); 437 | 438 | //if distance between positions is shorter than radius sum, then there is collision 439 | if(vector.lengthSq() <= Math.pow(ball.radius + child.radius, 2) + this.physics.ZERO) return false; 440 | 441 | } 442 | 443 | } 444 | 445 | return true; 446 | }; 447 | 448 | var ball = new Choc3D.Ball(); 449 | var radius = ball.radius; 450 | 451 | do { 452 | ball.position.x = Math.random() - 0.5; 453 | ball.position.y = Math.random() - 0.5; 454 | ball.position.z = Math.random() - 0.5; 455 | ball.velocity.x = (Math.random() - 0.5) / 1000; 456 | ball.velocity.y = (Math.random() - 0.5) / 1000; 457 | ball.velocity.z = (Math.random() - 0.5) / 1000; 458 | ball.radius = Math.random() / 25 + 0.05; 459 | 460 | //weight is proportional to the ball volume 461 | ball.weight = 2 * ballVolume.call( this, ball.radius ); 462 | 463 | } while( !ballCanBePlaced.call( this, ball ) ); 464 | 465 | ball.scale.multiplyScalar( ball.radius / radius ); 466 | 467 | this.add(ball); 468 | 469 | }; 470 | 471 | Choc3D.Choc3DScene.prototype.add = function ( object ) { 472 | 473 | THREE.Scene.prototype.add.call( this, object ); 474 | 475 | this.physics.add( object ); 476 | 477 | }; 478 | 479 | Choc3D.Choc3DScene.prototype.remove = function ( object ) { 480 | 481 | this.physics.remove( object ); 482 | 483 | THREE.Scene.prototype.remove.call( this, object ); 484 | 485 | }; 486 | 487 | Choc3D.Choc3DScene.prototype.update = function( dt ) { 488 | 489 | this.physics.update( dt ); 490 | 491 | }; 492 | -------------------------------------------------------------------------------- /js/runs/Run.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @author Mathieu Ledru 3 | */ 4 | 5 | Choc3D.Run = function(container) { 6 | 7 | THREE.EventDispatcher.call( this ); 8 | 9 | var me = this; 10 | 11 | this.container = container; 12 | 13 | this.stats = new Stats(); 14 | this.stats.domElement.style.position = 'absolute'; 15 | this.stats.domElement.style.left = '0px'; 16 | this.stats.domElement.style.top = '0px'; 17 | this.container.appendChild(this.stats.domElement); 18 | 19 | this.choc3d = new Choc3D.App(); 20 | this.container.appendChild(this.choc3d.renderer.domElement); 21 | 22 | this.control = new THREE.OrbitControls(this.choc3d.camera); 23 | this.control.autoRotate = true; 24 | this.control.minDistance = 1; 25 | this.control.maxDistance = 4; 26 | this.control.addEventListener('change', function() { 27 | me.choc3d.light.position.copy(me.choc3d.camera.position); 28 | }); 29 | 30 | this.data = { 31 | rotation: '', 32 | zoom: '', 33 | material: function() {}, 34 | background: function() { 35 | 36 | var objects = { 37 | 'none': function() { 38 | return null; 39 | }, 40 | 'sky': function() { 41 | var skyGeo = new THREE.SphereGeometry( 4000, 32, 15 ); 42 | var skyMat = new THREE.ShaderMaterial( { 43 | vertexShader: THREE.SkyShader.vertexShader, 44 | fragmentShader: THREE.SkyShader.fragmentShader, 45 | uniforms: THREE.SkyShader.uniforms, 46 | side: THREE.BackSide 47 | } ); 48 | 49 | return new THREE.Mesh( skyGeo, skyMat ); 50 | } 51 | }; 52 | 53 | var background = null; 54 | var backgrounds = {}; 55 | for(var name in objects) { 56 | backgrounds[name] = (function(name) { 57 | return function() { 58 | if(background) me.choc3d.scene.remove(background); 59 | background = objects[name](); 60 | if(background) me.choc3d.scene.add(background); 61 | }; 62 | })(name); 63 | } 64 | 65 | return backgrounds; 66 | }, 67 | ground: function() { 68 | 69 | var objects = { 70 | 'none': function() { 71 | return null; 72 | }, 73 | 'earth': function() { 74 | var groundGeo = new THREE.PlaneGeometry( 10000, 10000 ); 75 | var groundMat = new THREE.MeshPhongMaterial( { ambient: 0xffffff, color: 0xffffff, specular: 0x050505 } ); 76 | groundMat.color.setHSV( 0.095, 0.5, 1 ); 77 | 78 | var ground = new THREE.Mesh( groundGeo, groundMat ); 79 | ground.rotation.x = -Math.PI/2; 80 | ground.position.y = -0.75; 81 | 82 | return ground; 83 | } 84 | }; 85 | 86 | var ground = null; 87 | var grounds = {}; 88 | for(var name in objects) { 89 | grounds[name] = (function(name) { 90 | return function() { 91 | if(ground) me.choc3d.scene.remove(ground); 92 | ground = objects[name](); 93 | if(ground) me.choc3d.scene.add(ground); 94 | }; 95 | })(name); 96 | } 97 | 98 | return grounds; 99 | }, 100 | lights: function() { 101 | 102 | var vars = { 103 | 'disco': { 104 | 'lights':[] 105 | } 106 | }; 107 | 108 | var objects = { 109 | 'default': function() { 110 | return []; 111 | }, 112 | 'directional': function() { 113 | var directional = new THREE.DirectionalLight( 0xffffff, 0.5 ); 114 | directional.position.set( -1, 1.75, 1 ); 115 | directional.position.multiplyScalar( 50 ); 116 | 117 | return [directional]; 118 | }, 119 | 'disco': function() { 120 | vars.disco.lights.splice(0, vars.disco.lights.length); 121 | 122 | var lightList = []; 123 | var c = new THREE.Vector3(); 124 | me.choc3d.scene.traverse(function(object) { 125 | if( object instanceof Choc3D.Ball ) { 126 | var light = new THREE.PointLight( 0xffffff, 2.0, 0.5 ); 127 | 128 | c.set( Math.random(), Math.random(), Math.random() ).normalize(); 129 | light.color.setRGB( c.x, c.y, c.z ); 130 | object.traverse(function(mesh) { 131 | if( mesh instanceof THREE.Mesh && mesh.material.color instanceof THREE.Color ) { 132 | light.color.copy(mesh.material.color); 133 | } 134 | }); 135 | 136 | light.position.copy(object.position); 137 | 138 | vars.disco.lights.push({light: light, ball: object}); 139 | 140 | lightList.push(light); 141 | } 142 | }); 143 | return lightList; 144 | } 145 | }; 146 | 147 | var update = { 148 | 'default': function() { 149 | }, 150 | 'directional': function() { 151 | }, 152 | 'disco': function() { 153 | var l = vars.disco.lights.length; 154 | for(var i = 0; i < l; i++) { 155 | vars.disco.lights[i]['light'].position.copy(vars.disco.lights[i]['ball'].position); 156 | } 157 | } 158 | }; 159 | 160 | var lightList = []; 161 | var lightState = null; 162 | var lights = {}; 163 | for(var name in objects) { 164 | lights[name] = (function(name) { 165 | return function() { 166 | var i; 167 | 168 | for(i = 0; i < lightList.length; i++) { 169 | me.choc3d.scene.remove(lightList[i]); 170 | } 171 | if(lightState) { 172 | me.removeEventListener('update', update[lightState]); 173 | } 174 | 175 | lightList = objects[name](); 176 | 177 | for(i = 0; i < lightList.length; i++) { 178 | me.choc3d.scene.add(lightList[i]); 179 | } 180 | me.addEventListener('update', update[name]); 181 | 182 | //update materials 183 | me.choc3d.scene.traverse(function(object) { 184 | if( object instanceof THREE.Mesh ) { 185 | object.material.needsUpdate = true; 186 | } 187 | }); 188 | 189 | lightState = name; 190 | }; 191 | })(name); 192 | } 193 | 194 | return lights; 195 | }, 196 | particles: function() { 197 | //load particleList into the scene 198 | var particlesLength = 2000; 199 | 200 | var particleList = new THREE.Geometry(); 201 | 202 | var Pool = { 203 | __pools: [], 204 | pop: function() { 205 | if ( this.__pools.length > 0 ) { 206 | return this.__pools.pop(); 207 | } 208 | 209 | console.log( "pool ran out!" ) 210 | return null; 211 | }, 212 | push: function( v ) { 213 | this.__pools.push( v ); 214 | }, 215 | list: function() { 216 | return this.__pools; 217 | } 218 | }; 219 | 220 | for ( i = 0; i < particlesLength; i ++ ) { 221 | particleList.vertices.push( new THREE.Vector3( Number.POSITIVE_INFINITY, Number.POSITIVE_INFINITY, Number.POSITIVE_INFINITY ) ); 222 | Pool.push( i ); 223 | } 224 | 225 | var sprite = generateSprite() ; 226 | 227 | function generateSprite() { 228 | 229 | var canvas = document.createElement( 'canvas' ); 230 | canvas.width = 128; 231 | canvas.height = 128; 232 | 233 | var context = canvas.getContext( '2d' ); 234 | 235 | context.beginPath(); 236 | context.arc( 64, 64, 60, 0, Math.PI * 2, false) ; 237 | context.closePath(); 238 | 239 | context.lineWidth = 0.5; 240 | context.stroke(); 241 | context.restore(); 242 | 243 | var gradient = context.createRadialGradient( canvas.width / 2, canvas.height / 2, 0, canvas.width / 2, canvas.height / 2, canvas.width / 2 ); 244 | 245 | gradient.addColorStop( 0, 'rgba(255,255,255,1)' ); 246 | gradient.addColorStop( 0.2, 'rgba(255,255,255,1)' ); 247 | gradient.addColorStop( 0.4, 'rgba(200,200,200,1)' ); 248 | gradient.addColorStop( 1, 'rgba(0,0,0,1)' ); 249 | 250 | context.fillStyle = gradient; 251 | 252 | context.fill(); 253 | 254 | return canvas; 255 | 256 | } 257 | 258 | var texture = new THREE.Texture( sprite ); 259 | texture.needsUpdate = true; 260 | 261 | var material = new THREE.ParticleBasicMaterial({ 262 | map: texture, 263 | depthWrite: false, 264 | transparent: true 265 | }); 266 | 267 | var particleCloud = new THREE.ParticleSystem( particleList, material ); 268 | 269 | me.choc3d.scene.add(particleCloud); 270 | 271 | var objects = { 272 | 'none': function() { 273 | return []; 274 | }, 275 | 'line': function() { 276 | var sparksEmitterList = []; 277 | me.choc3d.scene.traverse(function(object) { 278 | if( object instanceof Choc3D.Ball ) { 279 | var ball = object; 280 | 281 | var counter = new SPARKS.SteadyCounter( 50 ); 282 | var sparksEmitter = new SPARKS.Emitter( counter ); 283 | 284 | sparksEmitter.addInitializer( new SPARKS.Position( new SPARKS.PointZone( ball.position ) ) ); 285 | sparksEmitter.addInitializer( new SPARKS.Lifetime( 0.5 )); 286 | sparksEmitter.addInitializer( new SPARKS.Target( null, function() { 287 | // pop an available particle from pool 288 | var target = Pool.pop(); 289 | return target; 290 | })); 291 | //sparksEmitter.addInitializer( new SPARKS.Velocity( new SPARKS.PointZone( new THREE.Vector3( 0, -5, 1 ) ) ) ); 292 | 293 | sparksEmitter.addAction( new SPARKS.Age() ); 294 | //sparksEmitter.addAction( new SPARKS.Accelerate( 0, 0.5, 0.5 ) ); 295 | sparksEmitter.addAction( new SPARKS.Move() ); 296 | //sparksEmitter.addAction( new SPARKS.RandomDrift( 1, 1, 1) ); 297 | 298 | 299 | sparksEmitter.addCallback( SPARKS.EVENT_PARTICLE_CREATED , function( p ) { 300 | p.target.position = p.position; 301 | var target = p.target; 302 | 303 | if ( target ) { 304 | particleList.vertices[ target ] = p.position; 305 | } 306 | }); 307 | sparksEmitter.addCallback( SPARKS.EVENT_PARTICLE_DEAD , function( p ) { 308 | var target = p.target; 309 | 310 | if ( target ) { 311 | // Hide the particle 312 | particleList.vertices[ target ].set( Number.POSITIVE_INFINITY, Number.POSITIVE_INFINITY, Number.POSITIVE_INFINITY ); 313 | 314 | // Mark particle system as available by returning to pool 315 | Pool.push( p.target ); 316 | } 317 | }); 318 | 319 | sparksEmitterList.push(sparksEmitter); 320 | } 321 | }); 322 | return sparksEmitterList; 323 | } 324 | }; 325 | 326 | //emitter list 327 | var sparksEmitterList = []; 328 | me.addEventListener('update', function(args) { 329 | var dt = args.dt / 1000; 330 | for(var i = 0; i < sparksEmitterList.length; i++) { 331 | sparksEmitterList[i].update(dt); 332 | } 333 | }); 334 | 335 | me.addEventListener('update', function(args) { 336 | particleCloud.geometry.verticesNeedUpdate = true; 337 | }); 338 | 339 | var particles = {}; 340 | for(var name in objects) { 341 | particles[name] = (function(name) { 342 | return function() { 343 | for(var i = 0; i < sparksEmitterList.length; i++) { 344 | for(var j = 0; j < sparksEmitterList[i]._particles.length; j++) { 345 | sparksEmitterList[i].dispatchEvent( SPARKS.EVENT_PARTICLE_DEAD, sparksEmitterList[i]._particles[j]); 346 | } 347 | } 348 | sparksEmitterList = objects[name](); 349 | }; 350 | })(name); 351 | } 352 | 353 | return particles; 354 | } 355 | }; 356 | 357 | var gui = new dat.GUI(), folder, name, property; 358 | folder = gui.addFolder('Scene'); 359 | folder.add(this.data, 'rotation').listen(); 360 | folder.add(this.data, 'zoom').listen(); 361 | folder.open(); 362 | folder = gui.addFolder('Material'); 363 | folder.add(this.data, 'material'); 364 | folder.open(); 365 | folder = gui.addFolder('Background'); 366 | var backgrounds = this.data.background(); 367 | for(name in backgrounds) { 368 | property = 'background_' + name; 369 | this.data[property] = backgrounds[name]; 370 | folder.add(this.data, property).name(name); 371 | } 372 | folder.open(); 373 | folder = gui.addFolder('Grounds'); 374 | var grounds = this.data.ground(); 375 | for(name in grounds) { 376 | property = 'ground_' + name; 377 | this.data[property] = grounds[name]; 378 | folder.add(this.data, property).name(name); 379 | } 380 | folder.open(); 381 | folder = gui.addFolder('Lights'); 382 | var lights = this.data.lights(); 383 | for(name in lights) { 384 | property = 'light_' + name; 385 | this.data[property] = lights[name]; 386 | folder.add(this.data, property).name(name); 387 | } 388 | folder.open(); 389 | /*folder = gui.addFolder('Particles'); 390 | var particles = this.data.particles(); 391 | for(name in particles) { 392 | property = 'particle_' + name; 393 | this.data[property] = particles[name]; 394 | folder.add(this.data, property).name(name); 395 | } 396 | folder.open();*/ 397 | 398 | this.reshape(); 399 | }; 400 | 401 | Choc3D.Run.prototype = Object.create( THREE.EventDispatcher.prototype ); 402 | 403 | Choc3D.Run.prototype.animate = function (dt) { 404 | 405 | this.update(dt); 406 | this.render(); 407 | 408 | }; 409 | 410 | Choc3D.Run.prototype.update = function (dt) { 411 | var zoom = new THREE.Vector3(); 412 | zoom.sub(this.control.center, this.choc3d.camera.position); 413 | this.data.rotation = this.sprintf(this.choc3d.camera.rotation); 414 | this.data.zoom = this.sprintf(zoom.length()); 415 | 416 | this.stats.update(); 417 | this.control.update(); 418 | this.choc3d.update(dt); 419 | 420 | this.dispatchEvent( {type: 'update', dt: dt} ); 421 | }; 422 | 423 | Choc3D.Run.prototype.render = function () { 424 | this.choc3d.render(); 425 | 426 | this.dispatchEvent( {type: 'render'} ); 427 | }; 428 | 429 | Choc3D.Run.prototype.reshape = function () { 430 | var width = this.container.offsetWidth; 431 | var height = this.container.offsetHeight; 432 | 433 | this.choc3d.reshape(width, height); 434 | }; 435 | 436 | Choc3D.Run.prototype.sprintf = function(data) { 437 | function round(num, dec) { 438 | return Math.round(num*Math.pow(10,dec))/Math.pow(10,dec); 439 | } 440 | 441 | if( data instanceof THREE.Vector3 ) { 442 | return '( ' + round(data.x, 2) + ', ' + round(data.y, 2) + ', ' + round(data.z, 2) + ' )'; 443 | } else if ((typeof(data) === 'number' || typeof(data) === 'string') && data !== '' && !isNaN(data)) { 444 | return round(data, 2); 445 | } 446 | 447 | return data; 448 | }; -------------------------------------------------------------------------------- /js/Sparks.js: -------------------------------------------------------------------------------- 1 | /* 2 | * @author zz85 (http://github.com/zz85 http://www.lab4games.net/zz85/blog) 3 | * 4 | * a simple to use javascript 3d particles system inspired by FliNT and Stardust 5 | * created with TWEEN.js and THREE.js 6 | * 7 | * for feature requests or bugs, please visit https://github.com/zz85/sparks.js 8 | * 9 | * licensed under the MIT license 10 | */ 11 | 12 | var SPARKS = {}; 13 | 14 | /******************************** 15 | * Emitter Class 16 | * 17 | * Creates and Manages Particles 18 | *********************************/ 19 | 20 | SPARKS.Emitter = function (counter) { 21 | 22 | this._counter = counter ? counter : new SPARKS.SteadyCounter(10); // provides number of particles to produce 23 | 24 | this._particles = []; 25 | 26 | 27 | this._initializers = []; // use for creation of particles 28 | this._actions = []; // uses action to update particles 29 | this._activities = []; // not supported yet 30 | 31 | this._handlers = []; 32 | 33 | this.callbacks = {}; 34 | }; 35 | 36 | 37 | SPARKS.Emitter.prototype = { 38 | 39 | _TIMESTEP: 15, 40 | _timer: null, 41 | _lastTime: null, 42 | _timerStep: 10, 43 | _velocityVerlet: true, 44 | 45 | // run its built in timer / stepping 46 | start: function() { 47 | this._lastTime = Date.now(); 48 | this._timer = setTimeout(this.step, this._timerStep, this); 49 | this._isRunning = true; 50 | }, 51 | 52 | stop: function() { 53 | this._isRunning = false; 54 | clearTimeout(this._timer); 55 | }, 56 | 57 | isRunning: function() { 58 | return this._isRunning & true; 59 | }, 60 | 61 | // Step gets called upon by the engine 62 | // but attempts to call update() on a regular basics 63 | // This method is also described in http://gameclosure.com/2011/04/11/deterministic-delta-tee-in-js-games/ 64 | step: function(emitter) { 65 | 66 | var time = Date.now(); 67 | var elapsed = time - emitter._lastTime; 68 | 69 | if (!this._velocityVerlet) { 70 | // if elapsed is way higher than time step, (usually after switching tabs, or excution cached in ff) 71 | // we will drop cycles. perhaps set to a limit of 10 or something? 72 | var maxBlock = emitter._TIMESTEP * 20; 73 | 74 | if (elapsed >= maxBlock) { 75 | //console.log('warning: sparks.js is fast fowarding engine, skipping steps', elapsed / emitter._TIMESTEP); 76 | //emitter.update( (elapsed - maxBlock) / 1000); 77 | elapsed = maxBlock; 78 | } 79 | 80 | while(elapsed >= emitter._TIMESTEP) { 81 | emitter.update(emitter._TIMESTEP / 1000); 82 | elapsed -= emitter._TIMESTEP; 83 | } 84 | emitter._lastTime = time - elapsed; 85 | 86 | } else { 87 | emitter.update(elapsed/1000); 88 | emitter._lastTime = time; 89 | } 90 | 91 | 92 | 93 | if (emitter._isRunning) 94 | setTimeout(emitter.step, emitter._timerStep, emitter); 95 | 96 | }, 97 | 98 | 99 | // Update particle engine in seconds, not milliseconds 100 | update: function(time) { 101 | 102 | var i, j; 103 | var len = this._counter.updateEmitter( this, time ); 104 | 105 | // Create particles 106 | for( i = 0; i < len; i++ ) { 107 | this.createParticle(); 108 | } 109 | 110 | // Update activities 111 | len = this._activities.length; 112 | for ( i = 0; i < len; i++ ) 113 | { 114 | this._activities[i].update( this, time ); 115 | } 116 | 117 | 118 | len = this._actions.length; 119 | 120 | var particle; 121 | var action; 122 | var len2 = this._particles.length; 123 | 124 | for( j = 0; j < len; j++ ) 125 | { 126 | action = this._actions[j]; 127 | for ( i = 0; i < len2; ++i ) 128 | { 129 | particle = this._particles[i]; 130 | action.update( this, particle, time ); 131 | } 132 | } 133 | 134 | 135 | // remove dead particles 136 | for ( i = len2; i--; ) 137 | { 138 | particle = this._particles[i]; 139 | if ( particle.isDead ) 140 | { 141 | //particle = 142 | this._particles.splice( i, 1 ); 143 | this.dispatchEvent("dead", particle); 144 | SPARKS.VectorPool.release(particle.position); // 145 | SPARKS.VectorPool.release(particle.velocity); 146 | 147 | } else { 148 | this.dispatchEvent("updated", particle); 149 | } 150 | } 151 | 152 | this.dispatchEvent("loopUpdated"); 153 | 154 | }, 155 | 156 | createParticle: function() { 157 | var particle = new SPARKS.Particle(); 158 | // In future, use a Particle Factory 159 | var len = this._initializers.length, i; 160 | 161 | for ( i = 0; i < len; i++ ) { 162 | this._initializers[i].initialize( this, particle ); 163 | } 164 | 165 | this._particles.push( particle ); 166 | 167 | this.dispatchEvent("created", particle); // ParticleCreated 168 | 169 | return particle; 170 | }, 171 | 172 | addInitializer: function (initializer) { 173 | this._initializers.push(initializer); 174 | }, 175 | 176 | addAction: function (action) { 177 | this._actions.push(action); 178 | }, 179 | 180 | removeInitializer: function (initializer) { 181 | var index = this._initializers.indexOf(initializer); 182 | if (index > -1) { 183 | this._initializers.splice( index, 1 ); 184 | } 185 | }, 186 | 187 | removeAction: function (action) { 188 | var index = this._actions.indexOf(action); 189 | if (index > -1) { 190 | this._actions.splice( index, 1 ); 191 | } 192 | //console.log('removeAction', index, this._actions); 193 | }, 194 | 195 | addCallback: function(name, callback) { 196 | this.callbacks[name] = callback; 197 | }, 198 | 199 | dispatchEvent: function(name, args) { 200 | var callback = this.callbacks[name]; 201 | if (callback) { 202 | callback(args); 203 | } 204 | 205 | } 206 | 207 | 208 | }; 209 | 210 | 211 | /* 212 | * Constant Names for 213 | * Events called by emitter.dispatchEvent() 214 | * 215 | */ 216 | SPARKS.EVENT_PARTICLE_CREATED = "created" 217 | SPARKS.EVENT_PARTICLE_UPDATED = "updated" 218 | SPARKS.EVENT_PARTICLE_DEAD = "dead"; 219 | SPARKS.EVENT_LOOP_UPDATED = "loopUpdated"; 220 | 221 | 222 | 223 | /* 224 | * Steady Counter attempts to produces a particle rate steadily 225 | * 226 | */ 227 | 228 | // Number of particles per seconds 229 | SPARKS.SteadyCounter = function(rate) { 230 | this.rate = rate; 231 | 232 | // we use a shortfall counter to make up for slow emitters 233 | this.leftover = 0; 234 | 235 | }; 236 | 237 | SPARKS.SteadyCounter.prototype.updateEmitter = function(emitter, time) { 238 | 239 | var targetRelease = time * this.rate + this.leftover; 240 | var actualRelease = Math.floor(targetRelease); 241 | 242 | this.leftover = targetRelease - actualRelease; 243 | 244 | return actualRelease; 245 | }; 246 | 247 | 248 | /* 249 | * Shot Counter produces specified particles 250 | * on a single impluse or burst 251 | */ 252 | 253 | SPARKS.ShotCounter = function(particles) { 254 | this.particles = particles; 255 | this.used = false; 256 | }; 257 | 258 | SPARKS.ShotCounter.prototype.updateEmitter = function(emitter, time) { 259 | 260 | if (this.used) { 261 | return 0; 262 | } else { 263 | this.used = true; 264 | } 265 | 266 | return this.particles; 267 | }; 268 | 269 | 270 | /******************************** 271 | * Particle Class 272 | * 273 | * Represents a single particle 274 | *********************************/ 275 | SPARKS.Particle = function() { 276 | 277 | /** 278 | * The lifetime of the particle, in seconds. 279 | */ 280 | this.lifetime = 0; 281 | 282 | /** 283 | * The age of the particle, in seconds. 284 | */ 285 | this.age = 0; 286 | 287 | /** 288 | * The energy of the particle. 289 | */ 290 | this.energy = 1; 291 | 292 | /** 293 | * Whether the particle is dead and should be removed from the stage. 294 | */ 295 | this.isDead = false; 296 | 297 | this.target = null; // tag 298 | 299 | /** 300 | * For 3D 301 | */ 302 | 303 | this.position = SPARKS.VectorPool.get().set(0,0,0); //new THREE.Vector3( 0, 0, 0 ); 304 | this.velocity = SPARKS.VectorPool.get().set(0,0,0); //new THREE.Vector3( 0, 0, 0 ); 305 | this._oldvelocity = SPARKS.VectorPool.get().set(0,0,0); 306 | // rotation vec3 307 | // angVelocity vec3 308 | // faceAxis vec3 309 | 310 | }; 311 | 312 | 313 | /******************************** 314 | * Action Classes 315 | * 316 | * An abstract class which have 317 | * update function 318 | *********************************/ 319 | SPARKS.Action = function() { 320 | this._priority = 0; 321 | }; 322 | 323 | 324 | SPARKS.Age = function(easing) { 325 | this._easing = (easing == null) ? TWEEN.Easing.Linear.None : easing; 326 | }; 327 | 328 | SPARKS.Age.prototype.update = function (emitter, particle, time) { 329 | particle.age += time; 330 | if( particle.age >= particle.lifetime ) 331 | { 332 | particle.energy = 0; 333 | particle.isDead = true; 334 | } 335 | else 336 | { 337 | var t = this._easing(particle.age / particle.lifetime); 338 | particle.energy = -1 * t + 1; 339 | } 340 | }; 341 | 342 | /* 343 | // Mark particle as dead when particle's < 0 344 | 345 | SPARKS.Death = function(easing) { 346 | this._easing = (easing == null) ? TWEEN.Linear.None : easing; 347 | }; 348 | 349 | SPARKS.Death.prototype.update = function (emitter, particle, time) { 350 | if (particle.life <= 0) { 351 | particle.isDead = true; 352 | } 353 | }; 354 | */ 355 | 356 | 357 | SPARKS.Move = function() { 358 | 359 | }; 360 | 361 | SPARKS.Move.prototype.update = function(emitter, particle, time) { 362 | // attempt verlet velocity updating. 363 | var p = particle.position; 364 | var v = particle.velocity; 365 | var old = particle._oldvelocity; 366 | 367 | if (this._velocityVerlet) { 368 | p.x += (v.x + old.x) * 0.5 * time; 369 | p.y += (v.y + old.y) * 0.5 * time; 370 | p.z += (v.z + old.z) * 0.5 * time; 371 | } else { 372 | p.x += v.x * time; 373 | p.y += v.y * time; 374 | p.z += v.z * time; 375 | } 376 | 377 | // OldVel = Vel; 378 | // Vel = Vel + Accel * dt; 379 | // Pos = Pos + (vel + Vel + Accel * dt) * 0.5 * dt; 380 | 381 | 382 | 383 | }; 384 | 385 | /* Marks particles found in specified zone dead */ 386 | SPARKS.DeathZone = function(zone) { 387 | this.zone = zone; 388 | }; 389 | 390 | SPARKS.DeathZone.prototype.update = function(emitter, particle, time) { 391 | 392 | if (this.zone.contains(particle.position)) { 393 | particle.isDead = true; 394 | } 395 | 396 | }; 397 | 398 | /* 399 | * SPARKS.ActionZone applies an action when particle is found in zone 400 | */ 401 | SPARKS.ActionZone = function(action, zone) { 402 | this.action = action; 403 | this.zone = zone; 404 | }; 405 | 406 | SPARKS.ActionZone.prototype.update = function(emitter, particle, time) { 407 | 408 | if (this.zone.contains(particle.position)) { 409 | this.action.update( emitter, particle, time ); 410 | } 411 | 412 | }; 413 | 414 | /* 415 | * Accelerate action affects velocity in specified 3d direction 416 | */ 417 | SPARKS.Accelerate = function(x,y,z) { 418 | 419 | if (x instanceof THREE.Vector3) { 420 | this.acceleration = x; 421 | return; 422 | } 423 | 424 | this.acceleration = new THREE.Vector3(x,y,z); 425 | 426 | }; 427 | 428 | SPARKS.Accelerate.prototype.update = function(emitter, particle, time) { 429 | var acc = this.acceleration; 430 | 431 | var v = particle.velocity; 432 | 433 | particle._oldvelocity.set(v.x, v.y, v.z); 434 | 435 | v.x += acc.x * time; 436 | v.y += acc.y * time; 437 | v.z += acc.z * time; 438 | 439 | }; 440 | 441 | /* 442 | * Accelerate Factor accelerate based on a factor of particle's velocity. 443 | */ 444 | SPARKS.AccelerateFactor = function(factor) { 445 | this.factor = factor; 446 | }; 447 | 448 | SPARKS.AccelerateFactor.prototype.update = function(emitter, particle, time) { 449 | var factor = this.factor; 450 | 451 | var v = particle.velocity; 452 | var len = v.length(); 453 | var adjFactor; 454 | if (len>0) { 455 | 456 | adjFactor = factor * time / len; 457 | adjFactor += 1; 458 | 459 | v.multiplyScalar(adjFactor); 460 | // v.x *= adjFactor; 461 | // v.y *= adjFactor; 462 | // v.z *= adjFactor; 463 | } 464 | 465 | }; 466 | 467 | /* 468 | AccelerateNormal 469 | * AccelerateVelocity affects velocity based on its velocity direction 470 | */ 471 | SPARKS.AccelerateVelocity = function(factor) { 472 | 473 | this.factor = factor; 474 | 475 | }; 476 | 477 | SPARKS.AccelerateVelocity.prototype.update = function(emitter, particle, time) { 478 | var factor = this.factor; 479 | 480 | var v = particle.velocity; 481 | 482 | 483 | v.z += - v.x * factor; 484 | v.y += v.z * factor; 485 | v.x += v.y * factor; 486 | 487 | }; 488 | 489 | 490 | /* Set the max ammount of x,y,z drift movements in a second */ 491 | SPARKS.RandomDrift = function(x,y,z) { 492 | if (x instanceof THREE.Vector3) { 493 | this.drift = x; 494 | return; 495 | } 496 | 497 | this.drift = new THREE.Vector3(x,y,z); 498 | } 499 | 500 | 501 | SPARKS.RandomDrift.prototype.update = function(emitter, particle, time) { 502 | var drift = this.drift; 503 | 504 | var v = particle.velocity; 505 | 506 | v.x += ( Math.random() - 0.5 ) * drift.x * time; 507 | v.y += ( Math.random() - 0.5 ) * drift.y * time; 508 | v.z += ( Math.random() - 0.5 ) * drift.z * time; 509 | 510 | }; 511 | 512 | /******************************** 513 | * Zone Classes 514 | * 515 | * An abstract classes which have 516 | * getLocation() function 517 | *********************************/ 518 | SPARKS.Zone = function() { 519 | }; 520 | 521 | // TODO, contains() for Zone 522 | 523 | SPARKS.PointZone = function(pos) { 524 | this.pos = pos; 525 | }; 526 | 527 | SPARKS.PointZone.prototype.getLocation = function() { 528 | return this.pos; 529 | }; 530 | 531 | SPARKS.PointZone = function(pos) { 532 | this.pos = pos; 533 | }; 534 | 535 | SPARKS.PointZone.prototype.getLocation = function() { 536 | return this.pos; 537 | }; 538 | 539 | SPARKS.LineZone = function(start, end) { 540 | this.start = start; 541 | this.end = end; 542 | this._length = end.clone().subSelf( start ); 543 | }; 544 | 545 | SPARKS.LineZone.prototype.getLocation = function() { 546 | var len = this._length.clone(); 547 | 548 | len.multiplyScalar( Math.random() ); 549 | return len.addSelf( this.start ); 550 | 551 | }; 552 | 553 | // Basically a RectangleZone 554 | SPARKS.ParallelogramZone = function(corner, side1, side2) { 555 | this.corner = corner; 556 | this.side1 = side1; 557 | this.side2 = side2; 558 | }; 559 | 560 | SPARKS.ParallelogramZone.prototype.getLocation = function() { 561 | 562 | var d1 = this.side1.clone().multiplyScalar( Math.random() ); 563 | var d2 = this.side2.clone().multiplyScalar( Math.random() ); 564 | d1.addSelf(d2); 565 | return d1.addSelf( this.corner ); 566 | 567 | }; 568 | 569 | SPARKS.CubeZone = function(position, x, y, z) { 570 | this.position = position; 571 | this.x = x; 572 | this.y = y; 573 | this.z = z; 574 | }; 575 | 576 | SPARKS.CubeZone.prototype.getLocation = function() { 577 | //TODO use pool? 578 | 579 | var location = this.position.clone(); 580 | location.x += Math.random() * this.x; 581 | location.y += Math.random() * this.y; 582 | location.z += Math.random() * this.z; 583 | 584 | return location; 585 | 586 | }; 587 | 588 | 589 | SPARKS.CubeZone.prototype.contains = function(position) { 590 | 591 | var startX = this.position.x; 592 | var startY = this.position.y; 593 | var startZ = this.position.z; 594 | var x = this.x; // width 595 | var y = this.y; // depth 596 | var z = this.z; // height 597 | 598 | if (x<0) { 599 | startX += x; 600 | x = Math.abs(x); 601 | } 602 | 603 | if (y<0) { 604 | startY += y; 605 | y = Math.abs(y); 606 | } 607 | 608 | if (z<0) { 609 | startZ += z; 610 | z = Math.abs(z); 611 | } 612 | 613 | var diffX = position.x - startX; 614 | var diffY = position.y - startY; 615 | var diffZ = position.z - startZ; 616 | 617 | if ( (diffX > 0) && (diffX < x) && 618 | (diffY > 0) && (diffY < y) && 619 | (diffZ > 0) && (diffZ < z) ) { 620 | return true; 621 | } 622 | 623 | return false; 624 | 625 | }; 626 | 627 | 628 | 629 | /** 630 | * The constructor creates a DiscZone 3D zone. 631 | * 632 | * @param centre The point at the center of the disc. 633 | * @param normal A vector normal to the disc. 634 | * @param outerRadius The outer radius of the disc. 635 | * @param innerRadius The inner radius of the disc. This defines the hole 636 | * in the center of the disc. If set to zero, there is no hole. 637 | */ 638 | 639 | /* 640 | // BUGGY!! 641 | SPARKS.DiscZone = function(center, radiusNormal, outerRadius, innerRadius) { 642 | this.center = center; 643 | this.radiusNormal = radiusNormal; 644 | this.outerRadius = (outerRadius==undefined) ? 0 : outerRadius; 645 | this.innerRadius = (innerRadius==undefined) ? 0 : innerRadius; 646 | 647 | }; 648 | 649 | SPARKS.DiscZone.prototype.getLocation = function() { 650 | var rand = Math.random(); 651 | var _innerRadius = this.innerRadius; 652 | var _outerRadius = this.outerRadius; 653 | var center = this.center; 654 | var _normal = this.radiusNormal; 655 | 656 | _distToOrigin = _normal.dot( center ); 657 | 658 | var radius = _innerRadius + (1 - rand * rand ) * ( _outerRadius - _innerRadius ); 659 | var angle = Math.random() * SPARKS.Utils.TWOPI; 660 | 661 | var _distToOrigin = _normal.dot( center ); 662 | var axes = SPARKS.Utils.getPerpendiculars( _normal.clone() ); 663 | var _planeAxis1 = axes[0]; 664 | var _planeAxis2 = axes[1]; 665 | 666 | var p = _planeAxis1.clone(); 667 | p.multiplyScalar( radius * Math.cos( angle ) ); 668 | var p2 = _planeAxis2.clone(); 669 | p2.multiplyScalar( radius * Math.sin( angle ) ); 670 | p.addSelf( p2 ); 671 | return _center.add( p ); 672 | 673 | }; 674 | */ 675 | 676 | SPARKS.SphereCapZone = function(x, y, z, minr, maxr, angle) { 677 | this.x = x; 678 | this.y = y; 679 | this.z = z; 680 | this.minr = minr; 681 | this.maxr = maxr; 682 | this.angle = angle; 683 | }; 684 | 685 | SPARKS.SphereCapZone.prototype.getLocation = function() { 686 | var theta = Math.PI *2 * SPARKS.Utils.random(); 687 | var r = SPARKS.Utils.random(); 688 | 689 | //new THREE.Vector3 690 | var v = SPARKS.VectorPool.get().set(r * Math.cos(theta), -1 / Math.tan(this.angle * SPARKS.Utils.DEGREE_TO_RADIAN), r * Math.sin(theta)); 691 | 692 | //v.length = StardustMath.interpolate(0, _minRadius, 1, _maxRadius, Math.random()); 693 | 694 | var i = this.minr - ((this.minr-this.maxr) * Math.random() ); 695 | v.multiplyScalar(i); 696 | 697 | v.__markedForReleased = true; 698 | 699 | return v; 700 | }; 701 | 702 | 703 | /******************************** 704 | * Initializer Classes 705 | * 706 | * Classes which initializes 707 | * particles. Implements initialize( emitter:Emitter, particle:Particle ) 708 | *********************************/ 709 | 710 | // Specifies random life between max and min 711 | SPARKS.Lifetime = function(min, max) { 712 | this._min = min; 713 | 714 | this._max = max ? max : min; 715 | 716 | }; 717 | 718 | SPARKS.Lifetime.prototype.initialize = function( emitter/*Emitter*/, particle/*Particle*/ ) { 719 | particle.lifetime = this._min + SPARKS.Utils.random() * ( this._max - this._min ); 720 | }; 721 | 722 | 723 | SPARKS.Position = function(zone) { 724 | this.zone = zone; 725 | }; 726 | 727 | SPARKS.Position.prototype.initialize = function( emitter/*Emitter*/, particle/*Particle*/ ) { 728 | var pos = this.zone.getLocation(); 729 | particle.position.set(pos.x, pos.y, pos.z); 730 | }; 731 | 732 | SPARKS.Velocity = function(zone) { 733 | this.zone = zone; 734 | }; 735 | 736 | SPARKS.Velocity.prototype.initialize = function( emitter/*Emitter*/, particle/*Particle*/ ) { 737 | var pos = this.zone.getLocation(); 738 | particle.velocity.set(pos.x, pos.y, pos.z); 739 | if (pos.__markedForReleased) { 740 | //console.log("release"); 741 | SPARKS.VectorPool.release(pos); 742 | pos.__markedForReleased = false; 743 | } 744 | }; 745 | 746 | SPARKS.Target = function(target, callback) { 747 | this.target = target; 748 | this.callback = callback; 749 | }; 750 | 751 | SPARKS.Target.prototype.initialize = function( emitter, particle ) { 752 | 753 | if (this.callback) { 754 | particle.target = this.callback(); 755 | } else { 756 | particle.target = this.target; 757 | } 758 | 759 | }; 760 | 761 | /******************************** 762 | * VectorPool 763 | * 764 | * Reuse much of Vectors if possible 765 | *********************************/ 766 | 767 | SPARKS.VectorPool = { 768 | __pools: [], 769 | 770 | // Get a new Vector 771 | get: function() { 772 | if (this.__pools.length>0) { 773 | return this.__pools.pop(); 774 | } 775 | 776 | return this._addToPool(); 777 | 778 | }, 779 | 780 | // Release a vector back into the pool 781 | release: function(v) { 782 | this.__pools.push(v); 783 | }, 784 | 785 | // Create a bunch of vectors and add to the pool 786 | _addToPool: function() { 787 | //console.log("creating some pools"); 788 | 789 | for (var i=0, size = 100; i < size; i++) { 790 | this.__pools.push(new THREE.Vector3()); 791 | } 792 | 793 | return new THREE.Vector3(); 794 | 795 | } 796 | 797 | 798 | 799 | }; 800 | 801 | 802 | /******************************** 803 | * Util Classes 804 | * 805 | * Classes which initializes 806 | * particles. Implements initialize( emitter:Emitter, particle:Particle ) 807 | *********************************/ 808 | SPARKS.Utils = { 809 | random: function() { 810 | return Math.random(); 811 | }, 812 | DEGREE_TO_RADIAN: Math.PI / 180, 813 | TWOPI: Math.PI * 2, 814 | 815 | getPerpendiculars: function(normal) { 816 | var p1 = this.getPerpendicular( normal ); 817 | var p2 = normal.cross( p1 ); 818 | p2.normalize(); 819 | return [ p1, p2 ]; 820 | }, 821 | 822 | getPerpendicular: function( v ) 823 | { 824 | if( v.x == 0 ) 825 | { 826 | return new THREE.Vector3D( 1, 0, 0 ); 827 | } 828 | else 829 | { 830 | var temp = new THREE.Vector3( v.y, -v.x, 0 ); 831 | return temp.normalize(); 832 | } 833 | } 834 | 835 | }; 836 | -------------------------------------------------------------------------------- /js/libs/dat.gui.min.js: -------------------------------------------------------------------------------- 1 | /** 2 | * dat-gui JavaScript Controller Library 3 | * http://code.google.com/p/dat-gui 4 | * 5 | * Copyright 2011 Data Arts Team, Google Creative Lab 6 | * 7 | * Licensed under the Apache License, Version 2.0 (the "License"); 8 | * you may not use this file except in compliance with the License. 9 | * You may obtain a copy of the License at 10 | * 11 | * http://www.apache.org/licenses/LICENSE-2.0 12 | */ 13 | var dat=dat||{};dat.gui=dat.gui||{};dat.utils=dat.utils||{};dat.controllers=dat.controllers||{};dat.dom=dat.dom||{};dat.color=dat.color||{};dat.utils.css=function(){return{load:function(e,a){var a=a||document,c=a.createElement("link");c.type="text/css";c.rel="stylesheet";c.href=e;a.getElementsByTagName("head")[0].appendChild(c)},inject:function(e,a){var a=a||document,c=document.createElement("style");c.type="text/css";c.innerHTML=e;a.getElementsByTagName("head")[0].appendChild(c)}}}(); 14 | dat.utils.common=function(){var e=Array.prototype.forEach,a=Array.prototype.slice;return{BREAK:{},extend:function(c){this.each(a.call(arguments,1),function(a){for(var f in a)this.isUndefined(a[f])||(c[f]=a[f])},this);return c},defaults:function(c){this.each(a.call(arguments,1),function(a){for(var f in a)this.isUndefined(c[f])&&(c[f]=a[f])},this);return c},compose:function(){var c=a.call(arguments);return function(){for(var d=a.call(arguments),f=c.length-1;f>=0;f--)d=[c[f].apply(this,d)];return d[0]}}, 15 | each:function(a,d,f){if(e&&a.forEach===e)a.forEach(d,f);else if(a.length===a.length+0)for(var b=0,n=a.length;b-1?d.length-d.indexOf(".")-1:0};c.superclass=e;a.extend(c.prototype,e.prototype,{setValue:function(a){if(this.__min!==void 0&&athis.__max)a=this.__max;this.__step!==void 0&&a%this.__step!=0&&(a=Math.round(a/this.__step)*this.__step);return c.superclass.prototype.setValue.call(this,a)},min:function(a){this.__min=a;return this},max:function(a){this.__max=a;return this},step:function(a){this.__step=a;return this}});return c}(dat.controllers.Controller,dat.utils.common); 29 | dat.controllers.NumberControllerBox=function(e,a,c){var d=function(f,b,e){function h(){var a=parseFloat(l.__input.value);c.isNaN(a)||l.setValue(a)}function j(a){var b=o-a.clientY;l.setValue(l.getValue()+b*l.__impliedStep);o=a.clientY}function m(){a.unbind(window,"mousemove",j);a.unbind(window,"mouseup",m)}this.__truncationSuspended=false;d.superclass.call(this,f,b,e);var l=this,o;this.__input=document.createElement("input");this.__input.setAttribute("type","text");a.bind(this.__input,"change",h); 30 | a.bind(this.__input,"blur",function(){h();l.__onFinishChange&&l.__onFinishChange.call(l,l.getValue())});a.bind(this.__input,"mousedown",function(b){a.bind(window,"mousemove",j);a.bind(window,"mouseup",m);o=b.clientY});a.bind(this.__input,"keydown",function(a){if(a.keyCode===13)l.__truncationSuspended=true,this.blur(),l.__truncationSuspended=false});this.updateDisplay();this.domElement.appendChild(this.__input)};d.superclass=e;c.extend(d.prototype,e.prototype,{updateDisplay:function(){var a=this.__input, 31 | b;if(this.__truncationSuspended)b=this.getValue();else{b=this.getValue();var c=Math.pow(10,this.__precision);b=Math.round(b*c)/c}a.value=b;return d.superclass.prototype.updateDisplay.call(this)}});return d}(dat.controllers.NumberController,dat.dom.dom,dat.utils.common); 32 | dat.controllers.NumberControllerSlider=function(e,a,c,d,f){var b=function(d,c,f,e,l){function o(b){b.preventDefault();var d=a.getOffset(g.__background),c=a.getWidth(g.__background);g.setValue(g.__min+(g.__max-g.__min)*((b.clientX-d.left)/(d.left+c-d.left)));return false}function y(){a.unbind(window,"mousemove",o);a.unbind(window,"mouseup",y);g.__onFinishChange&&g.__onFinishChange.call(g,g.getValue())}b.superclass.call(this,d,c,{min:f,max:e,step:l});var g=this;this.__background=document.createElement("div"); 33 | this.__foreground=document.createElement("div");a.bind(this.__background,"mousedown",function(b){a.bind(window,"mousemove",o);a.bind(window,"mouseup",y);o(b)});a.addClass(this.__background,"slider");a.addClass(this.__foreground,"slider-fg");this.updateDisplay();this.__background.appendChild(this.__foreground);this.domElement.appendChild(this.__background)};b.superclass=e;b.useDefaultStyles=function(){c.inject(f)};d.extend(b.prototype,e.prototype,{updateDisplay:function(){this.__foreground.style.width= 34 | (this.getValue()-this.__min)/(this.__max-this.__min)*100+"%";return b.superclass.prototype.updateDisplay.call(this)}});return b}(dat.controllers.NumberController,dat.dom.dom,dat.utils.css,dat.utils.common,".slider {\n box-shadow: inset 0 2px 4px rgba(0,0,0,0.15);\n height: 1em;\n border-radius: 1em;\n background-color: #eee;\n padding: 0 0.5em;\n overflow: hidden;\n}\n\n.slider-fg {\n padding: 1px 0 2px 0;\n background-color: #aaa;\n height: 1em;\n margin-left: -0.5em;\n padding-right: 0.5em;\n border-radius: 1em 0 0 1em;\n}\n\n.slider-fg:after {\n display: inline-block;\n border-radius: 1em;\n background-color: #fff;\n border: 1px solid #aaa;\n content: '';\n float: right;\n margin-right: -1em;\n margin-top: -1px;\n height: 0.9em;\n width: 0.9em;\n}"); 35 | dat.controllers.FunctionController=function(e,a,c){var d=function(c,b,e){d.superclass.call(this,c,b);var h=this;this.__button=document.createElement("div");this.__button.innerHTML=e===void 0?"Fire":e;a.bind(this.__button,"click",function(a){a.preventDefault();h.fire();return false});a.addClass(this.__button,"button");this.domElement.appendChild(this.__button)};d.superclass=e;c.extend(d.prototype,e.prototype,{fire:function(){this.__onChange&&this.__onChange.call(this);this.__onFinishChange&&this.__onFinishChange.call(this, 36 | this.getValue());this.getValue().call(this.object)}});return d}(dat.controllers.Controller,dat.dom.dom,dat.utils.common); 37 | dat.controllers.BooleanController=function(e,a,c){var d=function(c,b){d.superclass.call(this,c,b);var e=this;this.__prev=this.getValue();this.__checkbox=document.createElement("input");this.__checkbox.setAttribute("type","checkbox");a.bind(this.__checkbox,"change",function(){e.setValue(!e.__prev)},false);this.domElement.appendChild(this.__checkbox);this.updateDisplay()};d.superclass=e;c.extend(d.prototype,e.prototype,{setValue:function(a){a=d.superclass.prototype.setValue.call(this,a);this.__onFinishChange&& 38 | this.__onFinishChange.call(this,this.getValue());this.__prev=this.getValue();return a},updateDisplay:function(){this.getValue()===true?(this.__checkbox.setAttribute("checked","checked"),this.__checkbox.checked=true):this.__checkbox.checked=false;return d.superclass.prototype.updateDisplay.call(this)}});return d}(dat.controllers.Controller,dat.dom.dom,dat.utils.common); 39 | dat.color.toString=function(e){return function(a){if(a.a==1||e.isUndefined(a.a)){for(a=a.hex.toString(16);a.length<6;)a="0"+a;return"#"+a}else return"rgba("+Math.round(a.r)+","+Math.round(a.g)+","+Math.round(a.b)+","+a.a+")"}}(dat.utils.common); 40 | dat.color.interpret=function(e,a){var c,d,f=[{litmus:a.isString,conversions:{THREE_CHAR_HEX:{read:function(a){a=a.match(/^#([A-F0-9])([A-F0-9])([A-F0-9])$/i);return a===null?false:{space:"HEX",hex:parseInt("0x"+a[1].toString()+a[1].toString()+a[2].toString()+a[2].toString()+a[3].toString()+a[3].toString())}},write:e},SIX_CHAR_HEX:{read:function(a){a=a.match(/^#([A-F0-9]{6})$/i);return a===null?false:{space:"HEX",hex:parseInt("0x"+a[1].toString())}},write:e},CSS_RGB:{read:function(a){a=a.match(/^rgb\(\s*(.+)\s*,\s*(.+)\s*,\s*(.+)\s*\)/); 41 | return a===null?false:{space:"RGB",r:parseFloat(a[1]),g:parseFloat(a[2]),b:parseFloat(a[3])}},write:e},CSS_RGBA:{read:function(a){a=a.match(/^rgba\(\s*(.+)\s*,\s*(.+)\s*,\s*(.+)\s*\,\s*(.+)\s*\)/);return a===null?false:{space:"RGB",r:parseFloat(a[1]),g:parseFloat(a[2]),b:parseFloat(a[3]),a:parseFloat(a[4])}},write:e}}},{litmus:a.isNumber,conversions:{HEX:{read:function(a){return{space:"HEX",hex:a,conversionName:"HEX"}},write:function(a){return a.hex}}}},{litmus:a.isArray,conversions:{RGB_ARRAY:{read:function(a){return a.length!= 42 | 3?false:{space:"RGB",r:a[0],g:a[1],b:a[2]}},write:function(a){return[a.r,a.g,a.b]}},RGBA_ARRAY:{read:function(a){return a.length!=4?false:{space:"RGB",r:a[0],g:a[1],b:a[2],a:a[3]}},write:function(a){return[a.r,a.g,a.b,a.a]}}}},{litmus:a.isObject,conversions:{RGBA_OBJ:{read:function(b){return a.isNumber(b.r)&&a.isNumber(b.g)&&a.isNumber(b.b)&&a.isNumber(b.a)?{space:"RGB",r:b.r,g:b.g,b:b.b,a:b.a}:false},write:function(a){return{r:a.r,g:a.g,b:a.b,a:a.a}}},RGB_OBJ:{read:function(b){return a.isNumber(b.r)&& 43 | a.isNumber(b.g)&&a.isNumber(b.b)?{space:"RGB",r:b.r,g:b.g,b:b.b}:false},write:function(a){return{r:a.r,g:a.g,b:a.b}}},HSVA_OBJ:{read:function(b){return a.isNumber(b.h)&&a.isNumber(b.s)&&a.isNumber(b.v)&&a.isNumber(b.a)?{space:"HSV",h:b.h,s:b.s,v:b.v,a:b.a}:false},write:function(a){return{h:a.h,s:a.s,v:a.v,a:a.a}}},HSV_OBJ:{read:function(b){return a.isNumber(b.h)&&a.isNumber(b.s)&&a.isNumber(b.v)?{space:"HSV",h:b.h,s:b.s,v:b.v}:false},write:function(a){return{h:a.h,s:a.s,v:a.v}}}}}];return function(){d= 44 | false;var b=arguments.length>1?a.toArray(arguments):arguments[0];a.each(f,function(e){if(e.litmus(b))return a.each(e.conversions,function(e,f){c=e.read(b);if(d===false&&c!==false)return d=c,c.conversionName=f,c.conversion=e,a.BREAK}),a.BREAK});return d}}(dat.color.toString,dat.utils.common); 45 | dat.GUI=dat.gui.GUI=function(e,a,c,d,f,b,n,h,j,m,l,o,y,g,i){function q(a,b,r,c){if(b[r]===void 0)throw Error("Object "+b+' has no property "'+r+'"');c.color?b=new l(b,r):(b=[b,r].concat(c.factoryArgs),b=d.apply(a,b));if(c.before instanceof f)c.before=c.before.__li;t(a,b);g.addClass(b.domElement,"c");r=document.createElement("span");g.addClass(r,"property-name");r.innerHTML=b.property;var e=document.createElement("div");e.appendChild(r);e.appendChild(b.domElement);c=s(a,e,c.before);g.addClass(c,k.CLASS_CONTROLLER_ROW); 46 | g.addClass(c,typeof b.getValue());p(a,c,b);a.__controllers.push(b);return b}function s(a,b,d){var c=document.createElement("li");b&&c.appendChild(b);d?a.__ul.insertBefore(c,params.before):a.__ul.appendChild(c);a.onResize();return c}function p(a,d,c){c.__li=d;c.__gui=a;i.extend(c,{options:function(b){if(arguments.length>1)return c.remove(),q(a,c.object,c.property,{before:c.__li.nextElementSibling,factoryArgs:[i.toArray(arguments)]});if(i.isArray(b)||i.isObject(b))return c.remove(),q(a,c.object,c.property, 47 | {before:c.__li.nextElementSibling,factoryArgs:[b]})},name:function(a){c.__li.firstElementChild.firstElementChild.innerHTML=a;return c},listen:function(){c.__gui.listen(c);return c},remove:function(){c.__gui.remove(c);return c}});if(c instanceof j){var e=new h(c.object,c.property,{min:c.__min,max:c.__max,step:c.__step});i.each(["updateDisplay","onChange","onFinishChange"],function(a){var b=c[a],H=e[a];c[a]=e[a]=function(){var a=Array.prototype.slice.call(arguments);b.apply(c,a);return H.apply(e,a)}}); 48 | g.addClass(d,"has-slider");c.domElement.insertBefore(e.domElement,c.domElement.firstElementChild)}else if(c instanceof h){var f=function(b){return i.isNumber(c.__min)&&i.isNumber(c.__max)?(c.remove(),q(a,c.object,c.property,{before:c.__li.nextElementSibling,factoryArgs:[c.__min,c.__max,c.__step]})):b};c.min=i.compose(f,c.min);c.max=i.compose(f,c.max)}else if(c instanceof b)g.bind(d,"click",function(){g.fakeEvent(c.__checkbox,"click")}),g.bind(c.__checkbox,"click",function(a){a.stopPropagation()}); 49 | else if(c instanceof n)g.bind(d,"click",function(){g.fakeEvent(c.__button,"click")}),g.bind(d,"mouseover",function(){g.addClass(c.__button,"hover")}),g.bind(d,"mouseout",function(){g.removeClass(c.__button,"hover")});else if(c instanceof l)g.addClass(d,"color"),c.updateDisplay=i.compose(function(a){d.style.borderLeftColor=c.__color.toString();return a},c.updateDisplay),c.updateDisplay();c.setValue=i.compose(function(b){a.getRoot().__preset_select&&c.isModified()&&B(a.getRoot(),true);return b},c.setValue)} 50 | function t(a,b){var c=a.getRoot(),d=c.__rememberedObjects.indexOf(b.object);if(d!=-1){var e=c.__rememberedObjectIndecesToControllers[d];e===void 0&&(e={},c.__rememberedObjectIndecesToControllers[d]=e);e[b.property]=b;if(c.load&&c.load.remembered){c=c.load.remembered;if(c[a.preset])c=c[a.preset];else if(c[w])c=c[w];else return;if(c[d]&&c[d][b.property]!==void 0)d=c[d][b.property],b.initialValue=d,b.setValue(d)}}}function I(a){var b=a.__save_row=document.createElement("li");g.addClass(a.domElement, 51 | "has-save");a.__ul.insertBefore(b,a.__ul.firstChild);g.addClass(b,"save-row");var c=document.createElement("span");c.innerHTML=" ";g.addClass(c,"button gears");var d=document.createElement("span");d.innerHTML="Save";g.addClass(d,"button");g.addClass(d,"save");var e=document.createElement("span");e.innerHTML="New";g.addClass(e,"button");g.addClass(e,"save-as");var f=document.createElement("span");f.innerHTML="Revert";g.addClass(f,"button");g.addClass(f,"revert");var m=a.__preset_select=document.createElement("select"); 52 | a.load&&a.load.remembered?i.each(a.load.remembered,function(b,c){C(a,c,c==a.preset)}):C(a,w,false);g.bind(m,"change",function(){for(var b=0;b0){a.preset=this.preset;if(!a.remembered)a.remembered={};a.remembered[this.preset]=z(this)}a.folders={};i.each(this.__folders,function(b, 69 | c){a.folders[c]=b.getSaveObject()});return a},save:function(){if(!this.load.remembered)this.load.remembered={};this.load.remembered[this.preset]=z(this);B(this,false)},saveAs:function(a){if(!this.load.remembered)this.load.remembered={},this.load.remembered[w]=z(this,true);this.load.remembered[a]=z(this);this.preset=a;C(this,a,true)},revert:function(a){i.each(this.__controllers,function(b){this.getRoot().load.remembered?t(a||this.getRoot(),b):b.setValue(b.initialValue)},this);i.each(this.__folders, 70 | function(a){a.revert(a)});a||B(this.getRoot(),false)},listen:function(a){var b=this.__listening.length==0;this.__listening.push(a);b&&E(this.__listening)}});return k}(dat.utils.css,'
\n\n Here\'s the new load parameter for your GUI\'s constructor:\n\n \n\n
\n\n Automatically save\n values to localStorage on exit.\n\n
The values saved to localStorage will\n override those passed to dat.GUI\'s constructor. This makes it\n easier to work incrementally, but localStorage is fragile,\n and your friends may not see the same values you do.\n \n
\n \n
\n\n
', 71 | ".dg ul{list-style:none;margin:0;padding:0;width:100%;clear:both}.dg.ac{position:fixed;top:0;left:0;right:0;height:0;z-index:0}.dg:not(.ac) .main{overflow:hidden}.dg.main{-webkit-transition:opacity 0.1s linear;-o-transition:opacity 0.1s linear;-moz-transition:opacity 0.1s linear;transition:opacity 0.1s linear}.dg.main.taller-than-window{overflow-y:auto}.dg.main.taller-than-window .close-button{opacity:1;margin-top:-1px;border-top:1px solid #2c2c2c}.dg.main ul.closed .close-button{opacity:1 !important}.dg.main:hover .close-button,.dg.main .close-button.drag{opacity:1}.dg.main .close-button{-webkit-transition:opacity 0.1s linear;-o-transition:opacity 0.1s linear;-moz-transition:opacity 0.1s linear;transition:opacity 0.1s linear;border:0;position:absolute;line-height:19px;height:20px;cursor:pointer;text-align:center;background-color:#000}.dg.main .close-button:hover{background-color:#111}.dg.a{float:right;margin-right:15px;overflow-x:hidden}.dg.a.has-save ul{margin-top:27px}.dg.a.has-save ul.closed{margin-top:0}.dg.a .save-row{position:fixed;top:0;z-index:1002}.dg li{-webkit-transition:height 0.1s ease-out;-o-transition:height 0.1s ease-out;-moz-transition:height 0.1s ease-out;transition:height 0.1s ease-out}.dg li:not(.folder){cursor:auto;height:27px;line-height:27px;overflow:hidden;padding:0 4px 0 5px}.dg li.folder{padding:0;border-left:4px solid rgba(0,0,0,0)}.dg li.title{cursor:pointer;margin-left:-4px}.dg .closed li:not(.title),.dg .closed ul li,.dg .closed ul li > *{height:0;overflow:hidden;border:0}.dg .cr{clear:both;padding-left:3px;height:27px}.dg .property-name{cursor:default;float:left;clear:left;width:40%;overflow:hidden;text-overflow:ellipsis}.dg .c{float:left;width:60%}.dg .c input[type=text]{border:0;margin-top:4px;padding:3px;width:100%;float:right}.dg .has-slider input[type=text]{width:30%;margin-left:0}.dg .slider{float:left;width:66%;margin-left:-5px;margin-right:0;height:19px;margin-top:4px}.dg .slider-fg{height:100%}.dg .c input[type=checkbox]{margin-top:9px}.dg .c select{margin-top:5px}.dg .cr.function,.dg .cr.function .property-name,.dg .cr.function *,.dg .cr.boolean,.dg .cr.boolean *{cursor:pointer}.dg .selector{display:none;position:absolute;margin-left:-9px;margin-top:23px;z-index:10}.dg .c:hover .selector,.dg .selector.drag{display:block}.dg li.save-row{padding:0}.dg li.save-row .button{display:inline-block;padding:0px 6px}.dg.dialogue{background-color:#222;width:460px;padding:15px;font-size:13px;line-height:15px}#dg-new-constructor{padding:10px;color:#222;font-family:Monaco, monospace;font-size:10px;border:0;resize:none;box-shadow:inset 1px 1px 1px #888;word-wrap:break-word;margin:12px 0;display:block;width:440px;overflow-y:scroll;height:100px;position:relative}#dg-local-explain{display:none;font-size:11px;line-height:17px;border-radius:3px;background-color:#333;padding:8px;margin-top:10px}#dg-local-explain code{font-size:10px}#dat-gui-save-locally{display:none}.dg{color:#eee;font:11px 'Lucida Grande', sans-serif;text-shadow:0 -1px 0 #111}.dg.main::-webkit-scrollbar{width:5px;background:#1a1a1a}.dg.main::-webkit-scrollbar-corner{height:0;display:none}.dg.main::-webkit-scrollbar-thumb{border-radius:5px;background:#676767}.dg li:not(.folder){background:#1a1a1a;border-bottom:1px solid #2c2c2c}.dg li.save-row{line-height:25px;background:#dad5cb;border:0}.dg li.save-row select{margin-left:5px;width:108px}.dg li.save-row .button{margin-left:5px;margin-top:1px;border-radius:2px;font-size:9px;line-height:7px;padding:4px 4px 5px 4px;background:#c5bdad;color:#fff;text-shadow:0 1px 0 #b0a58f;box-shadow:0 -1px 0 #b0a58f;cursor:pointer}.dg li.save-row .button.gears{background:#c5bdad url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAsAAAANCAYAAAB/9ZQ7AAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAQJJREFUeNpiYKAU/P//PwGIC/ApCABiBSAW+I8AClAcgKxQ4T9hoMAEUrxx2QSGN6+egDX+/vWT4e7N82AMYoPAx/evwWoYoSYbACX2s7KxCxzcsezDh3evFoDEBYTEEqycggWAzA9AuUSQQgeYPa9fPv6/YWm/Acx5IPb7ty/fw+QZblw67vDs8R0YHyQhgObx+yAJkBqmG5dPPDh1aPOGR/eugW0G4vlIoTIfyFcA+QekhhHJhPdQxbiAIguMBTQZrPD7108M6roWYDFQiIAAv6Aow/1bFwXgis+f2LUAynwoIaNcz8XNx3Dl7MEJUDGQpx9gtQ8YCueB+D26OECAAQDadt7e46D42QAAAABJRU5ErkJggg==) 2px 1px no-repeat;height:7px;width:8px}.dg li.save-row .button:hover{background-color:#bab19e;box-shadow:0 -1px 0 #b0a58f}.dg li.folder{border-bottom:0}.dg li.title{padding-left:16px;background:#000 url(data:image/gif;base64,R0lGODlhBQAFAJEAAP////Pz8////////yH5BAEAAAIALAAAAAAFAAUAAAIIlI+hKgFxoCgAOw==) 6px 10px no-repeat;cursor:pointer;border-bottom:1px solid rgba(255,255,255,0.2)}.dg .closed li.title{background-image:url(data:image/gif;base64,R0lGODlhBQAFAJEAAP////Pz8////////yH5BAEAAAIALAAAAAAFAAUAAAIIlGIWqMCbWAEAOw==)}.dg .cr.boolean{border-left:3px solid #806787}.dg .cr.function{border-left:3px solid #e61d5f}.dg .cr.number{border-left:3px solid #2fa1d6}.dg .cr.number input[type=text]{color:#2fa1d6}.dg .cr.string{border-left:3px solid #1ed36f}.dg .cr.string input[type=text]{color:#1ed36f}.dg .cr.function:hover,.dg .cr.boolean:hover{background:#111}.dg .c input[type=text]{background:#303030;outline:none}.dg .c input[type=text]:hover{background:#3c3c3c}.dg .c input[type=text]:focus{background:#494949;color:#fff}.dg .c .slider{background:#303030;cursor:ew-resize}.dg .c .slider-fg{background:#2fa1d6}.dg .c .slider:hover{background:#3c3c3c}.dg .c .slider:hover .slider-fg{background:#44abda}\n", 72 | dat.controllers.factory=function(e,a,c,d,f,b,n){return function(h,j,m,l){var o=h[j];if(n.isArray(m)||n.isObject(m))return new e(h,j,m);if(n.isNumber(o))return n.isNumber(m)&&n.isNumber(l)?new c(h,j,m,l):new a(h,j,{min:m,max:l});if(n.isString(o))return new d(h,j);if(n.isFunction(o))return new f(h,j,"");if(n.isBoolean(o))return new b(h,j)}}(dat.controllers.OptionController,dat.controllers.NumberControllerBox,dat.controllers.NumberControllerSlider,dat.controllers.StringController=function(e,a,c){var d= 73 | function(c,b){function e(){h.setValue(h.__input.value)}d.superclass.call(this,c,b);var h=this;this.__input=document.createElement("input");this.__input.setAttribute("type","text");a.bind(this.__input,"keyup",e);a.bind(this.__input,"change",e);a.bind(this.__input,"blur",function(){h.__onFinishChange&&h.__onFinishChange.call(h,h.getValue())});a.bind(this.__input,"keydown",function(a){a.keyCode===13&&this.blur()});this.updateDisplay();this.domElement.appendChild(this.__input)};d.superclass=e;c.extend(d.prototype, 74 | e.prototype,{updateDisplay:function(){if(!a.isActive(this.__input))this.__input.value=this.getValue();return d.superclass.prototype.updateDisplay.call(this)}});return d}(dat.controllers.Controller,dat.dom.dom,dat.utils.common),dat.controllers.FunctionController,dat.controllers.BooleanController,dat.utils.common),dat.controllers.Controller,dat.controllers.BooleanController,dat.controllers.FunctionController,dat.controllers.NumberControllerBox,dat.controllers.NumberControllerSlider,dat.controllers.OptionController, 75 | dat.controllers.ColorController=function(e,a,c,d,f){function b(a,b,c,d){a.style.background="";f.each(j,function(e){a.style.cssText+="background: "+e+"linear-gradient("+b+", "+c+" 0%, "+d+" 100%); "})}function n(a){a.style.background="";a.style.cssText+="background: -moz-linear-gradient(top, #ff0000 0%, #ff00ff 17%, #0000ff 34%, #00ffff 50%, #00ff00 67%, #ffff00 84%, #ff0000 100%);";a.style.cssText+="background: -webkit-linear-gradient(top, #ff0000 0%,#ff00ff 17%,#0000ff 34%,#00ffff 50%,#00ff00 67%,#ffff00 84%,#ff0000 100%);"; 76 | a.style.cssText+="background: -o-linear-gradient(top, #ff0000 0%,#ff00ff 17%,#0000ff 34%,#00ffff 50%,#00ff00 67%,#ffff00 84%,#ff0000 100%);";a.style.cssText+="background: -ms-linear-gradient(top, #ff0000 0%,#ff00ff 17%,#0000ff 34%,#00ffff 50%,#00ff00 67%,#ffff00 84%,#ff0000 100%);";a.style.cssText+="background: linear-gradient(top, #ff0000 0%,#ff00ff 17%,#0000ff 34%,#00ffff 50%,#00ff00 67%,#ffff00 84%,#ff0000 100%);"}var h=function(e,l){function o(b){q(b);a.bind(window,"mousemove",q);a.bind(window, 77 | "mouseup",j)}function j(){a.unbind(window,"mousemove",q);a.unbind(window,"mouseup",j)}function g(){var a=d(this.value);a!==false?(p.__color.__state=a,p.setValue(p.__color.toOriginal())):this.value=p.__color.toString()}function i(){a.unbind(window,"mousemove",s);a.unbind(window,"mouseup",i)}function q(b){b.preventDefault();var c=a.getWidth(p.__saturation_field),d=a.getOffset(p.__saturation_field),e=(b.clientX-d.left+document.body.scrollLeft)/c,b=1-(b.clientY-d.top+document.body.scrollTop)/c;b>1?b= 78 | 1:b<0&&(b=0);e>1?e=1:e<0&&(e=0);p.__color.v=b;p.__color.s=e;p.setValue(p.__color.toOriginal());return false}function s(b){b.preventDefault();var c=a.getHeight(p.__hue_field),d=a.getOffset(p.__hue_field),b=1-(b.clientY-d.top+document.body.scrollTop)/c;b>1?b=1:b<0&&(b=0);p.__color.h=b*360;p.setValue(p.__color.toOriginal());return false}h.superclass.call(this,e,l);this.__color=new c(this.getValue());this.__temp=new c(0);var p=this;this.domElement=document.createElement("div");a.makeSelectable(this.domElement, 79 | false);this.__selector=document.createElement("div");this.__selector.className="selector";this.__saturation_field=document.createElement("div");this.__saturation_field.className="saturation-field";this.__field_knob=document.createElement("div");this.__field_knob.className="field-knob";this.__field_knob_border="2px solid ";this.__hue_knob=document.createElement("div");this.__hue_knob.className="hue-knob";this.__hue_field=document.createElement("div");this.__hue_field.className="hue-field";this.__input= 80 | document.createElement("input");this.__input.type="text";this.__input_textShadow="0 1px 1px ";a.bind(this.__input,"keydown",function(a){a.keyCode===13&&g.call(this)});a.bind(this.__input,"blur",g);a.bind(this.__selector,"mousedown",function(){a.addClass(this,"drag").bind(window,"mouseup",function(){a.removeClass(p.__selector,"drag")})});var t=document.createElement("div");f.extend(this.__selector.style,{width:"122px",height:"102px",padding:"3px",backgroundColor:"#222",boxShadow:"0px 1px 3px rgba(0,0,0,0.3)"}); 81 | f.extend(this.__field_knob.style,{position:"absolute",width:"12px",height:"12px",border:this.__field_knob_border+(this.__color.v<0.5?"#fff":"#000"),boxShadow:"0px 1px 3px rgba(0,0,0,0.5)",borderRadius:"12px",zIndex:1});f.extend(this.__hue_knob.style,{position:"absolute",width:"15px",height:"2px",borderRight:"4px solid #fff",zIndex:1});f.extend(this.__saturation_field.style,{width:"100px",height:"100px",border:"1px solid #555",marginRight:"3px",display:"inline-block",cursor:"pointer"});f.extend(t.style, 82 | {width:"100%",height:"100%",background:"none"});b(t,"top","rgba(0,0,0,0)","#000");f.extend(this.__hue_field.style,{width:"15px",height:"100px",display:"inline-block",border:"1px solid #555",cursor:"ns-resize"});n(this.__hue_field);f.extend(this.__input.style,{outline:"none",textAlign:"center",color:"#fff",border:0,fontWeight:"bold",textShadow:this.__input_textShadow+"rgba(0,0,0,0.7)"});a.bind(this.__saturation_field,"mousedown",o);a.bind(this.__field_knob,"mousedown",o);a.bind(this.__hue_field,"mousedown", 83 | function(b){s(b);a.bind(window,"mousemove",s);a.bind(window,"mouseup",i)});this.__saturation_field.appendChild(t);this.__selector.appendChild(this.__field_knob);this.__selector.appendChild(this.__saturation_field);this.__selector.appendChild(this.__hue_field);this.__hue_field.appendChild(this.__hue_knob);this.domElement.appendChild(this.__input);this.domElement.appendChild(this.__selector);this.updateDisplay()};h.superclass=e;f.extend(h.prototype,e.prototype,{updateDisplay:function(){var a=d(this.getValue()); 84 | if(a!==false){var e=false;f.each(c.COMPONENTS,function(b){if(!f.isUndefined(a[b])&&!f.isUndefined(this.__color.__state[b])&&a[b]!==this.__color.__state[b])return e=true,{}},this);e&&f.extend(this.__color.__state,a)}f.extend(this.__temp.__state,this.__color.__state);this.__temp.a=1;var h=this.__color.v<0.5||this.__color.s>0.5?255:0,j=255-h;f.extend(this.__field_knob.style,{marginLeft:100*this.__color.s-7+"px",marginTop:100*(1-this.__color.v)-7+"px",backgroundColor:this.__temp.toString(),border:this.__field_knob_border+ 85 | "rgb("+h+","+h+","+h+")"});this.__hue_knob.style.marginTop=(1-this.__color.h/360)*100+"px";this.__temp.s=1;this.__temp.v=1;b(this.__saturation_field,"left","#fff",this.__temp.toString());f.extend(this.__input.style,{backgroundColor:this.__input.value=this.__color.toString(),color:"rgb("+h+","+h+","+h+")",textShadow:this.__input_textShadow+"rgba("+j+","+j+","+j+",.7)"})}});var j=["-moz-","-o-","-webkit-","-ms-",""];return h}(dat.controllers.Controller,dat.dom.dom,dat.color.Color=function(e,a,c,d){function f(a, 86 | b,c){Object.defineProperty(a,b,{get:function(){if(this.__state.space==="RGB")return this.__state[b];n(this,b,c);return this.__state[b]},set:function(a){if(this.__state.space!=="RGB")n(this,b,c),this.__state.space="RGB";this.__state[b]=a}})}function b(a,b){Object.defineProperty(a,b,{get:function(){if(this.__state.space==="HSV")return this.__state[b];h(this);return this.__state[b]},set:function(a){if(this.__state.space!=="HSV")h(this),this.__state.space="HSV";this.__state[b]=a}})}function n(b,c,e){if(b.__state.space=== 87 | "HEX")b.__state[c]=a.component_from_hex(b.__state.hex,e);else if(b.__state.space==="HSV")d.extend(b.__state,a.hsv_to_rgb(b.__state.h,b.__state.s,b.__state.v));else throw"Corrupted color state";}function h(b){var c=a.rgb_to_hsv(b.r,b.g,b.b);d.extend(b.__state,{s:c.s,v:c.v});if(d.isNaN(c.h)){if(d.isUndefined(b.__state.h))b.__state.h=0}else b.__state.h=c.h}var j=function(){this.__state=e.apply(this,arguments);if(this.__state===false)throw"Failed to interpret color arguments";this.__state.a=this.__state.a|| 88 | 1};j.COMPONENTS="r,g,b,h,s,v,hex,a".split(",");d.extend(j.prototype,{toString:function(){return c(this)},toOriginal:function(){return this.__state.conversion.write(this)}});f(j.prototype,"r",2);f(j.prototype,"g",1);f(j.prototype,"b",0);b(j.prototype,"h");b(j.prototype,"s");b(j.prototype,"v");Object.defineProperty(j.prototype,"a",{get:function(){return this.__state.a},set:function(a){this.__state.a=a}});Object.defineProperty(j.prototype,"hex",{get:function(){if(!this.__state.space!=="HEX")this.__state.hex= 89 | a.rgb_to_hex(this.r,this.g,this.b);return this.__state.hex},set:function(a){this.__state.space="HEX";this.__state.hex=a}});return j}(dat.color.interpret,dat.color.math=function(){var e;return{hsv_to_rgb:function(a,c,d){var e=a/60-Math.floor(a/60),b=d*(1-c),n=d*(1-e*c),c=d*(1-(1-e)*c),a=[[d,c,b],[n,d,b],[b,d,c],[b,n,d],[c,b,d],[d,b,n]][Math.floor(a/60)%6];return{r:a[0]*255,g:a[1]*255,b:a[2]*255}},rgb_to_hsv:function(a,c,d){var e=Math.min(a,c,d),b=Math.max(a,c,d),e=b-e;if(b==0)return{h:NaN,s:0,v:0}; 90 | a=a==b?(c-d)/e:c==b?2+(d-a)/e:4+(a-c)/e;a/=6;a<0&&(a+=1);return{h:a*360,s:e/b,v:b/255}},rgb_to_hex:function(a,c,d){a=this.hex_with_component(0,2,a);a=this.hex_with_component(a,1,c);return a=this.hex_with_component(a,0,d)},component_from_hex:function(a,c){return a>>c*8&255},hex_with_component:function(a,c,d){return d<<(e=c*8)|a&~(255<