@galacean/effects-specification 2.0.0-alpha.0 → 2.0.0-alpha.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/fallback.mjs CHANGED
@@ -2,9 +2,74 @@
2
2
  * Name: @galacean/effects-specification
3
3
  * Description: Galacean Effects JSON Specification
4
4
  * Author: Ant Group CO., Ltd.
5
- * Version: v2.0.0-alpha.0
5
+ * Version: v2.0.0-alpha.1
6
6
  */
7
7
 
8
+ const randomUUID = typeof crypto !== 'undefined' && crypto.randomUUID && crypto.randomUUID.bind(crypto);
9
+ var native = {
10
+ randomUUID
11
+ };
12
+
13
+ // Unique ID creation requires a high quality random # generator. In the browser we therefore
14
+ // require the crypto API and do not support built-in fallback to lower quality random number
15
+ // generators (like Math.random()).
16
+ let getRandomValues;
17
+ const rnds8 = new Uint8Array(16);
18
+ function rng() {
19
+ // lazy load so that environments that need to polyfill have a chance to do so
20
+ if (!getRandomValues) {
21
+ // getRandomValues needs to be invoked in a context where "this" is a Crypto implementation.
22
+ getRandomValues = typeof crypto !== 'undefined' && crypto.getRandomValues && crypto.getRandomValues.bind(crypto);
23
+
24
+ if (!getRandomValues) {
25
+ throw new Error('crypto.getRandomValues() not supported. See https://github.com/uuidjs/uuid#getrandomvalues-not-supported');
26
+ }
27
+ }
28
+
29
+ return getRandomValues(rnds8);
30
+ }
31
+
32
+ /**
33
+ * Convert array of 16 byte values to UUID string format of the form:
34
+ * XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX
35
+ */
36
+
37
+ const byteToHex = [];
38
+
39
+ for (let i = 0; i < 256; ++i) {
40
+ byteToHex.push((i + 0x100).toString(16).slice(1));
41
+ }
42
+
43
+ function unsafeStringify(arr, offset = 0) {
44
+ // Note: Be careful editing this code! It's been tuned for performance
45
+ // and works in ways you may not expect. See https://github.com/uuidjs/uuid/pull/434
46
+ return byteToHex[arr[offset + 0]] + byteToHex[arr[offset + 1]] + byteToHex[arr[offset + 2]] + byteToHex[arr[offset + 3]] + '-' + byteToHex[arr[offset + 4]] + byteToHex[arr[offset + 5]] + '-' + byteToHex[arr[offset + 6]] + byteToHex[arr[offset + 7]] + '-' + byteToHex[arr[offset + 8]] + byteToHex[arr[offset + 9]] + '-' + byteToHex[arr[offset + 10]] + byteToHex[arr[offset + 11]] + byteToHex[arr[offset + 12]] + byteToHex[arr[offset + 13]] + byteToHex[arr[offset + 14]] + byteToHex[arr[offset + 15]];
47
+ }
48
+
49
+ function v4(options, buf, offset) {
50
+ if (native.randomUUID && !buf && !options) {
51
+ return native.randomUUID();
52
+ }
53
+
54
+ options = options || {};
55
+ const rnds = options.random || (options.rng || rng)(); // Per 4.4, set bits for version and `clock_seq_hi_and_reserved`
56
+
57
+ rnds[6] = rnds[6] & 0x0f | 0x40;
58
+ rnds[8] = rnds[8] & 0x3f | 0x80; // Copy bytes to buffer, if provided
59
+
60
+ if (buf) {
61
+ offset = offset || 0;
62
+
63
+ for (let i = 0; i < 16; ++i) {
64
+ buf[offset + i] = rnds[i];
65
+ }
66
+
67
+ return buf;
68
+ }
69
+
70
+ return unsafeStringify(rnds);
71
+ }
72
+
8
73
  /*********************************************/
9
74
  /* 元素属性参数类型 */
10
75
  /*********************************************/
@@ -267,6 +332,10 @@ var ItemType;
267
332
  * 天空盒元素
268
333
  */
269
334
  ItemType["skybox"] = "skybox";
