derivativeBlock.ts 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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 { _TypeStore } from '../../../../Misc/typeStore';
  7. /**
  8. * Block used to get the derivative value on x and y of a given input
  9. */
  10. export class DerivativeBlock extends NodeMaterialBlock {
  11. /**
  12. * Create a new DerivativeBlock
  13. * @param name defines the block name
  14. */
  15. public constructor(name: string) {
  16. super(name, NodeMaterialBlockTargets.Fragment);
  17. this.registerInput("input", NodeMaterialBlockConnectionPointTypes.AutoDetect, false);
  18. this.registerOutput("dx", NodeMaterialBlockConnectionPointTypes.BasedOnInput);
  19. this.registerOutput("dy", NodeMaterialBlockConnectionPointTypes.BasedOnInput);
  20. this._outputs[0]._typeConnectionSource = this._inputs[0];
  21. this._outputs[1]._typeConnectionSource = this._inputs[0];
  22. }
  23. /**
  24. * Gets the current class name
  25. * @returns the class name
  26. */
  27. public getClassName() {
  28. return "DerivativeBlock";
  29. }
  30. /**
  31. * Gets the input component
  32. */
  33. public get input(): NodeMaterialConnectionPoint {
  34. return this._inputs[0];
  35. }
  36. /**
  37. * Gets the derivative output on x
  38. */
  39. public get dx(): NodeMaterialConnectionPoint {
  40. return this._outputs[0];
  41. }
  42. /**
  43. * Gets the derivative output on y
  44. */
  45. public get dy(): NodeMaterialConnectionPoint {
  46. return this._outputs[1];
  47. }
  48. protected _buildBlock(state: NodeMaterialBuildState) {
  49. super._buildBlock(state);
  50. let dx = this._outputs[0];
  51. let dy = this._outputs[1];
  52. state._emitExtension("derivatives", "#extension GL_OES_standard_derivatives : enable");
  53. if (dx.hasEndpoints) {
  54. state.compilationString += this._declareOutput(dx, state) + ` = dFdx(${this.input.associatedVariableName});\r\n`;
  55. }
  56. if (dy.hasEndpoints) {
  57. state.compilationString += this._declareOutput(dy, state) + ` = dFdy(${this.input.associatedVariableName});\r\n`;
  58. }
  59. return this;
  60. }
  61. }
  62. _TypeStore.RegisteredTypes["BABYLON.DerivativeBlock"] = DerivativeBlock;