instancesBlock.ts 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. import { NodeMaterialBlock } from '../../nodeMaterialBlock';
  2. import { NodeMaterialBlockTargets } from '../../nodeMaterialBlockTargets';
  3. import { NodeMaterialBlockConnectionPointTypes } from '../../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 { NodeMaterialWellKnownValues } from '../../nodeMaterialWellKnownValues';
  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. }
  29. /**
  30. * Gets the current class name
  31. * @returns the class name
  32. */
  33. public getClassName() {
  34. return "InstancesBlock";
  35. }
  36. /**
  37. * Gets the first world row input component
  38. */
  39. public get world0(): NodeMaterialConnectionPoint {
  40. return this._inputs[0];
  41. }
  42. /**
  43. * Gets the second world row input component
  44. */
  45. public get world1(): NodeMaterialConnectionPoint {
  46. return this._inputs[1];
  47. }
  48. /**
  49. * Gets the third world row input component
  50. */
  51. public get world2(): NodeMaterialConnectionPoint {
  52. return this._inputs[2];
  53. }
  54. /**
  55. * Gets the forth world row input component
  56. */
  57. public get world3(): NodeMaterialConnectionPoint {
  58. return this._inputs[3];
  59. }
  60. /**
  61. * Gets the world input component
  62. */
  63. public get world(): NodeMaterialConnectionPoint {
  64. return this._inputs[4];
  65. }
  66. /**
  67. * Gets the output component
  68. */
  69. public get output(): NodeMaterialConnectionPoint {
  70. return this._outputs[0];
  71. }
  72. public autoConfigure(material: NodeMaterial) {
  73. if (!this.world0.connectedPoint) {
  74. let world0Input = material.getInputBlockByPredicate((b) => b.isAttribute && b.name === "world0");
  75. if (!world0Input) {
  76. world0Input = new InputBlock("world0");
  77. world0Input.setAsAttribute("world0");
  78. }
  79. world0Input.output.connectTo(this.world0);
  80. }
  81. if (!this.world1.connectedPoint) {
  82. let world1Input = material.getInputBlockByPredicate((b) => b.isAttribute && b.name === "world1");
  83. if (!world1Input) {
  84. world1Input = new InputBlock("world1");
  85. world1Input.setAsAttribute("world1");
  86. }
  87. world1Input.output.connectTo(this.world1);
  88. }
  89. if (!this.world2.connectedPoint) {
  90. let world2Input = material.getInputBlockByPredicate((b) => b.isAttribute && b.name === "world2");
  91. if (!world2Input) {
  92. world2Input = new InputBlock("world2");
  93. world2Input.setAsAttribute("world2");
  94. }
  95. world2Input.output.connectTo(this.world2);
  96. }
  97. if (!this.world3.connectedPoint) {
  98. let world3Input = material.getInputBlockByPredicate((b) => b.isAttribute && b.name === "world3");
  99. if (!world3Input) {
  100. world3Input = new InputBlock("world3");
  101. world3Input.setAsAttribute("world3");
  102. }
  103. world3Input.output.connectTo(this.world3);
  104. }
  105. if (!this.world.connectedPoint) {
  106. let worldInput = material.getInputBlockByPredicate((b) => b.isAttribute && b.name === "world");
  107. if (!worldInput) {
  108. worldInput = new InputBlock("world");
  109. worldInput.setAsWellKnownValue(NodeMaterialWellKnownValues.World);
  110. }
  111. worldInput.output.connectTo(this.world);
  112. }
  113. this.world.define = "!INSTANCES";
  114. }
  115. public prepareDefines(mesh: AbstractMesh, nodeMaterial: NodeMaterial, defines: NodeMaterialDefines, useInstances: boolean = false) {
  116. let changed = false;
  117. if (defines["INSTANCES"] !== useInstances) {
  118. defines.setValue("INSTANCES", useInstances);
  119. changed = true;
  120. }
  121. if (changed) {
  122. defines.markAsUnprocessed();
  123. }
  124. }
  125. protected _buildBlock(state: NodeMaterialBuildState) {
  126. super._buildBlock(state);
  127. // Register for defines
  128. state.sharedData.blocksWithDefines.push(this);
  129. // Emit code
  130. let output = this._outputs[0];
  131. let world0 = this.world0;
  132. let world1 = this.world1;
  133. let world2 = this.world2;
  134. let world3 = this.world3;
  135. state.compilationString += `#ifdef INSTANCES\r\n`;
  136. state.compilationString += this._declareOutput(output, state) + ` = mat4(${world0.associatedVariableName}, ${world1.associatedVariableName}, ${world2.associatedVariableName}, ${world3.associatedVariableName});\r\n`;
  137. state.compilationString += `#else\r\n`;
  138. state.compilationString += this._declareOutput(output, state) + ` = ${this.world.associatedVariableName};\r\n`;
  139. state.compilationString += `#endif\r\n`;
  140. return this;
  141. }
  142. }
  143. _TypeStore.RegisteredTypes["BABYLON.InstancesBlock"] = InstancesBlock;