modalParse.ts 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. import { IBaseProps, IGroupParams, IPointLight } from './type/types';
  2. /**
  3. * 生成基本参数 旋转 位移 缩放等属性
  4. */
  5. const genBaseStruct = (obj: THREE.Object3D): IBaseProps => {
  6. const { type, name, quaternion: q, position: p, rotation: r, scale: s, up: u, userData, visible, matrix } = obj;
  7. const quaternion: IBaseProps['quaternion'] = [q.x, q.y, q.z, q.w];
  8. const position: IBaseProps['position'] = [p.x, p.y, p.z];
  9. const rotation: IBaseProps['rotation'] = [r.x, r.y, r.z, r.order];
  10. const scale: IBaseProps['scale'] = [s.x, s.y, s.z];
  11. const up: IBaseProps['up'] = [u.x, u.y, u.z];
  12. return {
  13. type,
  14. name,
  15. quaternion,
  16. position,
  17. rotation,
  18. scale,
  19. up,
  20. matrix,
  21. userData,
  22. visible,
  23. children: genObject3DChildren(obj.children),
  24. animations: genAnimations(obj.animations),
  25. };
  26. };
  27. /**
  28. * 生成动画结构
  29. */
  30. export const genAnimations = (animations: THREE.AnimationClip[]) => {
  31. return animations.map((animation) => {
  32. animation['tracks'].forEach((t) => {
  33. //删除这个方法就可以传递过去了
  34. //@ts-ignore
  35. delete t['createInterpolant'];
  36. t['type'] = t['ValueTypeName']; //vector quaternion
  37. });
  38. return animation;
  39. });
  40. };
  41. /**
  42. * 生成物体参数
  43. */
  44. const genMeshStruct = (mesh: THREE.Mesh) => {
  45. const { geometry, material } = mesh;
  46. return {
  47. geometry,
  48. material,
  49. ...genBaseStruct(mesh),
  50. };
  51. };
  52. const genPointLightStruct = (pointLight: THREE.PointLight): IPointLight => {
  53. return {
  54. power: pointLight.power,
  55. color: pointLight.color,
  56. decay: pointLight.decay,
  57. castShadow: pointLight.castShadow,
  58. distance: pointLight.distance,
  59. frustumCulled: pointLight.frustumCulled,
  60. intensity: pointLight.intensity,
  61. layers: pointLight.layers,
  62. ...genBaseStruct(pointLight),
  63. };
  64. };
  65. const genObject3DStruct = (object: THREE.Object3D) => {
  66. return {
  67. ...genBaseStruct(object),
  68. };
  69. };
  70. /**
  71. * 生成子元素结构
  72. */
  73. const genObject3DChildren = (children: THREE.Object3D[]) => {
  74. const childStruct: IGroupParams['children'] = [];
  75. for (const child of children) {
  76. const { type } = child;
  77. if (type === 'Mesh') {
  78. childStruct.push(genMeshStruct(child as THREE.Mesh));
  79. } else if (type === 'Group') {
  80. childStruct.push(genGroupStruct(child as THREE.Group));
  81. } else if (type === 'PointLight') {
  82. childStruct.push(genPointLightStruct(child as THREE.PointLight));
  83. } else if (type === 'Object3D') {
  84. childStruct.push(genObject3DStruct(child));
  85. }
  86. }
  87. return childStruct;
  88. };
  89. /**
  90. * 生成物体组结构
  91. */
  92. export const genGroupStruct = (group: THREE.Group) => {
  93. const struct: IGroupParams = { ...genBaseStruct(group) };
  94. return struct;
  95. };