TextureCubeNode.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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 { NormalNode } from '../accessors/NormalNode.js';
  10. import { ColorSpaceNode } from '../utils/ColorSpaceNode.js';
  11. import { SpecularMIPLevelNode } from '../utils/SpecularMIPLevelNode.js';
  12. function TextureCubeNode( value, textureSize, uv, bias ) {
  13. TempNode.call( this, 'v4' );
  14. this.value = value;
  15. textureSize = textureSize || new FloatNode( 1024 );
  16. this.radianceCache = { uv: new TextureCubeUVNode(
  17. uv || new ReflectNode( ReflectNode.VECTOR ),
  18. textureSize,
  19. // bias should be replaced in builder.context in build process
  20. bias
  21. ) };
  22. this.irradianceCache = { uv: new TextureCubeUVNode(
  23. new NormalNode( NormalNode.WORLD ),
  24. textureSize,
  25. new FloatNode( 1 ).setReadonly( true )
  26. ) };
  27. }
  28. TextureCubeNode.prototype = Object.create( TempNode.prototype );
  29. TextureCubeNode.prototype.constructor = TextureCubeNode;
  30. TextureCubeNode.prototype.nodeType = "TextureCube";
  31. TextureCubeNode.prototype.generateTextureCubeUV = function ( builder, cache ) {
  32. var uv_10 = cache.uv.build( builder ) + '.uv_10',
  33. uv_20 = cache.uv.build( builder ) + '.uv_20',
  34. t = cache.uv.build( builder ) + '.t';
  35. var color10 = 'texture2D( ' + this.value.build( builder, 'sampler2D' ) + ', ' + uv_10 + ' )',
  36. color20 = 'texture2D( ' + this.value.build( builder, 'sampler2D' ) + ', ' + uv_20 + ' )';
  37. // add a custom context for fix incompatibility with the core
  38. // include ColorSpace function only for vertex shader (in fragment shader color space functions is added automatically by core)
  39. // this should be removed in the future
  40. // context.include =: is used to include or not functions if used FunctionNode
  41. // context.ignoreCache =: not create temp variables nodeT0..9 to optimize the code
  42. var context = { include: builder.isShader( 'vertex' ), ignoreCache: true };
  43. var outputType = this.getType( builder );
  44. builder.addContext( context );
  45. cache.colorSpace10 = cache.colorSpace10 || new ColorSpaceNode( new ExpressionNode( '', outputType ) );
  46. cache.colorSpace10.fromDecoding( builder.getTextureEncodingFromMap( this.value.value ) );
  47. cache.colorSpace10.input.parse( color10 );
  48. color10 = cache.colorSpace10.build( builder, outputType );
  49. cache.colorSpace20 = cache.colorSpace20 || new ColorSpaceNode( new ExpressionNode( '', outputType ) );
  50. cache.colorSpace20.fromDecoding( builder.getTextureEncodingFromMap( this.value.value ) );
  51. cache.colorSpace20.input.parse( color20 );
  52. color20 = cache.colorSpace20.build( builder, outputType );
  53. // end custom context
  54. builder.removeContext();
  55. return 'mix( ' + color10 + ', ' + color20 + ', ' + t + ' ).rgb';
  56. };
  57. TextureCubeNode.prototype.generate = function ( builder, output ) {
  58. if ( builder.isShader( 'fragment' ) ) {
  59. builder.require( 'irradiance' );
  60. if ( builder.context.bias ) {
  61. builder.context.bias.setTexture( this );
  62. }
  63. var cache = builder.slot === 'irradiance' ? this.irradianceCache : this.radianceCache;
  64. var result = this.generateTextureCubeUV( builder, cache );
  65. return builder.format( 'vec4( ' + result + ', 1.0 )', this.getType( builder ), output );
  66. } else {
  67. console.warn( "THREE.TextureCubeNode is not compatible with " + builder.shader + " shader." );
  68. return builder.format( 'vec4( 0.0 )', this.getType( builder ), output );
  69. }
  70. };
  71. TextureCubeNode.prototype.copy = function ( source ) {
  72. TempNode.prototype.copy.call( this, source );
  73. this.value = source.value;
  74. return this;
  75. };
  76. TextureCubeNode.prototype.toJSON = function ( meta ) {
  77. var data = this.getJSONNode( meta );
  78. if ( ! data ) {
  79. data = this.createJSONNode( meta );
  80. data.value = this.value.toJSON( meta ).uuid;
  81. }
  82. return data;
  83. };
  84. export { TextureCubeNode };