Sidebar.Material.js 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751
  1. import * as THREE from 'three';
  2. import { UIButton, UIInput, UIPanel, UIRow, UISelect, UIText, UITextArea } from './libs/ui.js';
  3. import { SetMaterialCommand } from './commands/SetMaterialCommand.js';
  4. import { SetMaterialValueCommand } from './commands/SetMaterialValueCommand.js';
  5. import { SidebarMaterialBooleanProperty } from './Sidebar.Material.BooleanProperty.js';
  6. import { SidebarMaterialColorProperty } from './Sidebar.Material.ColorProperty.js';
  7. import { SidebarMaterialConstantProperty } from './Sidebar.Material.ConstantProperty.js';
  8. import { SidebarMaterialMapProperty } from './Sidebar.Material.MapProperty.js';
  9. import { SidebarMaterialNumberProperty } from './Sidebar.Material.NumberProperty.js';
  10. import { SidebarMaterialRangeValueProperty } from './Sidebar.Material.RangeValueProperty.js';
  11. import { SidebarMaterialProgram } from './Sidebar.Material.Program.js';
  12. function SidebarMaterial( editor ) {
  13. const signals = editor.signals;
  14. const strings = editor.strings;
  15. let currentObject;
  16. let currentMaterialSlot = 0;
  17. const container = new UIPanel();
  18. container.setBorderTop( '0' );
  19. container.setDisplay( 'none' );
  20. container.setPaddingTop( '20px' );
  21. // Current material slot
  22. const materialSlotRow = new UIRow();
  23. materialSlotRow.add( new UIText( strings.getKey( 'sidebar/material/slot' ) ).setClass( 'Label' ) );
  24. const materialSlotSelect = new UISelect().setWidth( '170px' ).setFontSize( '12px' ).onChange( update );
  25. materialSlotSelect.setOptions( { 0: '' } ).setValue( 0 );
  26. materialSlotRow.add( materialSlotSelect );
  27. container.add( materialSlotRow );
  28. // type
  29. const materialClassRow = new UIRow();
  30. const materialClass = new UISelect().setWidth( '150px' ).setFontSize( '12px' ).onChange( update );
  31. materialClassRow.add( new UIText( strings.getKey( 'sidebar/material/type' ) ).setClass( 'Label' ) );
  32. materialClassRow.add( materialClass );
  33. container.add( materialClassRow );
  34. // uuid
  35. const materialUUIDRow = new UIRow();
  36. const materialUUID = new UIInput().setWidth( '102px' ).setFontSize( '12px' ).setDisabled( true );
  37. const materialUUIDRenew = new UIButton( strings.getKey( 'sidebar/material/new' ) ).setMarginLeft( '7px' );
  38. materialUUIDRenew.onClick( function () {
  39. materialUUID.setValue( THREE.MathUtils.generateUUID() );
  40. update();
  41. } );
  42. materialUUIDRow.add( new UIText( strings.getKey( 'sidebar/material/uuid' ) ).setClass( 'Label' ) );
  43. materialUUIDRow.add( materialUUID );
  44. materialUUIDRow.add( materialUUIDRenew );
  45. container.add( materialUUIDRow );
  46. // name
  47. const materialNameRow = new UIRow();
  48. const materialName = new UIInput().setWidth( '150px' ).setFontSize( '12px' ).onChange( function () {
  49. editor.execute( new SetMaterialValueCommand( editor, editor.selected, 'name', materialName.getValue(), currentMaterialSlot ) );
  50. } );
  51. materialNameRow.add( new UIText( strings.getKey( 'sidebar/material/name' ) ).setClass( 'Label' ) );
  52. materialNameRow.add( materialName );
  53. container.add( materialNameRow );
  54. // program
  55. const materialProgram = new SidebarMaterialProgram( editor, 'vertexShader' );
  56. container.add( materialProgram );
  57. // color
  58. const materialColor = new SidebarMaterialColorProperty( editor, 'color', strings.getKey( 'sidebar/material/color' ) );
  59. container.add( materialColor );
  60. // specular
  61. const materialSpecular = new SidebarMaterialColorProperty( editor, 'specular', strings.getKey( 'sidebar/material/specular' ) );
  62. container.add( materialSpecular );
  63. // shininess
  64. const materialShininess = new SidebarMaterialNumberProperty( editor, 'shininess', strings.getKey( 'sidebar/material/shininess' ) );
  65. container.add( materialShininess );
  66. // emissive
  67. const materialEmissive = new SidebarMaterialColorProperty( editor, 'emissive', strings.getKey( 'sidebar/material/emissive' ) );
  68. container.add( materialEmissive );
  69. // reflectivity
  70. const materialReflectivity = new SidebarMaterialNumberProperty( editor, 'reflectivity', strings.getKey( 'sidebar/material/reflectivity' ) );
  71. container.add( materialReflectivity );
  72. // ior
  73. const materialIOR = new SidebarMaterialNumberProperty( editor, 'ior', strings.getKey( 'sidebar/material/ior' ), [ 1, 2.333 ], 3 );
  74. container.add( materialIOR );
  75. // roughness
  76. const materialRoughness = new SidebarMaterialNumberProperty( editor, 'roughness', strings.getKey( 'sidebar/material/roughness' ), [ 0, 1 ] );
  77. container.add( materialRoughness );
  78. // metalness
  79. const materialMetalness = new SidebarMaterialNumberProperty( editor, 'metalness', strings.getKey( 'sidebar/material/metalness' ), [ 0, 1 ] );
  80. container.add( materialMetalness );
  81. // clearcoat
  82. const materialClearcoat = new SidebarMaterialNumberProperty( editor, 'clearcoat', strings.getKey( 'sidebar/material/clearcoat' ), [ 0, 1 ] );
  83. container.add( materialClearcoat );
  84. // clearcoatRoughness
  85. const materialClearcoatRoughness = new SidebarMaterialNumberProperty( editor, 'clearcoatRoughness', strings.getKey( 'sidebar/material/clearcoatroughness' ), [ 0, 1 ] );
  86. container.add( materialClearcoatRoughness );
  87. // dispersion
  88. const materialDispersion = new SidebarMaterialNumberProperty( editor, 'dispersion', strings.getKey( 'sidebar/material/dispersion' ), [ 0, 10 ] );
  89. container.add( materialDispersion );
  90. // iridescence
  91. const materialIridescence = new SidebarMaterialNumberProperty( editor, 'iridescence', strings.getKey( 'sidebar/material/iridescence' ), [ 0, 1 ] );
  92. container.add( materialIridescence );
  93. // iridescenceIOR
  94. const materialIridescenceIOR = new SidebarMaterialNumberProperty( editor, 'iridescenceIOR', strings.getKey( 'sidebar/material/iridescenceIOR' ), [ 1, 5 ] );
  95. container.add( materialIridescenceIOR );
  96. // iridescenceThicknessMax
  97. const materialIridescenceThicknessMax = new SidebarMaterialRangeValueProperty( editor, 'iridescenceThicknessRange', strings.getKey( 'sidebar/material/iridescenceThicknessMax' ), false, [ 0, Infinity ], 0, 10, 1, 'nm' );
  98. container.add( materialIridescenceThicknessMax );
  99. // sheen
  100. const materialSheen = new SidebarMaterialNumberProperty( editor, 'sheen', strings.getKey( 'sidebar/material/sheen' ), [ 0, 1 ] );
  101. container.add( materialSheen );
  102. // sheen roughness
  103. const materialSheenRoughness = new SidebarMaterialNumberProperty( editor, 'sheenRoughness', strings.getKey( 'sidebar/material/sheenroughness' ), [ 0, 1 ] );
  104. container.add( materialSheenRoughness );
  105. // sheen color
  106. const materialSheenColor = new SidebarMaterialColorProperty( editor, 'sheenColor', strings.getKey( 'sidebar/material/sheencolor' ) );
  107. container.add( materialSheenColor );
  108. // transmission
  109. const materialTransmission = new SidebarMaterialNumberProperty( editor, 'transmission', strings.getKey( 'sidebar/material/transmission' ), [ 0, 1 ] );
  110. container.add( materialTransmission );
  111. // attenuation distance
  112. const materialAttenuationDistance = new SidebarMaterialNumberProperty( editor, 'attenuationDistance', strings.getKey( 'sidebar/material/attenuationDistance' ) );
  113. container.add( materialAttenuationDistance );
  114. // attenuation tint
  115. const materialAttenuationColor = new SidebarMaterialColorProperty( editor, 'attenuationColor', strings.getKey( 'sidebar/material/attenuationColor' ) );
  116. container.add( materialAttenuationColor );
  117. // thickness
  118. const materialThickness = new SidebarMaterialNumberProperty( editor, 'thickness', strings.getKey( 'sidebar/material/thickness' ) );
  119. container.add( materialThickness );
  120. // vertex colors
  121. const materialVertexColors = new SidebarMaterialBooleanProperty( editor, 'vertexColors', strings.getKey( 'sidebar/material/vertexcolors' ) );
  122. container.add( materialVertexColors );
  123. // depth packing
  124. const materialDepthPackingOptions = {
  125. [ THREE.BasicDepthPacking ]: 'Basic',
  126. [ THREE.RGBADepthPacking ]: 'RGBA'
  127. };
  128. const materialDepthPacking = new SidebarMaterialConstantProperty( editor, 'depthPacking', strings.getKey( 'sidebar/material/depthPacking' ), materialDepthPackingOptions );
  129. container.add( materialDepthPacking );
  130. // map
  131. const materialMap = new SidebarMaterialMapProperty( editor, 'map', strings.getKey( 'sidebar/material/map' ) );
  132. container.add( materialMap );
  133. // specular map
  134. const materialSpecularMap = new SidebarMaterialMapProperty( editor, 'specularMap', strings.getKey( 'sidebar/material/specularmap' ) );
  135. container.add( materialSpecularMap );
  136. // emissive map
  137. const materialEmissiveMap = new SidebarMaterialMapProperty( editor, 'emissiveMap', strings.getKey( 'sidebar/material/emissivemap' ) );
  138. container.add( materialEmissiveMap );
  139. // matcap map
  140. const materialMatcapMap = new SidebarMaterialMapProperty( editor, 'matcap', strings.getKey( 'sidebar/material/matcap' ) );
  141. container.add( materialMatcapMap );
  142. // alpha map
  143. const materialAlphaMap = new SidebarMaterialMapProperty( editor, 'alphaMap', strings.getKey( 'sidebar/material/alphamap' ) );
  144. container.add( materialAlphaMap );
  145. // bump map
  146. const materialBumpMap = new SidebarMaterialMapProperty( editor, 'bumpMap', strings.getKey( 'sidebar/material/bumpmap' ) );
  147. container.add( materialBumpMap );
  148. // normal map
  149. const materialNormalMap = new SidebarMaterialMapProperty( editor, 'normalMap', strings.getKey( 'sidebar/material/normalmap' ) );
  150. container.add( materialNormalMap );
  151. // clearcoat map
  152. const materialClearcoatMap = new SidebarMaterialMapProperty( editor, 'clearcoatMap', strings.getKey( 'sidebar/material/clearcoatmap' ) );
  153. container.add( materialClearcoatMap );
  154. // clearcoat normal map
  155. const materialClearcoatNormalMap = new SidebarMaterialMapProperty( editor, 'clearcoatNormalMap', strings.getKey( 'sidebar/material/clearcoatnormalmap' ) );
  156. container.add( materialClearcoatNormalMap );
  157. // clearcoat roughness map
  158. const materialClearcoatRoughnessMap = new SidebarMaterialMapProperty( editor, 'clearcoatRoughnessMap', strings.getKey( 'sidebar/material/clearcoatroughnessmap' ) );
  159. container.add( materialClearcoatRoughnessMap );
  160. // displacement map
  161. const materialDisplacementMap = new SidebarMaterialMapProperty( editor, 'displacementMap', strings.getKey( 'sidebar/material/displacementmap' ) );
  162. container.add( materialDisplacementMap );
  163. // roughness map
  164. const materialRoughnessMap = new SidebarMaterialMapProperty( editor, 'roughnessMap', strings.getKey( 'sidebar/material/roughnessmap' ) );
  165. container.add( materialRoughnessMap );
  166. // metalness map
  167. const materialMetalnessMap = new SidebarMaterialMapProperty( editor, 'metalnessMap', strings.getKey( 'sidebar/material/metalnessmap' ) );
  168. container.add( materialMetalnessMap );
  169. // iridescence map
  170. const materialIridescenceMap = new SidebarMaterialMapProperty( editor, 'iridescenceMap', strings.getKey( 'sidebar/material/iridescencemap' ) );
  171. container.add( materialIridescenceMap );
  172. // sheen color map
  173. const materialSheenColorMap = new SidebarMaterialMapProperty( editor, 'sheenColorMap', strings.getKey( 'sidebar/material/sheencolormap' ) );
  174. container.add( materialSheenColorMap );
  175. // sheen roughness map
  176. const materialSheenRoughnessMap = new SidebarMaterialMapProperty( editor, 'sheenRoughnessMap', strings.getKey( 'sidebar/material/sheenroughnessmap' ) );
  177. container.add( materialSheenRoughnessMap );
  178. // iridescence thickness map
  179. const materialIridescenceThicknessMap = new SidebarMaterialMapProperty( editor, 'iridescenceThicknessMap', strings.getKey( 'sidebar/material/iridescencethicknessmap' ) );
  180. container.add( materialIridescenceThicknessMap );
  181. // env map
  182. const materialEnvMap = new SidebarMaterialMapProperty( editor, 'envMap', strings.getKey( 'sidebar/material/envmap' ) );
  183. container.add( materialEnvMap );
  184. // light map
  185. const materialLightMap = new SidebarMaterialMapProperty( editor, 'lightMap', strings.getKey( 'sidebar/material/lightmap' ) );
  186. container.add( materialLightMap );
  187. // ambient occlusion map
  188. const materialAOMap = new SidebarMaterialMapProperty( editor, 'aoMap', strings.getKey( 'sidebar/material/aomap' ) );
  189. container.add( materialAOMap );
  190. // gradient map
  191. const materialGradientMap = new SidebarMaterialMapProperty( editor, 'gradientMap', strings.getKey( 'sidebar/material/gradientmap' ) );
  192. container.add( materialGradientMap );
  193. // transmission map
  194. const transmissionMap = new SidebarMaterialMapProperty( editor, 'transmissionMap', strings.getKey( 'sidebar/material/transmissionmap' ) );
  195. container.add( transmissionMap );
  196. // thickness map
  197. const thicknessMap = new SidebarMaterialMapProperty( editor, 'thicknessMap', strings.getKey( 'sidebar/material/thicknessmap' ) );
  198. container.add( thicknessMap );
  199. // side
  200. const materialSideOptions = {
  201. 0: 'Front',
  202. 1: 'Back',
  203. 2: 'Double'
  204. };
  205. const materialSide = new SidebarMaterialConstantProperty( editor, 'side', strings.getKey( 'sidebar/material/side' ), materialSideOptions );
  206. container.add( materialSide );
  207. // size
  208. const materialSize = new SidebarMaterialNumberProperty( editor, 'size', strings.getKey( 'sidebar/material/size' ), [ 0, Infinity ] );
  209. container.add( materialSize );
  210. // sizeAttenuation
  211. const materialSizeAttenuation = new SidebarMaterialBooleanProperty( editor, 'sizeAttenuation', strings.getKey( 'sidebar/material/sizeAttenuation' ) );
  212. container.add( materialSizeAttenuation );
  213. // flatShading
  214. const materialFlatShading = new SidebarMaterialBooleanProperty( editor, 'flatShading', strings.getKey( 'sidebar/material/flatShading' ) );
  215. container.add( materialFlatShading );
  216. // blending
  217. const materialBlendingOptions = {
  218. 0: 'No',
  219. 1: 'Normal',
  220. 2: 'Additive',
  221. 3: 'Subtractive',
  222. 4: 'Multiply',
  223. 5: 'Custom'
  224. };
  225. const materialBlending = new SidebarMaterialConstantProperty( editor, 'blending', strings.getKey( 'sidebar/material/blending' ), materialBlendingOptions );
  226. container.add( materialBlending );
  227. // opacity
  228. const materialOpacity = new SidebarMaterialNumberProperty( editor, 'opacity', strings.getKey( 'sidebar/material/opacity' ), [ 0, 1 ] );
  229. container.add( materialOpacity );
  230. // transparent
  231. const materialTransparent = new SidebarMaterialBooleanProperty( editor, 'transparent', strings.getKey( 'sidebar/material/transparent' ) );
  232. container.add( materialTransparent );
  233. // forceSinglePass
  234. const materialForceSinglePass = new SidebarMaterialBooleanProperty( editor, 'forceSinglePass', strings.getKey( 'sidebar/material/forcesinglepass' ) );
  235. container.add( materialForceSinglePass );
  236. // alpha test
  237. const materialAlphaTest = new SidebarMaterialNumberProperty( editor, 'alphaTest', strings.getKey( 'sidebar/material/alphatest' ), [ 0, 1 ] );
  238. container.add( materialAlphaTest );
  239. // depth test
  240. const materialDepthTest = new SidebarMaterialBooleanProperty( editor, 'depthTest', strings.getKey( 'sidebar/material/depthtest' ) );
  241. container.add( materialDepthTest );
  242. // depth write
  243. const materialDepthWrite = new SidebarMaterialBooleanProperty( editor, 'depthWrite', strings.getKey( 'sidebar/material/depthwrite' ) );
  244. container.add( materialDepthWrite );
  245. // wireframe
  246. const materialWireframe = new SidebarMaterialBooleanProperty( editor, 'wireframe', strings.getKey( 'sidebar/material/wireframe' ) );
  247. container.add( materialWireframe );
  248. // userData
  249. const materialUserDataRow = new UIRow();
  250. const materialUserData = new UITextArea().setWidth( '150px' ).setHeight( '40px' ).setFontSize( '12px' ).onChange( update );
  251. materialUserData.onKeyUp( function () {
  252. try {
  253. JSON.parse( materialUserData.getValue() );
  254. materialUserData.dom.classList.add( 'success' );
  255. materialUserData.dom.classList.remove( 'fail' );
  256. } catch ( error ) {
  257. materialUserData.dom.classList.remove( 'success' );
  258. materialUserData.dom.classList.add( 'fail' );
  259. }
  260. } );
  261. materialUserDataRow.add( new UIText( strings.getKey( 'sidebar/material/userdata' ) ).setClass( 'Label' ) );
  262. materialUserDataRow.add( materialUserData );
  263. container.add( materialUserDataRow );
  264. // Export JSON
  265. const exportJson = new UIButton( strings.getKey( 'sidebar/material/export' ) );
  266. exportJson.setMarginLeft( '120px' );
  267. exportJson.onClick( function () {
  268. const object = editor.selected;
  269. const material = Array.isArray( object.material ) ? object.material[ currentMaterialSlot ] : object.material;
  270. let output = material.toJSON();
  271. try {
  272. output = JSON.stringify( output, null, '\t' );
  273. output = output.replace( /[\n\t]+([\d\.e\-\[\]]+)/g, '$1' );
  274. } catch ( e ) {
  275. output = JSON.stringify( output );
  276. }
  277. editor.utils.save( new Blob( [ output ] ), `${ materialName.getValue() || 'material' }.json` );
  278. } );
  279. container.add( exportJson );
  280. //
  281. function update() {
  282. const previousSelectedSlot = currentMaterialSlot;
  283. currentMaterialSlot = parseInt( materialSlotSelect.getValue() );
  284. if ( currentMaterialSlot !== previousSelectedSlot ) {
  285. editor.signals.materialChanged.dispatch( currentObject, currentMaterialSlot );
  286. }
  287. let material = editor.getObjectMaterial( currentObject, currentMaterialSlot );
  288. if ( material ) {
  289. if ( material.uuid !== undefined && material.uuid !== materialUUID.getValue() ) {
  290. editor.execute( new SetMaterialValueCommand( editor, currentObject, 'uuid', materialUUID.getValue(), currentMaterialSlot ) );
  291. }
  292. if ( material.type !== materialClass.getValue() ) {
  293. material = new materialClasses[ materialClass.getValue() ]();
  294. if ( material.type === 'RawShaderMaterial' ) {
  295. material.vertexShader = vertexShaderVariables + material.vertexShader;
  296. }
  297. const currentMaterial = currentObject.material;
  298. if ( material.type === 'MeshPhysicalMaterial' && currentMaterial.type === 'MeshStandardMaterial' ) {
  299. // TODO Find a easier to maintain approach
  300. const properties = [
  301. 'color', 'emissive', 'roughness', 'metalness', 'map', 'emissiveMap', 'alphaMap',
  302. 'bumpMap', 'normalMap', 'normalScale', 'displacementMap', 'roughnessMap', 'metalnessMap',
  303. 'envMap', 'lightMap', 'aoMap', 'side'
  304. ];
  305. for ( const property of properties ) {
  306. const value = currentMaterial[ property ];
  307. if ( value === null ) continue;
  308. if ( value[ 'clone' ] !== undefined ) {
  309. material[ property ] = value.clone();
  310. } else {
  311. material[ property ] = value;
  312. }
  313. }
  314. }
  315. if ( Array.isArray( currentMaterial ) ) {
  316. // don't remove the entire multi-material. just the material of the selected slot
  317. editor.removeMaterial( currentMaterial[ currentMaterialSlot ] );
  318. } else {
  319. editor.removeMaterial( currentMaterial );
  320. }
  321. editor.execute( new SetMaterialCommand( editor, currentObject, material, currentMaterialSlot ), strings.getKey( 'command/SetMaterial' ) + ': ' + materialClass.getValue() );
  322. editor.addMaterial( material );
  323. // TODO Copy other references in the scene graph
  324. // keeping name and UUID then.
  325. // Also there should be means to create a unique
  326. // copy for the current object explicitly and to
  327. // attach the current material to other objects.
  328. }
  329. try {
  330. const userData = JSON.parse( materialUserData.getValue() );
  331. if ( JSON.stringify( material.userData ) != JSON.stringify( userData ) ) {
  332. editor.execute( new SetMaterialValueCommand( editor, currentObject, 'userData', userData, currentMaterialSlot ) );
  333. }
  334. } catch ( exception ) {
  335. console.warn( exception );
  336. }
  337. refreshUI();
  338. }
  339. }
  340. //
  341. function setRowVisibility() {
  342. const material = currentObject.material;
  343. if ( Array.isArray( material ) ) {
  344. materialSlotRow.setDisplay( '' );
  345. } else {
  346. materialSlotRow.setDisplay( 'none' );
  347. }
  348. }
  349. function refreshUI() {
  350. if ( ! currentObject ) return;
  351. let material = currentObject.material;
  352. if ( Array.isArray( material ) ) {
  353. const slotOptions = {};
  354. currentMaterialSlot = Math.max( 0, Math.min( material.length, currentMaterialSlot ) );
  355. for ( let i = 0; i < material.length; i ++ ) {
  356. slotOptions[ i ] = String( i + 1 ) + ': ' + material[ i ].name;
  357. }
  358. materialSlotSelect.setOptions( slotOptions ).setValue( currentMaterialSlot );
  359. }
  360. material = editor.getObjectMaterial( currentObject, currentMaterialSlot );
  361. if ( material.uuid !== undefined ) {
  362. materialUUID.setValue( material.uuid );
  363. }
  364. if ( material.name !== undefined ) {
  365. materialName.setValue( material.name );
  366. }
  367. if ( currentObject.isMesh ) {
  368. materialClass.setOptions( meshMaterialOptions );
  369. } else if ( currentObject.isSprite ) {
  370. materialClass.setOptions( spriteMaterialOptions );
  371. } else if ( currentObject.isPoints ) {
  372. materialClass.setOptions( pointsMaterialOptions );
  373. } else if ( currentObject.isLine ) {
  374. materialClass.setOptions( lineMaterialOptions );
  375. }
  376. materialClass.setValue( material.type );
  377. setRowVisibility();
  378. try {
  379. materialUserData.setValue( JSON.stringify( material.userData, null, ' ' ) );
  380. } catch ( error ) {
  381. console.log( error );
  382. }
  383. materialUserData.setBorderColor( 'transparent' );
  384. materialUserData.setBackgroundColor( '' );
  385. }
  386. // events
  387. signals.objectSelected.add( function ( object ) {
  388. let hasMaterial = false;
  389. if ( object && object.material ) {
  390. hasMaterial = true;
  391. if ( Array.isArray( object.material ) && object.material.length === 0 ) {
  392. hasMaterial = false;
  393. }
  394. }
  395. if ( hasMaterial ) {
  396. currentObject = object;
  397. refreshUI();
  398. container.setDisplay( '' );
  399. } else {
  400. currentObject = null;
  401. container.setDisplay( 'none' );
  402. }
  403. } );
  404. signals.materialChanged.add( refreshUI );
  405. return container;
  406. }
  407. const materialClasses = {
  408. 'LineBasicMaterial': THREE.LineBasicMaterial,
  409. 'LineDashedMaterial': THREE.LineDashedMaterial,
  410. 'MeshBasicMaterial': THREE.MeshBasicMaterial,
  411. 'MeshDepthMaterial': THREE.MeshDepthMaterial,
  412. 'MeshNormalMaterial': THREE.MeshNormalMaterial,
  413. 'MeshLambertMaterial': THREE.MeshLambertMaterial,
  414. 'MeshMatcapMaterial': THREE.MeshMatcapMaterial,
  415. 'MeshPhongMaterial': THREE.MeshPhongMaterial,
  416. 'MeshToonMaterial': THREE.MeshToonMaterial,
  417. 'MeshStandardMaterial': THREE.MeshStandardMaterial,
  418. 'MeshPhysicalMaterial': THREE.MeshPhysicalMaterial,
  419. 'RawShaderMaterial': THREE.RawShaderMaterial,
  420. 'ShaderMaterial': THREE.ShaderMaterial,
  421. 'ShadowMaterial': THREE.ShadowMaterial,
  422. 'SpriteMaterial': THREE.SpriteMaterial,
  423. 'PointsMaterial': THREE.PointsMaterial
  424. };
  425. const vertexShaderVariables = [
  426. 'uniform mat4 projectionMatrix;',
  427. 'uniform mat4 modelViewMatrix;\n',
  428. 'attribute vec3 position;\n\n',
  429. ].join( '\n' );
  430. const meshMaterialOptions = {
  431. 'MeshBasicMaterial': 'MeshBasicMaterial',
  432. 'MeshDepthMaterial': 'MeshDepthMaterial',
  433. 'MeshNormalMaterial': 'MeshNormalMaterial',
  434. 'MeshLambertMaterial': 'MeshLambertMaterial',
  435. 'MeshMatcapMaterial': 'MeshMatcapMaterial',
  436. 'MeshPhongMaterial': 'MeshPhongMaterial',
  437. 'MeshToonMaterial': 'MeshToonMaterial',
  438. 'MeshStandardMaterial': 'MeshStandardMaterial',
  439. 'MeshPhysicalMaterial': 'MeshPhysicalMaterial',
  440. 'RawShaderMaterial': 'RawShaderMaterial',
  441. 'ShaderMaterial': 'ShaderMaterial',
  442. 'ShadowMaterial': 'ShadowMaterial'
  443. };
  444. const lineMaterialOptions = {
  445. 'LineBasicMaterial': 'LineBasicMaterial',
  446. 'LineDashedMaterial': 'LineDashedMaterial',
  447. 'RawShaderMaterial': 'RawShaderMaterial',
  448. 'ShaderMaterial': 'ShaderMaterial'
  449. };
  450. const spriteMaterialOptions = {
  451. 'SpriteMaterial': 'SpriteMaterial',
  452. 'RawShaderMaterial': 'RawShaderMaterial',
  453. 'ShaderMaterial': 'ShaderMaterial'
  454. };
  455. const pointsMaterialOptions = {
  456. 'PointsMaterial': 'PointsMaterial',
  457. 'RawShaderMaterial': 'RawShaderMaterial',
  458. 'ShaderMaterial': 'ShaderMaterial'
  459. };
  460. export { SidebarMaterial };