123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374 |
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <title>three.js webgl - particles - dynamic - postprocessing</title>
- <meta charset="utf-8">
- <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
- <link type="text/css" rel="stylesheet" href="main.css">
- </head>
- <body>
- <div id="container"></div>
- <div id="info">
- <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> - webgl dynamic particles + postprocessing<br/>
- models by <a href="http://sketchup.google.com/3dwarehouse/details?mid=2c6fd128fca34052adc5f5b98d513da1" target="_blank" rel="noopener">Reallusion</a>
- <a href="http://sketchup.google.com/3dwarehouse/details?mid=f526cc4abf7cb68d76cab47c765b7255" target="_blank" rel="noopener">iClone</a>
- </div>
- <script type="module">
- import * as THREE from '../build/three.module.js';
- import Stats from './jsm/libs/stats.module.js';
- import { EffectComposer } from './jsm/postprocessing/EffectComposer.js';
- import { RenderPass } from './jsm/postprocessing/RenderPass.js';
- import { ShaderPass } from './jsm/postprocessing/ShaderPass.js';
- import { BloomPass } from './jsm/postprocessing/BloomPass.js';
- import { FilmPass } from './jsm/postprocessing/FilmPass.js';
- import { FocusShader } from './jsm/shaders/FocusShader.js';
- import { OBJLoader } from './jsm/loaders/OBJLoader.js';
- var camera, scene, renderer, mesh;
- var parent, meshes = [], clonemeshes = [];
- var composer, effectFocus;
- var clock = new THREE.Clock();
- var stats;
- init();
- animate();
- function init() {
- var container = document.querySelector( '#container' );
- camera = new THREE.PerspectiveCamera( 20, window.innerWidth / window.innerHeight, 1, 50000 );
- camera.position.set( 0, 700, 7000 );
- scene = new THREE.Scene();
- scene.background = new THREE.Color( 0x000104 );
- scene.fog = new THREE.FogExp2( 0x000104, 0.0000675 );
- camera.lookAt( scene.position );
- var loader = new OBJLoader();
- loader.load( 'models/obj/male02/male02.obj', function ( object ) {
- var positions = combineBuffer( object, 'position' );
- createMesh( positions, scene, 4.05, - 500, - 350, 600, 0xff7744 );
- createMesh( positions, scene, 4.05, 500, - 350, 0, 0xff5522 );
- createMesh( positions, scene, 4.05, - 250, - 350, 1500, 0xff9922 );
- createMesh( positions, scene, 4.05, - 250, - 350, - 1500, 0xff99ff );
- } );
- loader.load( 'models/obj/female02/female02.obj', function ( object ) {
- var positions = combineBuffer( object, 'position' );
- createMesh( positions, scene, 4.05, - 1000, - 350, 0, 0xffdd44 );
- createMesh( positions, scene, 4.05, 0, - 350, 0, 0xffffff );
- createMesh( positions, scene, 4.05, 1000, - 350, 400, 0xff4422 );
- createMesh( positions, scene, 4.05, 250, - 350, 1500, 0xff9955 );
- createMesh( positions, scene, 4.05, 250, - 350, 2500, 0xff77dd );
- } );
- renderer = new THREE.WebGLRenderer();
- renderer.setPixelRatio( window.devicePixelRatio );
- renderer.setSize( window.innerWidth, window.innerHeight );
- renderer.autoClear = false;
- container.appendChild( renderer.domElement );
- parent = new THREE.Object3D();
- scene.add( parent );
- var grid = new THREE.Points( new THREE.PlaneBufferGeometry( 15000, 15000, 64, 64 ), new THREE.PointsMaterial( { color: 0xff0000, size: 10 } ) );
- grid.position.y = - 400;
- grid.rotation.x = - Math.PI / 2;
- parent.add( grid );
- // postprocessing
- var renderModel = new RenderPass( scene, camera );
- var effectBloom = new BloomPass( 0.75 );
- var effectFilm = new FilmPass( 0.5, 0.5, 1448, false );
- effectFocus = new ShaderPass( FocusShader );
- effectFocus.uniforms[ "screenWidth" ].value = window.innerWidth * window.devicePixelRatio;
- effectFocus.uniforms[ "screenHeight" ].value = window.innerHeight * window.devicePixelRatio;
- composer = new EffectComposer( renderer );
- composer.addPass( renderModel );
- composer.addPass( effectBloom );
- composer.addPass( effectFilm );
- composer.addPass( effectFocus );
- //stats
- stats = new Stats();
- container.appendChild( stats.dom );
- window.addEventListener( 'resize', onWindowResize, false );
- }
- function onWindowResize() {
- camera.aspect = window.innerWidth / window.innerHeight;
- camera.updateProjectionMatrix();
- camera.lookAt( scene.position );
- renderer.setSize( window.innerWidth, window.innerHeight );
- composer.setSize( window.innerWidth, window.innerHeight );
- effectFocus.uniforms[ "screenWidth" ].value = window.innerWidth * window.devicePixelRatio;
- effectFocus.uniforms[ "screenHeight" ].value = window.innerHeight * window.devicePixelRatio;
- }
- function combineBuffer( model, bufferName ) {
- let count = 0;
- model.traverse( function ( child ) {
- if ( child.isMesh ) {
- var buffer = child.geometry.attributes[ bufferName ];
- count += buffer.array.length;
- }
- } );
- var combined = new Float32Array( count );
- let offset = 0;
- model.traverse( function ( child ) {
- if ( child.isMesh ) {
- var buffer = child.geometry.attributes[ bufferName ];
- combined.set( buffer.array, offset );
- offset += buffer.array.length;
- }
- } );
- return new THREE.BufferAttribute( combined, 3 );
- }
- function createMesh( positions, scene, scale, x, y, z, color ) {
- var geometry = new THREE.BufferGeometry();
- geometry.setAttribute( 'position', positions.clone() );
- geometry.setAttribute( 'initialPosition', positions.clone() );
- geometry.attributes.position.setUsage( THREE.DynamicDrawUsage );
- var clones = [
- [ 6000, 0, - 4000 ],
- [ 5000, 0, 0 ],
- [ 1000, 0, 5000 ],
- [ 1000, 0, - 5000 ],
- [ 4000, 0, 2000 ],
- [ - 4000, 0, 1000 ],
- [ - 5000, 0, - 5000 ],
- [ 0, 0, 0 ]
- ];
- for ( var i = 0; i < clones.length; i ++ ) {
- var c = ( i < clones.length - 1 ) ? 0x252525 : color;
- mesh = new THREE.Points( geometry, new THREE.PointsMaterial( { size: 30, color: c } ) );
- mesh.scale.x = mesh.scale.y = mesh.scale.z = scale;
- mesh.position.x = x + clones[ i ][ 0 ];
- mesh.position.y = y + clones[ i ][ 1 ];
- mesh.position.z = z + clones[ i ][ 2 ];
- parent.add( mesh );
- clonemeshes.push( { mesh: mesh, speed: 0.5 + Math.random() } );
- }
- meshes.push( {
- mesh: mesh, verticesDown: 0, verticesUp: 0, direction: 0, speed: 15, delay: Math.floor( 200 + 200 * Math.random() ),
- start: Math.floor( 100 + 200 * Math.random() ),
- } );
- }
- function animate() {
- requestAnimationFrame( animate );
- render();
- stats.update();
- }
- function render() {
- var delta = 10 * clock.getDelta();
- delta = delta < 2 ? delta : 2;
- parent.rotation.y += - 0.02 * delta;
- for ( var j = 0; j < clonemeshes.length; j ++ ) {
- var cm = clonemeshes[ j ];
- cm.mesh.rotation.y += - 0.1 * delta * cm.speed;
- }
- for ( var j = 0; j < meshes.length; j ++ ) {
- var data = meshes[ j ];
- var positions = data.mesh.geometry.attributes.position;
- var initialPositions = data.mesh.geometry.attributes.initialPosition;
- var count = positions.count;
- if ( data.start > 0 ) {
- data.start -= 1;
- } else {
- if ( data.direction === 0 ) {
- data.direction = - 1;
- }
- }
- for ( var i = 0; i < count; i ++ ) {
- var px = positions.getX( i );
- var py = positions.getY( i );
- var pz = positions.getZ( i );
- // falling down
- if ( data.direction < 0 ) {
- if ( py > 0 ) {
- positions.setXYZ(
- i,
- px + 1.5 * ( 0.50 - Math.random() ) * data.speed * delta,
- py + 3.0 * ( 0.25 - Math.random() ) * data.speed * delta,
- pz + 1.5 * ( 0.50 - Math.random() ) * data.speed * delta
- );
- } else {
- data.verticesDown += 1;
- }
- }
- // rising up
- if ( data.direction > 0 ) {
- var ix = initialPositions.getX( i );
- var iy = initialPositions.getY( i );
- var iz = initialPositions.getZ( i );
- var dx = Math.abs( px - ix );
- var dy = Math.abs( py - iy );
- var dz = Math.abs( pz - iz );
- var d = dx + dy + dx;
- if ( d > 1 ) {
- positions.setXYZ(
- i,
- px - ( px - ix ) / dx * data.speed * delta * ( 0.85 - Math.random() ),
- py - ( py - iy ) / dy * data.speed * delta * ( 1 + Math.random() ),
- pz - ( pz - iz ) / dz * data.speed * delta * ( 0.85 - Math.random() )
- );
- } else {
- data.verticesUp += 1;
- }
- }
- }
- // all vertices down
- if ( data.verticesDown >= count ) {
- if ( data.delay <= 0 ) {
- data.direction = 1;
- data.speed = 5;
- data.verticesDown = 0;
- data.delay = 320;
- } else {
- data.delay -= 1;
- }
- }
- // all vertices up
- if ( data.verticesUp >= count ) {
- if ( data.delay <= 0 ) {
- data.direction = - 1;
- data.speed = 15;
- data.verticesUp = 0;
- data.delay = 120;
- } else {
- data.delay -= 1;
- }
- }
- positions.needsUpdate = true;
- }
- composer.render( 0.01 );
- }
- </script>
- </body>
- </html>
|