TextureNode.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. function TextureNode( value, coord, bias, project ) {
  8. InputNode.call( this, 'v4', { shared: true } );
  9. this.value = value;
  10. this.coord = coord || 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. var coord = this.coord.build( builder, this.project ? 'v4' : 'v2' );
  26. var bias = this.bias ? this.bias.build( builder, 'fv1' ) : undefined;
  27. if ( bias == undefined && builder.context.bias ) {
  28. bias = new builder.context.bias( this ).build( builder, 'fv1' );
  29. }
  30. var method, code;
  31. if ( this.project ) method = 'texture2DProj';
  32. else method = bias ? 'tex2DBias' : 'tex2D';
  33. if ( bias ) code = method + '( ' + tex + ', ' + coord + ', ' + bias + ' )';
  34. else code = method + '( ' + tex + ', ' + coord + ' )';
  35. code = builder.getTexelDecodingFunctionFromTexture( code, this.value );
  36. return builder.format( code, this.type, output );
  37. };
  38. TextureNode.prototype.copy = function ( source ) {
  39. InputNode.prototype.copy.call( this, source );
  40. if ( source.value ) this.value = source.value;
  41. this.coord = source.coord;
  42. if ( source.bias ) this.bias = source.bias;
  43. if ( source.project !== undefined ) this.project = source.project;
  44. };
  45. TextureNode.prototype.toJSON = function ( meta ) {
  46. var data = this.getJSONNode( meta );
  47. if ( ! data ) {
  48. data = this.createJSONNode( meta );
  49. if ( this.value ) data.value = this.value.uuid;
  50. data.coord = this.coord.toJSON( meta ).uuid;
  51. data.project = this.project;
  52. if ( this.bias ) data.bias = this.bias.toJSON( meta ).uuid;
  53. }
  54. return data;
  55. };
  56. export { TextureNode };