TextureNode.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. import UniformNode from '../core/UniformNode.js';
  2. import UVNode from './UVNode.js';
  3. class TextureNode extends UniformNode {
  4. constructor( value, uvNode = new UVNode(), levelNode = null ) {
  5. super( value, 'vec4' );
  6. this.isTextureNode = true;
  7. this.uvNode = uvNode;
  8. this.levelNode = levelNode;
  9. }
  10. getUniformHash( /*builder*/ ) {
  11. return this.value.uuid;
  12. }
  13. getInputType( /*builder*/ ) {
  14. return 'texture';
  15. }
  16. generate( builder, output ) {
  17. const texture = this.value;
  18. if ( ! texture || texture.isTexture !== true ) {
  19. throw new Error( 'TextureNode: Need a three.js texture.' );
  20. }
  21. const textureProperty = super.generate( builder, 'texture' );
  22. if ( output === 'sampler' ) {
  23. return textureProperty + '_sampler';
  24. } else if ( builder.isReference( output ) ) {
  25. return textureProperty;
  26. } else {
  27. const nodeData = builder.getDataFromNode( this );
  28. let snippet = nodeData.snippet;
  29. if ( snippet === undefined ) {
  30. const uvSnippet = this.uvNode.build( builder, 'vec2' );
  31. const levelNode = this.levelNode;
  32. if ( levelNode !== null ) {
  33. const levelSnippet = levelNode.build( builder, 'float' );
  34. snippet = builder.getTextureLevel( textureProperty, uvSnippet, levelSnippet );
  35. } else {
  36. snippet = builder.getTexture( textureProperty, uvSnippet );
  37. }
  38. nodeData.snippet = snippet;
  39. }
  40. return builder.format( snippet, 'vec4', output );
  41. }
  42. }
  43. serialize( data ) {
  44. super.serialize( data );
  45. data.value = this.value.toJSON( data.meta ).uuid;
  46. }
  47. deserialize( data ) {
  48. super.deserialize( data );
  49. this.value = data.meta.textures[ data.value ];
  50. }
  51. }
  52. export default TextureNode;