TextureCubeNode.js 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /**
  2. * @author sunag / http://www.sunag.com.br/
  3. */
  4. import { TempNode } from '../core/TempNode.js';
  5. import { TextureCubeUVNode } from './TextureCubeUVNode.js';
  6. import { ColorSpaceNode } from '../utils/ColorSpaceNode.js';
  7. function TextureCubeNode( value, uv ) {
  8. TempNode.call( this, 'v4' );
  9. this.value = value;
  10. this.uv = uv || new TextureCubeUVNode();
  11. }
  12. TextureCubeNode.prototype = Object.create( TempNode.prototype );
  13. TextureCubeNode.prototype.constructor = TextureCubeNode;
  14. TextureCubeNode.prototype.nodeType = "TextureCube";
  15. TextureCubeNode.prototype.generate = function ( builder, output ) {
  16. if ( builder.isShader( 'fragment' ) ) {
  17. var uv_10 = this.uv.build( builder ) + '.uv_10',
  18. uv_20 = this.uv.build( builder ) + '.uv_20',
  19. t = this.uv.build( builder ) + '.t';
  20. var format = this.value.value.encoding || THREE.LinearEncoding;
  21. var decoding = ColorSpaceNode.prototype.getDecodingMethod(format);
  22. function decode(input) {
  23. return decoding[0] + '( ' + input +
  24. (decoding[1] !== undefined ? ', ' + decoding[1] : '') +
  25. ' )';
  26. }
  27. var color10 = 'texture2D( ' + this.value.build( builder, 'sampler2D' ) + ', ' + uv_10 + ' )';
  28. color10 = decode(color10);
  29. var color20 = 'texture2D( ' + this.value.build( builder, 'sampler2D' ) + ', ' + uv_20 + ' )';
  30. color20 = decode(color20);
  31. return builder.format( 'vec4( mix( ' + color10 + ', ' + color20 + ', ' + t + ' ).rgb, 1.0 )', this.getType( builder ), output );
  32. } else {
  33. console.warn( "THREE.TextureCubeNode is not compatible with " + builder.shader + " shader." );
  34. return builder.format( 'vec4( 0.0 )', this.getType( builder ), output );
  35. }
  36. };
  37. TextureCubeNode.prototype.toJSON = function ( meta ) {
  38. var data = this.getJSONNode( meta );
  39. if ( ! data ) {
  40. data = this.createJSONNode( meta );
  41. data.uv = this.uv.toJSON( meta ).uuid;
  42. data.textureSize = this.textureSize.toJSON( meta ).uuid;
  43. data.blinnExponentToRoughness = this.blinnExponentToRoughness.toJSON( meta ).uuid;
  44. if ( this.roughness ) data.roughness = this.roughness.toJSON( meta ).uuid;
  45. }
  46. return data;
  47. };
  48. export { TextureCubeNode };