335
+ /**
336
+ * 特效元素
337
+ */
338
+ ItemType["effect"] = "effect";
270
339
  })(ItemType || (ItemType = {}));
271
340
  /**
272
341
  * 渲染模式
@@ -379,6 +448,10 @@ var CompositionEndBehavior;
379
448
  * 销毁并保留最后一帧
380
449
  */
381
450
  CompositionEndBehavior[CompositionEndBehavior["pause_destroy"] = END_BEHAVIOR_PAUSE_AND_DESTROY] = "pause_destroy";
451
+ /**
452
+ * 冻结
453
+ */
454
+ CompositionEndBehavior[CompositionEndBehavior["freeze"] = END_BEHAVIOR_FREEZE] = "freeze";
382
455
  })(CompositionEndBehavior || (CompositionEndBehavior = {}));
383
456
 
384
457
  /*********************************************/
@@ -763,11 +836,22 @@ function __read(o, n) {
763
836
  return ar;
764
837
  }
765
838
 
839
+ function __spreadArray(to, from, pack) {
840
+ if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
841
+ if (ar || !(i in from)) {
842
+ if (!ar) ar = Array.prototype.slice.call(from, 0, i);
843
+ ar[i] = from[i];
844
+ }
845
+ }
846
+ return to.concat(ar || Array.prototype.slice.call(from));
847
+ }
848
+
766
849
  typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
767
850
  var e = new Error(message);
768
851
  return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
769
852
  };
770
853
 
854
+ var _a;
771
855
  function arrAdd(arr, item) {
772
856
  if (!arr.includes(item)) {
773
857
  arr.push(item);
@@ -1003,6 +1087,34 @@ function rotationZYXFromQuat(out, quat) {
1003
1087
  }
1004
1088
  return out;
1005
1089
  }
1090
+ function generateGUID() {
1091
+ return v4().replace(/-/g, '');
1092
+ }
1093
+ /**
1094
+ * 提取并转换 JSON 数据中的 anchor 值
1095
+ */
1096
+ function convertAnchor(anchor, particleOrigin) {
1097
+ if (anchor) {
1098
+ return [anchor[0] - 0.5, 0.5 - anchor[1]];
1099
+ }
1100
+ else if (particleOrigin) {
1101
+ return particleOriginTranslateMap[particleOrigin];
1102
+ }
1103
+ else {
1104
+ return [0, 0];
1105
+ }
1106
+ }
1107
+ var particleOriginTranslateMap = (_a = {},
1108
+ _a[ParticleOrigin.PARTICLE_ORIGIN_CENTER] = [0, 0],
1109
+ _a[ParticleOrigin.PARTICLE_ORIGIN_CENTER_BOTTOM] = [0, -0.5],
1110
+ _a[ParticleOrigin.PARTICLE_ORIGIN_CENTER_TOP] = [0, 0.5],
1111
+ _a[ParticleOrigin.PARTICLE_ORIGIN_LEFT_TOP] = [-0.5, 0.5],
1112
+ _a[ParticleOrigin.PARTICLE_ORIGIN_LEFT_CENTER] = [-0.5, 0],
1113
+ _a[ParticleOrigin.PARTICLE_ORIGIN_LEFT_BOTTOM] = [-0.5, -0.5],
1114
+ _a[ParticleOrigin.PARTICLE_ORIGIN_RIGHT_CENTER] = [0.5, 0],
1115
+ _a[ParticleOrigin.PARTICLE_ORIGIN_RIGHT_BOTTOM] = [0.5, -0.5],
1116
+ _a[ParticleOrigin.PARTICLE_ORIGIN_RIGHT_TOP] = [0.5, 0.5],
1117
+ _a);
1006
1118
 
1007
1119
  function getStandardParticleContent(particle) {
1008
1120
  var _a;
@@ -1414,6 +1526,309 @@ function version22Migration(json) {
1414
1526
  });
1415
1527
  return json;
1416
1528
  }
