PointShapeEmitter.ts 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. import { BasicShapeEmitter } from './BasicShapeEmitter'
  2. const xrFrameSystem = wx.getXrFrameSystem();
  3. function randomBetween(v1, v2, randomSeed = Math.random()) {
  4. if (v1 === v2) {
  5. return v1;
  6. } else {
  7. return randomSeed * Math.abs(v1 - v2) + Math.min(v1, v2);
  8. }
  9. };
  10. // 控制粒子以原点为中心向外发射
  11. export default class PointShapeEmitter extends BasicShapeEmitter {
  12. /**
  13. * 粒子运动方向左区间。
  14. */
  15. public direction: xrFrameSystem.Vector3;
  16. /**
  17. * 粒子运动方向右区间。
  18. */
  19. public direction2: xrFrameSystem.Vector3;
  20. constructor() {
  21. super();
  22. this.direction = xrFrameSystem.Vector3.createFromNumber(0, 1.0, 0);
  23. this.direction2 = xrFrameSystem.Vector3.createFromNumber(0, 1.0, 0);
  24. }
  25. // 粒子起始发射方向
  26. public startDirection(worldMatrix: xrFrameSystem.Matrix4, direction: xrFrameSystem.Vector3) {
  27. var randX = randomBetween(this.direction.x, this.direction2.x);
  28. var randY = randomBetween(this.direction.y, this.direction2.y);
  29. var randZ = randomBetween(this.direction.z, this.direction2.z);
  30. var temp = xrFrameSystem.Vector3.createFromNumber(randX, randY, randZ);
  31. direction.set(temp.normalize().transformDirection(worldMatrix));
  32. }
  33. // 粒子起始发射位置
  34. public startPosition(worldMatrix: xrFrameSystem.Matrix4, position: xrFrameSystem.Vector3) {
  35. position.set(xrFrameSystem.Vector3.createFromNumber(0, 0, 0).applyMatrix4(worldMatrix));
  36. }
  37. }