CubeTextureNode.js 2.6 KB

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