1529
+ /**
1530
+ * 3.0 以下版本数据适配(runtime 2.0及以上版本支持)
1531
+ */
1532
+ function version30Migration(json) {
1533
+ var e_1, _a;
1534
+ var _b, _c, _d, _e;
1535
+ var result = Object.assign({}, json, {
1536
+ items: [],
1537
+ components: [],
1538
+ materials: [],
1539
+ shaders: [],
1540
+ geometries: [],
1541
+ });
1542
+ // 兼容老版本数据中不存在textures的情况
1543
+ (_b = result.textures) !== null && _b !== void 0 ? _b : (result.textures = []);
1544
+ result.textures.forEach(function (textureOptions) {
1545
+ Object.assign(textureOptions, {
1546
+ id: generateGUID(),
1547
+ dataType: DataType.Texture,
1548
+ });
1549
+ });
1550
+ if (result.textures.length < result.images.length) {
1551
+ for (var i = result.textures.length; i < result.images.length; i++) {
1552
+ result.textures.push({
1553
+ //@ts-expect-error
1554
+ id: generateGUID(),
1555
+ dataType: DataType.Texture,
1556
+ source: i,
1557
+ flipY: true,
1558
+ });
1559
+ }
1560
+ }
1561
+ var _loop_1 = function (composition) {
1562
+ var e_2, _h, e_3, _j;
1563
+ // composition 的 endbehaviour 兼容
1564
+ if (composition.endBehavior === END_BEHAVIOR_PAUSE_AND_DESTROY || composition.endBehavior === END_BEHAVIOR_PAUSE) {
1565
+ composition.endBehavior = END_BEHAVIOR_FREEZE;
1566
+ }
1567
+ var itemGuidMap = {};
1568
+ try {
1569
+ for (var _k = (e_2 = void 0, __values(composition.items)), _l = _k.next(); !_l.done; _l = _k.next()) {
1570
+ var item = _l.value;
1571
+ itemGuidMap[item.id] = generateGUID();
1572
+ // TODO: 编辑器测试用,上线后删除
1573
+ //@ts-expect-error
1574
+ item.oldId = item.id;
1575
+ item.id = itemGuidMap[item.id];
1576
+ }
1577
+ }
1578
+ catch (e_2_1) { e_2 = { error: e_2_1 }; }
1579
+ finally {
1580
+ try {
1581
+ if (_l && !_l.done && (_h = _k.return)) _h.call(_k);
1582
+ }
1583
+ finally { if (e_2) throw e_2.error; }
1584
+ }
1585
+ composition.items.forEach(function (item, index) {
1586
+ if (item.parentId) {
1587
+ if (item.parentId.includes('^')) {
1588
+ var parentId = (item.parentId).split('^')[0];
1589
+ var nodeId = (item.parentId).split('^')[1];
1590
+ item.parentId = itemGuidMap[parentId] + '^' + nodeId;
1591
+ }
1592
+ else {
1593
+ item.parentId = itemGuidMap[item.parentId];
1594
+ }
1595
+ }
1596
+ // @ts-expect-error fix item type
1597
+ result.items.push(item);
1598
+ // @ts-expect-error fix item type
1599
+ composition.items[index] = { id: item.id };
1600
+ });
1601
+ try {
1602
+ for (var _m = (e_3 = void 0, __values(result.items)), _o = _m.next(); !_o.done; _o = _m.next()) {
1603
+ var item = _o.value;
1604
+ // 原 texture 索引转为统一 guid 索引
1605
+ if (item.content) {
1606
+ if (item.content.renderer) {
1607
+ if (item.content.renderer.texture !== undefined) {
1608
+ var oldTextureId = item.content.renderer.texture;
1609
+ //@ts-expect-error
1610
+ item.content.renderer.texture = { id: result.textures[oldTextureId].id };
1611
+ }
1612
+ }
1613
+ if (item.content.trails) {
1614
+ if (item.content.trails.texture !== undefined) {
1615
+ var oldTextureId = item.content.trails.texture;
1616
+ //@ts-expect-error
1617
+ item.content.trails.texture = { id: result.textures[oldTextureId].id };
1618
+ }
1619
+ }
1620
+ }
1621
+ // item 的 transform 属性由数组转为 {x:n, y:n, z:n}
1622
+ if (item.transform) {
1623
+ //@ts-expect-error
1624
+ var position = __spreadArray([], __read((_c = item.transform.position) !== null && _c !== void 0 ? _c : [0, 0, 0]), false);
1625
+ //@ts-expect-error
1626
+ var rotation = __spreadArray([], __read((_d = item.transform.rotation) !== null && _d !== void 0 ? _d : [0, 0, 0]), false);
1627
+ //@ts-expect-error
1628
+ var scale = __spreadArray([], __read((_e = item.transform.scale) !== null && _e !== void 0 ? _e : [1, 1, 1]), false);
1629
+ Object.assign(item, {
1630
+ transform: {
1631
+ position: { x: position[0], y: position[1], z: position[2] },
1632
+ rotation: { x: rotation[0], y: rotation[1], z: rotation[2] },
1633
+ scale: { x: scale[0], y: scale[1], z: scale[0] },
1634
+ },
1635
+ });
1636
+ // sprite 的 scale 转为 size
1637
+ if (item.type === ItemType.sprite) {
1638
+ item.transform.size = { x: scale[0], y: scale[1] };
1639
+ item.transform.scale = { x: 1, y: 1, z: 1 };
1640
+ }
1641
+ // sprite 的 anchor 修正
1642
+ if (item.type === ItemType.sprite) {
1643
+ var content = item.content;
1644
+ if (!content.renderer) {
1645
+ content.renderer = {};
1646
+ }
1647
+ var renderer = content.renderer;
1648
+ var realAnchor = convertAnchor(renderer.anchor, renderer.particleOrigin);
1649
+ var startSize = item.transform.size;
1650
+ // 兼容旧JSON(anchor和particleOrigin可能同时存在)
1651
+ if (!renderer.anchor && renderer.particleOrigin !== undefined) {
1652
+ //@ts-expect-error
1653
+ item.transform.position.x += -realAnchor[0] * startSize.x;
1654
+ //@ts-expect-error
1655
+ item.transform.position.y += -realAnchor[1] * startSize.y;
1656
+ }
1657
+ //@ts-expect-error
1658
+ item.transform.anchor = { x: realAnchor[0] * startSize.x, y: realAnchor[1] * startSize.y };
1659
+ }
1660
+ }
1661
+ if (item.type === ItemType.particle) {
1662
+ var content = item.content;
1663
+ if (!content.renderer) {
1664
+ content.renderer = {};
1665
+ }
1666
+ var renderer = content.renderer;
1667
+ content.renderer.anchor = convertAnchor(renderer.anchor, renderer.particleOrigin);
1668
+ }
1669
+ // 动画数据转化 TODO: 动画数据移到 TimelineComponentData
1670
+ item.content.tracks = [];
1671
+ var tracks = item.content.tracks;
1672
+ if (item.type !== ItemType.particle) {
1673
+ tracks.push({
1674
+ clips: [
1675
+ {
1676
+ dataType: 'TransformAnimationPlayableAsset',
1677
+ animationClip: {
1678
+ sizeOverLifetime: item.content.sizeOverLifetime,
1679
+ rotationOverLifetime: item.content.rotationOverLifetime,
1680
+ positionOverLifetime: item.content.positionOverLifetime,
1681
+ },
1682
+ },
1683
+ ],
1684
+ });
1685
+ }
1686
+ if (item.type === ItemType.sprite) {
1687
+ tracks.push({
1688
+ clips: [
1689
+ {
1690
+ dataType: 'SpriteColorAnimationPlayableAsset',
1691
+ animationClip: {
1692
+ colorOverLifetime: item.content.colorOverLifetime,
1693
+ startColor: item.content.options.startColor,
1694
+ },
1695
+ },
1696
+ ],
1697
+ });
1698
+ }
1699
+ // gizmo 的 target id 转换为新的 item guid
1700
+ if (item.content.options.target) {
1701
+ item.content.options.target = itemGuidMap[item.content.options.target];
1702
+ }
1703
+ // item 的 content 转为 component data 加入 JSONScene.components
1704
+ var uuid = generateGUID();
1705
+ if (item.type === ItemType.sprite) {
1706
+ item.components = [];
1707
+ result.components.push(item.content);
1708
+ item.content.id = uuid;
1709
+ item.content.dataType = DataType.SpriteComponent;
1710
+ item.content.item = { id: item.id };
1711
+ item.dataType = DataType.VFXItemData;
1712
+ //@ts-expect-error
1713
+ item.components.push({ id: item.content.id });
1714
+ }
1715
+ else if (item.type === ItemType.particle) {
1716
+ item.components = [];
1717
+ result.components.push(item.content);
1718
+ item.content.id = uuid;
1719
+ item.content.dataType = DataType.ParticleSystem;
1720
+ item.content.item = { id: item.id };
1721
+ item.dataType = DataType.VFXItemData;
1722
+ //@ts-expect-error
1723
+ item.components.push({ id: item.content.id });
1724
+ }
1725
+ else if (item.type === ItemType.mesh) {
1726
+ item.components = [];
1727
+ result.components.push(item.content);
1728
+ item.content.id = uuid;
1729
+ item.content.dataType = DataType.MeshComponent;
1730
+ item.content.item = { id: item.id };
1731
+ item.dataType = DataType.VFXItemData;
1732
+ //@ts-expect-error
1733
+ item.components.push({ id: item.content.id });
1734
+ }
1735
+ else if (item.type === ItemType.skybox) {
1736
+ item.components = [];
1737
+ result.components.push(item.content);
1738
+ item.content.id = uuid;
1739
+ item.content.dataType = DataType.SkyboxComponent;
1740
+ item.content.item = { id: item.id };
1741
+ item.dataType = DataType.VFXItemData;
1742
+ //@ts-expect-error
1743
+ item.components.push({ id: item.content.id });
1744
+ }
1745
+ else if (item.type === ItemType.light) {
1746
+ item.components = [];
1747
+ result.components.push(item.content);
1748
+ item.content.id = uuid;
1749
+ item.content.dataType = DataType.LightComponent;
1750
+ item.content.item = { id: item.id };
1751
+ item.dataType = DataType.VFXItemData;
1752
+ //@ts-expect-error
1753
+ item.components.push({ id: item.content.id });
1754
+ }
1755
+ else if (item.type === 'camera') {
1756
+ item.components = [];
1757
+ result.components.push(item.content);
1758
+ item.content.id = uuid;
1759
+ item.content.dataType = DataType.CameraComponent;
1760
+ item.content.item = { id: item.id };
1761
+ item.dataType = DataType.VFXItemData;
1762
+ //@ts-expect-error
1763
+ item.components.push({ id: item.content.id });
1764
+ }
1765
+ else if (item.type === ItemType.tree) {
1766
+ item.components = [];
1767
+ result.components.push(item.content);
1768
+ item.content.id = uuid;
1769
+ item.content.dataType = DataType.TreeComponent;
1770
+ item.content.item = { id: item.id };
1771
+ item.dataType = DataType.VFXItemData;
1772
+ //@ts-expect-error
1773
+ item.components.push({ id: item.content.id });
1774
+ }
1775
+ else if (item.type === ItemType.interact) {
1776
+ item.components = [];
1777
+ result.components.push(item.content);
1778
+ item.content.id = uuid;
1779
+ item.content.dataType = DataType.InteractComponent;
1780
+ item.content.item = { id: item.id };
1781
+ item.dataType = DataType.VFXItemData;
1782
+ //@ts-expect-error
1783
+ item.components.push({ id: item.content.id });
1784
+ }
1785
+ else if (item.type === ItemType.camera) {
1786
+ item.components = [];
1787
+ result.components.push(item.content);
1788
+ item.content.id = uuid;
1789
+ item.content.dataType = DataType.CameraController;
1790
+ item.content.item = { id: item.id };
1791
+ item.dataType = DataType.VFXItemData;
1792
+ //@ts-expect-error
1793
+ item.components.push({ id: item.content.id });
1794
+ }
1795
+ else if (item.type === ItemType.text) {
1796
+ item.components = [];
1797
+ result.components.push(item.content);
1798
+ item.content.id = uuid;
1799
+ item.content.dataType = DataType.TextComponent;
1800
+ item.content.item = { id: item.id };
1801
+ item.dataType = DataType.VFXItemData;
1802
+ //@ts-expect-error
1803
+ item.components.push({ id: item.content.id });
1804
+ }
1805
+ }
1806
+ }
1807
+ catch (e_3_1) { e_3 = { error: e_3_1 }; }
1808
+ finally {
1809
+ try {
1810
+ if (_o && !_o.done && (_j = _m.return)) _j.call(_m);
1811
+ }
1812
+ finally { if (e_3) throw e_3.error; }
1813
+ }
1814
+ };
1815
+ try {
1816
+ // 更正Composition.endBehavior
1817
+ for (var _f = __values(json.compositions), _g = _f.next(); !_g.done; _g = _f.next()) {
1818
+ var composition = _g.value;
1819
+ _loop_1(composition);
1820
+ }
1821
+ }
1822
+ catch (e_1_1) { e_1 = { error: e_1_1 }; }
1823
+ finally {
1824
+ try {
1825
+ if (_g && !_g.done && (_a = _f.return)) _a.call(_f);
1826
+ }
1827
+ finally { if (e_1) throw e_1.error; }
1828
+ }
1829
+ result.version = '3.0';
1830
+ return result;
1831
+ }
1417
1832
 
