material.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590
  1. /**
  2. * @author TatumCreative (Greg Tatum) / http://gregtatum.com/
  3. */
  4. var constants = {
  5. combine: {
  6. 'THREE.MultiplyOperation': THREE.MultiplyOperation,
  7. 'THREE.MixOperation': THREE.MixOperation,
  8. 'THREE.AddOperation': THREE.AddOperation
  9. },
  10. side: {
  11. 'THREE.FrontSide': THREE.FrontSide,
  12. 'THREE.BackSide': THREE.BackSide,
  13. 'THREE.DoubleSide': THREE.DoubleSide
  14. },
  15. colors: {
  16. 'THREE.NoColors': THREE.NoColors,
  17. 'THREE.VertexColors': THREE.VertexColors
  18. },
  19. blendingMode: {
  20. 'THREE.NoBlending': THREE.NoBlending,
  21. 'THREE.NormalBlending': THREE.NormalBlending,
  22. 'THREE.AdditiveBlending': THREE.AdditiveBlending,
  23. 'THREE.SubtractiveBlending': THREE.SubtractiveBlending,
  24. 'THREE.MultiplyBlending': THREE.MultiplyBlending,
  25. 'THREE.CustomBlending': THREE.CustomBlending
  26. },
  27. equations: {
  28. 'THREE.AddEquation': THREE.AddEquation,
  29. 'THREE.SubtractEquation': THREE.SubtractEquation,
  30. 'THREE.ReverseSubtractEquation': THREE.ReverseSubtractEquation
  31. },
  32. destinationFactors: {
  33. 'THREE.ZeroFactor': THREE.ZeroFactor,
  34. 'THREE.OneFactor': THREE.OneFactor,
  35. 'THREE.SrcColorFactor': THREE.SrcColorFactor,
  36. 'THREE.OneMinusSrcColorFactor': THREE.OneMinusSrcColorFactor,
  37. 'THREE.SrcAlphaFactor': THREE.SrcAlphaFactor,
  38. 'THREE.OneMinusSrcAlphaFactor': THREE.OneMinusSrcAlphaFactor,
  39. 'THREE.DstAlphaFactor': THREE.DstAlphaFactor,
  40. 'THREE.OneMinusDstAlphaFactor': THREE.OneMinusDstAlphaFactor
  41. },
  42. sourceFactors: {
  43. 'THREE.DstColorFactor': THREE.DstColorFactor,
  44. 'THREE.OneMinusDstColorFactor': THREE.OneMinusDstColorFactor,
  45. 'THREE.SrcAlphaSaturateFactor': THREE.SrcAlphaSaturateFactor
  46. }
  47. };
  48. function getObjectsKeys( obj ) {
  49. var keys = [];
  50. for ( var key in obj ) {
  51. if ( obj.hasOwnProperty( key ) ) {
  52. keys.push( key );
  53. }
  54. }
  55. return keys;
  56. }
  57. var envMaps = ( function () {
  58. var path = '../../examples/textures/cube/SwedishRoyalCastle/';
  59. var format = '.jpg';
  60. var urls = [
  61. path + 'px' + format, path + 'nx' + format,
  62. path + 'py' + format, path + 'ny' + format,
  63. path + 'pz' + format, path + 'nz' + format
  64. ];
  65. var reflectionCube = new THREE.CubeTextureLoader().load( urls );
  66. reflectionCube.format = THREE.RGBFormat;
  67. var refractionCube = new THREE.CubeTextureLoader().load( urls );
  68. refractionCube.mapping = THREE.CubeRefractionMapping;
  69. refractionCube.format = THREE.RGBFormat;
  70. return {
  71. none: null,
  72. reflection: reflectionCube,
  73. refraction: refractionCube
  74. };
  75. } )();
  76. var envMapKeys = getObjectsKeys( envMaps );
  77. var textureMaps = ( function () {
  78. return {
  79. none: null,
  80. grass: new THREE.TextureLoader().load( '../../examples/textures/terrain/grasslight-thin.jpg' )
  81. };
  82. } )();
  83. var textureMapKeys = getObjectsKeys( textureMaps );
  84. var matcaps = ( function () {
  85. return {
  86. none: null,
  87. porcelainWhite: new THREE.TextureLoader().load( '../../examples/textures/matcaps/matcap-porcelain-white.jpg' )
  88. };
  89. } )();
  90. var matcapKeys = getObjectsKeys( matcaps );
  91. function generateVertexColors( geometry ) {
  92. var positionAttribute = geometry.attributes.position;
  93. var colors = [];
  94. var color = new THREE.Color();
  95. for ( var i = 0, il = positionAttribute.count; i < il; i ++ ) {
  96. color.setHSL( i / il * Math.random(), 0.5, 0.5 );
  97. colors.push( color.r, color.g, color.b );
  98. }
  99. geometry.addAttribute( 'color', new THREE.Float32BufferAttribute( colors, 3 ) );
  100. }
  101. function handleColorChange( color ) {
  102. return function ( value ) {
  103. if ( typeof value === 'string' ) {
  104. value = value.replace( '#', '0x' );
  105. }
  106. color.setHex( value );
  107. };
  108. }
  109. function needsUpdate( material, geometry ) {
  110. return function () {
  111. material.vertexColors = parseInt( material.vertexColors ); //Ensure number
  112. material.side = parseInt( material.side ); //Ensure number
  113. material.needsUpdate = true;
  114. geometry.attributes.position.needsUpdate = true;
  115. geometry.attributes.normal.needsUpdate = true;
  116. geometry.attributes.color.needsUpdate = true;
  117. };
  118. }
  119. function updateTexture( material, materialKey, textures ) {
  120. return function ( key ) {
  121. material[ materialKey ] = textures[ key ];
  122. material.needsUpdate = true;
  123. };
  124. }
  125. function guiScene( gui, scene ) {
  126. var folder = gui.addFolder( 'Scene' );
  127. var data = {
  128. background: '#000000',
  129. 'ambient light': ambientLight.color.getHex()
  130. };
  131. var color = new THREE.Color();
  132. var colorConvert = handleColorChange( color );
  133. folder.addColor( data, 'background' ).onChange( function ( value ) {
  134. colorConvert( value );
  135. renderer.setClearColor( color.getHex() );
  136. } );
  137. folder.addColor( data, 'ambient light' ).onChange( handleColorChange( ambientLight.color ) );
  138. guiSceneFog( folder, scene );
  139. }
  140. function guiSceneFog( folder, scene ) {
  141. var fogFolder = folder.addFolder( 'scene.fog' );
  142. var fog = new THREE.Fog( 0x3f7b9d, 0, 60 );
  143. var data = {
  144. fog: {
  145. 'THREE.Fog()': false,
  146. 'scene.fog.color': fog.color.getHex()
  147. }
  148. };
  149. fogFolder.add( data.fog, 'THREE.Fog()' ).onChange( function ( useFog ) {
  150. if ( useFog ) {
  151. scene.fog = fog;
  152. } else {
  153. scene.fog = null;
  154. }
  155. } );
  156. fogFolder.addColor( data.fog, 'scene.fog.color' ).onChange( handleColorChange( fog.color ) );
  157. }
  158. function guiMaterial( gui, mesh, material, geometry ) {
  159. var folder = gui.addFolder( 'THREE.Material' );
  160. folder.add( material, 'transparent' );
  161. folder.add( material, 'opacity', 0, 1 );
  162. // folder.add( material, 'blending', constants.blendingMode );
  163. // folder.add( material, 'blendSrc', constants.destinationFactors );
  164. // folder.add( material, 'blendDst', constants.destinationFactors );
  165. // folder.add( material, 'blendEquation', constants.equations );
  166. folder.add( material, 'depthTest' );
  167. folder.add( material, 'depthWrite' );
  168. // folder.add( material, 'polygonOffset' );
  169. // folder.add( material, 'polygonOffsetFactor' );
  170. // folder.add( material, 'polygonOffsetUnits' );
  171. folder.add( material, 'alphaTest', 0, 1 );
  172. folder.add( material, 'visible' );
  173. folder.add( material, 'side', constants.side ).onChange( needsUpdate( material, geometry ) );
  174. }
  175. function guiMeshBasicMaterial( gui, mesh, material, geometry ) {
  176. var data = {
  177. color: material.color.getHex(),
  178. envMaps: envMapKeys,
  179. map: textureMapKeys,
  180. specularMap: textureMapKeys,
  181. alphaMap: textureMapKeys
  182. };
  183. var folder = gui.addFolder( 'THREE.MeshBasicMaterial' );
  184. folder.addColor( data, 'color' ).onChange( handleColorChange( material.color ) );
  185. folder.add( material, 'wireframe' );
  186. folder.add( material, 'wireframeLinewidth', 0, 10 );
  187. folder.add( material, 'vertexColors', constants.colors ).onChange( needsUpdate( material, geometry ) );
  188. folder.add( material, 'fog' );
  189. folder.add( data, 'envMaps', envMapKeys ).onChange( updateTexture( material, 'envMap', envMaps ) );
  190. folder.add( data, 'map', textureMapKeys ).onChange( updateTexture( material, 'map', textureMaps ) );
  191. folder.add( data, 'specularMap', textureMapKeys ).onChange( updateTexture( material, 'specularMap', textureMaps ) );
  192. folder.add( data, 'alphaMap', textureMapKeys ).onChange( updateTexture( material, 'alphaMap', textureMaps ) );
  193. folder.add( material, 'combine', constants.combine );
  194. folder.add( material, 'reflectivity', 0, 1 );
  195. folder.add( material, 'refractionRatio', 0, 1 );
  196. }
  197. function guiMeshDepthMaterial( gui, mesh, material, geometry ) {
  198. var folder = gui.addFolder( 'THREE.MeshDepthMaterial' );
  199. folder.add( material, 'wireframe' );
  200. folder.add( material, 'wireframeLinewidth', 0, 10 );
  201. }
  202. function guiMeshNormalMaterial( gui, mesh, material, geometry ) {
  203. var folder = gui.addFolder( 'THREE.MeshNormalMaterial' );
  204. folder.add( material, 'flatShading' ).onChange( needsUpdate( material, geometry ) );
  205. folder.add( material, 'wireframe' );
  206. folder.add( material, 'wireframeLinewidth', 0, 10 );
  207. }
  208. function guiLineBasicMaterial( gui, mesh, material, geometry ) {
  209. var data = {
  210. color: material.color.getHex()
  211. };
  212. var folder = gui.addFolder( 'THREE.LineBasicMaterial' );
  213. folder.addColor( data, 'color' ).onChange( handleColorChange( material.color ) );
  214. folder.add( material, 'linewidth', 0, 10 );
  215. folder.add( material, 'linecap', [ 'butt', 'round', 'square' ] );
  216. folder.add( material, 'linejoin', [ 'round', 'bevel', 'miter' ] );
  217. folder.add( material, 'vertexColors', constants.colors ).onChange( needsUpdate( material, geometry ) );
  218. folder.add( material, 'fog' );
  219. }
  220. function guiMeshLambertMaterial( gui, mesh, material, geometry ) {
  221. var data = {
  222. color: material.color.getHex(),
  223. emissive: material.emissive.getHex(),
  224. envMaps: envMapKeys,
  225. map: textureMapKeys,
  226. specularMap: textureMapKeys,
  227. alphaMap: textureMapKeys
  228. };
  229. var folder = gui.addFolder( 'THREE.MeshLambertMaterial' );
  230. folder.addColor( data, 'color' ).onChange( handleColorChange( material.color ) );
  231. folder.addColor( data, 'emissive' ).onChange( handleColorChange( material.emissive ) );
  232. folder.add( material, 'wireframe' );
  233. folder.add( material, 'wireframeLinewidth', 0, 10 );
  234. folder.add( material, 'vertexColors', constants.colors ).onChange( needsUpdate( material, geometry ) );
  235. folder.add( material, 'fog' );
  236. folder.add( data, 'envMaps', envMapKeys ).onChange( updateTexture( material, 'envMap', envMaps ) );
  237. folder.add( data, 'map', textureMapKeys ).onChange( updateTexture( material, 'map', textureMaps ) );
  238. folder.add( data, 'specularMap', textureMapKeys ).onChange( updateTexture( material, 'specularMap', textureMaps ) );
  239. folder.add( data, 'alphaMap', textureMapKeys ).onChange( updateTexture( material, 'alphaMap', textureMaps ) );
  240. folder.add( material, 'combine', constants.combine );
  241. folder.add( material, 'reflectivity', 0, 1 );
  242. folder.add( material, 'refractionRatio', 0, 1 );
  243. }
  244. function guiMeshMatcapMaterial( gui, mesh, material ) {
  245. var data = {
  246. color: material.color.getHex(),
  247. map: textureMapKeys[ 0 ],
  248. matcap: matcapKeys[ 1 ]
  249. };
  250. var folder = gui.addFolder( 'THREE.MeshMatcapMaterial' );
  251. folder.addColor( data, 'color' ).onChange( handleColorChange( material.color ) );
  252. folder.add( data, 'map', textureMapKeys ).onChange( updateTexture( material, 'map', textureMaps ) );
  253. folder.add( data, 'matcap', matcapKeys ).onChange( updateTexture( material, 'matcap', matcaps ) );
  254. }
  255. function guiMeshPhongMaterial( gui, mesh, material, geometry ) {
  256. var data = {
  257. color: material.color.getHex(),
  258. emissive: material.emissive.getHex(),
  259. specular: material.specular.getHex(),
  260. envMaps: envMapKeys,
  261. map: textureMapKeys,
  262. lightMap: textureMapKeys,
  263. specularMap: textureMapKeys,
  264. alphaMap: textureMapKeys
  265. };
  266. var folder = gui.addFolder( 'THREE.MeshPhongMaterial' );
  267. folder.addColor( data, 'color' ).onChange( handleColorChange( material.color ) );
  268. folder.addColor( data, 'emissive' ).onChange( handleColorChange( material.emissive ) );
  269. folder.addColor( data, 'specular' ).onChange( handleColorChange( material.specular ) );
  270. folder.add( material, 'shininess', 0, 100 );
  271. folder.add( material, 'flatShading' ).onChange( needsUpdate( material, geometry ) );
  272. folder.add( material, 'wireframe' );
  273. folder.add( material, 'wireframeLinewidth', 0, 10 );
  274. folder.add( material, 'vertexColors', constants.colors ).onChange( needsUpdate( material, geometry ) );
  275. folder.add( material, 'fog' );
  276. folder.add( data, 'envMaps', envMapKeys ).onChange( updateTexture( material, 'envMap', envMaps ) );
  277. folder.add( data, 'map', textureMapKeys ).onChange( updateTexture( material, 'map', textureMaps ) );
  278. folder.add( data, 'lightMap', textureMapKeys ).onChange( updateTexture( material, 'lightMap', textureMaps ) );
  279. folder.add( data, 'specularMap', textureMapKeys ).onChange( updateTexture( material, 'specularMap', textureMaps ) );
  280. folder.add( data, 'alphaMap', textureMapKeys ).onChange( updateTexture( material, 'alphaMap', textureMaps ) );
  281. }
  282. function guiMeshStandardMaterial( gui, mesh, material, geometry ) {
  283. var data = {
  284. color: material.color.getHex(),
  285. emissive: material.emissive.getHex(),
  286. envMaps: envMapKeys,
  287. map: textureMapKeys,
  288. lightMap: textureMapKeys,
  289. specularMap: textureMapKeys,
  290. alphaMap: textureMapKeys
  291. };
  292. var folder = gui.addFolder( 'THREE.MeshStandardMaterial' );
  293. folder.addColor( data, 'color' ).onChange( handleColorChange( material.color ) );
  294. folder.addColor( data, 'emissive' ).onChange( handleColorChange( material.emissive ) );
  295. folder.add( material, 'roughness', 0, 1 );
  296. folder.add( material, 'metalness', 0, 1 );
  297. folder.add( material, 'flatShading' ).onChange( needsUpdate( material, geometry ) );
  298. folder.add( material, 'wireframe' );
  299. folder.add( material, 'wireframeLinewidth', 0, 10 );
  300. folder.add( material, 'vertexColors', constants.colors ).onChange( needsUpdate( material, geometry ) );
  301. folder.add( material, 'fog' );
  302. folder.add( data, 'envMaps', envMapKeys ).onChange( updateTexture( material, 'envMap', envMaps ) );
  303. folder.add( data, 'map', textureMapKeys ).onChange( updateTexture( material, 'map', textureMaps ) );
  304. folder.add( data, 'lightMap', textureMapKeys ).onChange( updateTexture( material, 'lightMap', textureMaps ) );
  305. folder.add( data, 'alphaMap', textureMapKeys ).onChange( updateTexture( material, 'alphaMap', textureMaps ) );
  306. // TODO roughnessMap and metalnessMap
  307. }
  308. function guiMeshPhysicalMaterial( gui, mesh, material, geometry ) {
  309. var data = {
  310. color: material.color.getHex(),
  311. emissive: material.emissive.getHex(),
  312. envMaps: envMapKeys,
  313. map: textureMapKeys,
  314. lightMap: textureMapKeys,
  315. specularMap: textureMapKeys,
  316. alphaMap: textureMapKeys
  317. };
  318. var folder = gui.addFolder( 'THREE.MeshPhysicalMaterial' );
  319. folder.addColor( data, 'color' ).onChange( handleColorChange( material.color ) );
  320. folder.addColor( data, 'emissive' ).onChange( handleColorChange( material.emissive ) );
  321. folder.add( material, 'roughness', 0, 1 );
  322. folder.add( material, 'metalness', 0, 1 );
  323. folder.add( material, 'reflectivity', 0, 1 );
  324. folder.add( material, 'clearCoat', 0, 1 ).step( 0.01 );
  325. folder.add( material, 'clearCoatRoughness', 0, 1 ).step( 0.01 );
  326. folder.add( material, 'flatShading' ).onChange( needsUpdate( material, geometry ) );
  327. folder.add( material, 'wireframe' );
  328. folder.add( material, 'wireframeLinewidth', 0, 10 );
  329. folder.add( material, 'vertexColors', constants.colors ).onChange( needsUpdate( material, geometry ) );
  330. folder.add( material, 'fog' );
  331. folder.add( data, 'envMaps', envMapKeys ).onChange( updateTexture( material, 'envMap', envMaps ) );
  332. folder.add( data, 'map', textureMapKeys ).onChange( updateTexture( material, 'map', textureMaps ) );
  333. folder.add( data, 'lightMap', textureMapKeys ).onChange( updateTexture( material, 'lightMap', textureMaps ) );
  334. folder.add( data, 'alphaMap', textureMapKeys ).onChange( updateTexture( material, 'alphaMap', textureMaps ) );
  335. // TODO roughnessMap and metalnessMap
  336. }
  337. function chooseFromHash( gui, mesh, geometry ) {
  338. var selectedMaterial = window.location.hash.substring( 1 ) || 'MeshBasicMaterial';
  339. var material;
  340. switch ( selectedMaterial ) {
  341. case 'MeshBasicMaterial' :
  342. material = new THREE.MeshBasicMaterial( { color: 0x2194CE } );
  343. guiMaterial( gui, mesh, material, geometry );
  344. guiMeshBasicMaterial( gui, mesh, material, geometry );
  345. return material;
  346. break;
  347. case 'MeshLambertMaterial' :
  348. material = new THREE.MeshLambertMaterial( { color: 0x2194CE } );
  349. guiMaterial( gui, mesh, material, geometry );
  350. guiMeshLambertMaterial( gui, mesh, material, geometry );
  351. return material;
  352. break;
  353. case 'MeshMatcapMaterial' :
  354. material = new THREE.MeshMatcapMaterial( { color: 0x2194CE, matcap: matcaps.porcelainWhite } );
  355. guiMaterial( gui, mesh, material, geometry );
  356. guiMeshMatcapMaterial( gui, mesh, material, geometry );
  357. return material;
  358. break;
  359. case 'MeshPhongMaterial' :
  360. material = new THREE.MeshPhongMaterial( { color: 0x2194CE } );
  361. guiMaterial( gui, mesh, material, geometry );
  362. guiMeshPhongMaterial( gui, mesh, material, geometry );
  363. return material;
  364. break;
  365. case 'MeshStandardMaterial' :
  366. material = new THREE.MeshStandardMaterial( { color: 0x2194CE } );
  367. guiMaterial( gui, mesh, material, geometry );
  368. guiMeshStandardMaterial( gui, mesh, material, geometry );
  369. return material;
  370. break;
  371. case 'MeshPhysicalMaterial' :
  372. material = new THREE.MeshPhysicalMaterial( { color: 0x2194CE } );
  373. guiMaterial( gui, mesh, material, geometry );
  374. guiMeshPhysicalMaterial( gui, mesh, material, geometry );
  375. return material;
  376. break;
  377. case 'MeshDepthMaterial' :
  378. material = new THREE.MeshDepthMaterial();
  379. guiMaterial( gui, mesh, material, geometry );
  380. guiMeshDepthMaterial( gui, mesh, material, geometry );
  381. return material;
  382. break;
  383. case 'MeshNormalMaterial' :
  384. material = new THREE.MeshNormalMaterial();
  385. guiMaterial( gui, mesh, material, geometry );
  386. guiMeshNormalMaterial( gui, mesh, material, geometry );
  387. return material;
  388. break;
  389. case 'LineBasicMaterial' :
  390. material = new THREE.LineBasicMaterial( { color: 0x2194CE } );
  391. guiMaterial( gui, mesh, material, geometry );
  392. guiLineBasicMaterial( gui, mesh, material, geometry );
  393. return material;
  394. break;
  395. }
  396. }