TextureCubeNode.js 3.8 KB

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