nodeMaterialBlockConnectionPoint.ts 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  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. /** @hidden */
  82. public _prioritizeVertex = false;
  83. private _target: NodeMaterialBlockTargets = NodeMaterialBlockTargets.VertexAndFragment;
  84. /** Gets or sets the target of that connection point */
  85. public get target(): NodeMaterialBlockTargets {
  86. if (!this._prioritizeVertex || !this._ownerBlock) {
  87. return this._target;
  88. }
  89. if (this._target !== NodeMaterialBlockTargets.VertexAndFragment) {
  90. return this._target;
  91. }
  92. if (this._ownerBlock.target === NodeMaterialBlockTargets.Fragment) {
  93. return NodeMaterialBlockTargets.Fragment;
  94. }
  95. return NodeMaterialBlockTargets.Vertex;
  96. }
  97. public set target(value: NodeMaterialBlockTargets) {
  98. this._target = value;
  99. }
  100. /**
  101. * Gets a boolean indicating that the current point is connected
  102. */
  103. public get isConnected(): boolean {
  104. return this.connectedPoint !== null;
  105. }
  106. /**
  107. * Gets a boolean indicating that the current point is connected to an input block
  108. */
  109. public get isConnectedToInputBlock(): boolean {
  110. return this.connectedPoint !== null && this.connectedPoint.ownerBlock.isInput;
  111. }
  112. /**
  113. * Gets a the connected input block (if any)
  114. */
  115. public get connectInputBlock(): Nullable<InputBlock> {
  116. if (!this.isConnectedToInputBlock) {
  117. return null;
  118. }
  119. return this.connectedPoint!.ownerBlock as InputBlock;
  120. }
  121. /** Get the other side of the connection (if any) */
  122. public get connectedPoint(): Nullable<NodeMaterialConnectionPoint> {
  123. return this._connectedPoint;
  124. }
  125. /** Get the block that owns this connection point */
  126. public get ownerBlock(): NodeMaterialBlock {
  127. return this._ownerBlock;
  128. }
  129. /** Get the block connected on the other side of this connection (if any) */
  130. public get sourceBlock(): Nullable<NodeMaterialBlock> {
  131. if (!this._connectedPoint) {
  132. return null;
  133. }
  134. return this._connectedPoint.ownerBlock;
  135. }
  136. /** Get the block connected on the endpoints of this connection (if any) */
  137. public get connectedBlocks(): Array<NodeMaterialBlock> {
  138. if (this._endpoints.length === 0) {
  139. return [];
  140. }
  141. return this._endpoints.map((e) => e.ownerBlock);
  142. }
  143. /** Gets the list of connected endpoints */
  144. public get endpoints() {
  145. return this._endpoints;
  146. }
  147. /** Gets a boolean indicating if that output point is connected to at least one input */
  148. public get hasEndpoints(): boolean {
  149. return this._endpoints && this._endpoints.length > 0;
  150. }
  151. /** Gets a boolean indicating that this connection will be used in the vertex shader */
  152. public get isConnectedInVertexShader(): boolean {
  153. if (this.target === NodeMaterialBlockTargets.Vertex) {
  154. return true;
  155. }
  156. if (!this.hasEndpoints) {
  157. return false;
  158. }
  159. for (var endpoint of this._endpoints) {
  160. if (endpoint.ownerBlock.target === NodeMaterialBlockTargets.Vertex) {
  161. return true;
  162. }
  163. if (endpoint.target === NodeMaterialBlockTargets.Vertex) {
  164. return true;
  165. }
  166. if (endpoint.ownerBlock.target === NodeMaterialBlockTargets.Neutral || endpoint.ownerBlock.target === NodeMaterialBlockTargets.VertexAndFragment) {
  167. if (endpoint.ownerBlock.outputs.some((o) => o.isConnectedInVertexShader)) {
  168. return true;
  169. }
  170. }
  171. }
  172. return false;
  173. }
  174. /** Gets a boolean indicating that this connection will be used in the fragment shader */
  175. public get isConnectedInFragmentShader(): boolean {
  176. if (this.target === NodeMaterialBlockTargets.Fragment) {
  177. return true;
  178. }
  179. if (!this.hasEndpoints) {
  180. return false;
  181. }
  182. for (var endpoint of this._endpoints) {
  183. if (endpoint.ownerBlock.target === NodeMaterialBlockTargets.Fragment) {
  184. return true;
  185. }
  186. if (endpoint.ownerBlock.target === NodeMaterialBlockTargets.Neutral || endpoint.ownerBlock.target === NodeMaterialBlockTargets.VertexAndFragment) {
  187. if (endpoint.ownerBlock.outputs.some((o) => o.isConnectedInFragmentShader)) {
  188. return true;
  189. }
  190. }
  191. }
  192. return false;
  193. }
  194. /**
  195. * Creates a new connection point
  196. * @param name defines the connection point name
  197. * @param ownerBlock defines the block hosting this connection point
  198. */
  199. public constructor(name: string, ownerBlock: NodeMaterialBlock) {
  200. this._ownerBlock = ownerBlock;
  201. this.name = name;
  202. }
  203. /**
  204. * Gets the current class name e.g. "NodeMaterialConnectionPoint"
  205. * @returns the class name
  206. */
  207. public getClassName(): string {
  208. return "NodeMaterialConnectionPoint";
  209. }
  210. /**
  211. * Gets an boolean indicating if the current point can be connected to another point
  212. * @param connectionPoint defines the other connection point
  213. * @returns true if the connection is possible
  214. */
  215. public canConnectTo(connectionPoint: NodeMaterialConnectionPoint) {
  216. if (this.type !== connectionPoint.type && connectionPoint.type !== NodeMaterialBlockConnectionPointTypes.AutoDetect) {
  217. // Equivalents
  218. switch (this.type) {
  219. case NodeMaterialBlockConnectionPointTypes.Vector3: {
  220. if (connectionPoint.type === NodeMaterialBlockConnectionPointTypes.Color3) {
  221. return true;
  222. }
  223. }
  224. case NodeMaterialBlockConnectionPointTypes.Vector4: {
  225. if (connectionPoint.type === NodeMaterialBlockConnectionPointTypes.Color4) {
  226. return true;
  227. }
  228. }
  229. case NodeMaterialBlockConnectionPointTypes.Color3: {
  230. if (connectionPoint.type === NodeMaterialBlockConnectionPointTypes.Vector3) {
  231. return true;
  232. }
  233. }
  234. case NodeMaterialBlockConnectionPointTypes.Color4: {
  235. if (connectionPoint.type === NodeMaterialBlockConnectionPointTypes.Vector4) {
  236. return true;
  237. }
  238. }
  239. }
  240. // Accepted types
  241. return (connectionPoint.acceptedConnectionPointTypes && connectionPoint.acceptedConnectionPointTypes.indexOf(this.type) !== -1);
  242. }
  243. // Excluded
  244. if ((connectionPoint.excludedConnectionPointTypes && connectionPoint.excludedConnectionPointTypes.indexOf(this.type) !== -1)) {
  245. return false;
  246. }
  247. return true;
  248. }
  249. /**
  250. * Connect this point to another connection point
  251. * @param connectionPoint defines the other connection point
  252. * @param ignoreConstraints defines if the system will ignore connection type constraints (default is false)
  253. * @returns the current connection point
  254. */
  255. public connectTo(connectionPoint: NodeMaterialConnectionPoint, ignoreConstraints = false): NodeMaterialConnectionPoint {
  256. if (!ignoreConstraints && !this.canConnectTo(connectionPoint)) {
  257. throw "Cannot connect these two connectors.";
  258. }
  259. this._endpoints.push(connectionPoint);
  260. connectionPoint._connectedPoint = this;
  261. this._enforceAssociatedVariableName = false;
  262. return this;
  263. }
  264. /**
  265. * Disconnect this point from one of his endpoint
  266. * @param endpoint defines the other connection point
  267. * @returns the current connection point
  268. */
  269. public disconnectFrom(endpoint: NodeMaterialConnectionPoint): NodeMaterialConnectionPoint {
  270. let index = this._endpoints.indexOf(endpoint);
  271. if (index === -1) {
  272. return this;
  273. }
  274. this._endpoints.splice(index, 1);
  275. endpoint._connectedPoint = null;
  276. this._enforceAssociatedVariableName = false;
  277. endpoint._enforceAssociatedVariableName = false;
  278. return this;
  279. }
  280. /**
  281. * Serializes this point in a JSON representation
  282. * @returns the serialized point object
  283. */
  284. public serialize(): any {
  285. let serializationObject: any = {};
  286. serializationObject.name = this.name;
  287. if (this.connectedPoint) {
  288. serializationObject.inputName = this.name;
  289. serializationObject.targetBlockId = this.connectedPoint.ownerBlock.uniqueId;
  290. serializationObject.targetConnectionName = this.connectedPoint.name;
  291. }
  292. return serializationObject;
  293. }
  294. }