serialization.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. import { Color3, Color4 } from "../Maths/math.color.js";
  2. import { Matrix, Quaternion, Vector2, Vector3, Vector4 } from "../Maths/math.vector.js";
  3. import { FlowGraphInteger } from "./flowGraphInteger.js";
  4. function isMeshClassName(className) {
  5. return (className === "Mesh" ||
  6. className === "AbstractMesh" ||
  7. className === "GroundMesh" ||
  8. className === "InstanceMesh" ||
  9. className === "LinesMesh" ||
  10. className === "GoldbergMesh" ||
  11. className === "GreasedLineMesh" ||
  12. className === "TrailMesh");
  13. }
  14. function isVectorClassName(className) {
  15. return className === "Vector2" || className === "Vector3" || className === "Vector4" || className === "Quaternion" || className === "Color3" || className === "Color4";
  16. }
  17. function parseVector(className, value) {
  18. if (className === "Vector2") {
  19. return Vector2.FromArray(value);
  20. }
  21. else if (className === "Vector3") {
  22. return Vector3.FromArray(value);
  23. }
  24. else if (className === "Vector4") {
  25. return Vector4.FromArray(value);
  26. }
  27. else if (className === "Quaternion") {
  28. return Quaternion.FromArray(value);
  29. }
  30. else if (className === "Color3") {
  31. return new Color3(value[0], value[1], value[2]);
  32. }
  33. else if (className === "Color4") {
  34. return new Color4(value[0], value[1], value[2], value[3]);
  35. }
  36. else {
  37. throw new Error(`Unknown vector class name ${className}`);
  38. }
  39. }
  40. /**
  41. * The default function that serializes values in a context object to a serialization object
  42. * @param key the key where the value should be stored in the serialization object
  43. * @param value the value to store
  44. * @param serializationObject the object where the value will be stored
  45. */
  46. export function defaultValueSerializationFunction(key, value, serializationObject) {
  47. const className = value?.getClassName?.() ?? "";
  48. if (isMeshClassName(className)) {
  49. serializationObject[key] = {
  50. name: value.name,
  51. className,
  52. };
  53. }
  54. else if (isVectorClassName(className)) {
  55. serializationObject[key] = {
  56. value: value.asArray(),
  57. className,
  58. };
  59. }
  60. else {
  61. serializationObject[key] = value;
  62. }
  63. }
  64. /**
  65. * The default function that parses values stored in a serialization object
  66. * @param key the key to the value that will be parsed
  67. * @param serializationObject the object that will be parsed
  68. * @param scene
  69. * @returns
  70. */
  71. export function defaultValueParseFunction(key, serializationObject, scene) {
  72. const intermediateValue = serializationObject[key];
  73. let finalValue;
  74. const className = intermediateValue?.className;
  75. if (isMeshClassName(className)) {
  76. finalValue = scene.getMeshByName(intermediateValue.name);
  77. }
  78. else if (isVectorClassName(className)) {
  79. finalValue = parseVector(className, intermediateValue.value);
  80. }
  81. else if (className === "Matrix") {
  82. finalValue = Matrix.FromArray(intermediateValue.value);
  83. }
  84. else if (className === FlowGraphInteger.ClassName) {
  85. finalValue = FlowGraphInteger.Parse(intermediateValue);
  86. }
  87. else if (intermediateValue && intermediateValue.value !== undefined) {
  88. finalValue = intermediateValue.value;
  89. }
  90. else {
  91. finalValue = intermediateValue;
  92. }
  93. return finalValue;
  94. }
  95. /**
  96. * Given a name of a flow graph block class, return if this
  97. * class needs to be created with a path converter. Used in
  98. * parsing.
  99. * @param className the name of the flow graph block class
  100. * @returns a boolean indicating if the class needs a path converter
  101. */
  102. export function needsPathConverter(className) {
  103. // I am not using the ClassName property here because it was causing a circular dependency
  104. // that jest didn't like!
  105. return className === "FGSetPropertyBlock" || className === "FGGetPropertyBlock" || className === "FGPlayAnimationBlock" || className === "FGMeshPickEventBlock";
  106. }
  107. //# sourceMappingURL=serialization.js.map