ParticleSystem.re.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. import * as RE from 'rogue-engine'
  2. import * as THREE from 'three'
  3. import { Color } from 'three';
  4. import Player from './Player.re';
  5. //https://www.youtube.com/watch?v=OFqENgtqRAY
  6. export default class ParticleSystem extends RE.Component {
  7. vertexShader = `
  8. uniform float pointMultiplier;
  9. varying vec4 vColor;
  10. void main() {
  11. vec4 mvPosition = modelViewMatrix * vec4(position, 1.0);
  12. gl_Position = projectionMatrix * mvPosition;
  13. gl_PointSize = pointMultiplier / gl_Position.w;
  14. vColor = color;
  15. }`;
  16. fragmentShader = `
  17. uniform sampler2D diffuseTexture;
  18. varying vec4 vColor;
  19. void main() {
  20. gl_FragColor = texture2D(diffuseTexture, gl_PointCoord) * vColor;
  21. }`;
  22. @RE.Prop("Texture") texture
  23. @RE.Prop("Color") color = new Color(255, 255, 255)
  24. @RE.Prop("Number") alpha = 1
  25. @RE.Prop("Select") blending = 0
  26. blendingOptions = ["Additive", "Normal", "Multiply", "None", "Subtractive"]
  27. //this is the "mesh" that we draw the particle textures into
  28. //it's a THREE.Points object that I added underneath the GameLogic Object3d
  29. @RE.Prop("Object3D") globalPointsObject
  30. @RE.Prop("Number") max = 500
  31. @RE.Prop("Number") spawnPerTick = 1
  32. @RE.Prop("Number") lifespan = 2
  33. //This is the Object3d where we spawn particles from. It uses the worldPosition
  34. //so things like rotations are accounted for
  35. @RE.Prop("Object3D") initialLocation
  36. @RE.Prop("Vector3") initialVelocity = new THREE.Vector3(0,0,0)
  37. //this is the "mesh" that we draw the particle textures into
  38. //it's a THREE.Points object that I added underneath the GameLogic Object3d
  39. @RE.Prop("Object3D") points
  40. @RE.Prop("Number") thrustScalar = 0.01
  41. @RE.Prop("Vector3") positionRandomBounds = new THREE.Vector3(1,1,1)
  42. @RE.Prop("Vector3") velocityRandomBounds = new THREE.Vector3(1,1,1)
  43. start() {
  44. const bounds = RE.Runtime.rogueDOMContainer.getBoundingClientRect();
  45. const uniforms = {
  46. diffuseTexture: {
  47. value: this.texture
  48. },
  49. pointMultiplier: {
  50. value: bounds.width - bounds.left
  51. }
  52. }
  53. this.material = new THREE.ShaderMaterial({
  54. uniforms: uniforms,
  55. vertexShader: this.vertexShader,
  56. fragmentShader: this.fragmentShader,
  57. blending: this.convertBlending(this.blending),
  58. depthTest: true,
  59. depthWrite: false,
  60. transparent: true,
  61. vertexColors: true,
  62. })
  63. this.geometry = new THREE.BufferGeometry()
  64. this.geometry.setAttribute('position', new THREE.Float32BufferAttribute([], 3));
  65. this.geometry.setAttribute('color', new THREE.Float32BufferAttribute([], 4));
  66. this.globalPointsObject.geometry = this.geometry
  67. this.globalPointsObject.material = this.material
  68. this.particles = []
  69. this.ship = RE.getComponent(Player, this.object3d)
  70. this.updateGeometry()
  71. }
  72. convertBlending(selectIndex) {
  73. console.log(selectIndex)
  74. switch(this.blendingOptions[selectIndex]) {
  75. case "Additive":
  76. return THREE.AdditiveBlending
  77. case "Normal":
  78. return THREE.NormalBlending
  79. case "Multiply":
  80. return THREE.MultiplyBlending
  81. case "None":
  82. return THREE.NoBlending
  83. case "Subtractive":
  84. return THREE.SubtractiveBlending
  85. default:
  86. return THREE.AdditiveBlending
  87. }
  88. }
  89. awake() {
  90. }
  91. update() {
  92. if(this.ship.isThrusting) {
  93. this.addParticles();
  94. }
  95. this.updateParticles(RE.Runtime.deltaTime);
  96. this.updateGeometry();
  97. }
  98. addParticles() {
  99. if(this.particles.length >= this.max) {
  100. return
  101. }
  102. for (let i = 0; i < this.spawnPerTick; i++) {
  103. const life = this.lifespan;
  104. const newPosition = new THREE.Vector3()
  105. this.initialLocation.getWorldPosition(newPosition)
  106. newPosition.add(new THREE.Vector3(
  107. 1 * Math.random() - 0.5,
  108. 1 * Math.random() - 0.5,
  109. 1 * Math.random() - 0.5
  110. ).multiply(this.positionRandomBounds))
  111. const newVelocity = this.initialVelocity.clone()
  112. if(this.ship) {
  113. newVelocity.add(new THREE.Vector3(
  114. -this.ship.bodyComponent.body.force.x,
  115. -this.ship.bodyComponent.body.force.y,
  116. -this.ship.bodyComponent.body.force.z)
  117. .multiplyScalar(this.thrustScalar))
  118. }
  119. newVelocity.add(new THREE.Vector3(
  120. 1 * Math.random() - 0.5,
  121. 1 * Math.random() - 0.5,
  122. 1 * Math.random() - 0.5
  123. ).multiply(this.velocityRandomBounds)
  124. )
  125. this.particles.push({
  126. position: newPosition,
  127. life: life,
  128. maxLife: life,
  129. velocity: newVelocity,
  130. color: this.color,
  131. alpha: this.alpha,
  132. });
  133. }
  134. }
  135. updateGeometry() {
  136. const positions = [];
  137. const colors = []
  138. for (let p of this.particles) {
  139. positions.push(p.position.x, p.position.y, p.position.z);
  140. colors.push(p.color.r, p.color.g, p.color.b, p.alpha);
  141. }
  142. this.geometry.setAttribute('position', new THREE.Float32BufferAttribute(positions, 3));
  143. this.geometry.setAttribute('color', new THREE.Float32BufferAttribute(colors, 4));
  144. this.geometry.attributes.position.needsUpdate = true;
  145. this.geometry.attributes.color.needsUpdate = true;
  146. }
  147. updateParticles(deltaTime) {
  148. for (let p of this.particles) {
  149. p.life -= deltaTime;
  150. }
  151. this.particles = this.particles.filter(p => {
  152. return p.life > 0.0;
  153. });
  154. for (let p of this.particles) {
  155. p.position.add(p.velocity.clone());
  156. }
  157. // RE.Debug.log(JSON.stringify(this.particles[0].position))
  158. let camera = RE.App.currentScene.getObjectByProperty('uuid', RE.App.activeCamera)
  159. this.particles.sort((a, b) => {
  160. const d1 = camera.position.distanceTo(a.position);
  161. const d2 = camera.position.distanceTo(b.position);
  162. if (d1 > d2) {
  163. return -1;
  164. }
  165. if (d1 < d2) {
  166. return 1;
  167. }
  168. return 0;
  169. });
  170. }
  171. }
  172. RE.registerComponent(ParticleSystem)