NodeUtils.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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 ) {
  17. for ( const property in node ) {
  18. const object = node[ property ];
  19. if ( Array.isArray( object ) === true ) {
  20. for ( let i = 0; i < object.length; i++ ) {
  21. const child = object[ i ];
  22. if ( child && child.isNode === true ) {
  23. yield { property, index: i, childNode: child };
  24. }
  25. }
  26. } else if ( object && object.isNode === true ) {
  27. yield { property, childNode: object };
  28. } else if ( typeof object === 'object' ) {
  29. for ( const subProperty in object ) {
  30. const child = object[ subProperty ];
  31. if ( child && child.isNode === true ) {
  32. yield { property, index: subProperty, childNode: child };
  33. }
  34. }
  35. }
  36. }
  37. }
  38. export function getValueType( value ) {
  39. if ( typeof value === 'number' ) {
  40. return 'float';
  41. } else if ( typeof value === 'boolean' ) {
  42. return 'bool';
  43. } else if ( value && value.isVector2 === true ) {
  44. return 'vec2';
  45. } else if ( value && value.isVector3 === true ) {
  46. return 'vec3';
  47. } else if ( value && value.isVector4 === true ) {
  48. return 'vec4';
  49. } else if ( value && value.isMatrix3 === true ) {
  50. return 'mat3';
  51. } else if ( value && value.isMatrix4 === true ) {
  52. return 'mat4';
  53. } else if ( value && value.isColor === true ) {
  54. return 'color';
  55. }
  56. return null;
  57. }
  58. export function getValueFromType( type, ...params ) {
  59. const last4 = type ? type.slice( - 4 ) : undefined;
  60. if ( type === 'color' ) {
  61. return new Color( ...params );
  62. } else if ( last4 === 'vec2' ) {
  63. return new Vector2( ...params );
  64. } else if ( last4 === 'vec3' ) {
  65. return new Vector3( ...params );
  66. } else if ( last4 === 'vec4' ) {
  67. return new Vector4( ...params );
  68. } else if ( last4 === 'mat3' ) {
  69. return new Matrix3( ...params );
  70. } else if ( last4 === 'mat4' ) {
  71. return new Matrix4( ...params );
  72. } else if ( type === 'bool' ) {
  73. return params[ 0 ] || false;
  74. } else if ( ( type === 'float' ) || ( type === 'int' ) || ( type === 'uint' ) ) {
  75. return params[ 0 ] || 0;
  76. }
  77. return null;
  78. }