NodeUtils.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. import { Color, Matrix3, Matrix4, Vector2, Vector3, Vector4 } from 'three';
  2. export function getCacheKey( object, force = false ) {
  3. let cacheKey = '{';
  4. if ( object.isNode === true ) {
  5. cacheKey += object.id;
  6. }
  7. for ( const { property, childNode } of getNodeChildren( object ) ) {
  8. cacheKey += ',' + property.slice( 0, - 4 ) + ':' + childNode.getCacheKey( force );
  9. }
  10. cacheKey += '}';
  11. return cacheKey;
  12. }
  13. export function* getNodeChildren( node, toJSON = false ) {
  14. for ( const property in node ) {
  15. // Ignore private properties.
  16. if ( property.startsWith( '_' ) === true ) continue;
  17. const object = node[ property ];
  18. if ( Array.isArray( object ) === true ) {
  19. for ( let i = 0; i < object.length; i ++ ) {
  20. const child = object[ i ];
  21. if ( child && ( child.isNode === true || toJSON && typeof child.toJSON === 'function' ) ) {
  22. yield { property, index: i, childNode: child };
  23. }
  24. }
  25. } else if ( object && object.isNode === true ) {
  26. yield { property, childNode: object };
  27. } else if ( typeof object === 'object' ) {
  28. for ( const subProperty in object ) {
  29. const child = object[ subProperty ];
  30. if ( child && ( child.isNode === true || toJSON && typeof child.toJSON === 'function' ) ) {
  31. yield { property, index: subProperty, childNode: child };
  32. }
  33. }
  34. }
  35. }
  36. }
  37. export function getValueType( value ) {
  38. if ( value === undefined || value === null ) return null;
  39. const typeOf = typeof value;
  40. if ( value.isNode === true ) {
  41. return 'node';
  42. } else if ( typeOf === 'number' ) {
  43. return 'float';
  44. } else if ( typeOf === 'boolean' ) {
  45. return 'bool';
  46. } else if ( typeOf === 'string' ) {
  47. return 'string';
  48. } else if ( typeOf === 'function' ) {
  49. return 'shader';
  50. } else if ( value.isVector2 === true ) {
  51. return 'vec2';
  52. } else if ( value.isVector3 === true ) {
  53. return 'vec3';
  54. } else if ( value.isVector4 === true ) {
  55. return 'vec4';
  56. } else if ( value.isMatrix3 === true ) {
  57. return 'mat3';
  58. } else if ( value.isMatrix4 === true ) {
  59. return 'mat4';
  60. } else if ( value.isColor === true ) {
  61. return 'color';
  62. } else if ( value instanceof ArrayBuffer ) {
  63. return 'ArrayBuffer';
  64. }
  65. return null;
  66. }
  67. export function getValueFromType( type, ...params ) {
  68. const last4 = type ? type.slice( - 4 ) : undefined;
  69. if ( params.length === 1 ) { // ensure same behaviour as in NodeBuilder.format()
  70. if ( last4 === 'vec2' ) params = [ params[ 0 ], params[ 0 ] ];
  71. else if ( last4 === 'vec3' ) params = [ params[ 0 ], params[ 0 ], params[ 0 ] ];
  72. else if ( last4 === 'vec4' ) params = [ params[ 0 ], params[ 0 ], params[ 0 ], params[ 0 ] ];
  73. }
  74. if ( type === 'color' ) {
  75. return new Color( ...params );
  76. } else if ( last4 === 'vec2' ) {
  77. return new Vector2( ...params );
  78. } else if ( last4 === 'vec3' ) {
  79. return new Vector3( ...params );
  80. } else if ( last4 === 'vec4' ) {
  81. return new Vector4( ...params );
  82. } else if ( last4 === 'mat3' ) {
  83. return new Matrix3( ...params );
  84. } else if ( last4 === 'mat4' ) {
  85. return new Matrix4( ...params );
  86. } else if ( type === 'bool' ) {
  87. return params[ 0 ] || false;
  88. } else if ( ( type === 'float' ) || ( type === 'int' ) || ( type === 'uint' ) ) {
  89. return params[ 0 ] || 0;
  90. } else if ( type === 'string' ) {
  91. return params[ 0 ] || '';
  92. } else if ( type === 'ArrayBuffer' ) {
  93. return base64ToArrayBuffer( params[ 0 ] );
  94. }
  95. return null;
  96. }
  97. export function arrayBufferToBase64( arrayBuffer ) {
  98. let chars = '';
  99. const array = new Uint8Array( arrayBuffer );
  100. for ( let i = 0; i < array.length; i ++ ) {
  101. chars += String.fromCharCode( array[ i ] );
  102. }
  103. return btoa( chars );
  104. }
  105. export function base64ToArrayBuffer( base64 ) {
  106. return Uint8Array.from( atob( base64 ), c => c.charCodeAt( 0 ) ).buffer;
  107. }