TextureNode.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /**
  2. * @author sunag / http://www.sunag.com.br/
  3. */
  4. import { InputNode } from '../core/InputNode.js';
  5. import { UVNode } from '../accessors/UVNode.js';
  6. import { ColorSpaceNode } from '../utils/ColorSpaceNode.js';
  7. function TextureNode( value, uv, bias, project ) {
  8. InputNode.call( this, 'v4', { shared: true } );
  9. this.value = value;
  10. this.uv = uv || new UVNode();
  11. this.bias = bias;
  12. this.project = project !== undefined ? project : false;
  13. }
  14. TextureNode.prototype = Object.create( InputNode.prototype );
  15. TextureNode.prototype.constructor = TextureNode;
  16. TextureNode.prototype.nodeType = "Texture";
  17. TextureNode.prototype.getTexture = function ( builder, output ) {
  18. return InputNode.prototype.generate.call( this, builder, output, this.value.uuid, 't' );
  19. };
  20. TextureNode.prototype.generate = function ( builder, output ) {
  21. if ( output === 'sampler2D' ) {
  22. return this.getTexture( builder, output );
  23. }
  24. var tex = this.getTexture( builder, output ),
  25. uv = this.uv.build( builder, this.project ? 'v4' : 'v2' ),
  26. bias = this.bias ? this.bias.build( builder, 'f' ) : undefined;
  27. if ( bias == undefined && builder.context.bias ) {
  28. bias = new builder.context.bias( this ).build( builder, 'f' );
  29. }
  30. var method, code;
  31. if ( this.project ) method = 'texture2DProj';
  32. else method = bias ? 'tex2DBias' : 'tex2D';
  33. if ( bias ) code = method + '( ' + tex + ', ' + uv + ', ' + bias + ' )';
  34. else code = method + '( ' + tex + ', ' + uv + ' )';
  35. // add this context to replace ColorSpaceNode.input to code
  36. builder.addContext( { input: code, encoding: builder.getTextureEncodingFromMap( this.value ), include: builder.isShader( 'vertex' ) } );
  37. this.colorSpace = this.colorSpace || new ColorSpaceNode( this );
  38. code = this.colorSpace.build( builder, this.type );
  39. builder.removeContext();
  40. return builder.format( code, this.type, output );
  41. };
  42. TextureNode.prototype.copy = function ( source ) {
  43. InputNode.prototype.copy.call( this, source );
  44. if ( source.value ) this.value = source.value;
  45. this.uv = source.uv;
  46. if ( source.bias ) this.bias = source.bias;
  47. if ( source.project !== undefined ) this.project = source.project;
  48. };
  49. TextureNode.prototype.toJSON = function ( meta ) {
  50. var data = this.getJSONNode( meta );
  51. if ( ! data ) {
  52. data = this.createJSONNode( meta );
  53. if ( this.value ) data.value = this.value.uuid;
  54. data.uv = this.uv.toJSON( meta ).uuid;
  55. data.project = this.project;
  56. if ( this.bias ) data.bias = this.bias.toJSON( meta ).uuid;
  57. }
  58. return data;
  59. };
  60. export { TextureNode };