1418
1833
  var v0 = /^(\d+)\.(\d+)\.(\d+)(-(\w+)\.\d+)?$/;
1419
1834
  var standardVersion = /^(\d+)\.(\d+)$/;
@@ -1423,16 +1838,16 @@ function getStandardJSON(json) {
1423
1838
  if (!json || typeof json !== 'object') {
1424
1839
  throw Error('expect a json object');
1425
1840
  }
1426
- // 修正老版本数据中,meshItem以及lightItem结束行为错误问题
1841
+ // 修正老版本数据中,meshItem 以及 lightItem 结束行为错误问题
1427
1842
  version22Migration(json);
1428
1843
  if (v0.test(json.version)) {
1429
1844
  reverseParticle = ((_a = (/^(\d+)/).exec(json.version)) === null || _a === void 0 ? void 0 : _a[0]) === '0';
1430
- return version21Migration(getStandardJSONFromV0(json));
1845
+ return version30Migration(version21Migration(getStandardJSONFromV0(json)));
1431
1846
  }
1432
1847
  var mainVersion = (_b = standardVersion.exec(json.version)) === null || _b === void 0 ? void 0 : _b[1];
1433
1848
  if (mainVersion) {
1434
- if (Number(mainVersion) < 2) {
1435
- return version21Migration(json);
1849
+ if (Number(mainVersion) < 3) {
1850
+ return version30Migration(version21Migration(json));
1436
1851
  }
1437
1852
  return json;
1438
1853
  }
@@ -1702,5 +2117,5 @@ function getStandardItem(item, opt) {
1702
2117
  }
1703
2118
  }
1704
2119
 
1705
- export { arrAdd, colorToArr, deleteEmptyValue, ensureColorExpression, ensureFixedNumber, ensureFixedNumberWithRandom, ensureFixedVec3, ensureGradient, ensureNumberExpression, ensureRGBAValue, ensureValueGetter, forEach, getGradientColor, getStandardComposition, getStandardImage, getStandardItem, getStandardJSON, normalizeColor, objectValueToNumber, parsePercent, quatFromXYZRotation, rotationZYXFromQuat };
2120
+ export { arrAdd, colorToArr, convertAnchor, deleteEmptyValue, ensureColorExpression, ensureFixedNumber, ensureFixedNumberWithRandom, ensureFixedVec3, ensureGradient, ensureNumberExpression, ensureRGBAValue, ensureValueGetter, forEach, generateGUID, getGradientColor, getStandardComposition, getStandardImage, getStandardItem, getStandardJSON, normalizeColor, objectValueToNumber, parsePercent, particleOriginTranslateMap, quatFromXYZRotation, rotationZYXFromQuat };
1706
2121
  //# sourceMappingURL=fallback.mjs.map