Sidebar.Material.js 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240
  1. /**
  2. * @author mrdoob / http://mrdoob.com/
  3. */
  4. Sidebar.Material = function ( editor ) {
  5. var signals = editor.signals;
  6. var currentObject;
  7. var container = new UI.CollapsiblePanel();
  8. container.setCollapsed( editor.config.getKey( 'ui/sidebar/material/collapsed' ) );
  9. container.onCollapsedChange( function ( boolean ) {
  10. editor.config.setKey( 'ui/sidebar/material/collapsed', boolean );
  11. } );
  12. container.setDisplay( 'none' );
  13. container.dom.classList.add( 'Material' );
  14. container.addStatic( new UI.Text().setValue( 'MATERIAL' ) );
  15. container.add( new UI.Break() );
  16. // uuid
  17. var materialUUIDRow = new UI.Panel();
  18. var materialUUID = new UI.Input().setWidth( '115px' ).setFontSize( '12px' ).setDisabled( true );
  19. var materialUUIDRenew = new UI.Button( '⟳' ).setMarginLeft( '7px' ).onClick( function () {
  20. materialUUID.setValue( THREE.Math.generateUUID() );
  21. update();
  22. } );
  23. materialUUIDRow.add( new UI.Text( 'UUID' ).setWidth( '90px' ) );
  24. materialUUIDRow.add( materialUUID );
  25. materialUUIDRow.add( materialUUIDRenew );
  26. container.add( materialUUIDRow );
  27. // name
  28. var materialNameRow = new UI.Panel();
  29. var materialName = new UI.Input().setWidth( '150px' ).setFontSize( '12px' ).onChange( function () {
  30. editor.execute( new SetMaterialValueCommand( editor.selected, 'name', materialName.getValue() ) );
  31. } );
  32. materialNameRow.add( new UI.Text( 'Name' ).setWidth( '90px' ) );
  33. materialNameRow.add( materialName );
  34. container.add( materialNameRow );
  35. // class
  36. var materialClassRow = new UI.Panel();
  37. var materialClass = new UI.Select().setOptions( {
  38. 'LineBasicMaterial': 'LineBasicMaterial',
  39. 'LineDashedMaterial': 'LineDashedMaterial',
  40. 'MeshBasicMaterial': 'MeshBasicMaterial',
  41. 'MeshDepthMaterial': 'MeshDepthMaterial',
  42. 'MeshNormalMaterial': 'MeshNormalMaterial',
  43. 'MeshLambertMaterial': 'MeshLambertMaterial',
  44. 'MeshPhongMaterial': 'MeshPhongMaterial',
  45. 'MeshStandardMaterial': 'MeshStandardMaterial',
  46. 'ShaderMaterial': 'ShaderMaterial',
  47. 'SpriteMaterial': 'SpriteMaterial'
  48. } ).setWidth( '150px' ).setFontSize( '12px' ).onChange( update );
  49. materialClassRow.add( new UI.Text( 'Type' ).setWidth( '90px' ) );
  50. materialClassRow.add( materialClass );
  51. container.add( materialClassRow );
  52. // program
  53. var materialProgramRow = new UI.Panel();
  54. materialProgramRow.add( new UI.Text( 'Program' ).setWidth( '90px' ) );
  55. var materialProgramInfo = new UI.Button( 'Info' );
  56. materialProgramInfo.setMarginLeft( '4px' );
  57. materialProgramInfo.onClick( function () {
  58. signals.editScript.dispatch( currentObject, 'programInfo' );
  59. } );
  60. materialProgramRow.add( materialProgramInfo );
  61. var materialProgramVertex = new UI.Button( 'Vertex' );
  62. materialProgramVertex.setMarginLeft( '4px' );
  63. materialProgramVertex.onClick( function () {
  64. signals.editScript.dispatch( currentObject, 'vertexShader' );
  65. } );
  66. materialProgramRow.add( materialProgramVertex );
  67. var materialProgramFragment = new UI.Button( 'Fragment' );
  68. materialProgramFragment.setMarginLeft( '4px' );
  69. materialProgramFragment.onClick( function () {
  70. signals.editScript.dispatch( currentObject, 'fragmentShader' );
  71. } );
  72. materialProgramRow.add( materialProgramFragment );
  73. container.add( materialProgramRow );
  74. // color
  75. var materialColorRow = new UI.Panel();
  76. var materialColor = new UI.Color().onChange( update );
  77. materialColorRow.add( new UI.Text( 'Color' ).setWidth( '90px' ) );
  78. materialColorRow.add( materialColor );
  79. container.add( materialColorRow );
  80. // roughness
  81. var materialRoughnessRow = new UI.Panel();
  82. var materialRoughness = new UI.Number( 0.5 ).setWidth( '60px' ).setRange( 0, 1 ).onChange( update );
  83. materialRoughnessRow.add( new UI.Text( 'Roughness' ).setWidth( '90px' ) );
  84. materialRoughnessRow.add( materialRoughness );
  85. container.add( materialRoughnessRow );
  86. // metalness
  87. var materialMetalnessRow = new UI.Panel();
  88. var materialMetalness = new UI.Number( 0.5 ).setWidth( '60px' ).setRange( 0, 1 ).onChange( update );
  89. materialMetalnessRow.add( new UI.Text( 'Metalness' ).setWidth( '90px' ) );
  90. materialMetalnessRow.add( materialMetalness );
  91. container.add( materialMetalnessRow );
  92. // emissive
  93. var materialEmissiveRow = new UI.Panel();
  94. var materialEmissive = new UI.Color().setHexValue( 0x000000 ).onChange( update );
  95. materialEmissiveRow.add( new UI.Text( 'Emissive' ).setWidth( '90px' ) );
  96. materialEmissiveRow.add( materialEmissive );
  97. container.add( materialEmissiveRow );
  98. // specular
  99. var materialSpecularRow = new UI.Panel();
  100. var materialSpecular = new UI.Color().setHexValue( 0x111111 ).onChange( update );
  101. materialSpecularRow.add( new UI.Text( 'Specular' ).setWidth( '90px' ) );
  102. materialSpecularRow.add( materialSpecular );
  103. container.add( materialSpecularRow );
  104. // shininess
  105. var materialShininessRow = new UI.Panel();
  106. var materialShininess = new UI.Number( 30 ).onChange( update );
  107. materialShininessRow.add( new UI.Text( 'Shininess' ).setWidth( '90px' ) );
  108. materialShininessRow.add( materialShininess );
  109. container.add( materialShininessRow );
  110. // vertex colors
  111. var materialVertexColorsRow = new UI.Panel();
  112. var materialVertexColors = new UI.Select().setOptions( {
  113. 0: 'No',
  114. 1: 'Face',
  115. 2: 'Vertex'
  116. } ).onChange( update );
  117. materialVertexColorsRow.add( new UI.Text( 'Vertex Colors' ).setWidth( '90px' ) );
  118. materialVertexColorsRow.add( materialVertexColors );
  119. container.add( materialVertexColorsRow );
  120. // skinning
  121. var materialSkinningRow = new UI.Panel();
  122. var materialSkinning = new UI.Checkbox( false ).onChange( update );
  123. materialSkinningRow.add( new UI.Text( 'Skinning' ).setWidth( '90px' ) );
  124. materialSkinningRow.add( materialSkinning );
  125. container.add( materialSkinningRow );
  126. // map
  127. var materialMapRow = new UI.Panel();
  128. var materialMapEnabled = new UI.Checkbox( false ).onChange( update );
  129. var materialMap = new UI.Texture().onChange( update );
  130. materialMapRow.add( new UI.Text( 'Map' ).setWidth( '90px' ) );
  131. materialMapRow.add( materialMapEnabled );
  132. materialMapRow.add( materialMap );
  133. container.add( materialMapRow );
  134. // alpha map
  135. var materialAlphaMapRow = new UI.Panel();
  136. var materialAlphaMapEnabled = new UI.Checkbox( false ).onChange( update );
  137. var materialAlphaMap = new UI.Texture().onChange( update );
  138. materialAlphaMapRow.add( new UI.Text( 'Alpha Map' ).setWidth( '90px' ) );
  139. materialAlphaMapRow.add( materialAlphaMapEnabled );
  140. materialAlphaMapRow.add( materialAlphaMap );
  141. container.add( materialAlphaMapRow );
  142. // bump map
  143. var materialBumpMapRow = new UI.Panel();
  144. var materialBumpMapEnabled = new UI.Checkbox( false ).onChange( update );
  145. var materialBumpMap = new UI.Texture().onChange( update );
  146. var materialBumpScale = new UI.Number( 1 ).setWidth( '30px' ).onChange( update );
  147. materialBumpMapRow.add( new UI.Text( 'Bump Map' ).setWidth( '90px' ) );
  148. materialBumpMapRow.add( materialBumpMapEnabled );
  149. materialBumpMapRow.add( materialBumpMap );
  150. materialBumpMapRow.add( materialBumpScale );
  151. container.add( materialBumpMapRow );
  152. // normal map
  153. var materialNormalMapRow = new UI.Panel();
  154. var materialNormalMapEnabled = new UI.Checkbox( false ).onChange( update );
  155. var materialNormalMap = new UI.Texture().onChange( update );
  156. materialNormalMapRow.add( new UI.Text( 'Normal Map' ).setWidth( '90px' ) );
  157. materialNormalMapRow.add( materialNormalMapEnabled );
  158. materialNormalMapRow.add( materialNormalMap );
  159. container.add( materialNormalMapRow );
  160. // displacement map
  161. var materialDisplacementMapRow = new UI.Panel();
  162. var materialDisplacementMapEnabled = new UI.Checkbox( false ).onChange( update );
  163. var materialDisplacementMap = new UI.Texture().onChange( update );
  164. var materialDisplacementScale = new UI.Number( 1 ).setWidth( '30px' ).onChange( update );
  165. materialDisplacementMapRow.add( new UI.Text( 'Displace Map' ).setWidth( '90px' ) );
  166. materialDisplacementMapRow.add( materialDisplacementMapEnabled );
  167. materialDisplacementMapRow.add( materialDisplacementMap );
  168. materialDisplacementMapRow.add( materialDisplacementScale );
  169. container.add( materialDisplacementMapRow );
  170. // roughness map
  171. var materialRoughnessMapRow = new UI.Panel();
  172. var materialRoughnessMapEnabled = new UI.Checkbox( false ).onChange( update );
  173. var materialRoughnessMap = new UI.Texture().onChange( update );
  174. materialRoughnessMapRow.add( new UI.Text( 'Rough. Map' ).setWidth( '90px' ) );
  175. materialRoughnessMapRow.add( materialRoughnessMapEnabled );
  176. materialRoughnessMapRow.add( materialRoughnessMap );
  177. container.add( materialRoughnessMapRow );
  178. // metalness map
  179. var materialMetalnessMapRow = new UI.Panel();
  180. var materialMetalnessMapEnabled = new UI.Checkbox( false ).onChange( update );
  181. var materialMetalnessMap = new UI.Texture().onChange( update );
  182. materialMetalnessMapRow.add( new UI.Text( 'Metal. Map' ).setWidth( '90px' ) );
  183. materialMetalnessMapRow.add( materialMetalnessMapEnabled );
  184. materialMetalnessMapRow.add( materialMetalnessMap );
  185. container.add( materialMetalnessMapRow );
  186. // specular map
  187. var materialSpecularMapRow = new UI.Panel();
  188. var materialSpecularMapEnabled = new UI.Checkbox( false ).onChange( update );
  189. var materialSpecularMap = new UI.Texture().onChange( update );
  190. materialSpecularMapRow.add( new UI.Text( 'Specular Map' ).setWidth( '90px' ) );
  191. materialSpecularMapRow.add( materialSpecularMapEnabled );
  192. materialSpecularMapRow.add( materialSpecularMap );
  193. container.add( materialSpecularMapRow );
  194. // env map
  195. var materialEnvMapRow = new UI.Panel();
  196. var materialEnvMapEnabled = new UI.Checkbox( false ).onChange( update );
  197. var materialEnvMap = new UI.Texture( THREE.SphericalReflectionMapping ).onChange( update );
  198. var materialReflectivity = new UI.Number( 1 ).setWidth( '30px' ).onChange( update );
  199. materialEnvMapRow.add( new UI.Text( 'Env Map' ).setWidth( '90px' ) );
  200. materialEnvMapRow.add( materialEnvMapEnabled );
  201. materialEnvMapRow.add( materialEnvMap );
  202. materialEnvMapRow.add( materialReflectivity );
  203. container.add( materialEnvMapRow );
  204. // light map
  205. var materialLightMapRow = new UI.Panel();
  206. var materialLightMapEnabled = new UI.Checkbox( false ).onChange( update );
  207. var materialLightMap = new UI.Texture().onChange( update );
  208. materialLightMapRow.add( new UI.Text( 'Light Map' ).setWidth( '90px' ) );
  209. materialLightMapRow.add( materialLightMapEnabled );
  210. materialLightMapRow.add( materialLightMap );
  211. container.add( materialLightMapRow );
  212. // ambient occlusion map
  213. var materialAOMapRow = new UI.Panel();
  214. var materialAOMapEnabled = new UI.Checkbox( false ).onChange( update );
  215. var materialAOMap = new UI.Texture().onChange( update );
  216. var materialAOScale = new UI.Number( 1 ).setRange( 0, 1 ).setWidth( '30px' ).onChange( update );
  217. materialAOMapRow.add( new UI.Text( 'AO Map' ).setWidth( '90px' ) );
  218. materialAOMapRow.add( materialAOMapEnabled );
  219. materialAOMapRow.add( materialAOMap );
  220. materialAOMapRow.add( materialAOScale );
  221. container.add( materialAOMapRow );
  222. // emissive map
  223. var materialEmissiveMapRow = new UI.Panel();
  224. var materialEmissiveMapEnabled = new UI.Checkbox( false ).onChange( update );
  225. var materialEmissiveMap = new UI.Texture().onChange( update );
  226. materialEmissiveMapRow.add( new UI.Text( 'Emissive Map' ).setWidth( '90px' ) );
  227. materialEmissiveMapRow.add( materialEmissiveMapEnabled );
  228. materialEmissiveMapRow.add( materialEmissiveMap );
  229. container.add( materialEmissiveMapRow );
  230. // side
  231. var materialSideRow = new UI.Panel();
  232. var materialSide = new UI.Select().setOptions( {
  233. 0: 'Front',
  234. 1: 'Back',
  235. 2: 'Double'
  236. } ).setWidth( '150px' ).setFontSize( '12px' ).onChange( update );
  237. materialSideRow.add( new UI.Text( 'Side' ).setWidth( '90px' ) );
  238. materialSideRow.add( materialSide );
  239. container.add( materialSideRow );
  240. // shading
  241. var materialShadingRow = new UI.Panel();
  242. var materialShading = new UI.Select().setOptions( {
  243. 0: 'No',
  244. 1: 'Flat',
  245. 2: 'Smooth'
  246. } ).setWidth( '150px' ).setFontSize( '12px' ).onChange( update );
  247. materialShadingRow.add( new UI.Text( 'Shading' ).setWidth( '90px' ) );
  248. materialShadingRow.add( materialShading );
  249. container.add( materialShadingRow );
  250. // blending
  251. var materialBlendingRow = new UI.Panel();
  252. var materialBlending = new UI.Select().setOptions( {
  253. 0: 'No',
  254. 1: 'Normal',
  255. 2: 'Additive',
  256. 3: 'Subtractive',
  257. 4: 'Multiply',
  258. 5: 'Custom'
  259. } ).setWidth( '150px' ).setFontSize( '12px' ).onChange( update );
  260. materialBlendingRow.add( new UI.Text( 'Blending' ).setWidth( '90px' ) );
  261. materialBlendingRow.add( materialBlending );
  262. container.add( materialBlendingRow );
  263. // opacity
  264. var materialOpacityRow = new UI.Panel();
  265. var materialOpacity = new UI.Number( 1 ).setWidth( '60px' ).setRange( 0, 1 ).onChange( update );
  266. materialOpacityRow.add( new UI.Text( 'Opacity' ).setWidth( '90px' ) );
  267. materialOpacityRow.add( materialOpacity );
  268. container.add( materialOpacityRow );
  269. // transparent
  270. var materialTransparentRow = new UI.Panel();
  271. var materialTransparent = new UI.Checkbox().setLeft( '100px' ).onChange( update );
  272. materialTransparentRow.add( new UI.Text( 'Transparent' ).setWidth( '90px' ) );
  273. materialTransparentRow.add( materialTransparent );
  274. container.add( materialTransparentRow );
  275. // alpha test
  276. var materialAlphaTestRow = new UI.Panel();
  277. var materialAlphaTest = new UI.Number().setWidth( '60px' ).setRange( 0, 1 ).onChange( update );
  278. materialAlphaTestRow.add( new UI.Text( 'Alpha Test' ).setWidth( '90px' ) );
  279. materialAlphaTestRow.add( materialAlphaTest );
  280. container.add( materialAlphaTestRow );
  281. // wireframe
  282. var materialWireframeRow = new UI.Panel();
  283. var materialWireframe = new UI.Checkbox( false ).onChange( update );
  284. var materialWireframeLinewidth = new UI.Number( 1 ).setWidth( '60px' ).setRange( 0, 100 ).onChange( update );
  285. materialWireframeRow.add( new UI.Text( 'Wireframe' ).setWidth( '90px' ) );
  286. materialWireframeRow.add( materialWireframe );
  287. materialWireframeRow.add( materialWireframeLinewidth );
  288. container.add( materialWireframeRow );
  289. //
  290. function update() {
  291. var object = currentObject;
  292. var geometry = object.geometry;
  293. var material = object.material;
  294. var textureWarning = false;
  295. var objectHasUvs = false;
  296. if ( object instanceof THREE.Sprite ) objectHasUvs = true;
  297. if ( geometry instanceof THREE.Geometry && geometry.faceVertexUvs[ 0 ].length > 0 ) objectHasUvs = true;
  298. if ( geometry instanceof THREE.BufferGeometry && geometry.attributes.uv !== undefined ) objectHasUvs = true;
  299. if ( material ) {
  300. if ( material.uuid !== undefined && material.uuid !== materialUUID.getValue() ) {
  301. editor.execute( new SetMaterialValueCommand( currentObject, 'uuid', materialUUID.getValue() ) );
  302. }
  303. if ( material instanceof THREE[ materialClass.getValue() ] === false ) {
  304. material = new THREE[ materialClass.getValue() ]();
  305. editor.execute( new SetMaterialCommand( currentObject, material ), 'New Material: ' + materialClass.getValue() );
  306. // TODO Copy other references in the scene graph
  307. // keeping name and UUID then.
  308. // Also there should be means to create a unique
  309. // copy for the current object explicitly and to
  310. // attach the current material to other objects.
  311. }
  312. if ( material.color !== undefined && material.color.getHex() !== materialColor.getHexValue() ) {
  313. editor.execute( new SetMaterialColorCommand( currentObject, 'color', materialColor.getHexValue() ) );
  314. }
  315. if ( material.roughness !== undefined && Math.abs( material.roughness - materialRoughness.getValue() ) >= 0.01 ) {
  316. editor.execute( new SetMaterialValueCommand( currentObject, 'roughness', materialRoughness.getValue() ) );
  317. }
  318. if ( material.metalness !== undefined && Math.abs( material.metalness - materialMetalness.getValue() ) >= 0.01 ) {
  319. editor.execute( new SetMaterialValueCommand( currentObject, 'metalness', materialMetalness.getValue() ) );
  320. }
  321. if ( material.emissive !== undefined && material.emissive.getHex() !== materialEmissive.getHexValue() ) {
  322. editor.execute( new SetMaterialColorCommand( currentObject, 'emissive', materialEmissive.getHexValue() ) );
  323. }
  324. if ( material.specular !== undefined && material.specular.getHex() !== materialSpecular.getHexValue() ) {
  325. editor.execute( new SetMaterialColorCommand( currentObject, 'specular', materialSpecular.getHexValue() ) );
  326. }
  327. if ( material.shininess !== undefined && Math.abs( material.shininess - materialShininess.getValue() ) >= 0.01 ) {
  328. editor.execute( new SetMaterialValueCommand( currentObject, 'shininess', materialShininess.getValue() ) );
  329. }
  330. if ( material.vertexColors !== undefined ) {
  331. var vertexColors = parseInt( materialVertexColors.getValue() );
  332. if ( material.vertexColors !== vertexColors ) {
  333. editor.execute( new SetMaterialValueCommand( currentObject, 'vertexColors', vertexColors ) );
  334. }
  335. }
  336. if ( material.skinning !== undefined && material.skinning !== materialSkinning.getValue() ) {
  337. editor.execute( new SetMaterialValueCommand( currentObject, 'skinning', materialSkinning.getValue() ) );
  338. }
  339. if ( material.map !== undefined ) {
  340. var mapEnabled = materialMapEnabled.getValue() === true;
  341. if ( objectHasUvs ) {
  342. var map = mapEnabled ? materialMap.getValue() : null;
  343. if ( material.map !== map ) {
  344. editor.execute( new SetMaterialMapCommand( currentObject, 'map', map ) );
  345. }
  346. } else {
  347. if ( mapEnabled ) textureWarning = true;
  348. }
  349. }
  350. if ( material.alphaMap !== undefined ) {
  351. var mapEnabled = materialAlphaMapEnabled.getValue() === true;
  352. if ( objectHasUvs ) {
  353. var alphaMap = mapEnabled ? materialAlphaMap.getValue() : null;
  354. if ( material.alphaMap !== alphaMap ) {
  355. editor.execute( new SetMaterialMapCommand( currentObject, 'alphaMap', alphaMap ) );
  356. }
  357. } else {
  358. if ( mapEnabled ) textureWarning = true;
  359. }
  360. }
  361. if ( material.bumpMap !== undefined ) {
  362. var bumpMapEnabled = materialBumpMapEnabled.getValue() === true;
  363. if ( objectHasUvs ) {
  364. var bumpMap = bumpMapEnabled ? materialBumpMap.getValue() : null;
  365. if ( material.bumpMap !== bumpMap ) {
  366. editor.execute( new SetMaterialMapCommand( currentObject, 'bumpMap', bumpMap ) );
  367. }
  368. if ( material.bumpScale !== materialBumpScale.getValue() ) {
  369. editor.execute( new SetMaterialValueCommand( currentObject, 'bumpScale', materialBumpScale.getValue() ) );
  370. }
  371. } else {
  372. if ( bumpMapEnabled ) textureWarning = true;
  373. }
  374. }
  375. if ( material.normalMap !== undefined ) {
  376. var normalMapEnabled = materialNormalMapEnabled.getValue() === true;
  377. if ( objectHasUvs ) {
  378. var normalMap = normalMapEnabled ? materialNormalMap.getValue() : null;
  379. if ( material.normalMap !== normalMap ) {
  380. editor.execute( new SetMaterialMapCommand( currentObject, 'normalMap', normalMap ) );
  381. }
  382. } else {
  383. if ( normalMapEnabled ) textureWarning = true;
  384. }
  385. }
  386. if ( material.displacementMap !== undefined ) {
  387. var displacementMapEnabled = materialDisplacementMapEnabled.getValue() === true;
  388. if ( objectHasUvs ) {
  389. var displacementMap = displacementMapEnabled ? materialDisplacementMap.getValue() : null;
  390. if ( material.displacementMap !== displacementMap ) {
  391. editor.execute( new SetMaterialMapCommand( currentObject, 'displacementMap', displacementMap ) );
  392. }
  393. if ( material.displacementScale !== materialDisplacementScale.getValue() ) {
  394. editor.execute( new SetMaterialValueCommand( currentObject, 'displacementScale', materialDisplacementScale.getValue() ) );
  395. }
  396. } else {
  397. if ( displacementMapEnabled ) textureWarning = true;
  398. }
  399. }
  400. if ( material.roughnessMap !== undefined ) {
  401. var roughnessMapEnabled = materialRoughnessMapEnabled.getValue() === true;
  402. if ( objectHasUvs ) {
  403. var roughnessMap = roughnessMapEnabled ? materialRoughnessMap.getValue() : null;
  404. if ( material.roughnessMap !== roughnessMap ) {
  405. editor.execute( new SetMaterialMapCommand( currentObject, 'roughnessMap', roughnessMap ) );
  406. }
  407. if ( material.displacementScale !== materialDisplacementScale.getValue() ) {
  408. editor.execute( new SetMaterialValueCommand( currentObject, 'displacementScale', materialDisplacementScale.getValue() ) );
  409. }
  410. } else {
  411. if ( roughnessMapEnabled ) textureWarning = true;
  412. }
  413. }
  414. if ( material.metalnessMap !== undefined ) {
  415. var metalnessMapEnabled = materialMetalnessMapEnabled.getValue() === true;
  416. if ( objectHasUvs ) {
  417. var metalnessMap = metalnessMapEnabled ? materialMetalnessMap.getValue() : null;
  418. if ( material.metalnessMap !== metalnessMap ) {
  419. editor.execute( new SetMaterialMapCommand( currentObject, 'metalnessMap', metalnessMap ) );
  420. }
  421. if ( material.displacementScale !== materialDisplacementScale.getValue() ) {
  422. editor.execute( new SetMaterialValueCommand( currentObject, 'displacementScale', materialDisplacementScale.getValue() ) );
  423. }
  424. } else {
  425. if ( metalnessMapEnabled ) textureWarning = true;
  426. }
  427. }
  428. if ( material.specularMap !== undefined ) {
  429. var specularMapEnabled = materialSpecularMapEnabled.getValue() === true;
  430. if ( objectHasUvs ) {
  431. var specularMap = specularMapEnabled ? materialSpecularMap.getValue() : null;
  432. if ( material.specularMap !== specularMap ) {
  433. editor.execute( new SetMaterialMapCommand( currentObject, 'specularMap', specularMap ) );
  434. }
  435. } else {
  436. if ( specularMapEnabled ) textureWarning = true;
  437. }
  438. }
  439. if ( material.envMap !== undefined ) {
  440. var envMapEnabled = materialEnvMapEnabled.getValue() === true;
  441. var envMap = envMapEnabled ? materialEnvMap.getValue() : null;
  442. if ( material.envMap !== envMap ) {
  443. editor.execute( new SetMaterialMapCommand( currentObject, 'envMap', envMap ) );
  444. }
  445. if ( material.reflectivity !== materialReflectivity.getValue() ) {
  446. editor.execute( new SetMaterialValueCommand( currentObject, 'reflectivity', materialReflectivity.getValue() ) );
  447. }
  448. }
  449. if ( material.lightMap !== undefined ) {
  450. var lightMapEnabled = materialLightMapEnabled.getValue() === true;
  451. if ( objectHasUvs ) {
  452. var lightMap = lightMapEnabled ? materialLightMap.getValue() : null;
  453. if ( material.lightMap !== lightMap ) {
  454. editor.execute( new SetMaterialMapCommand( currentObject, 'lightMap', lightMap ) );
  455. }
  456. } else {
  457. if ( lightMapEnabled ) textureWarning = true;
  458. }
  459. }
  460. if ( material.aoMap !== undefined ) {
  461. var aoMapEnabled = materialAOMapEnabled.getValue() === true;
  462. if ( objectHasUvs ) {
  463. var aoMap = aoMapEnabled ? materialAOMap.getValue() : null;
  464. if ( material.aoMap !== aoMap ) {
  465. editor.execute( new SetMaterialMapCommand( currentObject, 'aoMap', aoMap ) );
  466. }
  467. if ( material.aoMapIntensity !== materialAOScale.getValue() ) {
  468. editor.execute( new SetMaterialValueCommand( currentObject, 'aoMapIntensity', materialAOScale.getValue() ) );
  469. }
  470. } else {
  471. if ( aoMapEnabled ) textureWarning = true;
  472. }
  473. }
  474. if ( material.emissiveMap !== undefined ) {
  475. var emissiveMapEnabled = materialEmissiveMapEnabled.getValue() === true;
  476. if ( objectHasUvs ) {
  477. var emissiveMap = emissiveMapEnabled ? materialEmissiveMap.getValue() : null;
  478. if ( material.emissiveMap !== emissiveMap ) {
  479. editor.execute( new SetMaterialMapCommand( currentObject, 'emissiveMap', emissiveMap ) );
  480. }
  481. } else {
  482. if ( emissiveMapEnabled ) textureWarning = true;
  483. }
  484. }
  485. if ( material.side !== undefined ) {
  486. var side = parseInt( materialSide.getValue() );
  487. if ( material.side !== side ) {
  488. editor.execute( new SetMaterialValueCommand( currentObject, 'side', side ) );
  489. }
  490. }
  491. if ( material.shading !== undefined ) {
  492. var shading = parseInt( materialShading.getValue() );
  493. if ( material.shading !== shading ) {
  494. editor.execute( new SetMaterialValueCommand( currentObject, 'shading', shading ) );
  495. }
  496. }
  497. if ( material.blending !== undefined ) {
  498. var blending = parseInt( materialBlending.getValue() );
  499. if ( material.blending !== blending ) {
  500. editor.execute( new SetMaterialValueCommand( currentObject, 'blending', blending ) );
  501. }
  502. }
  503. if ( material.opacity !== undefined && Math.abs( material.opacity - materialOpacity.getValue() ) >= 0.01 ) {
  504. editor.execute( new SetMaterialValueCommand( currentObject, 'opacity', materialOpacity.getValue() ) );
  505. }
  506. if ( material.transparent !== undefined && material.transparent !== materialTransparent.getValue() ) {
  507. editor.execute( new SetMaterialValueCommand( currentObject, 'transparent', materialTransparent.getValue() ) );
  508. }
  509. if ( material.alphaTest !== undefined && Math.abs( material.alphaTest - materialAlphaTest.getValue() ) >= 0.01 ) {
  510. editor.execute( new SetMaterialValueCommand( currentObject, 'alphaTest', materialAlphaTest.getValue() ) );
  511. }
  512. if ( material.wireframe !== undefined && material.wireframe !== materialWireframe.getValue() ) {
  513. editor.execute( new SetMaterialValueCommand( currentObject, 'wireframe', materialWireframe.getValue() ) );
  514. }
  515. if ( material.wireframeLinewidth !== undefined && Math.abs( material.wireframeLinewidth - materialWireframeLinewidth.getValue() ) >= 0.01 ) {
  516. editor.execute( new SetMaterialValueCommand( currentObject, 'wireframeLinewidth', materialWireframeLinewidth.getValue() ) );
  517. }
  518. refreshUI( false );
  519. signals.materialChanged.dispatch( material );
  520. }
  521. if ( textureWarning ) {
  522. console.warn( "Can't set texture, model doesn't have texture coordinates" );
  523. }
  524. }
  525. //
  526. function setRowVisibility() {
  527. var properties = {
  528. 'name': materialNameRow,
  529. 'color': materialColorRow,
  530. 'roughness': materialRoughnessRow,
  531. 'metalness': materialMetalnessRow,
  532. 'emissive': materialEmissiveRow,
  533. 'specular': materialSpecularRow,
  534. 'shininess': materialShininessRow,
  535. 'vertexShader': materialProgramRow,
  536. 'vertexColors': materialVertexColorsRow,
  537. 'skinning': materialSkinningRow,
  538. 'map': materialMapRow,
  539. 'alphaMap': materialAlphaMapRow,
  540. 'bumpMap': materialBumpMapRow,
  541. 'normalMap': materialNormalMapRow,
  542. 'displacementMap': materialDisplacementMapRow,
  543. 'roughnessMap': materialRoughnessMapRow,
  544. 'metalnessMap': materialMetalnessMapRow,
  545. 'specularMap': materialSpecularMapRow,
  546. 'envMap': materialEnvMapRow,
  547. 'lightMap': materialLightMapRow,
  548. 'aoMap': materialAOMapRow,
  549. 'emissiveMap': materialEmissiveMapRow,
  550. 'side': materialSideRow,
  551. 'shading': materialShadingRow,
  552. 'blending': materialBlendingRow,
  553. 'opacity': materialOpacityRow,
  554. 'transparent': materialTransparentRow,
  555. 'alphaTest': materialAlphaTestRow,
  556. 'wireframe': materialWireframeRow
  557. };
  558. var material = currentObject.material;
  559. for ( var property in properties ) {
  560. properties[ property ].setDisplay( material[ property ] !== undefined ? '' : 'none' );
  561. }
  562. }
  563. function refreshUI( resetTextureSelectors ) {
  564. if ( ! currentObject ) return;
  565. var material = currentObject.material;
  566. if ( material.uuid !== undefined ) {
  567. materialUUID.setValue( material.uuid );
  568. }
  569. if ( material.name !== undefined ) {
  570. materialName.setValue( material.name );
  571. }
  572. materialClass.setValue( material.type );
  573. if ( material.color !== undefined ) {
  574. materialColor.setHexValue( material.color.getHexString() );
  575. }
  576. if ( material.roughness !== undefined ) {
  577. materialRoughness.setValue( material.roughness );
  578. }
  579. if ( material.metalness !== undefined ) {
  580. materialMetalness.setValue( material.metalness );
  581. }
  582. if ( material.emissive !== undefined ) {
  583. materialEmissive.setHexValue( material.emissive.getHexString() );
  584. }
  585. if ( material.specular !== undefined ) {
  586. materialSpecular.setHexValue( material.specular.getHexString() );
  587. }
  588. if ( material.shininess !== undefined ) {
  589. materialShininess.setValue( material.shininess );
  590. }
  591. if ( material.vertexColors !== undefined ) {
  592. materialVertexColors.setValue( material.vertexColors );
  593. }
  594. if ( material.skinning !== undefined ) {
  595. materialSkinning.setValue( material.skinning );
  596. }
  597. if ( material.map !== undefined ) {
  598. materialMapEnabled.setValue( material.map !== null );
  599. if ( material.map !== null || resetTextureSelectors ) {
  600. materialMap.setValue( material.map );
  601. }
  602. }
  603. if ( material.alphaMap !== undefined ) {
  604. materialAlphaMapEnabled.setValue( material.alphaMap !== null );
  605. if ( material.alphaMap !== null || resetTextureSelectors ) {
  606. materialAlphaMap.setValue( material.alphaMap );
  607. }
  608. }
  609. if ( material.bumpMap !== undefined ) {
  610. materialBumpMapEnabled.setValue( material.bumpMap !== null );
  611. if ( material.bumpMap !== null || resetTextureSelectors ) {
  612. materialBumpMap.setValue( material.bumpMap );
  613. }
  614. materialBumpScale.setValue( material.bumpScale );
  615. }
  616. if ( material.normalMap !== undefined ) {
  617. materialNormalMapEnabled.setValue( material.normalMap !== null );
  618. if ( material.normalMap !== null || resetTextureSelectors ) {
  619. materialNormalMap.setValue( material.normalMap );
  620. }
  621. }
  622. if ( material.displacementMap !== undefined ) {
  623. materialDisplacementMapEnabled.setValue( material.displacementMap !== null );
  624. if ( material.displacementMap !== null || resetTextureSelectors ) {
  625. materialDisplacementMap.setValue( material.displacementMap );
  626. }
  627. materialDisplacementScale.setValue( material.displacementScale );
  628. }
  629. if ( material.roughnessMap !== undefined ) {
  630. materialRoughnessMapEnabled.setValue( material.roughnessMap !== null );
  631. if ( material.roughnessMap !== null || resetTextureSelectors ) {
  632. materialRoughnessMap.setValue( material.roughnessMap );
  633. }
  634. }
  635. if ( material.metalnessMap !== undefined ) {
  636. materialMetalnessMapEnabled.setValue( material.metalnessMap !== null );
  637. if ( material.metalnessMap !== null || resetTextureSelectors ) {
  638. materialMetalnessMap.setValue( material.metalnessMap );
  639. }
  640. }
  641. if ( material.specularMap !== undefined ) {
  642. materialSpecularMapEnabled.setValue( material.specularMap !== null );
  643. if ( material.specularMap !== null || resetTextureSelectors ) {
  644. materialSpecularMap.setValue( material.specularMap );
  645. }
  646. }
  647. if ( material.envMap !== undefined ) {
  648. materialEnvMapEnabled.setValue( material.envMap !== null );
  649. if ( material.envMap !== null || resetTextureSelectors ) {
  650. materialEnvMap.setValue( material.envMap );
  651. }
  652. materialReflectivity.setValue( material.reflectivity );
  653. }
  654. if ( material.lightMap !== undefined ) {
  655. materialLightMapEnabled.setValue( material.lightMap !== null );
  656. if ( material.lightMap !== null || resetTextureSelectors ) {
  657. materialLightMap.setValue( material.lightMap );
  658. }
  659. }
  660. if ( material.aoMap !== undefined ) {
  661. materialAOMapEnabled.setValue( material.aoMap !== null );
  662. if ( material.aoMap !== null || resetTextureSelectors ) {
  663. materialAOMap.setValue( material.aoMap );
  664. }
  665. materialAOScale.setValue( material.aoMapIntensity );
  666. }
  667. if ( material.emissiveMap !== undefined ) {
  668. materialEmissiveMapEnabled.setValue( material.emissiveMap !== null );
  669. if ( material.emissiveMap !== null || resetTextureSelectors ) {
  670. materialEmissiveMap.setValue( material.emissiveMap );
  671. }
  672. }
  673. if ( material.side !== undefined ) {
  674. materialSide.setValue( material.side );
  675. }
  676. if ( material.shading !== undefined ) {
  677. materialShading.setValue( material.shading );
  678. }
  679. if ( material.blending !== undefined ) {
  680. materialBlending.setValue( material.blending );
  681. }
  682. if ( material.opacity !== undefined ) {
  683. materialOpacity.setValue( material.opacity );
  684. }
  685. if ( material.transparent !== undefined ) {
  686. materialTransparent.setValue( material.transparent );
  687. }
  688. if ( material.alphaTest !== undefined ) {
  689. materialAlphaTest.setValue( material.alphaTest );
  690. }
  691. if ( material.wireframe !== undefined ) {
  692. materialWireframe.setValue( material.wireframe );
  693. }
  694. if ( material.wireframeLinewidth !== undefined ) {
  695. materialWireframeLinewidth.setValue( material.wireframeLinewidth );
  696. }
  697. setRowVisibility();
  698. }
  699. // events
  700. signals.objectSelected.add( function ( object ) {
  701. if ( object && object.material ) {
  702. var objectChanged = object !== currentObject;
  703. currentObject = object;
  704. refreshUI( objectChanged );
  705. container.setDisplay( '' );
  706. } else {
  707. currentObject = null;
  708. container.setDisplay( 'none' );
  709. }
  710. } );
  711. signals.materialChanged.add( function () {
  712. refreshUI();
  713. } );
  714. return container;
  715. };