Sidebar.Material.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547
  1. import * as THREE from '../../build/three.module.js';
  2. import { UIButton, UIInput, UIPanel, UIRow, UISelect, UIText } 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 { SidebarMaterialProgram } from './Sidebar.Material.Program.js';
  11. function SidebarMaterial( editor ) {
  12. const signals = editor.signals;
  13. const strings = editor.strings;
  14. let currentObject;
  15. let currentMaterialSlot = 0;
  16. const container = new UIPanel();
  17. container.setBorderTop( '0' );
  18. container.setDisplay( 'none' );
  19. container.setPaddingTop( '20px' );
  20. // Current material slot
  21. const materialSlotRow = new UIRow();
  22. materialSlotRow.add( new UIText( strings.getKey( 'sidebar/material/slot' ) ).setWidth( '90px' ) );
  23. const materialSlotSelect = new UISelect().setWidth( '170px' ).setFontSize( '12px' ).onChange( update );
  24. materialSlotSelect.setOptions( { 0: '' } ).setValue( 0 );
  25. materialSlotRow.add( materialSlotSelect );
  26. container.add( materialSlotRow );
  27. // type
  28. const materialClassRow = new UIRow();
  29. const materialClass = new UISelect().setWidth( '150px' ).setFontSize( '12px' ).onChange( update );
  30. materialClassRow.add( new UIText( strings.getKey( 'sidebar/material/type' ) ).setWidth( '90px' ) );
  31. materialClassRow.add( materialClass );
  32. container.add( materialClassRow );
  33. // uuid
  34. const materialUUIDRow = new UIRow();
  35. const materialUUID = new UIInput().setWidth( '102px' ).setFontSize( '12px' ).setDisabled( true );
  36. const materialUUIDRenew = new UIButton( strings.getKey( 'sidebar/material/new' ) ).setMarginLeft( '7px' );
  37. materialUUIDRenew.onClick( function () {
  38. materialUUID.setValue( THREE.MathUtils.generateUUID() );
  39. update();
  40. } );
  41. materialUUIDRow.add( new UIText( strings.getKey( 'sidebar/material/uuid' ) ).setWidth( '90px' ) );
  42. materialUUIDRow.add( materialUUID );
  43. materialUUIDRow.add( materialUUIDRenew );
  44. container.add( materialUUIDRow );
  45. // name
  46. const materialNameRow = new UIRow();
  47. const materialName = new UIInput().setWidth( '150px' ).setFontSize( '12px' ).onChange( function () {
  48. editor.execute( new SetMaterialValueCommand( editor, editor.selected, 'name', materialName.getValue(), currentMaterialSlot ) );
  49. } );
  50. materialNameRow.add( new UIText( strings.getKey( 'sidebar/material/name' ) ).setWidth( '90px' ) );
  51. materialNameRow.add( materialName );
  52. container.add( materialNameRow );
  53. // program
  54. const materialProgram = new SidebarMaterialProgram( editor, 'vertexShader' );
  55. container.add( materialProgram );
  56. // color
  57. const materialColor = new SidebarMaterialColorProperty( editor, 'color', strings.getKey( 'sidebar/material/color' ) );
  58. container.add( materialColor );
  59. // specular
  60. const materialSpecular = new SidebarMaterialColorProperty( editor, 'specular', strings.getKey( 'sidebar/material/specular' ) );
  61. container.add( materialSpecular );
  62. // shininess
  63. const materialShininess = new SidebarMaterialNumberProperty( editor, 'shininess', strings.getKey( 'sidebar/material/shininess' ) );
  64. container.add( materialShininess );
  65. // emissive
  66. const materialEmissive = new SidebarMaterialColorProperty( editor, 'emissive', strings.getKey( 'sidebar/material/emissive' ) );
  67. container.add( materialEmissive );
  68. // reflectivity
  69. const materialReflectivity = new SidebarMaterialNumberProperty( editor, 'reflectivity', strings.getKey( 'sidebar/material/reflectivity' ) );
  70. container.add( materialReflectivity );
  71. // roughness
  72. const materialRoughness = new SidebarMaterialNumberProperty( editor, 'roughness', strings.getKey( 'sidebar/material/roughness' ), [ 0, 1 ] );
  73. container.add( materialRoughness );
  74. // metalness
  75. const materialMetalness = new SidebarMaterialNumberProperty( editor, 'metalness', strings.getKey( 'sidebar/material/metalness' ), [ 0, 1 ] );
  76. container.add( materialMetalness );
  77. // clearcoat
  78. const materialClearcoat = new SidebarMaterialNumberProperty( editor, 'clearcoat', strings.getKey( 'sidebar/material/clearcoat' ), [ 0, 1 ] );
  79. container.add( materialClearcoat );
  80. // clearcoatRoughness
  81. const materialClearcoatRoughness = new SidebarMaterialNumberProperty( editor, 'clearcoatRoughness', strings.getKey( 'sidebar/material/clearcoatroughness' ), [ 0, 1 ] );
  82. container.add( materialClearcoatRoughness );
  83. // transmission
  84. const materialTransmission = new SidebarMaterialNumberProperty( editor, 'transmission', strings.getKey( 'sidebar/material/transmission' ), [ 0, 1 ] );
  85. container.add( materialTransmission );
  86. // attenuation distance
  87. const materialAttenuationDistance = new SidebarMaterialNumberProperty( editor, 'attenuationDistance', strings.getKey( 'sidebar/material/attenuationDistance' ) );
  88. container.add( materialAttenuationDistance );
  89. // attenuation tint
  90. const materialAttenuationColor = new SidebarMaterialColorProperty( editor, 'attenuationColor', strings.getKey( 'sidebar/material/attenuationColor' ) );
  91. container.add( materialAttenuationColor );
  92. // thickness
  93. const materialThickness = new SidebarMaterialNumberProperty( editor, 'thickness', strings.getKey( 'sidebar/material/thickness' ) );
  94. container.add( materialThickness );
  95. // vertex colors
  96. const materialVertexColors = new SidebarMaterialBooleanProperty( editor, 'vertexColors', strings.getKey( 'sidebar/material/vertexcolors' ) );
  97. container.add( materialVertexColors );
  98. // depth packing
  99. const materialDepthPackingOptions = {
  100. [ THREE.BasicDepthPacking ]: 'Basic',
  101. [ THREE.RGBADepthPacking ]: 'RGBA'
  102. };
  103. const materialDepthPacking = new SidebarMaterialConstantProperty( editor, 'depthPacking', strings.getKey( 'sidebar/material/depthPacking' ), materialDepthPackingOptions );
  104. container.add( materialDepthPacking );
  105. // map
  106. const materialMap = new SidebarMaterialMapProperty( editor, 'map', strings.getKey( 'sidebar/material/map' ) );
  107. container.add( materialMap );
  108. // specular map
  109. const materialSpecularMap = new SidebarMaterialMapProperty( editor, 'specularMap', strings.getKey( 'sidebar/material/specularmap' ) );
  110. container.add( materialSpecularMap );
  111. // emissive map
  112. const materialEmissiveMap = new SidebarMaterialMapProperty( editor, 'emissiveMap', strings.getKey( 'sidebar/material/emissivemap' ) );
  113. container.add( materialEmissiveMap );
  114. // matcap map
  115. const materialMatcapMap = new SidebarMaterialMapProperty( editor, 'matcap', strings.getKey( 'sidebar/material/matcap' ) );
  116. container.add( materialMatcapMap );
  117. // alpha map
  118. const materialAlphaMap = new SidebarMaterialMapProperty( editor, 'alphaMap', strings.getKey( 'sidebar/material/alphamap' ) );
  119. container.add( materialAlphaMap );
  120. // bump map
  121. const materialBumpMap = new SidebarMaterialMapProperty( editor, 'bumpMap', strings.getKey( 'sidebar/material/bumpmap' ) );
  122. container.add( materialBumpMap );
  123. // normal map
  124. const materialNormalMap = new SidebarMaterialMapProperty( editor, 'normalMap', strings.getKey( 'sidebar/material/normalmap' ) );
  125. container.add( materialNormalMap );
  126. // clearcoat normal map
  127. const materialClearcoatNormalMap = new SidebarMaterialMapProperty( editor, 'clearcoatNormalMap', strings.getKey( 'sidebar/material/clearcoatnormalmap' ) );
  128. container.add( materialClearcoatNormalMap );
  129. // displacement map
  130. const materialDisplacementMap = new SidebarMaterialMapProperty( editor, 'displacementMap', strings.getKey( 'sidebar/material/displacementmap' ) );
  131. container.add( materialDisplacementMap );
  132. // roughness map
  133. const materialRoughnessMap = new SidebarMaterialMapProperty( editor, 'roughnessMap', strings.getKey( 'sidebar/material/roughnessmap' ) );
  134. container.add( materialRoughnessMap );
  135. // metalness map
  136. const materialMetalnessMap = new SidebarMaterialMapProperty( editor, 'metalnessMap', strings.getKey( 'sidebar/material/metalnessmap' ) );
  137. container.add( materialMetalnessMap );
  138. // env map
  139. const materialEnvMap = new SidebarMaterialMapProperty( editor, 'envMap', strings.getKey( 'sidebar/material/envmap' ) );
  140. container.add( materialEnvMap );
  141. // light map
  142. const materialLightMap = new SidebarMaterialMapProperty( editor, 'lightMap', strings.getKey( 'sidebar/material/lightmap' ) );
  143. container.add( materialLightMap );
  144. // ambient occlusion map
  145. const materialAOMap = new SidebarMaterialMapProperty( editor, 'aoMap', strings.getKey( 'sidebar/material/aomap' ) );
  146. container.add( materialAOMap );
  147. // gradient map
  148. const materialGradientMap = new SidebarMaterialMapProperty( editor, 'gradientMap', strings.getKey( 'sidebar/material/gradientmap' ) );
  149. container.add( materialGradientMap );
  150. // side
  151. const materialSideOptions = {
  152. 0: 'Front',
  153. 1: 'Back',
  154. 2: 'Double'
  155. };
  156. const materialSide = new SidebarMaterialConstantProperty( editor, 'side', strings.getKey( 'sidebar/material/side' ), materialSideOptions );
  157. container.add( materialSide );
  158. // size
  159. const materialSize = new SidebarMaterialNumberProperty( editor, 'size', strings.getKey( 'sidebar/material/size' ), [ 0, Infinity ] );
  160. container.add( materialSize );
  161. // sizeAttenuation
  162. const materialSizeAttenuation = new SidebarMaterialBooleanProperty( editor, 'sizeAttenuation', strings.getKey( 'sidebar/material/sizeAttenuation' ) );
  163. container.add( materialSizeAttenuation );
  164. // flatShading
  165. const materialFlatShading = new SidebarMaterialBooleanProperty( editor, 'flatShading', strings.getKey( 'sidebar/material/flatShading' ) );
  166. container.add( materialFlatShading );
  167. // blending
  168. const materialBlendingOptions = {
  169. 0: 'No',
  170. 1: 'Normal',
  171. 2: 'Additive',
  172. 3: 'Subtractive',
  173. 4: 'Multiply',
  174. 5: 'Custom'
  175. };
  176. const materialBlending = new SidebarMaterialConstantProperty( editor, 'blending', strings.getKey( 'sidebar/material/blending' ), materialBlendingOptions );
  177. container.add( materialBlending );
  178. // opacity
  179. const materialOpacity = new SidebarMaterialNumberProperty( editor, 'opacity', strings.getKey( 'sidebar/material/opacity' ), [ 0, 1 ] );
  180. container.add( materialOpacity );
  181. // transparent
  182. const materialTransparent = new SidebarMaterialBooleanProperty( editor, 'transparent', strings.getKey( 'sidebar/material/transparent' ) );
  183. container.add( materialTransparent );
  184. // alpha test
  185. const materialAlphaTest = new SidebarMaterialNumberProperty( editor, 'alphaTest', strings.getKey( 'sidebar/material/alphatest' ), [ 0, 1 ] );
  186. container.add( materialAlphaTest );
  187. // depth test
  188. const materialDepthTest = new SidebarMaterialBooleanProperty( editor, 'depthTest', strings.getKey( 'sidebar/material/depthtest' ) );
  189. container.add( materialDepthTest );
  190. // depth write
  191. const materialDepthWrite = new SidebarMaterialBooleanProperty( editor, 'depthWrite', strings.getKey( 'sidebar/material/depthwrite' ) );
  192. container.add( materialDepthWrite );
  193. // wireframe
  194. const materialWireframe = new SidebarMaterialBooleanProperty( editor, 'wireframe', strings.getKey( 'sidebar/material/wireframe' ) );
  195. container.add( materialWireframe );
  196. //
  197. function update() {
  198. const previousSelectedSlot = currentMaterialSlot;
  199. currentMaterialSlot = parseInt( materialSlotSelect.getValue() );
  200. if ( currentMaterialSlot !== previousSelectedSlot ) refreshUI();
  201. let material = editor.getObjectMaterial( currentObject, currentMaterialSlot );
  202. if ( material ) {
  203. if ( material.uuid !== undefined && material.uuid !== materialUUID.getValue() ) {
  204. editor.execute( new SetMaterialValueCommand( editor, currentObject, 'uuid', materialUUID.getValue(), currentMaterialSlot ) );
  205. }
  206. if ( material.type !== materialClass.getValue() ) {
  207. material = new materialClasses[ materialClass.getValue() ]();
  208. if ( material.type === 'RawShaderMaterial' ) {
  209. material.vertexShader = vertexShaderVariables + material.vertexShader;
  210. }
  211. if ( Array.isArray( currentObject.material ) ) {
  212. // don't remove the entire multi-material. just the material of the selected slot
  213. editor.removeMaterial( currentObject.material[ currentMaterialSlot ] );
  214. } else {
  215. editor.removeMaterial( currentObject.material );
  216. }
  217. editor.execute( new SetMaterialCommand( editor, currentObject, material, currentMaterialSlot ), 'New Material: ' + materialClass.getValue() );
  218. editor.addMaterial( material );
  219. // TODO Copy other references in the scene graph
  220. // keeping name and UUID then.
  221. // Also there should be means to create a unique
  222. // copy for the current object explicitly and to
  223. // attach the current material to other objects.
  224. }
  225. refreshUI();
  226. }
  227. }
  228. //
  229. function setRowVisibility() {
  230. const material = currentObject.material;
  231. if ( Array.isArray( material ) ) {
  232. materialSlotRow.setDisplay( '' );
  233. } else {
  234. materialSlotRow.setDisplay( 'none' );
  235. }
  236. }
  237. function refreshUI() {
  238. if ( ! currentObject ) return;
  239. let material = currentObject.material;
  240. if ( Array.isArray( material ) ) {
  241. const slotOptions = {};
  242. currentMaterialSlot = Math.max( 0, Math.min( material.length, currentMaterialSlot ) );
  243. for ( let i = 0; i < material.length; i ++ ) {
  244. slotOptions[ i ] = String( i + 1 ) + ': ' + material[ i ].name;
  245. }
  246. materialSlotSelect.setOptions( slotOptions ).setValue( currentMaterialSlot );
  247. }
  248. material = editor.getObjectMaterial( currentObject, currentMaterialSlot );
  249. if ( material.uuid !== undefined ) {
  250. materialUUID.setValue( material.uuid );
  251. }
  252. if ( material.name !== undefined ) {
  253. materialName.setValue( material.name );
  254. }
  255. if ( currentObject.isMesh ) {
  256. materialClass.setOptions( meshMaterialOptions );
  257. } else if ( currentObject.isSprite ) {
  258. materialClass.setOptions( spriteMaterialOptions );
  259. } else if ( currentObject.isPoints ) {
  260. materialClass.setOptions( pointsMaterialOptions );
  261. } else if ( currentObject.isLine ) {
  262. materialClass.setOptions( lineMaterialOptions );
  263. }
  264. materialClass.setValue( material.type );
  265. setRowVisibility();
  266. }
  267. // events
  268. signals.objectSelected.add( function ( object ) {
  269. let hasMaterial = false;
  270. if ( object && object.material ) {
  271. hasMaterial = true;
  272. if ( Array.isArray( object.material ) && object.material.length === 0 ) {
  273. hasMaterial = false;
  274. }
  275. }
  276. if ( hasMaterial ) {
  277. currentObject = object;
  278. refreshUI();
  279. container.setDisplay( '' );
  280. } else {
  281. currentObject = null;
  282. container.setDisplay( 'none' );
  283. }
  284. } );
  285. signals.materialChanged.add( refreshUI );
  286. return container;
  287. }
  288. const materialClasses = {
  289. 'LineBasicMaterial': THREE.LineBasicMaterial,
  290. 'LineDashedMaterial': THREE.LineDashedMaterial,
  291. 'MeshBasicMaterial': THREE.MeshBasicMaterial,
  292. 'MeshDepthMaterial': THREE.MeshDepthMaterial,
  293. 'MeshNormalMaterial': THREE.MeshNormalMaterial,
  294. 'MeshLambertMaterial': THREE.MeshLambertMaterial,
  295. 'MeshMatcapMaterial': THREE.MeshMatcapMaterial,
  296. 'MeshPhongMaterial': THREE.MeshPhongMaterial,
  297. 'MeshToonMaterial': THREE.MeshToonMaterial,
  298. 'MeshStandardMaterial': THREE.MeshStandardMaterial,
  299. 'MeshPhysicalMaterial': THREE.MeshPhysicalMaterial,
  300. 'RawShaderMaterial': THREE.RawShaderMaterial,
  301. 'ShaderMaterial': THREE.ShaderMaterial,
  302. 'ShadowMaterial': THREE.ShadowMaterial,
  303. 'SpriteMaterial': THREE.SpriteMaterial,
  304. 'PointsMaterial': THREE.PointsMaterial
  305. };
  306. const vertexShaderVariables = [
  307. 'uniform mat4 projectionMatrix;',
  308. 'uniform mat4 modelViewMatrix;\n',
  309. 'attribute vec3 position;\n\n',
  310. ].join( '\n' );
  311. const meshMaterialOptions = {
  312. 'MeshBasicMaterial': 'MeshBasicMaterial',
  313. 'MeshDepthMaterial': 'MeshDepthMaterial',
  314. 'MeshNormalMaterial': 'MeshNormalMaterial',
  315. 'MeshLambertMaterial': 'MeshLambertMaterial',
  316. 'MeshMatcapMaterial': 'MeshMatcapMaterial',
  317. 'MeshPhongMaterial': 'MeshPhongMaterial',
  318. 'MeshToonMaterial': 'MeshToonMaterial',
  319. 'MeshStandardMaterial': 'MeshStandardMaterial',
  320. 'MeshPhysicalMaterial': 'MeshPhysicalMaterial',
  321. 'RawShaderMaterial': 'RawShaderMaterial',
  322. 'ShaderMaterial': 'ShaderMaterial',
  323. 'ShadowMaterial': 'ShadowMaterial'
  324. };
  325. const lineMaterialOptions = {
  326. 'LineBasicMaterial': 'LineBasicMaterial',
  327. 'LineDashedMaterial': 'LineDashedMaterial',
  328. 'RawShaderMaterial': 'RawShaderMaterial',
  329. 'ShaderMaterial': 'ShaderMaterial'
  330. };
  331. const spriteMaterialOptions = {
  332. 'SpriteMaterial': 'SpriteMaterial',
  333. 'RawShaderMaterial': 'RawShaderMaterial',
  334. 'ShaderMaterial': 'ShaderMaterial'
  335. };
  336. const pointsMaterialOptions = {
  337. 'PointsMaterial': 'PointsMaterial',
  338. 'RawShaderMaterial': 'RawShaderMaterial',
  339. 'ShaderMaterial': 'ShaderMaterial'
  340. };
  341. export { SidebarMaterial };