NodeUtils.js 3.6 KB

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