normalBlendBlock.ts 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. /**
  8. * Block used to blend normals
  9. */
  10. export class NormalBlendBlock extends NodeMaterialBlock {
  11. /**
  12. * Creates a new NormalBlendBlock
  13. * @param name defines the block name
  14. */
  15. public constructor(name: string) {
  16. super(name, NodeMaterialBlockTargets.Neutral);
  17. this.registerInput("normalMap0", NodeMaterialBlockConnectionPointTypes.Vector3);
  18. this.registerInput("normalMap1", NodeMaterialBlockConnectionPointTypes.Vector3);
  19. this.registerOutput("output", NodeMaterialBlockConnectionPointTypes.Vector3);
  20. this._inputs[0].acceptedConnectionPointTypes.push(NodeMaterialBlockConnectionPointTypes.Color3);
  21. this._inputs[0].acceptedConnectionPointTypes.push(NodeMaterialBlockConnectionPointTypes.Color4);
  22. this._inputs[0].acceptedConnectionPointTypes.push(NodeMaterialBlockConnectionPointTypes.Vector4);
  23. this._inputs[1].acceptedConnectionPointTypes.push(NodeMaterialBlockConnectionPointTypes.Color3);
  24. this._inputs[1].acceptedConnectionPointTypes.push(NodeMaterialBlockConnectionPointTypes.Color4);
  25. this._inputs[1].acceptedConnectionPointTypes.push(NodeMaterialBlockConnectionPointTypes.Vector4);
  26. }
  27. /**
  28. * Gets the current class name
  29. * @returns the class name
  30. */
  31. public getClassName() {
  32. return "NormalBlendBlock";
  33. }
  34. /**
  35. * Gets the first input component
  36. */
  37. public get normalMap0(): NodeMaterialConnectionPoint {
  38. return this._inputs[0];
  39. }
  40. /**
  41. * Gets the second input component
  42. */
  43. public get normalMap1(): NodeMaterialConnectionPoint {
  44. return this._inputs[1];
  45. }
  46. /**
  47. * Gets the output component
  48. */
  49. public get output(): NodeMaterialConnectionPoint {
  50. return this._outputs[0];
  51. }
  52. protected _buildBlock(state: NodeMaterialBuildState) {
  53. super._buildBlock(state);
  54. let output = this._outputs[0];
  55. let input0 = this._inputs[0];
  56. let input1 = this._inputs[1];
  57. let stepR = state._getFreeVariableName("stepR");
  58. let stepG = state._getFreeVariableName("stepG");
  59. state.compilationString += `float ${stepR} = step(0.5, ${input0.associatedVariableName}.r);\r\n`;
  60. state.compilationString += `float ${stepG} = step(0.5, ${input0.associatedVariableName}.g);\r\n`;
  61. state.compilationString += this._declareOutput(output, state) + `;\r\n`;
  62. state.compilationString += `${output.associatedVariableName}.r = (1.0 - ${stepR}) * ${input0.associatedVariableName}.r * ${input1.associatedVariableName}.r * 2.0 + ${stepR} * (1.0 - ${input0.associatedVariableName}.r) * (1.0 - ${input1.associatedVariableName}.r) * 2.0;\r\n`;
  63. state.compilationString += `${output.associatedVariableName}.g = (1.0 - ${stepG}) * ${input0.associatedVariableName}.g * ${input1.associatedVariableName}.g * 2.0 + ${stepG} * (1.0 - ${input0.associatedVariableName}.g) * (1.0 - ${input1.associatedVariableName}.g) * 2.0;\r\n`;
  64. state.compilationString += `${output.associatedVariableName}.b = ${input0.associatedVariableName}.b * ${input1.associatedVariableName}.b;\r\n`;
  65. return this;
  66. }
  67. }
  68. _TypeStore.RegisteredTypes["BABYLON.NormalBlendBlock"] = NormalBlendBlock;