nodeMaterialBlockConnectionPoint.ts 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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 isConnectedToInputBlock(): 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.isConnectedToInputBlock) {
  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._endpoints;
  120. }
  121. /** Gets a boolean indicating if that output point is connected to at least one input */
  122. public get hasEndpoints(): boolean {
  123. return this._endpoints && this._endpoints.length > 0;
  124. }
  125. /**
  126. * Creates a new connection point
  127. * @param name defines the connection point name
  128. * @param ownerBlock defines the block hosting this connection point
  129. */
  130. public constructor(name: string, ownerBlock: NodeMaterialBlock) {
  131. this._ownerBlock = ownerBlock;
  132. this.name = name;
  133. }
  134. /**
  135. * Gets the current class name e.g. "NodeMaterialConnectionPoint"
  136. * @returns the class name
  137. */
  138. public getClassName(): string {
  139. return "NodeMaterialConnectionPoint";
  140. }
  141. /**
  142. * Gets an boolean indicating if the current point can be connected to another point
  143. * @param connectionPoint defines the other connection point
  144. * @returns true if the connection is possible
  145. */
  146. public canConnectTo(connectionPoint: NodeMaterialConnectionPoint) {
  147. if (this.type !== connectionPoint.type && connectionPoint.type !== NodeMaterialBlockConnectionPointTypes.AutoDetect) {
  148. return (connectionPoint.acceptedConnectionPointTypes && connectionPoint.acceptedConnectionPointTypes.indexOf(this.type) !== -1);
  149. }
  150. return true;
  151. }
  152. /**
  153. * Connect this point to another connection point
  154. * @param connectionPoint defines the other connection point
  155. * @returns the current connection point
  156. */
  157. public connectTo(connectionPoint: NodeMaterialConnectionPoint): NodeMaterialConnectionPoint {
  158. if (!this.canConnectTo(connectionPoint)) {
  159. throw "Cannot connect two different connection types.";
  160. }
  161. this._endpoints.push(connectionPoint);
  162. connectionPoint._connectedPoint = this;
  163. this._enforceAssociatedVariableName = false;
  164. return this;
  165. }
  166. /**
  167. * Disconnect this point from one of his endpoint
  168. * @param endpoint defines the other connection point
  169. * @returns the current connection point
  170. */
  171. public disconnectFrom(endpoint: NodeMaterialConnectionPoint): NodeMaterialConnectionPoint {
  172. let index = this._endpoints.indexOf(endpoint);
  173. if (index === -1) {
  174. return this;
  175. }
  176. this._endpoints.splice(index, 1);
  177. endpoint._connectedPoint = null;
  178. this._enforceAssociatedVariableName = false;
  179. return this;
  180. }
  181. /**
  182. * Serializes this point in a JSON representation
  183. * @returns the serialized point object
  184. */
  185. public serialize(): any {
  186. let serializationObject: any = {};
  187. serializationObject.name = this.name;
  188. if (this.connectedPoint) {
  189. serializationObject.inputName = this.name;
  190. serializationObject.targetBlockId = this.connectedPoint.ownerBlock.uniqueId;
  191. serializationObject.targetConnectionName = this.connectedPoint.name;
  192. }
  193. return serializationObject;
  194. }
  195. }