UNPKG

three.proton

Version:

three.Proton is an easily customizable html5 particle engine for three.js

189 lines (160 loc) 5.46 kB
<!DOCTYPE HTML> <html> <head> <title>three.proton - helloworld</title> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0"> <style type="text/css"> body { font-family: Monospace; background-color: #fff; margin: 0; padding: 0; overflow: hidden; } </style> </head> <body> <div id="container"></div> <script src="../lib/stats.min.js"></script> <script src="../lib/three.min.js"></script> <script src="../build/three.proton.min.js"></script> <script src="./js/lib/TrackballControls.js"></script> <script> var proton, emitter; var camera, scene, renderer, stats, clock, spring, controls; var color1, color2, tha = 0, hcolor = 0; init(); function init() { addScene(); addControls(); addLights(); addStars(); addProton(); //addInteraction(); addStats(); animate(); } function addScene() { camera = new THREE.PerspectiveCamera(70, window.innerWidth / window.innerHeight, 1, 1000); camera.position.z = 500; scene = new THREE.Scene(); scene.fog = new THREE.Fog(0xffffff, 1, 10000); clock = new THREE.Clock(); renderer = new THREE.WebGLRenderer(); renderer.setPixelRatio(window.devicePixelRatio); renderer.setSize(window.innerWidth, window.innerHeight); document.body.appendChild(renderer.domElement); window.addEventListener('resize', onWindowResize, false); } function addControls() { controls = new THREE.TrackballControls(camera); controls.rotateSpeed = 1.0; controls.zoomSpeed = 1.2; controls.panSpeed = 0.8; controls.noZoom = false; controls.noPan = false; controls.staticMoving = true; controls.dynamicDampingFactor = 0.3; } function addLights() { var ambientLight = new THREE.AmbientLight(0x101010); scene.add(ambientLight); var pointLight = new THREE.PointLight(0xffffff, 2, 1000, 1); pointLight.position.set(0, 200, 200); scene.add(pointLight); } function addStats() { stats = new Stats(); stats.showPanel(0); stats.dom.style.position = 'absolute'; stats.dom.style.left = '0px'; stats.dom.style.top = '0px'; container.appendChild(stats.dom); } function addStars() { var geometry = new THREE.Geometry(); for (var i = 0; i < 10000; i++) { var vertex = new THREE.Vector3(); vertex.x = THREE.Math.randFloatSpread(2000); vertex.y = THREE.Math.randFloatSpread(2000); vertex.z = THREE.Math.randFloatSpread(2000); geometry.vertices.push(vertex); } var particles = new THREE.Points(geometry, new THREE.PointsMaterial({ color: 0x888888 })); scene.add(particles); } function addProton() { proton = new Proton(); emitter = new Proton.Emitter(); //setRate emitter.rate = new Proton.Rate(new Proton.Span(4, 16), new Proton.Span(.01)); //addInitialize emitter.addInitialize(new Proton.Position(new Proton.PointZone(0, 0))); emitter.addInitialize(new Proton.Mass(1)); emitter.addInitialize(new Proton.Radius(6, 12)); emitter.addInitialize(new Proton.Life(3)); emitter.addInitialize(new Proton.V(45, new Proton.Vector3D(0, 1, 0), 180)); //addBehaviour emitter.addBehaviour(new Proton.Alpha(1, 0)); emitter.addBehaviour(new Proton.Scale(.1, 1.3)); color1 = new THREE.Color(); color2 = new THREE.Color(); colorBehaviour = new Proton.Color(color1, color2); emitter.addBehaviour(colorBehaviour); emitter.emit(); //add emitter proton.addEmitter(emitter); proton.addRender(new Proton.SpriteRender(scene)); } function createSprite() { var map = new THREE.TextureLoader().load("./img/dot.png"); var material = new THREE.SpriteMaterial({ map: map, color: 0xff0000, blending: THREE.AdditiveBlending, fog: true }); return new THREE.Sprite(material); } function setColor() { hcolor += .01; color1.setHSL(hcolor - (hcolor >> 0), 1, .5); color2.setHSL(hcolor - (hcolor >> 0) + .3, 1, .5); //colorBehaviour.reset(color1, color2); } function animate() { stats.begin(); requestAnimationFrame(animate); render(); stats.end(); } var ctha = 0; var r = 500; function render() { var delta = clock.getDelta(); setColor(); delta < 5 / 60 && proton.update(delta); tha += Math.PI / 150; var p = 300 * Math.sin(2 * tha) emitter.p.x = p * Math.cos(tha); emitter.p.y = p * Math.sin(tha); emitter.p.z = p * Math.tan(tha) / 2; renderer.render(scene, camera); //controls.update(); camera.lookAt(scene.position); ctha += .016; r = 300; camera.position.x = Math.sin(ctha) * r; camera.position.z = Math.cos(ctha) * r; camera.position.y = Math.sin(ctha) * r; } function onWindowResize() { } </script> </body> </html>