nodeMaterialBlockConnectionPoint.ts 16 KB

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