NodeEditorUtils.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  1. import { StringInput, NumberInput, ColorInput, Element, LabelElement } from 'flow';
  2. import { string, float, vec2, vec3, vec4, color } from 'three/nodes';
  3. export function exportJSON( object, name ) {
  4. const json = JSON.stringify( object );
  5. const a = document.createElement( 'a' );
  6. const file = new Blob( [ json ], { type: 'text/plain' } );
  7. a.href = URL.createObjectURL( file );
  8. a.download = name + '.json';
  9. a.click();
  10. }
  11. export function disposeScene( scene ) {
  12. scene.traverse( object => {
  13. if ( ! object.isMesh ) return;
  14. object.geometry.dispose();
  15. if ( object.material.isMaterial ) {
  16. disposeMaterial( object.material );
  17. } else {
  18. for ( const material of object.material ) {
  19. disposeMaterial( material );
  20. }
  21. }
  22. } );
  23. }
  24. export function disposeMaterial( material ) {
  25. material.dispose();
  26. for ( const key of Object.keys( material ) ) {
  27. const value = material[ key ];
  28. if ( value && typeof value === 'object' && typeof value.dispose === 'function' ) {
  29. value.dispose();
  30. }
  31. }
  32. }
  33. export const colorLib = {
  34. // gpu
  35. string: '#ff0000',
  36. // cpu
  37. Material: '#228b22',
  38. Object3D: '#00a1ff',
  39. CodeNode: '#ff00ff',
  40. Texture: '#ffa500',
  41. URL: '#ff0080',
  42. String: '#ff0000'
  43. };
  44. export function getColorFromType( type ) {
  45. return colorLib[ type ];
  46. }
  47. export function getTypeFromValue( value ) {
  48. if ( value && value.isScriptableValueNode ) value = value.value;
  49. if ( ! value ) return;
  50. if ( value.isNode && value.nodeType === 'string' ) return 'string';
  51. if ( value.isNode && value.nodeType === 'ArrayBuffer' ) return 'URL';
  52. for ( const type of Object.keys( colorLib ).reverse() ) {
  53. if ( value[ 'is' + type ] === true ) return type;
  54. }
  55. }
  56. export function getColorFromValue( value ) {
  57. const type = getTypeFromValue( value );
  58. return getColorFromType( type );
  59. }
  60. export const createColorInput = ( node, element ) => {
  61. const input = new ColorInput().onChange( () => {
  62. node.value.setHex( input.getValue() );
  63. element.dispatchEvent( new Event( 'changeInput' ) );
  64. } );
  65. element.add( input );
  66. };
  67. export const createFloatInput = ( node, element ) => {
  68. const input = new NumberInput().onChange( () => {
  69. node.value = input.getValue();
  70. element.dispatchEvent( new Event( 'changeInput' ) );
  71. } );
  72. element.add( input );
  73. };
  74. export const createStringInput = ( node, element, settings = {} ) => {
  75. const input = new StringInput().onChange( () => {
  76. let value = input.getValue();
  77. if ( settings.transform === 'lowercase' ) value = value.toLowerCase();
  78. else if ( settings.transform === 'uppercase' ) value = value.toUpperCase();
  79. node.value = value;
  80. element.dispatchEvent( new Event( 'changeInput' ) );
  81. } );
  82. element.add( input );
  83. if ( settings.options ) {
  84. for ( const option of settings.options ) {
  85. input.addOption( option );
  86. }
  87. }
  88. const field = input.getInput();
  89. if ( settings.allows ) field.addEventListener( 'input', () => field.value = field.value.replace( new RegExp( '[^\\s' + settings.allows + ']', 'gi' ), '' ) );
  90. if ( settings.maxLength ) field.maxLength = settings.maxLength;
  91. if ( settings.transform ) field.style[ 'text-transform' ] = settings.transform;
  92. };
  93. export const createVector2Input = ( node, element ) => {
  94. const onUpdate = () => {
  95. node.value.x = fieldX.getValue();
  96. node.value.y = fieldY.getValue();
  97. element.dispatchEvent( new Event( 'changeInput' ) );
  98. };
  99. const fieldX = new NumberInput().setTagColor( 'red' ).onChange( onUpdate );
  100. const fieldY = new NumberInput().setTagColor( 'green' ).onChange( onUpdate );
  101. element.add( fieldX ).add( fieldY );
  102. };
  103. export const createVector3Input = ( node, element ) => {
  104. const onUpdate = () => {
  105. node.value.x = fieldX.getValue();
  106. node.value.y = fieldY.getValue();
  107. node.value.z = fieldZ.getValue();
  108. element.dispatchEvent( new Event( 'changeInput' ) );
  109. };
  110. const fieldX = new NumberInput().setTagColor( 'red' ).onChange( onUpdate );
  111. const fieldY = new NumberInput().setTagColor( 'green' ).onChange( onUpdate );
  112. const fieldZ = new NumberInput().setTagColor( 'blue' ).onChange( onUpdate );
  113. element.add( fieldX ).add( fieldY ).add( fieldZ );
  114. };
  115. export const createVector4Input = ( node, element ) => {
  116. const onUpdate = () => {
  117. node.value.x = fieldX.getValue();
  118. node.value.y = fieldY.getValue();
  119. node.value.z = fieldZ.getValue();
  120. node.value.w = fieldZ.getValue();
  121. element.dispatchEvent( new Event( 'changeInput' ) );
  122. };
  123. const fieldX = new NumberInput().setTagColor( 'red' ).onChange( onUpdate );
  124. const fieldY = new NumberInput().setTagColor( 'green' ).onChange( onUpdate );
  125. const fieldZ = new NumberInput().setTagColor( 'blue' ).onChange( onUpdate );
  126. const fieldW = new NumberInput( 1 ).setTagColor( 'white' ).onChange( onUpdate );
  127. element.add( fieldX ).add( fieldY ).add( fieldZ ).add( fieldW );
  128. };
  129. export const createInputLib = {
  130. // gpu
  131. string: createStringInput,
  132. float: createFloatInput,
  133. vec2: createVector2Input,
  134. vec3: createVector3Input,
  135. vec4: createVector4Input,
  136. color: createColorInput,
  137. // cpu
  138. Number: createFloatInput,
  139. String: createStringInput,
  140. Vector2: createVector2Input,
  141. Vector3: createVector3Input,
  142. Vector4: createVector4Input,
  143. Color: createColorInput
  144. };
  145. export const inputNodeLib = {
  146. // gpu
  147. string,
  148. float,
  149. vec2,
  150. vec3,
  151. vec4,
  152. color,
  153. // cpu
  154. Number: float,
  155. String: string,
  156. Vector2: vec2,
  157. Vector3: vec3,
  158. Vector4: vec4,
  159. Color: color
  160. };
  161. export function createElementFromJSON( json ) {
  162. const { inputType, outputType, nullable } = json;
  163. const id = json.id || json.name;
  164. const element = json.name ? new LabelElement( json.name ) : new Element();
  165. const field = nullable !== true && json.field !== false;
  166. //
  167. let inputNode = null;
  168. if ( nullable !== true && inputNodeLib[ inputType ] !== undefined ) {
  169. inputNode = inputNodeLib[ inputType ]();
  170. }
  171. element.value = inputNode;
  172. //
  173. if ( json.height ) element.setHeight( json.height );
  174. if ( inputType ) {
  175. if ( field && createInputLib[ inputType ] ) {
  176. createInputLib[ inputType ]( inputNode, element, json );
  177. }
  178. element.onConnect( () => {
  179. const externalNode = element.getLinkedObject();
  180. element.setEnabledInputs( externalNode === null );
  181. element.value = externalNode || inputNode;
  182. } );
  183. }
  184. //
  185. if ( inputType && json.inputConnection !== false ) {
  186. element.setInputColor( getColorFromType( inputType ) );
  187. //element.setInputStyle( 'dotted' ); // 'border-style: dotted;'
  188. element.setInput( 1 );
  189. element.onValid( onValidType( inputType ) );
  190. }
  191. if ( outputType ) {
  192. element.setInputColor( getColorFromType( outputType ) );
  193. //element.setInputStyle( 'dotted' ); // 'border-style: dotted;'
  194. element.setOutput( 1 );
  195. }
  196. return { id, element, inputNode, inputType, outputType };
  197. }
  198. export function isGPUNode( object ) {
  199. return object && object.isNode === true && object.isCodeNode !== true && object.nodeType !== 'string' && object.nodeType !== 'ArrayBuffer';
  200. }
  201. export function isValidTypeToType( sourceType, targetType ) {
  202. if ( sourceType === targetType ) return true;
  203. return false;
  204. }
  205. export const onValidNode = onValidType();
  206. export function onValidType( types = 'node', node = null ) {
  207. return ( source, target, stage ) => {
  208. const targetObject = target.getObject();
  209. if ( targetObject ) {
  210. for ( const type of types.split( '|' ) ) {
  211. let object = targetObject;
  212. if ( object.isScriptableValueNode ) {
  213. if ( object.outputType ) {
  214. if ( isValidTypeToType( object.outputType, type ) ) {
  215. return true;
  216. }
  217. }
  218. object = object.value;
  219. }
  220. if ( object === null || object === undefined ) continue;
  221. let isValid = false;
  222. if ( type === 'any' ) {
  223. isValid = true;
  224. } else if ( type === 'node' ) {
  225. isValid = isGPUNode( object );
  226. } else if ( type === 'string' || type === 'String' ) {
  227. isValid = object.nodeType === 'string';
  228. } else if ( type === 'Number' ) {
  229. isValid = object.isInputNode && typeof object.value === 'number';
  230. } else if ( type === 'URL' ) {
  231. isValid = object.nodeType === 'string' || object.nodeType === 'ArrayBuffer';
  232. } else if ( object[ 'is' + type ] === true ) {
  233. isValid = true;
  234. }
  235. if ( isValid ) return true;
  236. }
  237. if ( node !== null && stage === 'dragged' ) {
  238. const name = target.node.getName();
  239. node.editor.tips.error( `"${name}" is not a "${types}".` );
  240. }
  241. return false;
  242. }
  243. };
  244. }