BoxShapeEmitter.ts 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. import { BasicShapeEmitter } from './BasicShapeEmitter'
  2. const xrFrameSystem = wx.getXrFrameSystem();
  3. // 返回v1到v2间的随机值
  4. function randomBetween(v1, v2, randomSeed = Math.random()) {
  5. if (v1 === v2) {
  6. return v1;
  7. } else {
  8. return randomSeed * Math.abs(v1 - v2) + Math.min(v1, v2);
  9. }
  10. };
  11. // 控制粒子在箱型区域内进行发射
  12. export default class BoxShapeEmitter extends BasicShapeEmitter {
  13. public direction: xrFrameSystem.Vector3;
  14. public direction2: xrFrameSystem.Vector3;
  15. // 箱型区域的坐标起始点
  16. public minEmitBox: xrFrameSystem.Vector3;
  17. // 箱型区域的坐标终点
  18. public maxEmitBox: xrFrameSystem.Vector3;
  19. constructor() {
  20. super();
  21. this.direction = xrFrameSystem.Vector3.createFromNumber(0, 1.0, 0);
  22. this.direction2 = xrFrameSystem.Vector3.createFromNumber(0, 1.0, 0);
  23. this.minEmitBox = xrFrameSystem.Vector3.createFromNumber(-0.5, -0.5, -0.5);
  24. this.maxEmitBox = xrFrameSystem.Vector3.createFromNumber(0.5, 0.5, 0.5);
  25. }
  26. // 决定粒子起始的运动方向
  27. public startDirection(worldMatrix, direction) {
  28. var randX = randomBetween(this.direction.x, this.direction2.x);
  29. var randY = randomBetween(this.direction.y, this.direction2.y);
  30. var randZ = randomBetween(this.direction.z, this.direction2.z);
  31. var temp = xrFrameSystem.Vector3.createFromNumber(randX, randY, randZ);
  32. direction.set(temp.normalize().transformDirection(worldMatrix));
  33. }
  34. // 决定粒子起始的运动位置
  35. public startPosition(worldMatrix, position: xrFrameSystem.Vector3) {
  36. var randX = randomBetween(this.minEmitBox.x, this.maxEmitBox.x);
  37. var randY = randomBetween(this.minEmitBox.y, this.maxEmitBox.y);
  38. var randZ = randomBetween(this.minEmitBox.z, this.maxEmitBox.z);
  39. var temp = xrFrameSystem.Vector3.createFromNumber(randX, randY, randZ);
  40. position.set(temp.applyMatrix4(worldMatrix));
  41. }
  42. }