TextureNode.js 1.6 KB

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