2
0

TextureNode.js 2.5 KB

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