SphereShapeEmitter.ts 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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 SphereShapeEmitter extends BasicShapeEmitter {
  12. /**
  13. * 球形半径
  14. */
  15. public radius: number;
  16. /**
  17. * 球形区域覆盖范围[0-1]
  18. */
  19. public radiusRange: number;
  20. /**
  21. * 粒子在球形内生成的角度区间[0-360]
  22. */
  23. public arc: number;
  24. /**
  25. * 粒子运动方向偏离程度[0-1]
  26. */
  27. public randomizeDirection: number;
  28. constructor() {
  29. super();
  30. this.radius = 3;
  31. this.radiusRange = 0;
  32. this.arc = 360;
  33. this.randomizeDirection = 0;
  34. }
  35. public startDirection(worldMatrix: xrFrameSystem.Matrix4, direction: xrFrameSystem.Vector3, position: xrFrameSystem.Vector3) {
  36. var centerVec = xrFrameSystem.Vector3.createFromNumber(worldMatrix.getValue(3, 0), worldMatrix.getValue(3, 1), worldMatrix.getValue(3, 2));
  37. var tempDirection = position.sub(centerVec).normalize();
  38. var randX = randomBetween(0, this.randomizeDirection);
  39. var randY = randomBetween(0, this.randomizeDirection);
  40. var randZ = randomBetween(0, this.randomizeDirection);
  41. var temp = xrFrameSystem.Vector3.createFromNumber(randX, randY, randZ);
  42. direction.set(tempDirection.add(temp).normalize().transformDirection(worldMatrix));
  43. }
  44. public startPosition(worldMatrix: xrFrameSystem.Matrix4, position: xrFrameSystem.Vector3) {
  45. var randRadius = this.radius - randomBetween(0, this.radius * this.radiusRange);
  46. var cosV = randomBetween(0, 1);
  47. var phi = randomBetween(0, this.arc / 360 * 2 * Math.PI);
  48. var theta = Math.acos(2 * cosV - 1);
  49. var randX = randRadius * Math.cos(phi) * Math.sin(theta);
  50. var randY = randRadius * Math.cos(theta);
  51. var randZ = randRadius * Math.sin(phi) * Math.sin(theta);
  52. var temp = xrFrameSystem.Vector3.createFromNumber(randX, randY, randZ);
  53. position.set(temp.applyMatrix4(worldMatrix));
  54. }
  55. }