transformBlock.ts 4.1 KB

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