TextureNode.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. import { ExpressionNode } from '../core/ExpressionNode.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 a custom context for fix incompatibility with the core
  37. // include ColorSpace function only for vertex shader (in fragment shader color space functions is added automatically by core)
  38. // this should be removed in the future
  39. // context.include is used to include or not functions if used FunctionNode
  40. // context.ignoreCache =: not create temp variables nodeT0..9 to optimize the code
  41. var context = { include: builder.isShader( 'vertex' ), ignoreCache: true };
  42. var outputType = this.getType( builder );
  43. builder.addContext( context );
  44. this.colorSpace = this.colorSpace || new ColorSpaceNode( new ExpressionNode( '', outputType ) );
  45. this.colorSpace.fromEncoding( builder.getTextureEncodingFromMap( this.value ) );
  46. this.colorSpace.input.parse( code );
  47. code = this.colorSpace.build( builder, outputType );
  48. // end custom context
  49. builder.removeContext();
  50. return builder.format( code, outputType, output );
  51. };
  52. TextureNode.prototype.copy = function ( source ) {
  53. InputNode.prototype.copy.call( this, source );
  54. if ( source.value ) this.value = source.value;
  55. this.uv = source.uv;
  56. if ( source.bias ) this.bias = source.bias;
  57. if ( source.project !== undefined ) this.project = source.project;
  58. };
  59. TextureNode.prototype.toJSON = function ( meta ) {
  60. var data = this.getJSONNode( meta );
  61. if ( ! data ) {
  62. data = this.createJSONNode( meta );
  63. if ( this.value ) data.value = this.value.uuid;
  64. data.uv = this.uv.toJSON( meta ).uuid;
  65. data.project = this.project;
  66. if ( this.bias ) data.bias = this.bias.toJSON( meta ).uuid;
  67. }
  68. return data;
  69. };
  70. export { TextureNode };