NodeUtils.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. import { Color, Matrix3, Matrix4, Vector2, Vector3, Vector4 } from 'three';
  2. export const getCacheKey = ( object ) => {
  3. let cacheKey = '{';
  4. if ( object.isNode === true ) {
  5. cacheKey += `uuid:"${ object.uuid }",`;
  6. }
  7. for ( const property of getNodesKeys( object ) ) {
  8. const node = object[ property ];
  9. // @TODO: Think about implement NodeArray and NodeObject.
  10. if ( Array.isArray( node ) ) {
  11. for ( const subNode of node ) {
  12. cacheKey += `${ property }:${ subNode.getCacheKey() },`;
  13. }
  14. } else {
  15. cacheKey += `${ property }:${ node.getCacheKey() },`;
  16. }
  17. }
  18. cacheKey += '}';
  19. return cacheKey;
  20. };
  21. export const getNodesKeys = ( object ) => {
  22. const props = [];
  23. for ( const name in object ) {
  24. const value = object[ name ];
  25. if ( Array.isArray( value ) ) {
  26. if ( value[ 0 ] && value[ 0 ].isNode === true ) {
  27. props.push( name );
  28. }
  29. } else if ( value && value.isNode === true ) {
  30. props.push( name );
  31. }
  32. }
  33. return props;
  34. };
  35. export const getValueType = ( value ) => {
  36. if ( typeof value === 'number' ) {
  37. return 'float';
  38. } else if ( typeof value === 'boolean' ) {
  39. return 'bool';
  40. } else if ( value && value.isVector2 === true ) {
  41. return 'vec2';
  42. } else if ( value && value.isVector3 === true ) {
  43. return 'vec3';
  44. } else if ( value && value.isVector4 === true ) {
  45. return 'vec4';
  46. } else if ( value && value.isMatrix3 === true ) {
  47. return 'mat3';
  48. } else if ( value && value.isMatrix4 === true ) {
  49. return 'mat4';
  50. } else if ( value && value.isColor === true ) {
  51. return 'color';
  52. }
  53. return null;
  54. };
  55. export const getValueFromType = ( type, ...params ) => {
  56. const last4 = type ? type.slice( - 4 ) : undefined;
  57. if ( type === 'color' ) {
  58. return new Color( ...params );
  59. } else if ( last4 === 'vec2' ) {
  60. return new Vector2( ...params );
  61. } else if ( last4 === 'vec3' ) {
  62. return new Vector3( ...params );
  63. } else if ( last4 === 'vec4' ) {
  64. return new Vector4( ...params );
  65. } else if ( last4 === 'mat3' ) {
  66. return new Matrix3( ...params );
  67. } else if ( last4 === 'mat4' ) {
  68. return new Matrix4( ...params );
  69. } else if ( type === 'bool' ) {
  70. return params[ 0 ] || false;
  71. } else if ( ( type === 'float' ) || ( type === 'int' ) || ( type === 'uint' ) ) {
  72. return params[ 0 ] || 0;
  73. }
  74. return null;
  75. };