transformBlock.ts 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. import { Scene } from '../../../scene';
  8. import { InputBlock } from './Input/inputBlock';
  9. /**
  10. * Block used to transform a vector (2, 3 or 4) with a matrix. It will generate a Vector4
  11. */
  12. export class TransformBlock extends NodeMaterialBlock {
  13. /**
  14. * Defines the value to use to complement W value to transform it to a Vector4
  15. */
  16. public complementW = 1;
  17. /**
  18. * Defines the value to use to complement z value to transform it to a Vector4
  19. */
  20. public complementZ = 0;
  21. /**
  22. * Creates a new TransformBlock
  23. * @param name defines the block name
  24. */
  25. public constructor(name: string) {
  26. super(name, NodeMaterialBlockTargets.Vertex);
  27. this.registerInput("vector", NodeMaterialBlockConnectionPointTypes.AutoDetect);
  28. this.registerInput("transform", NodeMaterialBlockConnectionPointTypes.Matrix);
  29. this.registerOutput("output", NodeMaterialBlockConnectionPointTypes.Vector4);
  30. this._inputs[0].onConnectionObservable.add((other) => {
  31. if (other.ownerBlock.isInput) {
  32. let otherAsInput = other.ownerBlock as InputBlock;
  33. if (otherAsInput.name === "normal") {
  34. this.complementW = 0;
  35. }
  36. }
  37. });
  38. }
  39. /**
  40. * Gets the current class name
  41. * @returns the class name
  42. */
  43. public getClassName() {
  44. return "TransformBlock";
  45. }
  46. /**
  47. * Gets the vector input
  48. */
  49. public get vector(): NodeMaterialConnectionPoint {
  50. return this._inputs[0];
  51. }
  52. /**
  53. * Gets the output component
  54. */
  55. public get output(): NodeMaterialConnectionPoint {
  56. return this._outputs[0];
  57. }
  58. /**
  59. * Gets the matrix transform input
  60. */
  61. public get transform(): NodeMaterialConnectionPoint {
  62. return this._inputs[1];
  63. }
  64. protected _buildBlock(state: NodeMaterialBuildState) {
  65. super._buildBlock(state);
  66. let output = this._outputs[0];
  67. let vector = this.vector;
  68. let transform = this.transform;
  69. if (vector.connectedPoint) {
  70. switch (vector.connectedPoint.type) {
  71. case NodeMaterialBlockConnectionPointTypes.Vector2:
  72. state.compilationString += this._declareOutput(output, state) + ` = ${transform.associatedVariableName} * vec4(${vector.associatedVariableName}, ${this._writeFloat(this.complementZ)}, ${this._writeFloat(this.complementW)});\r\n`;
  73. break;
  74. case NodeMaterialBlockConnectionPointTypes.Vector3:
  75. case NodeMaterialBlockConnectionPointTypes.Color3:
  76. state.compilationString += this._declareOutput(output, state) + ` = ${transform.associatedVariableName} * vec4(${vector.associatedVariableName}, ${this._writeFloat(this.complementW)});\r\n`;
  77. break;
  78. default:
  79. state.compilationString += this._declareOutput(output, state) + ` = ${transform.associatedVariableName} * ${vector.associatedVariableName};\r\n`;
  80. break;
  81. }
  82. }
  83. return this;
  84. }
  85. public serialize(): any {
  86. let serializationObject = super.serialize();
  87. serializationObject.complementZ = this.complementZ;
  88. serializationObject.complementW = this.complementW;
  89. return serializationObject;
  90. }
  91. public _deserialize(serializationObject: any, scene: Scene, rootUrl: string) {
  92. super._deserialize(serializationObject, scene, rootUrl);
  93. this.complementZ = serializationObject.complementZ !== undefined ? serializationObject.complementZ : 0.0;
  94. this.complementW = serializationObject.complementW !== undefined ? serializationObject.complementW : 1.0;
  95. }
  96. protected _dumpPropertiesCode() {
  97. var codeString = `${this._codeVariableName}.complementZ = ${this.complementZ};\r\n`;
  98. codeString += `${this._codeVariableName}.complementW = ${this.complementW};\r\n`;
  99. return codeString;
  100. }
  101. }
  102. _TypeStore.RegisteredTypes["BABYLON.TransformBlock"] = TransformBlock;