TextureNode.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. import InputNode from '../core/InputNode.js';
  2. import UVNode from '../accessors/UVNode.js';
  3. class TextureNode extends InputNode {
  4. constructor( value = null, uv = new UVNode(), bias = null ) {
  5. super( 'texture' );
  6. this.value = value;
  7. this.uv = uv;
  8. this.bias = bias;
  9. }
  10. generate( builder, output ) {
  11. const texture = this.value;
  12. if ( ! texture || texture.isTexture !== true ) {
  13. throw new Error( 'TextureNode: Need a three.js texture.' );
  14. }
  15. const type = this.getNodeType( builder );
  16. const textureProperty = super.generate( builder, type );
  17. if ( output === 'sampler2D' || output === 'texture2D' ) {
  18. return textureProperty;
  19. } else if ( output === 'sampler' ) {
  20. return textureProperty + '_sampler';
  21. } else {
  22. const nodeData = builder.getDataFromNode( this );
  23. let snippet = nodeData.snippet;
  24. if ( snippet === undefined ) {
  25. const uvSnippet = this.uv.build( builder, 'vec2' );
  26. const bias = this.bias;
  27. let biasSnippet = null;
  28. if ( bias !== null ) {
  29. biasSnippet = bias.build( builder, 'float' );
  30. }
  31. snippet = builder.getTexture( textureProperty, uvSnippet, biasSnippet );
  32. nodeData.snippet = snippet;
  33. }
  34. return builder.format( snippet, 'vec4', output );
  35. }
  36. }
  37. }
  38. TextureNode.prototype.isTextureNode = true;
  39. export default TextureNode;