nodeMaterialBlockConnectionPoint.ts 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  1. import { NodeMaterialBlockConnectionPointTypes } from './Enums/nodeMaterialBlockConnectionPointTypes';
  2. import { NodeMaterialBlockTargets } from './Enums/nodeMaterialBlockTargets';
  3. import { Nullable } from '../../types';
  4. import { InputBlock } from './Blocks/Input/inputBlock';
  5. import { Observable } from '../../Misc/observable';
  6. declare type NodeMaterialBlock = import("./nodeMaterialBlock").NodeMaterialBlock;
  7. /**
  8. * Enum used to define the compatibility state between two connection points
  9. */
  10. export enum NodeMaterialConnectionPointCompatibilityStates {
  11. /** Points are compatibles */
  12. Compatible,
  13. /** Points are incompatible because of their types */
  14. TypeIncompatible,
  15. /** Points are incompatible because of their targets (vertex vs fragment) */
  16. TargetIncompatible
  17. }
  18. /**
  19. * Defines the direction of a connection point
  20. */
  21. export enum NodeMaterialConnectionPointDirection {
  22. /** Input */
  23. Input,
  24. /** Output */
  25. Output
  26. }
  27. /**
  28. * Defines a connection point for a block
  29. */
  30. export class NodeMaterialConnectionPoint {
  31. /** @hidden */
  32. public _ownerBlock: NodeMaterialBlock;
  33. /** @hidden */
  34. public _connectedPoint: Nullable<NodeMaterialConnectionPoint> = null;
  35. private _endpoints = new Array<NodeMaterialConnectionPoint>();
  36. private _associatedVariableName: string;
  37. private _direction: NodeMaterialConnectionPointDirection;
  38. /** @hidden */
  39. public _typeConnectionSource: Nullable<NodeMaterialConnectionPoint> = null;
  40. /** @hidden */
  41. public _linkedConnectionSource: Nullable<NodeMaterialConnectionPoint> = null;
  42. private _type = NodeMaterialBlockConnectionPointTypes.Float;
  43. /** @hidden */
  44. public _enforceAssociatedVariableName = false;
  45. /** Gets the direction of the point */
  46. public get direction() {
  47. return this._direction;
  48. }
  49. /** Indicates that this connection point needs dual validation before being connected to another point */
  50. public needDualDirectionValidation: boolean = false;
  51. /**
  52. * Gets or sets the additional types supported by this connection point
  53. */
  54. public acceptedConnectionPointTypes = new Array<NodeMaterialBlockConnectionPointTypes>();
  55. /**
  56. * Gets or sets the additional types excluded by this connection point
  57. */
  58. public excludedConnectionPointTypes = new Array<NodeMaterialBlockConnectionPointTypes>();
  59. /**
  60. * Observable triggered when this point is connected
  61. */
  62. public onConnectionObservable = new Observable<NodeMaterialConnectionPoint>();
  63. /**
  64. * Gets or sets the associated variable name in the shader
  65. */
  66. public get associatedVariableName(): string {
  67. if (this._ownerBlock.isInput) {
  68. return (this._ownerBlock as InputBlock).associatedVariableName;
  69. }
  70. if ((!this._enforceAssociatedVariableName || !this._associatedVariableName) && this._connectedPoint) {
  71. return this._connectedPoint.associatedVariableName;
  72. }
  73. return this._associatedVariableName;
  74. }
  75. public set associatedVariableName(value: string) {
  76. this._associatedVariableName = value;
  77. }
  78. /** Get the inner type (ie AutoDetect for instance instead of the inferred one) */
  79. public get innerType() {
  80. if (this._linkedConnectionSource && this._linkedConnectionSource.isConnected) {
  81. return this.type;
  82. }
  83. return this._type;
  84. }
  85. /**
  86. * Gets or sets the connection point type (default is float)
  87. */
  88. public get type(): NodeMaterialBlockConnectionPointTypes {
  89. if (this._type === NodeMaterialBlockConnectionPointTypes.AutoDetect) {
  90. if (this._ownerBlock.isInput) {
  91. return (this._ownerBlock as InputBlock).type;
  92. }
  93. if (this._connectedPoint) {
  94. return this._connectedPoint.type;
  95. }
  96. if (this._linkedConnectionSource && this._linkedConnectionSource.isConnected) {
  97. return this._linkedConnectionSource.type;
  98. }
  99. }
  100. if (this._type === NodeMaterialBlockConnectionPointTypes.BasedOnInput && this._typeConnectionSource) {
  101. return this._typeConnectionSource.type;
  102. }
  103. return this._type;
  104. }
  105. public set type(value: NodeMaterialBlockConnectionPointTypes) {
  106. this._type = value;
  107. }
  108. /**
  109. * Gets or sets the connection point name
  110. */
  111. public name: string;
  112. /**
  113. * Gets or sets the connection point name
  114. */
  115. public displayName: string;
  116. /**
  117. * Gets or sets a boolean indicating that this connection point can be omitted
  118. */
  119. public isOptional: boolean;
  120. /**
  121. * Gets or sets a boolean indicating that this connection point is exposed on a frame
  122. */
  123. public isExposedOnFrame: boolean = false;
  124. /**
  125. * Gets or sets number indicating the position that the port is exposed to on a frame
  126. */
  127. public exposedPortPosition: number = -1;
  128. /**
  129. * Gets or sets a string indicating that this uniform must be defined under a #ifdef
  130. */
  131. public define: string;
  132. /** @hidden */
  133. public _prioritizeVertex = false;
  134. private _target: NodeMaterialBlockTargets = NodeMaterialBlockTargets.VertexAndFragment;
  135. /** Gets or sets the target of that connection point */
  136. public get target(): NodeMaterialBlockTargets {
  137. if (!this._prioritizeVertex || !this._ownerBlock) {
  138. return this._target;
  139. }
  140. if (this._target !== NodeMaterialBlockTargets.VertexAndFragment) {
  141. return this._target;
  142. }
  143. if (this._ownerBlock.target === NodeMaterialBlockTargets.Fragment) {
  144. return NodeMaterialBlockTargets.Fragment;
  145. }
  146. return NodeMaterialBlockTargets.Vertex;
  147. }
  148. public set target(value: NodeMaterialBlockTargets) {
  149. this._target = value;
  150. }
  151. /**
  152. * Gets a boolean indicating that the current point is connected to another NodeMaterialBlock
  153. */
  154. public get isConnected(): boolean {
  155. return this.connectedPoint !== null || this.hasEndpoints;
  156. }
  157. /**
  158. * Gets a boolean indicating that the current point is connected to an input block
  159. */
  160. public get isConnectedToInputBlock(): boolean {
  161. return this.connectedPoint !== null && this.connectedPoint.ownerBlock.isInput;
  162. }
  163. /**
  164. * Gets a the connected input block (if any)
  165. */
  166. public get connectInputBlock(): Nullable<InputBlock> {
  167. if (!this.isConnectedToInputBlock) {
  168. return null;
  169. }
  170. return this.connectedPoint!.ownerBlock as InputBlock;
  171. }
  172. /** Get the other side of the connection (if any) */
  173. public get connectedPoint(): Nullable<NodeMaterialConnectionPoint> {
  174. return this._connectedPoint;
  175. }
  176. /** Get the block that owns this connection point */
  177. public get ownerBlock(): NodeMaterialBlock {
  178. return this._ownerBlock;
  179. }
  180. /** Get the block connected on the other side of this connection (if any) */
  181. public get sourceBlock(): Nullable<NodeMaterialBlock> {
  182. if (!this._connectedPoint) {
  183. return null;
  184. }
  185. return this._connectedPoint.ownerBlock;
  186. }
  187. /** Get the block connected on the endpoints of this connection (if any) */
  188. public get connectedBlocks(): Array<NodeMaterialBlock> {
  189. if (this._endpoints.length === 0) {
  190. return [];
  191. }
  192. return this._endpoints.map((e) => e.ownerBlock);
  193. }
  194. /** Gets the list of connected endpoints */
  195. public get endpoints() {
  196. return this._endpoints;
  197. }
  198. /** Gets a boolean indicating if that output point is connected to at least one input */
  199. public get hasEndpoints(): boolean {
  200. return this._endpoints && this._endpoints.length > 0;
  201. }
  202. /** Gets a boolean indicating that this connection will be used in the vertex shader */
  203. public get isConnectedInVertexShader(): boolean {
  204. if (this.target === NodeMaterialBlockTargets.Vertex) {
  205. return true;
  206. }
  207. if (!this.hasEndpoints) {
  208. return false;
  209. }
  210. for (var endpoint of this._endpoints) {
  211. if (endpoint.ownerBlock.target === NodeMaterialBlockTargets.Vertex) {
  212. return true;
  213. }
  214. if (endpoint.target === NodeMaterialBlockTargets.Vertex) {
  215. return true;
  216. }
  217. if (endpoint.ownerBlock.target === NodeMaterialBlockTargets.Neutral || endpoint.ownerBlock.target === NodeMaterialBlockTargets.VertexAndFragment) {
  218. if (endpoint.ownerBlock.outputs.some((o) => o.isConnectedInVertexShader)) {
  219. return true;
  220. }
  221. }
  222. }
  223. return false;
  224. }
  225. /** Gets a boolean indicating that this connection will be used in the fragment shader */
  226. public get isConnectedInFragmentShader(): boolean {
  227. if (this.target === NodeMaterialBlockTargets.Fragment) {
  228. return true;
  229. }
  230. if (!this.hasEndpoints) {
  231. return false;
  232. }
  233. for (var endpoint of this._endpoints) {
  234. if (endpoint.ownerBlock.target === NodeMaterialBlockTargets.Fragment) {
  235. return true;
  236. }
  237. if (endpoint.ownerBlock.target === NodeMaterialBlockTargets.Neutral || endpoint.ownerBlock.target === NodeMaterialBlockTargets.VertexAndFragment) {
  238. if (endpoint.ownerBlock.outputs.some((o) => o.isConnectedInFragmentShader)) {
  239. return true;
  240. }
  241. }
  242. }
  243. return false;
  244. }
  245. /**
  246. * Creates a block suitable to be used as an input for this input point.
  247. * If null is returned, a block based on the point type will be created.
  248. * @returns The returned string parameter is the name of the output point of NodeMaterialBlock (first parameter of the returned array) that can be connected to the input
  249. */
  250. public createCustomInputBlock(): Nullable<[NodeMaterialBlock, string]> {
  251. return null;
  252. }
  253. /**
  254. * Creates a new connection point
  255. * @param name defines the connection point name
  256. * @param ownerBlock defines the block hosting this connection point
  257. * @param direction defines the direction of the connection point
  258. */
  259. public constructor(name: string, ownerBlock: NodeMaterialBlock, direction: NodeMaterialConnectionPointDirection) {
  260. this._ownerBlock = ownerBlock;
  261. this.name = name;
  262. this._direction = direction;
  263. }
  264. /**
  265. * Gets the current class name e.g. "NodeMaterialConnectionPoint"
  266. * @returns the class name
  267. */
  268. public getClassName(): string {
  269. return "NodeMaterialConnectionPoint";
  270. }
  271. /**
  272. * Gets a boolean indicating if the current point can be connected to another point
  273. * @param connectionPoint defines the other connection point
  274. * @returns a boolean
  275. */
  276. public canConnectTo(connectionPoint: NodeMaterialConnectionPoint) {
  277. return this.checkCompatibilityState(connectionPoint) === NodeMaterialConnectionPointCompatibilityStates.Compatible;
  278. }
  279. /**
  280. * Gets a number indicating if the current point can be connected to another point
  281. * @param connectionPoint defines the other connection point
  282. * @returns a number defining the compatibility state
  283. */
  284. public checkCompatibilityState(connectionPoint: NodeMaterialConnectionPoint): NodeMaterialConnectionPointCompatibilityStates {
  285. const ownerBlock = this._ownerBlock;
  286. if (ownerBlock.target === NodeMaterialBlockTargets.Fragment) {
  287. // Let's check we are not going reverse
  288. const otherBlock = connectionPoint.ownerBlock;
  289. if (otherBlock.target === NodeMaterialBlockTargets.Vertex) {
  290. return NodeMaterialConnectionPointCompatibilityStates.TargetIncompatible;
  291. }
  292. for (var output of otherBlock.outputs) {
  293. if (output.isConnectedInVertexShader) {
  294. return NodeMaterialConnectionPointCompatibilityStates.TargetIncompatible;
  295. }
  296. }
  297. }
  298. if (this.type !== connectionPoint.type && connectionPoint.innerType !== NodeMaterialBlockConnectionPointTypes.AutoDetect) {
  299. // Equivalents
  300. switch (this.type) {
  301. case NodeMaterialBlockConnectionPointTypes.Vector3: {
  302. if (connectionPoint.type === NodeMaterialBlockConnectionPointTypes.Color3) {
  303. return NodeMaterialConnectionPointCompatibilityStates.Compatible;
  304. }
  305. break;
  306. }
  307. case NodeMaterialBlockConnectionPointTypes.Vector4: {
  308. if (connectionPoint.type === NodeMaterialBlockConnectionPointTypes.Color4) {
  309. return NodeMaterialConnectionPointCompatibilityStates.Compatible;
  310. }
  311. break;
  312. }
  313. case NodeMaterialBlockConnectionPointTypes.Color3: {
  314. if (connectionPoint.type === NodeMaterialBlockConnectionPointTypes.Vector3) {
  315. return NodeMaterialConnectionPointCompatibilityStates.Compatible;
  316. }
  317. break;
  318. }
  319. case NodeMaterialBlockConnectionPointTypes.Color4: {
  320. if (connectionPoint.type === NodeMaterialBlockConnectionPointTypes.Vector4) {
  321. return NodeMaterialConnectionPointCompatibilityStates.Compatible;
  322. }
  323. break;
  324. }
  325. }
  326. // Accepted types
  327. if (connectionPoint.acceptedConnectionPointTypes && connectionPoint.acceptedConnectionPointTypes.indexOf(this.type) !== -1) {
  328. return NodeMaterialConnectionPointCompatibilityStates.Compatible;
  329. } else {
  330. return NodeMaterialConnectionPointCompatibilityStates.TypeIncompatible;
  331. }
  332. }
  333. // Excluded
  334. if ((connectionPoint.excludedConnectionPointTypes && connectionPoint.excludedConnectionPointTypes.indexOf(this.type) !== -1)) {
  335. return 1;
  336. }
  337. return NodeMaterialConnectionPointCompatibilityStates.Compatible;
  338. }
  339. /**
  340. * Connect this point to another connection point
  341. * @param connectionPoint defines the other connection point
  342. * @param ignoreConstraints defines if the system will ignore connection type constraints (default is false)
  343. * @returns the current connection point
  344. */
  345. public connectTo(connectionPoint: NodeMaterialConnectionPoint, ignoreConstraints = false): NodeMaterialConnectionPoint {
  346. if (!ignoreConstraints && !this.canConnectTo(connectionPoint)) {
  347. throw "Cannot connect these two connectors.";
  348. }
  349. this._endpoints.push(connectionPoint);
  350. connectionPoint._connectedPoint = this;
  351. this._enforceAssociatedVariableName = false;
  352. this.onConnectionObservable.notifyObservers(connectionPoint);
  353. connectionPoint.onConnectionObservable.notifyObservers(this);
  354. return this;
  355. }
  356. /**
  357. * Disconnect this point from one of his endpoint
  358. * @param endpoint defines the other connection point
  359. * @returns the current connection point
  360. */
  361. public disconnectFrom(endpoint: NodeMaterialConnectionPoint): NodeMaterialConnectionPoint {
  362. let index = this._endpoints.indexOf(endpoint);
  363. if (index === -1) {
  364. return this;
  365. }
  366. this._endpoints.splice(index, 1);
  367. endpoint._connectedPoint = null;
  368. this._enforceAssociatedVariableName = false;
  369. endpoint._enforceAssociatedVariableName = false;
  370. return this;
  371. }
  372. /**
  373. * Serializes this point in a JSON representation
  374. * @param isInput defines if the connection point is an input (default is true)
  375. * @returns the serialized point object
  376. */
  377. public serialize(isInput = true): any {
  378. let serializationObject: any = {};
  379. serializationObject.name = this.name;
  380. serializationObject.displayName = this.displayName;
  381. if (isInput && this.connectedPoint) {
  382. serializationObject.inputName = this.name;
  383. serializationObject.targetBlockId = this.connectedPoint.ownerBlock.uniqueId;
  384. serializationObject.targetConnectionName = this.connectedPoint.name;
  385. serializationObject.isExposedOnFrame = true;
  386. serializationObject.exposedPortPosition = this.exposedPortPosition;
  387. }
  388. if (this.isExposedOnFrame) {
  389. serializationObject.isExposedOnFrame = this.isExposedOnFrame;
  390. serializationObject.exposedPortPosition = this.exposedPortPosition;
  391. }
  392. return serializationObject;
  393. }
  394. /**
  395. * Release resources
  396. */
  397. public dispose() {
  398. this.onConnectionObservable.clear();
  399. }
  400. }