instancesBlock.ts 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. import { NodeMaterialBlock } from '../../nodeMaterialBlock';
  2. import { NodeMaterialBlockTargets } from '../../Enums/nodeMaterialBlockTargets';
  3. import { NodeMaterialBlockConnectionPointTypes } from '../../Enums/nodeMaterialBlockConnectionPointTypes';
  4. import { NodeMaterialConnectionPoint } from '../../nodeMaterialBlockConnectionPoint';
  5. import { NodeMaterialBuildState } from '../../nodeMaterialBuildState';
  6. import { AbstractMesh } from '../../../../Meshes/abstractMesh';
  7. import { NodeMaterial, NodeMaterialDefines } from '../../nodeMaterial';
  8. import { NodeMaterialSystemValues } from '../../Enums/nodeMaterialSystemValues';
  9. import { InputBlock } from '../Input/inputBlock';
  10. import { _TypeStore } from '../../../../Misc/typeStore';
  11. /**
  12. * Block used to add support for instances
  13. * @see https://doc.babylonjs.com/how_to/how_to_use_instances
  14. */
  15. export class InstancesBlock extends NodeMaterialBlock {
  16. /**
  17. * Creates a new InstancesBlock
  18. * @param name defines the block name
  19. */
  20. public constructor(name: string) {
  21. super(name, NodeMaterialBlockTargets.Vertex);
  22. this.registerInput("world0", NodeMaterialBlockConnectionPointTypes.Vector4);
  23. this.registerInput("world1", NodeMaterialBlockConnectionPointTypes.Vector4);
  24. this.registerInput("world2", NodeMaterialBlockConnectionPointTypes.Vector4);
  25. this.registerInput("world3", NodeMaterialBlockConnectionPointTypes.Vector4);
  26. this.registerInput("world", NodeMaterialBlockConnectionPointTypes.Matrix, true);
  27. this.registerOutput("output", NodeMaterialBlockConnectionPointTypes.Matrix);
  28. this.registerOutput("instanceID", NodeMaterialBlockConnectionPointTypes.Float);
  29. }
  30. /**
  31. * Gets the current class name
  32. * @returns the class name
  33. */
  34. public getClassName() {
  35. return "InstancesBlock";
  36. }
  37. /**
  38. * Gets the first world row input component
  39. */
  40. public get world0(): NodeMaterialConnectionPoint {
  41. return this._inputs[0];
  42. }
  43. /**
  44. * Gets the second world row input component
  45. */
  46. public get world1(): NodeMaterialConnectionPoint {
  47. return this._inputs[1];
  48. }
  49. /**
  50. * Gets the third world row input component
  51. */
  52. public get world2(): NodeMaterialConnectionPoint {
  53. return this._inputs[2];
  54. }
  55. /**
  56. * Gets the forth world row input component
  57. */
  58. public get world3(): NodeMaterialConnectionPoint {
  59. return this._inputs[3];
  60. }
  61. /**
  62. * Gets the world input component
  63. */
  64. public get world(): NodeMaterialConnectionPoint {
  65. return this._inputs[4];
  66. }
  67. /**
  68. * Gets the output component
  69. */
  70. public get output(): NodeMaterialConnectionPoint {
  71. return this._outputs[0];
  72. }
  73. /**
  74. * Gets the isntanceID component
  75. */
  76. public get instanceID(): NodeMaterialConnectionPoint {
  77. return this._outputs[1];
  78. }
  79. public autoConfigure(material: NodeMaterial) {
  80. if (!this.world0.connectedPoint) {
  81. let world0Input = material.getInputBlockByPredicate((b) => b.isAttribute && b.name === "world0");
  82. if (!world0Input) {
  83. world0Input = new InputBlock("world0");
  84. world0Input.setAsAttribute("world0");
  85. }
  86. world0Input.output.connectTo(this.world0);
  87. }
  88. if (!this.world1.connectedPoint) {
  89. let world1Input = material.getInputBlockByPredicate((b) => b.isAttribute && b.name === "world1");
  90. if (!world1Input) {
  91. world1Input = new InputBlock("world1");
  92. world1Input.setAsAttribute("world1");
  93. }
  94. world1Input.output.connectTo(this.world1);
  95. }
  96. if (!this.world2.connectedPoint) {
  97. let world2Input = material.getInputBlockByPredicate((b) => b.isAttribute && b.name === "world2");
  98. if (!world2Input) {
  99. world2Input = new InputBlock("world2");
  100. world2Input.setAsAttribute("world2");
  101. }
  102. world2Input.output.connectTo(this.world2);
  103. }
  104. if (!this.world3.connectedPoint) {
  105. let world3Input = material.getInputBlockByPredicate((b) => b.isAttribute && b.name === "world3");
  106. if (!world3Input) {
  107. world3Input = new InputBlock("world3");
  108. world3Input.setAsAttribute("world3");
  109. }
  110. world3Input.output.connectTo(this.world3);
  111. }
  112. if (!this.world.connectedPoint) {
  113. let worldInput = material.getInputBlockByPredicate((b) => b.isAttribute && b.name === "world");
  114. if (!worldInput) {
  115. worldInput = new InputBlock("world");
  116. worldInput.setAsSystemValue(NodeMaterialSystemValues.World);
  117. }
  118. worldInput.output.connectTo(this.world);
  119. }
  120. this.world.define = "!INSTANCES";
  121. }
  122. public prepareDefines(mesh: AbstractMesh, nodeMaterial: NodeMaterial, defines: NodeMaterialDefines, useInstances: boolean = false) {
  123. let changed = false;
  124. if (defines["INSTANCES"] !== useInstances) {
  125. defines.setValue("INSTANCES", useInstances);
  126. changed = true;
  127. }
  128. if (changed) {
  129. defines.markAsUnprocessed();
  130. }
  131. }
  132. protected _buildBlock(state: NodeMaterialBuildState) {
  133. super._buildBlock(state);
  134. // Register for defines
  135. state.sharedData.blocksWithDefines.push(this);
  136. // Emit code
  137. let output = this._outputs[0];
  138. let instanceID = this._outputs[1];
  139. let world0 = this.world0;
  140. let world1 = this.world1;
  141. let world2 = this.world2;
  142. let world3 = this.world3;
  143. state.compilationString += `#ifdef INSTANCES\r\n`;
  144. state.compilationString += this._declareOutput(output, state) + ` = mat4(${world0.associatedVariableName}, ${world1.associatedVariableName}, ${world2.associatedVariableName}, ${world3.associatedVariableName});\r\n`;
  145. state.compilationString += this._declareOutput(instanceID, state) + ` = float(gl_InstanceID);\r\n`;
  146. state.compilationString += `#else\r\n`;
  147. state.compilationString += this._declareOutput(output, state) + ` = ${this.world.associatedVariableName};\r\n`;
  148. state.compilationString += this._declareOutput(instanceID, state) + ` = 0.0;\r\n`;
  149. state.compilationString += `#endif\r\n`;
  150. return this;
  151. }
  152. }
  153. _TypeStore.RegisteredTypes["BABYLON.InstancesBlock"] = InstancesBlock;