gradientBlock.ts 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. import { NodeMaterialBlock } from '../nodeMaterialBlock';
  2. import { NodeMaterialBlockConnectionPointTypes } from '../Enums/nodeMaterialBlockConnectionPointTypes';
  3. import { NodeMaterialBuildState } from '../nodeMaterialBuildState';
  4. import { NodeMaterialConnectionPoint } from '../nodeMaterialBlockConnectionPoint';
  5. import { NodeMaterialBlockTargets } from '../Enums/nodeMaterialBlockTargets';
  6. import { _TypeStore } from '../../../Misc/typeStore';
  7. import { Color3 } from '../../../Maths/math.color';
  8. import { Scene } from '../../../scene';
  9. /**
  10. * Class used to store a color step for the GradientBlock
  11. */
  12. export class GradientBlockColorStep {
  13. /**
  14. * Creates a new GradientBlockColorStep
  15. * @param step defines a value indicating which step this color is associated with (between 0 and 1)
  16. * @param color defines the color associated with this step
  17. */
  18. public constructor(
  19. /**
  20. * Gets or sets a value indicating which step this color is associated with (between 0 and 1)
  21. */
  22. public step: number,
  23. /**
  24. * Gets or sets the color associated with this step
  25. */
  26. public color: Color3) {
  27. }
  28. }
  29. /**
  30. * Block used to return a color from a gradient based on an input value between 0 and 1
  31. */
  32. export class GradientBlock extends NodeMaterialBlock {
  33. /**
  34. * Gets or sets the list of color steps
  35. */
  36. public colorSteps: GradientBlockColorStep[] = [
  37. new GradientBlockColorStep(0, Color3.Black()),
  38. new GradientBlockColorStep(1.0, Color3.White())
  39. ];
  40. /**
  41. * Creates a new GradientBlock
  42. * @param name defines the block name
  43. */
  44. public constructor(name: string) {
  45. super(name, NodeMaterialBlockTargets.Neutral);
  46. this.registerInput("gradient", NodeMaterialBlockConnectionPointTypes.Float);
  47. this.registerOutput("output", NodeMaterialBlockConnectionPointTypes.Color3);
  48. this._inputs[0].acceptedConnectionPointTypes.push(NodeMaterialBlockConnectionPointTypes.Vector2);
  49. this._inputs[0].acceptedConnectionPointTypes.push(NodeMaterialBlockConnectionPointTypes.Vector3);
  50. this._inputs[0].acceptedConnectionPointTypes.push(NodeMaterialBlockConnectionPointTypes.Vector4);
  51. this._inputs[0].acceptedConnectionPointTypes.push(NodeMaterialBlockConnectionPointTypes.Color3);
  52. this._inputs[0].acceptedConnectionPointTypes.push(NodeMaterialBlockConnectionPointTypes.Color4);
  53. }
  54. /**
  55. * Gets the current class name
  56. * @returns the class name
  57. */
  58. public getClassName() {
  59. return "GradientBlock";
  60. }
  61. /**
  62. * Gets the gradient input component
  63. */
  64. public get gradient(): NodeMaterialConnectionPoint {
  65. return this._inputs[0];
  66. }
  67. /**
  68. * Gets the output component
  69. */
  70. public get output(): NodeMaterialConnectionPoint {
  71. return this._outputs[0];
  72. }
  73. private _writeColorConstant(index: number) {
  74. let step = this.colorSteps[index];
  75. return `vec3(${step.color.r}, ${step.color.g}, ${step.color.b})`;
  76. }
  77. protected _buildBlock(state: NodeMaterialBuildState) {
  78. super._buildBlock(state);
  79. let output = this._outputs[0];
  80. if (!this.colorSteps.length || !this.gradient.connectedPoint) {
  81. state.compilationString += this._declareOutput(output, state) + ` = vec3(0., 0., 0.);\r\n`;
  82. return;
  83. }
  84. let tempColor = state._getFreeVariableName("gradientTempColor");
  85. let tempPosition = state._getFreeVariableName("gradientTempPosition");
  86. state.compilationString += `vec3 ${tempColor} = ${this._writeColorConstant(0)};\r\n`;
  87. state.compilationString += `float ${tempPosition};\r\n`;
  88. let gradientSource = this.gradient.associatedVariableName;
  89. if (this.gradient.connectedPoint!.type !== NodeMaterialBlockConnectionPointTypes.Float) {
  90. gradientSource += ".x";
  91. }
  92. for (var index = 1; index < this.colorSteps.length; index++) {
  93. let step = this.colorSteps[index];
  94. let previousStep = this.colorSteps[index - 1];
  95. state.compilationString += `${tempPosition} = clamp((${gradientSource} - ${state._emitFloat(previousStep.step)}) / (${state._emitFloat(step.step)} - ${state._emitFloat(previousStep.step)}), 0.0, 1.0) * step(${state._emitFloat(index)}, ${state._emitFloat(this.colorSteps.length - 1)});\r\n`;
  96. state.compilationString += `${tempColor} = mix(${tempColor}, ${this._writeColorConstant(index)}, ${tempPosition});\r\n`;
  97. }
  98. state.compilationString += this._declareOutput(output, state) + ` = ${tempColor};\r\n`;
  99. return this;
  100. }
  101. public serialize(): any {
  102. let serializationObject = super.serialize();
  103. serializationObject.colorSteps = this.colorSteps;
  104. return serializationObject;
  105. }
  106. public _deserialize(serializationObject: any, scene: Scene, rootUrl: string) {
  107. super._deserialize(serializationObject, scene, rootUrl);
  108. this.colorSteps = [];
  109. for (var step of serializationObject.colorSteps) {
  110. this.colorSteps.push(new GradientBlockColorStep(step.step, new Color3(step.color.r, step.color.g, step.color.b)));
  111. }
  112. }
  113. protected _dumpPropertiesCode() {
  114. var codeString = "";
  115. for (var colorStep of this.colorSteps) {
  116. codeString += `${this._codeVariableName}.colorSteps.push(new BABYLON.GradientBlockColorStep(${colorStep.step}, new BABYLON.Color3(${colorStep.color.r}, ${colorStep.color.g}, ${colorStep.color.b})));\r\n`;
  117. }
  118. return codeString;
  119. }
  120. }
  121. _TypeStore.RegisteredTypes["BABYLON.GradientBlock"] = GradientBlock;