Advertisement
yskang

threejs-minecraft-27

Apr 21st, 2020
1,116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class Explosion {
  2.     constructor( x, y, z, color, scene, pointCount, movementSpeed, size ) {
  3.         this.scene = scene;
  4.  
  5.         const geometry = new THREE.Geometry();
  6.         const textureLoader = new THREE.TextureLoader();
  7.         const smokeTexture = textureLoader.load( 'img/smoke.png' );
  8.  
  9.         this.material = new THREE.PointsMaterial({
  10.             size: size || 10,
  11.             color: color,
  12.             map: smokeTexture,
  13.             blending: THREE.CustomBlending,
  14.             depthWrite: false,
  15.             transparent: true,
  16.             opacity: 0.7
  17.        });
  18.  
  19.        this.pCount = pointCount || 1000;
  20.        this.movementSpeed = movementSpeed || 10;
  21.        this.dirs = [];
  22.        for( let i = 0; i < this.pCount; i++ ) {
  23.            const vertex = new THREE.Vector3( x, y, z );
  24.            geometry.vertices.push( vertex );
  25.            const r = this.movementSpeed * THREE.Math.randFloat( 0, 1 ) + 0.5;
  26.            const theta = Math.random() * Math.PI * 2;
  27.            const phi = Math.random() * Math.PI;
  28.            this.dirs.push({
  29.                x: r * Math.sin( phi ) * Math.cos( theta ),
  30.                y: r * Math.sin( phi ) * Math.sin( theta ),
  31.                z: r * Math.cos( phi )
  32.            });
  33.        }
  34.  
  35.        const points = new THREE.Points( geometry, this.material );
  36.        this.object = points; this.scene.add( this.object );
  37.     }
  38.  
  39.     update() {
  40.         let p = this.pCount;
  41.         const d = this.dirs;
  42.         while( p-- ) {
  43.             let particle = this.object.geometry.vertices[p];
  44.             particle.x += d[p].x;
  45.             particle.y += d[p].y;
  46.             particle.z += d[p].z;
  47.        }
  48.  
  49.        this.object.geometry.verticesNeedUpdate = true;
  50.     }
  51.  
  52.     destroy() {
  53.         this.object.geometry.dispose();
  54.         this.scene.remove( this.object );
  55.         this.dirs.length = 0;
  56.     }
  57.  
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement