nodeGeometryBlock.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  1. import { __decorate } from "../../tslib.es6.js";
  2. import { GetClass } from "../../Misc/typeStore.js";
  3. import { serialize } from "../../Misc/decorators.js";
  4. import { UniqueIdGenerator } from "../../Misc/uniqueIdGenerator.js";
  5. import { NodeGeometryConnectionPoint, NodeGeometryConnectionPointDirection } from "./nodeGeometryBlockConnectionPoint.js";
  6. import { Observable } from "../../Misc/observable.js";
  7. import { PrecisionDate } from "../../Misc/precisionDate.js";
  8. import { Logger } from "../../Misc/logger.js";
  9. /**
  10. * Defines a block that can be used inside a node based geometry
  11. */
  12. export class NodeGeometryBlock {
  13. /**
  14. * Gets the time spent to build this block (in ms)
  15. */
  16. get buildExecutionTime() {
  17. return this._buildExecutionTime;
  18. }
  19. /**
  20. * Gets the list of input points
  21. */
  22. get inputs() {
  23. return this._inputs;
  24. }
  25. /** Gets the list of output points */
  26. get outputs() {
  27. return this._outputs;
  28. }
  29. /**
  30. * Gets or set the name of the block
  31. */
  32. get name() {
  33. return this._name;
  34. }
  35. set name(value) {
  36. this._name = value;
  37. }
  38. /**
  39. * Gets a boolean indicating if this block is an input
  40. */
  41. get isInput() {
  42. return this._isInput;
  43. }
  44. /**
  45. * Gets a boolean indicating if this block is a teleport out
  46. */
  47. get isTeleportOut() {
  48. return this._isTeleportOut;
  49. }
  50. /**
  51. * Gets a boolean indicating if this block is a teleport in
  52. */
  53. get isTeleportIn() {
  54. return this._isTeleportIn;
  55. }
  56. /**
  57. * Gets a boolean indicating if this block is a debug block
  58. */
  59. get isDebug() {
  60. return this._isDebug;
  61. }
  62. /**
  63. * Gets a boolean indicating that this block can only be used once per NodeGeometry
  64. */
  65. get isUnique() {
  66. return this._isUnique;
  67. }
  68. /**
  69. * Gets the current class name e.g. "NodeGeometryBlock"
  70. * @returns the class name
  71. */
  72. getClassName() {
  73. return "NodeGeometryBlock";
  74. }
  75. _inputRename(name) {
  76. return name;
  77. }
  78. _outputRename(name) {
  79. return name;
  80. }
  81. /**
  82. * Checks if the current block is an ancestor of a given block
  83. * @param block defines the potential descendant block to check
  84. * @returns true if block is a descendant
  85. */
  86. isAnAncestorOf(block) {
  87. for (const output of this._outputs) {
  88. if (!output.hasEndpoints) {
  89. continue;
  90. }
  91. for (const endpoint of output.endpoints) {
  92. if (endpoint.ownerBlock === block) {
  93. return true;
  94. }
  95. if (endpoint.ownerBlock.isAnAncestorOf(block)) {
  96. return true;
  97. }
  98. }
  99. }
  100. return false;
  101. }
  102. /**
  103. * Checks if the current block is an ancestor of a given type
  104. * @param type defines the potential type to check
  105. * @returns true if block is a descendant
  106. */
  107. isAnAncestorOfType(type) {
  108. if (this.getClassName() === type) {
  109. return true;
  110. }
  111. for (const output of this._outputs) {
  112. if (!output.hasEndpoints) {
  113. continue;
  114. }
  115. for (const endpoint of output.endpoints) {
  116. if (endpoint.ownerBlock.isAnAncestorOfType(type)) {
  117. return true;
  118. }
  119. }
  120. }
  121. return false;
  122. }
  123. /**
  124. * Get the first descendant using a predicate
  125. * @param predicate defines the predicate to check
  126. * @returns descendant or null if none found
  127. */
  128. getDescendantOfPredicate(predicate) {
  129. if (predicate(this)) {
  130. return this;
  131. }
  132. for (const output of this._outputs) {
  133. if (!output.hasEndpoints) {
  134. continue;
  135. }
  136. for (const endpoint of output.endpoints) {
  137. const descendant = endpoint.ownerBlock.getDescendantOfPredicate(predicate);
  138. if (descendant) {
  139. return descendant;
  140. }
  141. }
  142. }
  143. return null;
  144. }
  145. /**
  146. * Creates a new NodeGeometryBlock
  147. * @param name defines the block name
  148. */
  149. constructor(name) {
  150. this._name = "";
  151. this._isInput = false;
  152. this._isTeleportOut = false;
  153. this._isTeleportIn = false;
  154. this._isDebug = false;
  155. this._isUnique = false;
  156. this._buildExecutionTime = 0;
  157. /**
  158. * Gets an observable raised when the block is built
  159. */
  160. this.onBuildObservable = new Observable();
  161. /** @internal */
  162. this._inputs = new Array();
  163. /** @internal */
  164. this._outputs = new Array();
  165. /** @internal */
  166. this._codeVariableName = "";
  167. /** Gets or sets a boolean indicating that this input can be edited from a collapsed frame */
  168. this.visibleOnFrame = false;
  169. this._name = name;
  170. this.uniqueId = UniqueIdGenerator.UniqueId;
  171. }
  172. /**
  173. * Register a new input. Must be called inside a block constructor
  174. * @param name defines the connection point name
  175. * @param type defines the connection point type
  176. * @param isOptional defines a boolean indicating that this input can be omitted
  177. * @param value value to return if there is no connection
  178. * @param valueMin min value accepted for value
  179. * @param valueMax max value accepted for value
  180. * @returns the current block
  181. */
  182. registerInput(name, type, isOptional = false, value, valueMin, valueMax) {
  183. const point = new NodeGeometryConnectionPoint(name, this, NodeGeometryConnectionPointDirection.Input);
  184. point.type = type;
  185. point.isOptional = isOptional;
  186. point.defaultValue = value;
  187. point.value = value;
  188. point.valueMin = valueMin;
  189. point.valueMax = valueMax;
  190. this._inputs.push(point);
  191. return this;
  192. }
  193. /**
  194. * Register a new output. Must be called inside a block constructor
  195. * @param name defines the connection point name
  196. * @param type defines the connection point type
  197. * @param point an already created connection point. If not provided, create a new one
  198. * @returns the current block
  199. */
  200. registerOutput(name, type, point) {
  201. point = point ?? new NodeGeometryConnectionPoint(name, this, NodeGeometryConnectionPointDirection.Output);
  202. point.type = type;
  203. this._outputs.push(point);
  204. return this;
  205. }
  206. // eslint-disable-next-line @typescript-eslint/no-unused-vars
  207. _buildBlock(state) {
  208. // Empty. Must be defined by child nodes
  209. }
  210. // eslint-disable-next-line @typescript-eslint/no-unused-vars
  211. _customBuildStep(state) {
  212. // Must be implemented by children
  213. }
  214. /**
  215. * Build the current node and generate the vertex data
  216. * @param state defines the current generation state
  217. * @returns true if already built
  218. */
  219. build(state) {
  220. if (this._buildId === state.buildId) {
  221. return true;
  222. }
  223. if (this._outputs.length > 0) {
  224. if (!this._outputs.some((o) => o.hasEndpoints) && !this.isDebug) {
  225. return false;
  226. }
  227. this.outputs.forEach((o) => o._resetCounters());
  228. }
  229. this._buildId = state.buildId;
  230. // Check if "parent" blocks are compiled
  231. for (const input of this._inputs) {
  232. if (!input.connectedPoint) {
  233. if (!input.isOptional) {
  234. // Emit a warning
  235. state.notConnectedNonOptionalInputs.push(input);
  236. }
  237. continue;
  238. }
  239. const block = input.connectedPoint.ownerBlock;
  240. if (block && block !== this) {
  241. block.build(state);
  242. }
  243. }
  244. this._customBuildStep(state);
  245. // Logs
  246. if (state.verbose) {
  247. Logger.Log(`Building ${this.name} [${this.getClassName()}]`);
  248. }
  249. const now = PrecisionDate.Now;
  250. this._buildBlock(state);
  251. this._buildExecutionTime = PrecisionDate.Now - now;
  252. // Compile connected blocks
  253. for (const output of this._outputs) {
  254. for (const endpoint of output.endpoints) {
  255. const block = endpoint.ownerBlock;
  256. if (block) {
  257. block.build(state);
  258. }
  259. }
  260. }
  261. this.onBuildObservable.notifyObservers(this);
  262. return false;
  263. }
  264. _linkConnectionTypes(inputIndex0, inputIndex1, looseCoupling = false) {
  265. if (looseCoupling) {
  266. this._inputs[inputIndex1]._acceptedConnectionPointType = this._inputs[inputIndex0];
  267. }
  268. else {
  269. this._inputs[inputIndex0]._linkedConnectionSource = this._inputs[inputIndex1];
  270. }
  271. this._inputs[inputIndex1]._linkedConnectionSource = this._inputs[inputIndex0];
  272. }
  273. /**
  274. * Initialize the block and prepare the context for build
  275. */
  276. initialize() {
  277. // Do nothing
  278. }
  279. /**
  280. * Lets the block try to connect some inputs automatically
  281. */
  282. autoConfigure() {
  283. // Do nothing
  284. }
  285. /**
  286. * Find an input by its name
  287. * @param name defines the name of the input to look for
  288. * @returns the input or null if not found
  289. */
  290. getInputByName(name) {
  291. const filter = this._inputs.filter((e) => e.name === name);
  292. if (filter.length) {
  293. return filter[0];
  294. }
  295. return null;
  296. }
  297. /**
  298. * Find an output by its name
  299. * @param name defines the name of the output to look for
  300. * @returns the output or null if not found
  301. */
  302. getOutputByName(name) {
  303. const filter = this._outputs.filter((e) => e.name === name);
  304. if (filter.length) {
  305. return filter[0];
  306. }
  307. return null;
  308. }
  309. /**
  310. * Serializes this block in a JSON representation
  311. * @returns the serialized block object
  312. */
  313. serialize() {
  314. const serializationObject = {};
  315. serializationObject.customType = "BABYLON." + this.getClassName();
  316. serializationObject.id = this.uniqueId;
  317. serializationObject.name = this.name;
  318. serializationObject.inputs = [];
  319. serializationObject.outputs = [];
  320. for (const input of this.inputs) {
  321. serializationObject.inputs.push(input.serialize());
  322. }
  323. for (const output of this.outputs) {
  324. serializationObject.outputs.push(output.serialize(false));
  325. }
  326. return serializationObject;
  327. }
  328. /**
  329. * @internal
  330. */
  331. _deserialize(serializationObject) {
  332. this._name = serializationObject.name;
  333. this.comments = serializationObject.comments;
  334. this.visibleOnFrame = !!serializationObject.visibleOnFrame;
  335. this._deserializePortDisplayNamesAndExposedOnFrame(serializationObject);
  336. }
  337. _deserializePortDisplayNamesAndExposedOnFrame(serializationObject) {
  338. const serializedInputs = serializationObject.inputs;
  339. const serializedOutputs = serializationObject.outputs;
  340. if (serializedInputs) {
  341. serializedInputs.forEach((port) => {
  342. const input = this.inputs.find((i) => i.name === port.name);
  343. if (!input) {
  344. return;
  345. }
  346. if (port.displayName) {
  347. input.displayName = port.displayName;
  348. }
  349. if (port.isExposedOnFrame) {
  350. input.isExposedOnFrame = port.isExposedOnFrame;
  351. input.exposedPortPosition = port.exposedPortPosition;
  352. }
  353. if (port.value !== undefined && port.value !== null) {
  354. if (port.valueType === "number") {
  355. input.value = port.value;
  356. }
  357. else {
  358. const valueType = GetClass(port.valueType);
  359. if (valueType) {
  360. input.value = valueType.FromArray(port.value);
  361. }
  362. }
  363. }
  364. });
  365. }
  366. if (serializedOutputs) {
  367. serializedOutputs.forEach((port, i) => {
  368. if (port.displayName) {
  369. this.outputs[i].displayName = port.displayName;
  370. }
  371. if (port.isExposedOnFrame) {
  372. this.outputs[i].isExposedOnFrame = port.isExposedOnFrame;
  373. this.outputs[i].exposedPortPosition = port.exposedPortPosition;
  374. }
  375. });
  376. }
  377. }
  378. _dumpPropertiesCode() {
  379. const variableName = this._codeVariableName;
  380. return `${variableName}.visibleOnFrame = ${this.visibleOnFrame};\n`;
  381. }
  382. /**
  383. * @internal
  384. */
  385. _dumpCodeForOutputConnections(alreadyDumped) {
  386. let codeString = "";
  387. if (alreadyDumped.indexOf(this) !== -1) {
  388. return codeString;
  389. }
  390. alreadyDumped.push(this);
  391. for (const input of this.inputs) {
  392. if (!input.isConnected) {
  393. continue;
  394. }
  395. const connectedOutput = input.connectedPoint;
  396. const connectedBlock = connectedOutput.ownerBlock;
  397. codeString += connectedBlock._dumpCodeForOutputConnections(alreadyDumped);
  398. codeString += `${connectedBlock._codeVariableName}.${connectedBlock._outputRename(connectedOutput.name)}.connectTo(${this._codeVariableName}.${this._inputRename(input.name)});\n`;
  399. }
  400. return codeString;
  401. }
  402. /**
  403. * @internal
  404. */
  405. _dumpCode(uniqueNames, alreadyDumped) {
  406. alreadyDumped.push(this);
  407. // Get unique name
  408. const nameAsVariableName = this.name.replace(/[^A-Za-z_]+/g, "");
  409. this._codeVariableName = nameAsVariableName || `${this.getClassName()}_${this.uniqueId}`;
  410. if (uniqueNames.indexOf(this._codeVariableName) !== -1) {
  411. let index = 0;
  412. do {
  413. index++;
  414. this._codeVariableName = nameAsVariableName + index;
  415. } while (uniqueNames.indexOf(this._codeVariableName) !== -1);
  416. }
  417. uniqueNames.push(this._codeVariableName);
  418. // Declaration
  419. let codeString = `\n// ${this.getClassName()}\n`;
  420. if (this.comments) {
  421. codeString += `// ${this.comments}\n`;
  422. }
  423. const className = this.getClassName();
  424. if (className === "GeometryInputBlock") {
  425. const block = this;
  426. const blockType = block.type;
  427. codeString += `var ${this._codeVariableName} = new BABYLON.GeometryInputBlock("${this.name}", ${blockType});\n`;
  428. }
  429. else {
  430. codeString += `var ${this._codeVariableName} = new BABYLON.${className}("${this.name}");\n`;
  431. }
  432. // Properties
  433. codeString += this._dumpPropertiesCode();
  434. // Inputs
  435. for (const input of this.inputs) {
  436. if (!input.isConnected) {
  437. continue;
  438. }
  439. const connectedOutput = input.connectedPoint;
  440. const connectedBlock = connectedOutput.ownerBlock;
  441. if (alreadyDumped.indexOf(connectedBlock) === -1) {
  442. codeString += connectedBlock._dumpCode(uniqueNames, alreadyDumped);
  443. }
  444. }
  445. // Outputs
  446. for (const output of this.outputs) {
  447. if (!output.hasEndpoints) {
  448. continue;
  449. }
  450. for (const endpoint of output.endpoints) {
  451. const connectedBlock = endpoint.ownerBlock;
  452. if (connectedBlock && alreadyDumped.indexOf(connectedBlock) === -1) {
  453. codeString += connectedBlock._dumpCode(uniqueNames, alreadyDumped);
  454. }
  455. }
  456. }
  457. return codeString;
  458. }
  459. /**
  460. * Clone the current block to a new identical block
  461. * @returns a copy of the current block
  462. */
  463. clone() {
  464. const serializationObject = this.serialize();
  465. const blockType = GetClass(serializationObject.customType);
  466. if (blockType) {
  467. const block = new blockType();
  468. block._deserialize(serializationObject);
  469. return block;
  470. }
  471. return null;
  472. }
  473. /**
  474. * Release resources
  475. */
  476. dispose() {
  477. for (const input of this.inputs) {
  478. input.dispose();
  479. }
  480. for (const output of this.outputs) {
  481. output.dispose();
  482. }
  483. this.onBuildObservable.clear();
  484. }
  485. }
  486. __decorate([
  487. serialize("comment")
  488. ], NodeGeometryBlock.prototype, "comments", void 0);
  489. //# sourceMappingURL=nodeGeometryBlock.js.map