TextureNode.js 3.0 KB

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