textureBlock.ts 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. import { NodeMaterialBlock } from '../nodeMaterialBlock';
  2. import { NodeMaterialBlockConnectionPointTypes } from '../nodeMaterialBlockConnectionPointTypes';
  3. import { NodeMaterialBuildState } from '../nodeMaterialBuildState';
  4. import { NodeMaterialBlockTargets } from '../nodeMaterialBlockTargets';
  5. /**
  6. * Block used to read a texture from a sampler
  7. */
  8. export class TextureBlock extends NodeMaterialBlock {
  9. /**
  10. * Create a new TextureBlock
  11. * @param name defines the block name
  12. */
  13. public constructor(name: string) {
  14. super(name, NodeMaterialBlockTargets.Fragment);
  15. this.registerInput("uv", NodeMaterialBlockConnectionPointTypes.Vector2);
  16. this.registerInput("texture", NodeMaterialBlockConnectionPointTypes.Texture);
  17. this.registerOutput("color", NodeMaterialBlockConnectionPointTypes.Color4);
  18. }
  19. /**
  20. * Gets the current class name
  21. * @returns the class name
  22. */
  23. public getClassName() {
  24. return "TextureBlock";
  25. }
  26. protected _buildBlock(state: NodeMaterialBuildState) {
  27. super._buildBlock(state);
  28. let uvInput = this._inputs[0];
  29. let samplerInput = this._inputs[1];
  30. let output = this._outputs[0];
  31. state.compilationString += `vec4 ${output.associatedVariableName} = texture2D(${samplerInput.associatedVariableName}, ${uvInput.associatedVariableName});\r\n`;
  32. return this;
  33. }
  34. }