TextureNode.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. import { InputNode } from '../core/InputNode.js';
  2. import { UVNode } from '../accessors/UVNode.js';
  3. import { ColorSpaceNode } from '../utils/ColorSpaceNode.js';
  4. import { ExpressionNode } from '../core/ExpressionNode.js';
  5. class TextureNode extends InputNode {
  6. constructor( value, uv, bias, project ) {
  7. super( 'v4', { shared: true } );
  8. this.value = value;
  9. this.uv = uv || new UVNode();
  10. this.bias = bias;
  11. this.project = project !== undefined ? project : false;
  12. }
  13. getTexture( builder, output ) {
  14. return super.generate( builder, output, this.value.uuid, 't' );
  15. }
  16. generate( builder, output ) {
  17. if ( output === 'sampler2D' ) {
  18. return this.getTexture( builder, output );
  19. }
  20. const tex = this.getTexture( builder, output ),
  21. uv = this.uv.build( builder, this.project ? 'v4' : 'v2' );
  22. let bias = this.bias ? this.bias.build( builder, 'f' ) : undefined;
  23. if ( bias === undefined && builder.context.bias ) {
  24. bias = builder.context.bias.setTexture( this ).build( builder, 'f' );
  25. }
  26. let method, code;
  27. if ( this.project ) method = 'texture2DProj';
  28. else method = bias ? 'tex2DBias' : 'tex2D';
  29. if ( bias ) code = method + '( ' + tex + ', ' + uv + ', ' + bias + ' )';
  30. else code = method + '( ' + tex + ', ' + uv + ' )';
  31. // add a custom context for fix incompatibility with the core
  32. // include ColorSpace function only for vertex shader (in fragment shader color space functions is added automatically by core)
  33. // this should be removed in the future
  34. // context.include is used to include or not functions if used FunctionNode
  35. // context.ignoreCache =: not create temp variables nodeT0..9 to optimize the code
  36. const context = { include: builder.isShader( 'vertex' ), ignoreCache: true };
  37. const outputType = this.getType( builder );
  38. builder.addContext( context );
  39. this.colorSpace = this.colorSpace || new ColorSpaceNode( new ExpressionNode( '', outputType ) );
  40. this.colorSpace.fromDecoding( builder.getTextureEncodingFromMap( this.value ) );
  41. this.colorSpace.input.parse( code );
  42. code = this.colorSpace.build( builder, outputType );
  43. // end custom context
  44. builder.removeContext();
  45. return builder.format( code, outputType, output );
  46. }
  47. copy( source ) {
  48. super.copy( source );
  49. if ( source.value ) this.value = source.value;
  50. this.uv = source.uv;
  51. if ( source.bias ) this.bias = source.bias;
  52. if ( source.project !== undefined ) this.project = source.project;
  53. return this;
  54. }
  55. toJSON( meta ) {
  56. let data = this.getJSONNode( meta );
  57. if ( ! data ) {
  58. data = this.createJSONNode( meta );
  59. if ( this.value ) data.value = this.value.uuid;
  60. data.uv = this.uv.toJSON( meta ).uuid;
  61. data.project = this.project;
  62. if ( this.bias ) data.bias = this.bias.toJSON( meta ).uuid;
  63. }
  64. return data;
  65. }
  66. }
  67. TextureNode.prototype.nodeType = 'Texture';
  68. export { TextureNode };