particleTextureBlock.ts 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. import { NodeMaterialBlock } from '../../nodeMaterialBlock';
  2. import { NodeMaterialBlockConnectionPointTypes } from '../../Enums/nodeMaterialBlockConnectionPointTypes';
  3. import { NodeMaterialBuildState } from '../../nodeMaterialBuildState';
  4. import { NodeMaterialBlockTargets } from '../../Enums/nodeMaterialBlockTargets';
  5. import { NodeMaterialConnectionPoint } from '../../nodeMaterialBlockConnectionPoint';
  6. import { AbstractMesh } from '../../../../Meshes/abstractMesh';
  7. import { NodeMaterialDefines } from '../../nodeMaterial';
  8. import { InputBlock } from '../Input/inputBlock';
  9. import { BaseTexture } from '../../../Textures/baseTexture';
  10. import { Nullable } from '../../../../types';
  11. import { _TypeStore } from '../../../../Misc/typeStore';
  12. import { Texture } from '../../../Textures/texture';
  13. import { Scene } from '../../../../scene';
  14. declare type NodeMaterial = import("../../nodeMaterial").NodeMaterial;
  15. /**
  16. * Base block used for the particle texture
  17. */
  18. export class ParticleTextureBlock extends NodeMaterialBlock {
  19. private _samplerName = "diffuseSampler";
  20. private _linearDefineName: string;
  21. private _gammaDefineName: string;
  22. private _tempTextureRead: string;
  23. /**
  24. * Gets or sets the texture associated with the node
  25. */
  26. public texture: Nullable<BaseTexture>;
  27. /**
  28. * Gets or sets a boolean indicating if content needs to be converted to gamma space
  29. */
  30. public convertToGammaSpace = false;
  31. /**
  32. * Gets or sets a boolean indicating if content needs to be converted to linear space
  33. */
  34. public convertToLinearSpace = false;
  35. /**
  36. * Create a new ParticleTextureBlock
  37. * @param name defines the block name
  38. */
  39. public constructor(name: string) {
  40. super(name, NodeMaterialBlockTargets.Fragment);
  41. this._isUnique = false;
  42. this.registerInput("uv", NodeMaterialBlockConnectionPointTypes.Vector2, false, NodeMaterialBlockTargets.VertexAndFragment);
  43. this.registerOutput("rgba", NodeMaterialBlockConnectionPointTypes.Color4, NodeMaterialBlockTargets.Neutral);
  44. this.registerOutput("rgb", NodeMaterialBlockConnectionPointTypes.Color3, NodeMaterialBlockTargets.Neutral);
  45. this.registerOutput("r", NodeMaterialBlockConnectionPointTypes.Float, NodeMaterialBlockTargets.Neutral);
  46. this.registerOutput("g", NodeMaterialBlockConnectionPointTypes.Float, NodeMaterialBlockTargets.Neutral);
  47. this.registerOutput("b", NodeMaterialBlockConnectionPointTypes.Float, NodeMaterialBlockTargets.Neutral);
  48. this.registerOutput("a", NodeMaterialBlockConnectionPointTypes.Float, NodeMaterialBlockTargets.Neutral);
  49. this._inputs[0].acceptedConnectionPointTypes.push(NodeMaterialBlockConnectionPointTypes.Vector3);
  50. this._inputs[0].acceptedConnectionPointTypes.push(NodeMaterialBlockConnectionPointTypes.Vector4);
  51. }
  52. /**
  53. * Gets the current class name
  54. * @returns the class name
  55. */
  56. public getClassName() {
  57. return "ParticleTextureBlock";
  58. }
  59. /**
  60. * Gets the uv input component
  61. */
  62. public get uv(): NodeMaterialConnectionPoint {
  63. return this._inputs[0];
  64. }
  65. /**
  66. * Gets the rgba output component
  67. */
  68. public get rgba(): NodeMaterialConnectionPoint {
  69. return this._outputs[0];
  70. }
  71. /**
  72. * Gets the rgb output component
  73. */
  74. public get rgb(): NodeMaterialConnectionPoint {
  75. return this._outputs[1];
  76. }
  77. /**
  78. * Gets the r output component
  79. */
  80. public get r(): NodeMaterialConnectionPoint {
  81. return this._outputs[2];
  82. }
  83. /**
  84. * Gets the g output component
  85. */
  86. public get g(): NodeMaterialConnectionPoint {
  87. return this._outputs[3];
  88. }
  89. /**
  90. * Gets the b output component
  91. */
  92. public get b(): NodeMaterialConnectionPoint {
  93. return this._outputs[4];
  94. }
  95. /**
  96. * Gets the a output component
  97. */
  98. public get a(): NodeMaterialConnectionPoint {
  99. return this._outputs[5];
  100. }
  101. /**
  102. * Initialize the block and prepare the context for build
  103. * @param state defines the state that will be used for the build
  104. */
  105. public initialize(state: NodeMaterialBuildState) {
  106. state._excludeVariableName("diffuseSampler");
  107. }
  108. public autoConfigure(material: NodeMaterial) {
  109. if (!this.uv.isConnected) {
  110. let uvInput = material.getInputBlockByPredicate((b) => b.isAttribute && b.name === "particle_uv");
  111. if (!uvInput) {
  112. uvInput = new InputBlock("uv");
  113. uvInput.setAsAttribute("particle_uv");
  114. }
  115. uvInput.output.connectTo(this.uv);
  116. }
  117. }
  118. public prepareDefines(mesh: AbstractMesh, nodeMaterial: NodeMaterial, defines: NodeMaterialDefines) {
  119. defines.setValue(this._linearDefineName, this.convertToGammaSpace, true);
  120. defines.setValue(this._gammaDefineName, this.convertToLinearSpace, true);
  121. }
  122. public isReady() {
  123. if (this.texture && !this.texture.isReadyOrNotBlocking()) {
  124. return false;
  125. }
  126. return true;
  127. }
  128. private _writeOutput(state: NodeMaterialBuildState, output: NodeMaterialConnectionPoint, swizzle: string) {
  129. state.compilationString += `${this._declareOutput(output, state)} = ${this._tempTextureRead}.${swizzle};\r\n`;
  130. state.compilationString += `#ifdef ${this._linearDefineName}\r\n`;
  131. state.compilationString += `${output.associatedVariableName} = toGammaSpace(${output.associatedVariableName});\r\n`;
  132. state.compilationString += `#endif\r\n`;
  133. state.compilationString += `#ifdef ${this._gammaDefineName}\r\n`;
  134. state.compilationString += `${output.associatedVariableName} = toLinearSpace(${output.associatedVariableName});\r\n`;
  135. state.compilationString += `#endif\r\n`;
  136. }
  137. protected _buildBlock(state: NodeMaterialBuildState) {
  138. super._buildBlock(state);
  139. if (state.target === NodeMaterialBlockTargets.Vertex) {
  140. return;
  141. }
  142. this._tempTextureRead = state._getFreeVariableName("tempTextureRead");
  143. state._emit2DSampler(this._samplerName);
  144. state.sharedData.blockingBlocks.push(this);
  145. state.sharedData.textureBlocks.push(this);
  146. state.sharedData.blocksWithDefines.push(this);
  147. this._linearDefineName = state._getFreeDefineName("ISLINEAR");
  148. this._gammaDefineName = state._getFreeDefineName("ISGAMMA");
  149. let comments = `//${this.name}`;
  150. state._emitFunctionFromInclude("helperFunctions", comments);
  151. state.compilationString += `vec4 ${this._tempTextureRead} = texture2D(${this._samplerName}, ${this.uv.associatedVariableName});\r\n`;
  152. for (var output of this._outputs) {
  153. if (output.hasEndpoints) {
  154. this._writeOutput(state, output, output.name);
  155. }
  156. }
  157. return this;
  158. }
  159. public serialize(): any {
  160. let serializationObject = super.serialize();
  161. serializationObject.convertToGammaSpace = this.convertToGammaSpace;
  162. serializationObject.convertToLinearSpace = this.convertToLinearSpace;
  163. if (this.texture && !this.texture.isRenderTarget) {
  164. serializationObject.texture = this.texture.serialize();
  165. }
  166. return serializationObject;
  167. }
  168. public _deserialize(serializationObject: any, scene: Scene, rootUrl: string) {
  169. super._deserialize(serializationObject, scene, rootUrl);
  170. this.convertToGammaSpace = serializationObject.convertToGammaSpace;
  171. this.convertToLinearSpace = !!serializationObject.convertToLinearSpace;
  172. if (serializationObject.texture) {
  173. rootUrl = serializationObject.texture.url.indexOf("data:") === 0 ? "" : rootUrl;
  174. this.texture = Texture.Parse(serializationObject.texture, scene, rootUrl) as Texture;
  175. }
  176. }
  177. }
  178. _TypeStore.RegisteredTypes["BABYLON.ParticleTextureBlock"] = ParticleTextureBlock;