nodeMaterialBlockConnectionPoint.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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. declare type NodeMaterialBlock = import("./nodeMaterialBlock").NodeMaterialBlock;
  6. /**
  7. * Defines a connection point for a block
  8. */
  9. export class NodeMaterialConnectionPoint {
  10. /** @hidden */
  11. public _ownerBlock: NodeMaterialBlock;
  12. /** @hidden */
  13. public _connectedPoint: Nullable<NodeMaterialConnectionPoint> = null;
  14. private _endpoints = new Array<NodeMaterialConnectionPoint>();
  15. private _associatedVariableName: string;
  16. /** @hidden */
  17. public _typeConnectionSource: Nullable<NodeMaterialConnectionPoint> = null;
  18. /** @hidden */
  19. public _linkedConnectionSource: Nullable<NodeMaterialConnectionPoint> = null;
  20. private _type = NodeMaterialBlockConnectionPointTypes.Float;
  21. /** @hidden */
  22. public _enforceAssociatedVariableName = false;
  23. /**
  24. * Gets or sets the additional types supported by this connection point
  25. */
  26. public acceptedConnectionPointTypes = new Array<NodeMaterialBlockConnectionPointTypes>();
  27. /**
  28. * Gets or sets the additional types excluded by this connection point
  29. */
  30. public excludedConnectionPointTypes = new Array<NodeMaterialBlockConnectionPointTypes>();
  31. /**
  32. * Gets or sets the associated variable name in the shader
  33. */
  34. public get associatedVariableName(): string {
  35. if (this._ownerBlock.isInput) {
  36. return (this._ownerBlock as InputBlock).associatedVariableName;
  37. }
  38. if ((!this._enforceAssociatedVariableName || !this._associatedVariableName) && this._connectedPoint) {
  39. return this._connectedPoint.associatedVariableName;
  40. }
  41. return this._associatedVariableName;
  42. }
  43. public set associatedVariableName(value: string) {
  44. this._associatedVariableName = value;
  45. }
  46. /**
  47. * Gets or sets the connection point type (default is float)
  48. */
  49. public get type(): NodeMaterialBlockConnectionPointTypes {
  50. if (this._type === NodeMaterialBlockConnectionPointTypes.AutoDetect) {
  51. if (this._ownerBlock.isInput) {
  52. return (this._ownerBlock as InputBlock).type;
  53. }
  54. if (this._connectedPoint) {
  55. return this._connectedPoint.type;
  56. }
  57. if (this._linkedConnectionSource && this._linkedConnectionSource.isConnected) {
  58. return this._linkedConnectionSource.type;
  59. }
  60. }
  61. if (this._type === NodeMaterialBlockConnectionPointTypes.BasedOnInput && this._typeConnectionSource) {
  62. return this._typeConnectionSource.type;
  63. }
  64. return this._type;
  65. }
  66. public set type(value: NodeMaterialBlockConnectionPointTypes) {
  67. this._type = value;
  68. }
  69. /**
  70. * Gets or sets the connection point name
  71. */
  72. public name: string;
  73. /**
  74. * Gets or sets a boolean indicating that this connection point can be omitted
  75. */
  76. public isOptional: boolean;
  77. /**
  78. * Gets or sets a string indicating that this uniform must be defined under a #ifdef
  79. */
  80. public define: string;
  81. /** Gets or sets the target of that connection point */
  82. public target: NodeMaterialBlockTargets = NodeMaterialBlockTargets.VertexAndFragment;
  83. /**
  84. * Gets a boolean indicating that the current point is connected
  85. */
  86. public get isConnected(): boolean {
  87. return this.connectedPoint !== null;
  88. }
  89. /**
  90. * Gets a boolean indicating that the current point is connected to an input block
  91. */
  92. public get isConnectedToInputBlock(): boolean {
  93. return this.connectedPoint !== null && this.connectedPoint.ownerBlock.isInput;
  94. }
  95. /**
  96. * Gets a the connected input block (if any)
  97. */
  98. public get connectInputBlock(): Nullable<InputBlock> {
  99. if (!this.isConnectedToInputBlock) {
  100. return null;
  101. }
  102. return this.connectedPoint!.ownerBlock as InputBlock;
  103. }
  104. /** Get the other side of the connection (if any) */
  105. public get connectedPoint(): Nullable<NodeMaterialConnectionPoint> {
  106. return this._connectedPoint;
  107. }
  108. /** Get the block that owns this connection point */
  109. public get ownerBlock(): NodeMaterialBlock {
  110. return this._ownerBlock;
  111. }
  112. /** Get the block connected on the other side of this connection (if any) */
  113. public get sourceBlock(): Nullable<NodeMaterialBlock> {
  114. if (!this._connectedPoint) {
  115. return null;
  116. }
  117. return this._connectedPoint.ownerBlock;
  118. }
  119. /** Get the block connected on the endpoints of this connection (if any) */
  120. public get connectedBlocks(): Array<NodeMaterialBlock> {
  121. if (this._endpoints.length === 0) {
  122. return [];
  123. }
  124. return this._endpoints.map((e) => e.ownerBlock);
  125. }
  126. /** Gets the list of connected endpoints */
  127. public get endpoints() {
  128. return this._endpoints;
  129. }
  130. /** Gets a boolean indicating if that output point is connected to at least one input */
  131. public get hasEndpoints(): boolean {
  132. return this._endpoints && this._endpoints.length > 0;
  133. }
  134. /** Gets a boolean indicating that this connection will be used in the vertex shader */
  135. public get isConnectedInVertexShader(): boolean {
  136. if (this.target === NodeMaterialBlockTargets.Vertex) {
  137. return true;
  138. }
  139. if (!this.hasEndpoints) {
  140. return false;
  141. }
  142. for (var endpoint of this._endpoints) {
  143. if (endpoint.ownerBlock.target === NodeMaterialBlockTargets.Vertex) {
  144. return true;
  145. }
  146. if (endpoint.ownerBlock.target === NodeMaterialBlockTargets.Neutral || endpoint.ownerBlock.target === NodeMaterialBlockTargets.VertexAndFragment) {
  147. if (endpoint.ownerBlock.outputs.some((o) => o.isConnectedInVertexShader)) {
  148. return true;
  149. }
  150. }
  151. }
  152. return false;
  153. }
  154. /** Gets a boolean indicating that this connection will be used in the fragment shader */
  155. public get isConnectedInFragmentShader(): boolean {
  156. if (this.target === NodeMaterialBlockTargets.Fragment) {
  157. return true;
  158. }
  159. if (!this.hasEndpoints) {
  160. return false;
  161. }
  162. for (var endpoint of this._endpoints) {
  163. if (endpoint.ownerBlock.target === NodeMaterialBlockTargets.Fragment) {
  164. return true;
  165. }
  166. if (endpoint.ownerBlock.target === NodeMaterialBlockTargets.Neutral || endpoint.ownerBlock.target === NodeMaterialBlockTargets.VertexAndFragment) {
  167. if (endpoint.ownerBlock.outputs.some((o) => o.isConnectedInFragmentShader)) {
  168. return true;
  169. }
  170. }
  171. }
  172. return false;
  173. }
  174. /**
  175. * Creates a new connection point
  176. * @param name defines the connection point name
  177. * @param ownerBlock defines the block hosting this connection point
  178. */
  179. public constructor(name: string, ownerBlock: NodeMaterialBlock) {
  180. this._ownerBlock = ownerBlock;
  181. this.name = name;
  182. }
  183. /**
  184. * Gets the current class name e.g. "NodeMaterialConnectionPoint"
  185. * @returns the class name
  186. */
  187. public getClassName(): string {
  188. return "NodeMaterialConnectionPoint";
  189. }
  190. /**
  191. * Gets an boolean indicating if the current point can be connected to another point
  192. * @param connectionPoint defines the other connection point
  193. * @returns true if the connection is possible
  194. */
  195. public canConnectTo(connectionPoint: NodeMaterialConnectionPoint) {
  196. if (this.type !== connectionPoint.type && connectionPoint.type !== NodeMaterialBlockConnectionPointTypes.AutoDetect) {
  197. // Equivalents
  198. switch (this.type) {
  199. case NodeMaterialBlockConnectionPointTypes.Vector3: {
  200. if (connectionPoint.type === NodeMaterialBlockConnectionPointTypes.Color3) {
  201. return true;
  202. }
  203. }
  204. case NodeMaterialBlockConnectionPointTypes.Vector4: {
  205. if (connectionPoint.type === NodeMaterialBlockConnectionPointTypes.Color4) {
  206. return true;
  207. }
  208. }
  209. case NodeMaterialBlockConnectionPointTypes.Color3: {
  210. if (connectionPoint.type === NodeMaterialBlockConnectionPointTypes.Vector3) {
  211. return true;
  212. }
  213. }
  214. case NodeMaterialBlockConnectionPointTypes.Color4: {
  215. if (connectionPoint.type === NodeMaterialBlockConnectionPointTypes.Vector4) {
  216. return true;
  217. }
  218. }
  219. }
  220. // Accepted types
  221. return (connectionPoint.acceptedConnectionPointTypes && connectionPoint.acceptedConnectionPointTypes.indexOf(this.type) !== -1);
  222. }
  223. // Excluded
  224. if ((connectionPoint.excludedConnectionPointTypes && connectionPoint.excludedConnectionPointTypes.indexOf(this.type) !== -1)) {
  225. return false;
  226. }
  227. return true;
  228. }
  229. /**
  230. * Connect this point to another connection point
  231. * @param connectionPoint defines the other connection point
  232. * @param ignoreConstraints defines if the system will ignore connection type constraints (default is false)
  233. * @returns the current connection point
  234. */
  235. public connectTo(connectionPoint: NodeMaterialConnectionPoint, ignoreConstraints = false): NodeMaterialConnectionPoint {
  236. if (!ignoreConstraints && !this.canConnectTo(connectionPoint)) {
  237. throw "Cannot connect these two connectors.";
  238. }
  239. this._endpoints.push(connectionPoint);
  240. connectionPoint._connectedPoint = this;
  241. this._enforceAssociatedVariableName = false;
  242. return this;
  243. }
  244. /**
  245. * Disconnect this point from one of his endpoint
  246. * @param endpoint defines the other connection point
  247. * @returns the current connection point
  248. */
  249. public disconnectFrom(endpoint: NodeMaterialConnectionPoint): NodeMaterialConnectionPoint {
  250. let index = this._endpoints.indexOf(endpoint);
  251. if (index === -1) {
  252. return this;
  253. }
  254. this._endpoints.splice(index, 1);
  255. endpoint._connectedPoint = null;
  256. this._enforceAssociatedVariableName = false;
  257. endpoint._enforceAssociatedVariableName = false;
  258. return this;
  259. }
  260. /**
  261. * Serializes this point in a JSON representation
  262. * @returns the serialized point object
  263. */
  264. public serialize(): any {
  265. let serializationObject: any = {};
  266. serializationObject.name = this.name;
  267. if (this.connectedPoint) {
  268. serializationObject.inputName = this.name;
  269. serializationObject.targetBlockId = this.connectedPoint.ownerBlock.uniqueId;
  270. serializationObject.targetConnectionName = this.connectedPoint.name;
  271. }
  272. return serializationObject;
  273. }
  274. }