TextureCubeNode.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /**
  2. * @author sunag / http://www.sunag.com.br/
  3. */
  4. import { TempNode } from '../core/TempNode.js';
  5. import { FloatNode } from '../inputs/FloatNode.js';
  6. import { ExpressionNode } from '../core/ExpressionNode.js';
  7. import { TextureCubeUVNode } from './TextureCubeUVNode.js';
  8. import { ReflectNode } from '../accessors/ReflectNode.js';
  9. import { ColorSpaceNode } from '../utils/ColorSpaceNode.js';
  10. function TextureCubeNode( value ) {
  11. TempNode.call( this, 'v4' );
  12. this.value = value;
  13. this.radianceCache = { uv: new TextureCubeUVNode() };
  14. this.irradianceCache = { uv: new TextureCubeUVNode( new ReflectNode( ReflectNode.VECTOR2 ), undefined, new FloatNode( 1 ).setReadonly( true ) ) };
  15. }
  16. TextureCubeNode.prototype = Object.create( TempNode.prototype );
  17. TextureCubeNode.prototype.constructor = TextureCubeNode;
  18. TextureCubeNode.prototype.nodeType = "TextureCube";
  19. TextureCubeNode.prototype.generateTextureCubeUV = function ( builder, cache ) {
  20. var uv_10 = cache.uv.build( builder ) + '.uv_10',
  21. uv_20 = cache.uv.build( builder ) + '.uv_20',
  22. t = cache.uv.build( builder ) + '.t';
  23. var color10 = 'texture2D( ' + this.value.build( builder, 'sampler2D' ) + ', ' + uv_10 + ' )',
  24. color20 = 'texture2D( ' + this.value.build( builder, 'sampler2D' ) + ', ' + uv_20 + ' )';
  25. // add a custom context for fix incompatibility with the core
  26. // include ColorSpace function only for vertex shader (in fragment shader color space functions is added automatically by core)
  27. // this should be removed in the future
  28. // context.include =: is used to include or not functions if used FunctionNode
  29. // context.ignoreCache =: not create variables nodeT0..9 to optimize the code
  30. var context = { include: builder.isShader( 'vertex' ), ignoreCache: true };
  31. var outputType = this.getType( builder );
  32. builder.addContext( context );
  33. cache.colorSpace10 = cache.colorSpace10 || new ColorSpaceNode( new ExpressionNode('', outputType ) );
  34. cache.colorSpace10.fromDecoding( builder.getTextureEncodingFromMap( this.value.value ) );
  35. cache.colorSpace10.input.parse( color10 );
  36. color10 = cache.colorSpace10.build( builder, outputType );
  37. cache.colorSpace20 = cache.colorSpace20 || new ColorSpaceNode( new ExpressionNode('', outputType ) );
  38. cache.colorSpace20.fromDecoding( builder.getTextureEncodingFromMap( this.value.value ) );
  39. cache.colorSpace20.input.parse( color20 );
  40. color20 = cache.colorSpace20.build( builder, outputType );
  41. // end custom context
  42. builder.removeContext();
  43. return 'mix( ' + color10 + ', ' + color20 + ', ' + t + ' ).rgb';
  44. };
  45. TextureCubeNode.prototype.generate = function ( builder, output ) {
  46. if ( builder.isShader( 'fragment' ) ) {
  47. var radiance = this.generateTextureCubeUV( builder, this.radianceCache );
  48. var irradiance = this.generateTextureCubeUV( builder, this.irradianceCache );
  49. builder.context.extra.irradiance = '( PI * ' + irradiance + ' )';
  50. return builder.format( 'vec4( ' + radiance + ', 1.0 )', this.getType( builder ), output );
  51. } else {
  52. console.warn( "THREE.TextureCubeNode is not compatible with " + builder.shader + " shader." );
  53. return builder.format( 'vec4( 0.0 )', this.getType( builder ), output );
  54. }
  55. };
  56. TextureCubeNode.prototype.toJSON = function ( meta ) {
  57. var data = this.getJSONNode( meta );
  58. if ( ! data ) {
  59. data = this.createJSONNode( meta );
  60. data.value = this.value.toJSON( meta ).uuid;
  61. }
  62. return data;
  63. };
  64. export { TextureCubeNode };