CubeTextureNode.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. import { InputNode } from '../core/InputNode.js';
  2. import { ReflectNode } from '../accessors/ReflectNode.js';
  3. import { ColorSpaceNode } from '../utils/ColorSpaceNode.js';
  4. import { ExpressionNode } from '../core/ExpressionNode.js';
  5. function CubeTextureNode( value, uv, bias ) {
  6. InputNode.call( this, 'v4', { shared: true } );
  7. this.value = value;
  8. this.uv = uv || new ReflectNode();
  9. this.bias = bias;
  10. }
  11. CubeTextureNode.prototype = Object.create( InputNode.prototype );
  12. CubeTextureNode.prototype.constructor = CubeTextureNode;
  13. CubeTextureNode.prototype.nodeType = 'CubeTexture';
  14. CubeTextureNode.prototype.getTexture = function ( builder, output ) {
  15. return InputNode.prototype.generate.call( this, builder, output, this.value.uuid, 'tc' );
  16. };
  17. CubeTextureNode.prototype.generate = function ( builder, output ) {
  18. if ( output === 'samplerCube' ) {
  19. return this.getTexture( builder, output );
  20. }
  21. var cubetex = this.getTexture( builder, output );
  22. var uv = this.uv.build( builder, 'v3' );
  23. var bias = this.bias ? this.bias.build( builder, 'f' ) : undefined;
  24. if ( bias === undefined && builder.context.bias ) {
  25. bias = builder.context.bias.setTexture( this ).build( builder, 'f' );
  26. }
  27. var code;
  28. if ( bias ) code = 'texCubeBias( ' + cubetex + ', ' + uv + ', ' + bias + ' )';
  29. else code = 'texCube( ' + cubetex + ', ' + uv + ' )';
  30. // add a custom context for fix incompatibility with the core
  31. // include ColorSpace function only for vertex shader (in fragment shader color space functions is added automatically by core)
  32. // this should be removed in the future
  33. // context.include =: is used to include or not functions if used FunctionNode
  34. // context.ignoreCache =: not create variables temp nodeT0..9 to optimize the code
  35. var context = { include: builder.isShader( 'vertex' ), ignoreCache: true };
  36. var outputType = this.getType( builder );
  37. builder.addContext( context );
  38. this.colorSpace = this.colorSpace || new ColorSpaceNode( new ExpressionNode( '', outputType ) );
  39. this.colorSpace.fromDecoding( builder.getTextureEncodingFromMap( this.value ) );
  40. this.colorSpace.input.parse( code );
  41. code = this.colorSpace.build( builder, outputType );
  42. // end custom context
  43. builder.removeContext();
  44. return builder.format( code, outputType, output );
  45. };
  46. CubeTextureNode.prototype.copy = function ( source ) {
  47. InputNode.prototype.copy.call( this, source );
  48. if ( source.value ) this.value = source.value;
  49. this.uv = source.uv;
  50. if ( source.bias ) this.bias = source.bias;
  51. return this;
  52. };
  53. CubeTextureNode.prototype.toJSON = function ( meta ) {
  54. var data = this.getJSONNode( meta );
  55. if ( ! data ) {
  56. data = this.createJSONNode( meta );
  57. data.value = this.value.uuid;
  58. data.uv = this.uv.toJSON( meta ).uuid;
  59. if ( this.bias ) data.bias = this.bias.toJSON( meta ).uuid;
  60. }
  61. return data;
  62. };
  63. export { CubeTextureNode };