TextureCubeNode.js 3.8 KB

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