nodeMaterialBlockConnectionPoint.ts 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. import { NodeMaterialBlockConnectionPointTypes } from './nodeMaterialBlockConnectionPointTypes';
  2. import { NodeMaterialBlockTargets } from './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. private _type = NodeMaterialBlockConnectionPointTypes.Float;
  19. /** @hidden */
  20. public _enforceAssociatedVariableName = false;
  21. /**
  22. * Gets or sets the additional types supported byt this connection point
  23. */
  24. public acceptedConnectionPointTypes = new Array<NodeMaterialBlockConnectionPointTypes>();
  25. /**
  26. * Gets or sets the associated variable name in the shader
  27. */
  28. public get associatedVariableName(): string {
  29. if (this._ownerBlock.isInput) {
  30. return (this._ownerBlock as InputBlock).associatedVariableName;
  31. }
  32. if (!this._enforceAssociatedVariableName && this._connectedPoint) {
  33. return this._connectedPoint.associatedVariableName;
  34. }
  35. return this._associatedVariableName;
  36. }
  37. public set associatedVariableName(value: string) {
  38. this._associatedVariableName = value;
  39. }
  40. /**
  41. * Gets or sets the connection point type (default is float)
  42. */
  43. public get type(): NodeMaterialBlockConnectionPointTypes {
  44. if (this._type === NodeMaterialBlockConnectionPointTypes.AutoDetect) {
  45. if (this._ownerBlock.isInput) {
  46. return (this._ownerBlock as InputBlock).type;
  47. }
  48. if (this._connectedPoint) {
  49. return this._connectedPoint.type;
  50. }
  51. }
  52. if (this._type === NodeMaterialBlockConnectionPointTypes.BasedOnInput && this._typeConnectionSource) {
  53. return this._typeConnectionSource.type;
  54. }
  55. return this._type;
  56. }
  57. public set type(value: NodeMaterialBlockConnectionPointTypes) {
  58. this._type = value;
  59. }
  60. /**
  61. * Gets or sets the connection point name
  62. */
  63. public name: string;
  64. /**
  65. * Gets or sets a boolean indicating that this connection point can be omitted
  66. */
  67. public isOptional: boolean;
  68. /**
  69. * Gets or sets a string indicating that this uniform must be defined under a #ifdef
  70. */
  71. public define: string;
  72. /** Gets or sets the target of that connection point */
  73. public target: NodeMaterialBlockTargets = NodeMaterialBlockTargets.VertexAndFragment;
  74. /**
  75. * Gets a boolean indicating that the current point is connected
  76. */
  77. public get isConnected(): boolean {
  78. return this.connectedPoint !== null;
  79. }
  80. /**
  81. * Gets a boolean indicating that the current point is connected to an input block
  82. */
  83. public get isConnectedToInput(): boolean {
  84. return this.connectedPoint !== null && this.connectedPoint.ownerBlock.isInput;
  85. }
  86. /**
  87. * Gets a the connected input block (if any)
  88. */
  89. public get connectInputBlock(): Nullable<InputBlock> {
  90. if (!this.isConnectedToInput) {
  91. return null;
  92. }
  93. return this.connectedPoint!.ownerBlock as InputBlock;
  94. }
  95. /** Get the other side of the connection (if any) */
  96. public get connectedPoint(): Nullable<NodeMaterialConnectionPoint> {
  97. return this._connectedPoint;
  98. }
  99. /** Get the block that owns this connection point */
  100. public get ownerBlock(): NodeMaterialBlock {
  101. return this._ownerBlock;
  102. }
  103. /** Get the block connected on the other side of this connection (if any) */
  104. public get sourceBlock(): Nullable<NodeMaterialBlock> {
  105. if (!this._connectedPoint) {
  106. return null;
  107. }
  108. return this._connectedPoint.ownerBlock;
  109. }
  110. /** Get the block connected on the endpoints of this connection (if any) */
  111. public get connectedBlocks(): Array<NodeMaterialBlock> {
  112. if (this._endpoints.length === 0) {
  113. return [];
  114. }
  115. return this._endpoints.map((e) => e.ownerBlock);
  116. }
  117. /** Gets the list of connected endpoints */
  118. public get endpoints() {
  119. return this, this._endpoints;
  120. }
  121. /**
  122. * Creates a new connection point
  123. * @param name defines the connection point name
  124. * @param ownerBlock defines the block hosting this connection point
  125. */
  126. public constructor(name: string, ownerBlock: NodeMaterialBlock) {
  127. this._ownerBlock = ownerBlock;
  128. this.name = name;
  129. }
  130. /**
  131. * Gets the current class name e.g. "NodeMaterialConnectionPoint"
  132. * @returns the class name
  133. */
  134. public getClassName(): string {
  135. return "NodeMaterialConnectionPoint";
  136. }
  137. /**
  138. * Gets an boolean indicating if the current point can be connected to another point
  139. * @param connectionPoint defines the other connection point
  140. * @returns true if the connection is possible
  141. */
  142. public canConnectTo(connectionPoint: NodeMaterialConnectionPoint) {
  143. if (this.type !== connectionPoint.type && connectionPoint.type !== NodeMaterialBlockConnectionPointTypes.AutoDetect) {
  144. return (connectionPoint.acceptedConnectionPointTypes.indexOf(this.type) !== -1);
  145. }
  146. return true;
  147. }
  148. /**
  149. * Connect this point to another connection point
  150. * @param connectionPoint defines the other connection point
  151. * @returns the current connection point
  152. */
  153. public connectTo(connectionPoint: NodeMaterialConnectionPoint): NodeMaterialConnectionPoint {
  154. if (!this.canConnectTo(connectionPoint)) {
  155. throw "Cannot connect two different connection types.";
  156. }
  157. this._endpoints.push(connectionPoint);
  158. connectionPoint._connectedPoint = this;
  159. this._enforceAssociatedVariableName = false;
  160. return this;
  161. }
  162. /**
  163. * Disconnect this point from one of his endpoint
  164. * @param endpoint defines the other connection point
  165. * @returns the current connection point
  166. */
  167. public disconnectFrom(endpoint: NodeMaterialConnectionPoint): NodeMaterialConnectionPoint {
  168. let index = this._endpoints.indexOf(endpoint);
  169. if (index === -1) {
  170. return this;
  171. }
  172. this._endpoints.splice(index, 1);
  173. endpoint._connectedPoint = null;
  174. this._enforceAssociatedVariableName = false;
  175. return this;
  176. }
  177. /**
  178. * Serializes this point in a JSON representation
  179. * @returns the serialized point object
  180. */
  181. public serialize(): any {
  182. let serializationObject: any = {};
  183. serializationObject.name = this.name;
  184. if (this.connectedPoint) {
  185. serializationObject.inputName = this.name;
  186. serializationObject.targetBlockId = this.connectedPoint.ownerBlock.uniqueId;
  187. serializationObject.targetConnectionName = this.connectedPoint.name;
  188. }
  189. return serializationObject;
  190. }
  191. }