CubeTextureNode.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. import TextureNode from './TextureNode.js';
  2. import UniformNode from '../core/UniformNode.js';
  3. import ReflectVectorNode from './ReflectVectorNode.js';
  4. import { negate, vec3, nodeObject } from '../shadernode/ShaderNodeBaseElements.js';
  5. class CubeTextureNode extends TextureNode {
  6. constructor( value, uvNode = null, levelNode = null ) {
  7. super( value, uvNode, levelNode );
  8. this.isCubeTextureNode = true;
  9. }
  10. getInputType( /*builder*/ ) {
  11. return 'cubeTexture';
  12. }
  13. getConstructHash( builder ) {
  14. return `${ this.uuid } / ${ builder.context.environmentContext?.uuid || '' }`;
  15. }
  16. construct( builder ) {
  17. const properties = builder.getNodeProperties( this );
  18. const uvNode = this.uvNode || builder.context.uvNode || new ReflectVectorNode();
  19. let levelNode = this.levelNode || builder.context.levelNode;
  20. if ( levelNode?.isNode === true ) {
  21. const texture = this.value;
  22. levelNode = builder.context.levelShaderNode ? builder.context.levelShaderNode.call( { texture, levelNode }, builder ) : levelNode;
  23. }
  24. properties.uvNode = uvNode;
  25. properties.levelNode = levelNode;
  26. }
  27. generate( builder, output ) {
  28. const { uvNode, levelNode } = builder.getNodeProperties( this );
  29. const texture = this.value;
  30. if ( ! texture || texture.isCubeTexture !== true ) {
  31. throw new Error( 'CubeTextureNode: Need a three.js cube texture.' );
  32. }
  33. const textureProperty = UniformNode.prototype.generate.call( this, builder, 'cubeTexture' );
  34. if ( output === 'sampler' ) {
  35. return textureProperty + '_sampler';
  36. } else if ( builder.isReference( output ) ) {
  37. return textureProperty;
  38. } else {
  39. const nodeData = builder.getDataFromNode( this );
  40. let snippet = nodeData.snippet;
  41. if ( snippet === undefined || builder.context.tempRead === false ) {
  42. const uvNodeObject = nodeObject( uvNode );
  43. const cubeUV = vec3( negate( uvNodeObject.x ), uvNodeObject.yz );
  44. const uvSnippet = cubeUV.build( builder, 'vec3' );
  45. if ( levelNode ) {
  46. const levelSnippet = levelNode.build( builder, 'float' );
  47. snippet = builder.getCubeTextureLevel( textureProperty, uvSnippet, levelSnippet );
  48. } else {
  49. snippet = builder.getCubeTexture( textureProperty, uvSnippet );
  50. }
  51. nodeData.snippet = snippet;
  52. }
  53. return builder.format( snippet, 'vec4', output );
  54. }
  55. }
  56. }
  57. export default CubeTextureNode;