MaterialXUtils.js 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. import {
  2. bool,
  3. element,
  4. float,
  5. vec3,
  6. } from 'three/tsl';
  7. const BOOLEAN_OPERATOR_OPS = new Set( [ '&&', '||', '^^', '!', '==', '!=', '<', '>', '<=', '>=' ] );
  8. function normalizeSpaceName( value, fallback = 'world' ) {
  9. if ( typeof value !== 'string' ) return fallback;
  10. const normalized = value.trim().toLowerCase();
  11. if ( normalized === '' ) return fallback;
  12. if ( normalized === 'world' ) return 'world';
  13. if ( normalized === 'object' || normalized === 'model' ) return 'object';
  14. return fallback;
  15. }
  16. function isBooleanNode( node ) {
  17. return node && ( node.nodeType === 'bool' || ( node.isOperatorNode && BOOLEAN_OPERATOR_OPS.has( node.op ) ) );
  18. }
  19. function toBooleanNode( node ) {
  20. if ( ! node ) return bool( false );
  21. if ( typeof node === 'boolean' ) return bool( node );
  22. if ( typeof node === 'number' ) return bool( node !== 0 );
  23. if ( isBooleanNode( node ) ) return node;
  24. return node.notEqual( float( 0 ) );
  25. }
  26. function getComponentCountForType( type ) {
  27. if ( type === 'vector2' ) return 2;
  28. if ( type === 'vector3' || type === 'color3' ) return 3;
  29. return 4;
  30. }
  31. function toVec3Channels( input ) {
  32. return vec3( element( input, 0 ), element( input, 1 ), element( input, 2 ) );
  33. }
  34. export {
  35. getComponentCountForType,
  36. isBooleanNode,
  37. normalizeSpaceName,
  38. toBooleanNode,
  39. toVec3Channels,
  40. };
粤ICP备19079148号