material-browser.html 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8">
  5. <title>Three.js Material Browser</title>
  6. <link rel="shortcut icon" href="../../files/favicon.ico" />
  7. <link rel="stylesheet" type="text/css" href="../../files/main.css">
  8. <style>
  9. canvas {
  10. display: block;
  11. width: 100%;
  12. height: 100%;
  13. }
  14. #newWindow {
  15. display: block;
  16. position: absolute;
  17. bottom: 0.3em;
  18. left: 0.5em;
  19. color: #fff;
  20. }
  21. </style>
  22. </head>
  23. <body>
  24. <script type="importmap">
  25. {
  26. "imports": {
  27. "three": "../../build/three.module.js",
  28. "three/addons/": "../../examples/jsm/"
  29. }
  30. }
  31. </script>
  32. <a id='newWindow' href='./material-browser.html' target='_blank'>Open in New Window</a>
  33. <script type="module">
  34. import * as THREE from 'three';
  35. import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
  36. import { RoomEnvironment } from 'three/addons/environments/RoomEnvironment.js';
  37. const constants = {
  38. combine: {
  39. 'THREE.MultiplyOperation': THREE.MultiplyOperation,
  40. 'THREE.MixOperation': THREE.MixOperation,
  41. 'THREE.AddOperation': THREE.AddOperation
  42. },
  43. side: {
  44. 'THREE.FrontSide': THREE.FrontSide,
  45. 'THREE.BackSide': THREE.BackSide,
  46. 'THREE.DoubleSide': THREE.DoubleSide
  47. },
  48. blendingMode: {
  49. 'THREE.NoBlending': THREE.NoBlending,
  50. 'THREE.NormalBlending': THREE.NormalBlending,
  51. 'THREE.AdditiveBlending': THREE.AdditiveBlending,
  52. 'THREE.SubtractiveBlending': THREE.SubtractiveBlending,
  53. 'THREE.MultiplyBlending': THREE.MultiplyBlending,
  54. 'THREE.CustomBlending': THREE.CustomBlending
  55. },
  56. equations: {
  57. 'THREE.AddEquation': THREE.AddEquation,
  58. 'THREE.SubtractEquation': THREE.SubtractEquation,
  59. 'THREE.ReverseSubtractEquation': THREE.ReverseSubtractEquation
  60. },
  61. destinationFactors: {
  62. 'THREE.ZeroFactor': THREE.ZeroFactor,
  63. 'THREE.OneFactor': THREE.OneFactor,
  64. 'THREE.SrcColorFactor': THREE.SrcColorFactor,
  65. 'THREE.OneMinusSrcColorFactor': THREE.OneMinusSrcColorFactor,
  66. 'THREE.SrcAlphaFactor': THREE.SrcAlphaFactor,
  67. 'THREE.OneMinusSrcAlphaFactor': THREE.OneMinusSrcAlphaFactor,
  68. 'THREE.DstAlphaFactor': THREE.DstAlphaFactor,
  69. 'THREE.OneMinusDstAlphaFactor': THREE.OneMinusDstAlphaFactor
  70. },
  71. sourceFactors: {
  72. 'THREE.DstColorFactor': THREE.DstColorFactor,
  73. 'THREE.OneMinusDstColorFactor': THREE.OneMinusDstColorFactor,
  74. 'THREE.SrcAlphaSaturateFactor': THREE.SrcAlphaSaturateFactor
  75. }
  76. };
  77. function getObjectsKeys( obj ) {
  78. const keys = [];
  79. for ( const key in obj ) {
  80. if ( obj.hasOwnProperty( key ) ) {
  81. keys.push( key );
  82. }
  83. }
  84. return keys;
  85. }
  86. const textureLoader = new THREE.TextureLoader();
  87. const cubeTextureLoader = new THREE.CubeTextureLoader();
  88. const envMaps = ( function () {
  89. const path = '../../examples/textures/cube/SwedishRoyalCastle/';
  90. const format = '.jpg';
  91. const urls = [
  92. path + 'px' + format, path + 'nx' + format,
  93. path + 'py' + format, path + 'ny' + format,
  94. path + 'pz' + format, path + 'nz' + format
  95. ];
  96. const reflectionCube = cubeTextureLoader.load( urls );
  97. const refractionCube = cubeTextureLoader.load( urls );
  98. refractionCube.mapping = THREE.CubeRefractionMapping;
  99. return {
  100. none: null,
  101. reflection: reflectionCube,
  102. refraction: refractionCube
  103. };
  104. } )();
  105. const diffuseMaps = ( function () {
  106. const bricks = textureLoader.load( '../../examples/textures/brick_diffuse.jpg' );
  107. bricks.wrapS = THREE.RepeatWrapping;
  108. bricks.wrapT = THREE.RepeatWrapping;
  109. bricks.repeat.set( 9, 1 );
  110. return {
  111. none: null,
  112. bricks: bricks
  113. };
  114. } )();
  115. const roughnessMaps = ( function () {
  116. const bricks = textureLoader.load( '../../examples/textures/brick_roughness.jpg' );
  117. bricks.wrapT = THREE.RepeatWrapping;
  118. bricks.wrapS = THREE.RepeatWrapping;
  119. bricks.repeat.set( 9, 1 );
  120. return {
  121. none: null,
  122. bricks: bricks
  123. };
  124. } )();
  125. const matcaps = ( function () {
  126. return {
  127. none: null,
  128. porcelainWhite: textureLoader.load( '../../examples/textures/matcaps/matcap-porcelain-white.jpg' )
  129. };
  130. } )();
  131. const alphaMaps = ( function () {
  132. const fibers = textureLoader.load( '../../examples/textures/alphaMap.jpg' );
  133. fibers.wrapT = THREE.RepeatWrapping;
  134. fibers.wrapS = THREE.RepeatWrapping;
  135. fibers.repeat.set( 9, 1 );
  136. return {
  137. none: null,
  138. fibers: fibers
  139. };
  140. } )();
  141. const gradientMaps = ( function () {
  142. const threeTone = textureLoader.load( '../../examples/textures/gradientMaps/threeTone.jpg' );
  143. threeTone.minFilter = THREE.NearestFilter;
  144. threeTone.magFilter = THREE.NearestFilter;
  145. const fiveTone = textureLoader.load( '../../examples/textures/gradientMaps/fiveTone.jpg' );
  146. fiveTone.minFilter = THREE.NearestFilter;
  147. fiveTone.magFilter = THREE.NearestFilter;
  148. return {
  149. none: null,
  150. threeTone: threeTone,
  151. fiveTone: fiveTone
  152. };
  153. } )();
  154. const envMapKeys = getObjectsKeys( envMaps );
  155. const envMapKeysPBR = envMapKeys.slice( 0, 2 );
  156. const diffuseMapKeys = getObjectsKeys( diffuseMaps );
  157. const roughnessMapKeys = getObjectsKeys( roughnessMaps );
  158. const matcapKeys = getObjectsKeys( matcaps );
  159. const alphaMapKeys = getObjectsKeys( alphaMaps );
  160. const gradientMapKeys = getObjectsKeys( gradientMaps );
  161. function generateVertexColors( geometry ) {
  162. const positionAttribute = geometry.attributes.position;
  163. const colors = [];
  164. const color = new THREE.Color();
  165. for ( let i = 0, il = positionAttribute.count; i < il; i ++ ) {
  166. color.setHSL( i / il * Math.random(), 0.5, 0.5 );
  167. colors.push( color.r, color.g, color.b );
  168. }
  169. geometry.setAttribute( 'color', new THREE.Float32BufferAttribute( colors, 3 ) );
  170. }
  171. function handleColorChange( color ) {
  172. return function ( value ) {
  173. if ( typeof value === 'string' ) {
  174. value = value.replace( '#', '0x' );
  175. }
  176. color.setHex( value );
  177. };
  178. }
  179. function needsUpdate( material, geometry ) {
  180. return function () {
  181. material.side = parseInt( material.side ); //Ensure number
  182. material.needsUpdate = true;
  183. geometry.attributes.position.needsUpdate = true;
  184. geometry.attributes.normal.needsUpdate = true;
  185. geometry.attributes.color.needsUpdate = true;
  186. };
  187. }
  188. function updateCombine( material ) {
  189. return function ( combine ) {
  190. material.combine = parseInt( combine );
  191. material.needsUpdate = true;
  192. };
  193. }
  194. function updateTexture( material, materialKey, textures ) {
  195. return function ( key ) {
  196. material[ materialKey ] = textures[ key ];
  197. material.needsUpdate = true;
  198. };
  199. }
  200. function guiScene( gui, scene ) {
  201. const folder = gui.addFolder( 'Scene' );
  202. const data = {
  203. background: '#000000',
  204. 'ambient light': ambientLight.color.getHex()
  205. };
  206. folder.addColor( data, 'ambient light' ).onChange( handleColorChange( ambientLight.color ) );
  207. guiSceneFog( folder, scene );
  208. }
  209. function guiSceneFog( folder, scene ) {
  210. const fogFolder = folder.addFolder( 'scene.fog' );
  211. const fog = new THREE.Fog( 0x3f7b9d, 0, 60 );
  212. const data = {
  213. fog: {
  214. 'THREE.Fog()': false,
  215. 'scene.fog.color': fog.color.getHex()
  216. }
  217. };
  218. fogFolder.add( data.fog, 'THREE.Fog()' ).onChange( function ( useFog ) {
  219. if ( useFog ) {
  220. scene.fog = fog;
  221. } else {
  222. scene.fog = null;
  223. }
  224. } );
  225. fogFolder.addColor( data.fog, 'scene.fog.color' ).onChange( handleColorChange( fog.color ) );
  226. }
  227. function guiMaterial( gui, mesh, material, geometry ) {
  228. const folder = gui.addFolder( 'THREE.Material' );
  229. folder.add( material, 'transparent' ).onChange( needsUpdate( material, geometry ) );
  230. folder.add( material, 'opacity', 0, 1 ).step( 0.01 );
  231. // folder.add( material, 'blending', constants.blendingMode );
  232. // folder.add( material, 'blendSrc', constants.destinationFactors );
  233. // folder.add( material, 'blendDst', constants.destinationFactors );
  234. // folder.add( material, 'blendEquation', constants.equations );
  235. folder.add( material, 'depthTest' );
  236. folder.add( material, 'depthWrite' );
  237. // folder.add( material, 'polygonOffset' );
  238. // folder.add( material, 'polygonOffsetFactor' );
  239. // folder.add( material, 'polygonOffsetUnits' );
  240. folder.add( material, 'alphaTest', 0, 1 ).step( 0.01 ).onChange( needsUpdate( material, geometry ) );
  241. folder.add( material, 'visible' );
  242. folder.add( material, 'side', constants.side ).onChange( needsUpdate( material, geometry ) );
  243. }
  244. function guiMeshBasicMaterial( gui, mesh, material, geometry ) {
  245. const data = {
  246. color: material.color.getHex(),
  247. envMaps: envMapKeys[ 0 ],
  248. map: diffuseMapKeys[ 0 ],
  249. alphaMap: alphaMapKeys[ 0 ]
  250. };
  251. const folder = gui.addFolder( 'THREE.MeshBasicMaterial' );
  252. folder.addColor( data, 'color' ).onChange( handleColorChange( material.color ) );
  253. folder.add( material, 'wireframe' );
  254. folder.add( material, 'vertexColors' ).onChange( needsUpdate( material, geometry ) );
  255. folder.add( material, 'fog' ).onChange( needsUpdate( material, geometry ) );
  256. folder.add( data, 'envMaps', envMapKeys ).onChange( updateTexture( material, 'envMap', envMaps ) );
  257. folder.add( data, 'map', diffuseMapKeys ).onChange( updateTexture( material, 'map', diffuseMaps ) );
  258. folder.add( data, 'alphaMap', alphaMapKeys ).onChange( updateTexture( material, 'alphaMap', alphaMaps ) );
  259. folder.add( material, 'combine', constants.combine ).onChange( updateCombine( material ) );
  260. folder.add( material, 'reflectivity', 0, 1 );
  261. folder.add( material, 'refractionRatio', 0, 1 );
  262. }
  263. function guiMeshDepthMaterial( gui, mesh, material ) {
  264. const data = {
  265. alphaMap: alphaMapKeys[ 0 ]
  266. };
  267. const folder = gui.addFolder( 'THREE.MeshDepthMaterial' );
  268. folder.add( material, 'wireframe' );
  269. folder.add( data, 'alphaMap', alphaMapKeys ).onChange( updateTexture( material, 'alphaMap', alphaMaps ) );
  270. }
  271. function guiMeshNormalMaterial( gui, mesh, material, geometry ) {
  272. const folder = gui.addFolder( 'THREE.MeshNormalMaterial' );
  273. folder.add( material, 'flatShading' ).onChange( needsUpdate( material, geometry ) );
  274. folder.add( material, 'wireframe' );
  275. }
  276. function guiLineBasicMaterial( gui, mesh, material, geometry ) {
  277. const data = {
  278. color: material.color.getHex()
  279. };
  280. const folder = gui.addFolder( 'THREE.LineBasicMaterial' );
  281. folder.addColor( data, 'color' ).onChange( handleColorChange( material.color ) );
  282. folder.add( material, 'linewidth', 0, 10 );
  283. folder.add( material, 'linecap', [ 'butt', 'round', 'square' ] );
  284. folder.add( material, 'linejoin', [ 'round', 'bevel', 'miter' ] );
  285. folder.add( material, 'vertexColors' ).onChange( needsUpdate( material, geometry ) );
  286. folder.add( material, 'fog' ).onChange( needsUpdate( material, geometry ) );
  287. }
  288. function guiMeshLambertMaterial( gui, mesh, material, geometry ) {
  289. const data = {
  290. color: material.color.getHex(),
  291. emissive: material.emissive.getHex(),
  292. envMaps: envMapKeys[ 0 ],
  293. map: diffuseMapKeys[ 0 ],
  294. alphaMap: alphaMapKeys[ 0 ]
  295. };
  296. const folder = gui.addFolder( 'THREE.MeshLambertMaterial' );
  297. folder.addColor( data, 'color' ).onChange( handleColorChange( material.color ) );
  298. folder.addColor( data, 'emissive' ).onChange( handleColorChange( material.emissive ) );
  299. folder.add( material, 'wireframe' );
  300. folder.add( material, 'vertexColors' ).onChange( needsUpdate( material, geometry ) );
  301. folder.add( material, 'fog' ).onChange( needsUpdate( material, geometry ) );
  302. folder.add( data, 'envMaps', envMapKeys ).onChange( updateTexture( material, 'envMap', envMaps ) );
  303. folder.add( data, 'map', diffuseMapKeys ).onChange( updateTexture( material, 'map', diffuseMaps ) );
  304. folder.add( data, 'alphaMap', alphaMapKeys ).onChange( updateTexture( material, 'alphaMap', alphaMaps ) );
  305. folder.add( material, 'combine', constants.combine ).onChange( updateCombine( material ) );
  306. folder.add( material, 'reflectivity', 0, 1 );
  307. folder.add( material, 'refractionRatio', 0, 1 );
  308. }
  309. function guiMeshMatcapMaterial( gui, mesh, material, geometry ) {
  310. const data = {
  311. color: material.color.getHex(),
  312. matcap: matcapKeys[ 1 ],
  313. alphaMap: alphaMapKeys[ 0 ]
  314. };
  315. const folder = gui.addFolder( 'THREE.MeshMatcapMaterial' );
  316. folder.addColor( data, 'color' ).onChange( handleColorChange( material.color ) );
  317. folder.add( material, 'flatShading' ).onChange( needsUpdate( material, geometry ) );
  318. folder.add( data, 'matcap', matcapKeys ).onChange( updateTexture( material, 'matcap', matcaps ) );
  319. folder.add( data, 'alphaMap', alphaMapKeys ).onChange( updateTexture( material, 'alphaMap', alphaMaps ) );
  320. }
  321. function guiMeshPhongMaterial( gui, mesh, material, geometry ) {
  322. const data = {
  323. color: material.color.getHex(),
  324. emissive: material.emissive.getHex(),
  325. specular: material.specular.getHex(),
  326. envMaps: envMapKeys[ 0 ],
  327. map: diffuseMapKeys[ 0 ],
  328. alphaMap: alphaMapKeys[ 0 ]
  329. };
  330. const folder = gui.addFolder( 'THREE.MeshPhongMaterial' );
  331. folder.addColor( data, 'color' ).onChange( handleColorChange( material.color ) );
  332. folder.addColor( data, 'emissive' ).onChange( handleColorChange( material.emissive ) );
  333. folder.addColor( data, 'specular' ).onChange( handleColorChange( material.specular ) );
  334. folder.add( material, 'shininess', 0, 100 );
  335. folder.add( material, 'flatShading' ).onChange( needsUpdate( material, geometry ) );
  336. folder.add( material, 'wireframe' );
  337. folder.add( material, 'vertexColors' ).onChange( needsUpdate( material, geometry ) );
  338. folder.add( material, 'fog' ).onChange( needsUpdate( material, geometry ) );
  339. folder.add( data, 'envMaps', envMapKeys ).onChange( updateTexture( material, 'envMap', envMaps ) );
  340. folder.add( data, 'map', diffuseMapKeys ).onChange( updateTexture( material, 'map', diffuseMaps ) );
  341. folder.add( data, 'alphaMap', alphaMapKeys ).onChange( updateTexture( material, 'alphaMap', alphaMaps ) );
  342. folder.add( material, 'combine', constants.combine ).onChange( updateCombine( material ) );
  343. folder.add( material, 'reflectivity', 0, 1 );
  344. folder.add( material, 'refractionRatio', 0, 1 );
  345. }
  346. function guiMeshToonMaterial( gui, mesh, material ) {
  347. const data = {
  348. color: material.color.getHex(),
  349. map: diffuseMapKeys[ 0 ],
  350. gradientMap: gradientMapKeys[ 1 ],
  351. alphaMap: alphaMapKeys[ 0 ]
  352. };
  353. const folder = gui.addFolder( 'THREE.MeshToonMaterial' );
  354. folder.addColor( data, 'color' ).onChange( handleColorChange( material.color ) );
  355. folder.add( data, 'map', diffuseMapKeys ).onChange( updateTexture( material, 'map', diffuseMaps ) );
  356. folder.add( data, 'gradientMap', gradientMapKeys ).onChange( updateTexture( material, 'gradientMap', gradientMaps ) );
  357. folder.add( data, 'alphaMap', alphaMapKeys ).onChange( updateTexture( material, 'alphaMap', alphaMaps ) );
  358. }
  359. function guiMeshStandardMaterial( gui, mesh, material, geometry ) {
  360. const data = {
  361. color: material.color.getHex(),
  362. emissive: material.emissive.getHex(),
  363. envMaps: envMapKeysPBR[ 0 ],
  364. map: diffuseMapKeys[ 0 ],
  365. roughnessMap: roughnessMapKeys[ 0 ],
  366. alphaMap: alphaMapKeys[ 0 ],
  367. metalnessMap: alphaMapKeys[ 0 ]
  368. };
  369. const folder = gui.addFolder( 'THREE.MeshStandardMaterial' );
  370. folder.addColor( data, 'color' ).onChange( handleColorChange( material.color ) );
  371. folder.addColor( data, 'emissive' ).onChange( handleColorChange( material.emissive ) );
  372. folder.add( material, 'roughness', 0, 1 );
  373. folder.add( material, 'metalness', 0, 1 );
  374. folder.add( material, 'flatShading' ).onChange( needsUpdate( material, geometry ) );
  375. folder.add( material, 'wireframe' );
  376. folder.add( material, 'vertexColors' ).onChange( needsUpdate( material, geometry ) );
  377. folder.add( material, 'fog' ).onChange( needsUpdate( material, geometry ) );
  378. folder.add( data, 'envMaps', envMapKeysPBR ).onChange( updateTexture( material, 'envMap', envMaps ) );
  379. folder.add( data, 'map', diffuseMapKeys ).onChange( updateTexture( material, 'map', diffuseMaps ) );
  380. folder.add( data, 'roughnessMap', roughnessMapKeys ).onChange( updateTexture( material, 'roughnessMap', roughnessMaps ) );
  381. folder.add( data, 'alphaMap', alphaMapKeys ).onChange( updateTexture( material, 'alphaMap', alphaMaps ) );
  382. folder.add( data, 'metalnessMap', alphaMapKeys ).onChange( updateTexture( material, 'metalnessMap', alphaMaps ) );
  383. }
  384. function guiMeshPhysicalMaterial( gui, mesh, material, geometry ) {
  385. const data = {
  386. color: material.color.getHex(),
  387. emissive: material.emissive.getHex(),
  388. envMaps: envMapKeys[ 0 ],
  389. map: diffuseMapKeys[ 0 ],
  390. roughnessMap: roughnessMapKeys[ 0 ],
  391. alphaMap: alphaMapKeys[ 0 ],
  392. metalnessMap: alphaMapKeys[ 0 ],
  393. sheenColor: material.sheenColor.getHex(),
  394. specularColor: material.specularColor.getHex(),
  395. iridescenceMap: alphaMapKeys[ 0 ]
  396. };
  397. const folder = gui.addFolder( 'THREE.MeshPhysicalMaterial' );
  398. folder.addColor( data, 'color' ).onChange( handleColorChange( material.color ) );
  399. folder.addColor( data, 'emissive' ).onChange( handleColorChange( material.emissive ) );
  400. folder.add( material, 'roughness', 0, 1 );
  401. folder.add( material, 'metalness', 0, 1 );
  402. folder.add( material, 'ior', 1, 2.333 );
  403. folder.add( material, 'reflectivity', 0, 1 );
  404. folder.add( material, 'iridescence', 0, 1 );
  405. folder.add( material, 'iridescenceIOR', 1, 2.333 );
  406. folder.add( material, 'sheen', 0, 1 );
  407. folder.add( material, 'sheenRoughness', 0, 1 );
  408. folder.addColor( data, 'sheenColor' ).onChange( handleColorChange( material.sheenColor ) );
  409. folder.add( material, 'clearcoat', 0, 1 ).step( 0.01 );
  410. folder.add( material, 'clearcoatRoughness', 0, 1 ).step( 0.01 );
  411. folder.add( material, 'specularIntensity', 0, 1);
  412. folder.addColor( data, 'specularColor' ).onChange( handleColorChange( material.specularColor ) );
  413. folder.add( material, 'flatShading' ).onChange( needsUpdate( material, geometry ) );
  414. folder.add( material, 'wireframe' );
  415. folder.add( material, 'vertexColors' ).onChange( needsUpdate( material, geometry ) );
  416. folder.add( material, 'fog' ).onChange( needsUpdate( material, geometry ) );
  417. folder.add( data, 'envMaps', envMapKeysPBR ).onChange( updateTexture( material, 'envMap', envMaps ) );
  418. folder.add( data, 'map', diffuseMapKeys ).onChange( updateTexture( material, 'map', diffuseMaps ) );
  419. folder.add( data, 'roughnessMap', roughnessMapKeys ).onChange( updateTexture( material, 'roughnessMap', roughnessMaps ) );
  420. folder.add( data, 'alphaMap', alphaMapKeys ).onChange( updateTexture( material, 'alphaMap', alphaMaps ) );
  421. folder.add( data, 'metalnessMap', alphaMapKeys ).onChange( updateTexture( material, 'metalnessMap', alphaMaps ) );
  422. folder.add( data, 'iridescenceMap', alphaMapKeys ).onChange( updateTexture( material, 'iridescenceMap', alphaMaps ) );
  423. }
  424. function chooseFromHash( gui, mesh, geometry ) {
  425. const selectedMaterial = window.location.hash.substring( 1 ) || 'MeshBasicMaterial';
  426. let material;
  427. switch ( selectedMaterial ) {
  428. case 'MeshBasicMaterial' :
  429. material = new THREE.MeshBasicMaterial( { color: 0x049EF4 } );
  430. guiMaterial( gui, mesh, material, geometry );
  431. guiMeshBasicMaterial( gui, mesh, material, geometry );
  432. return material;
  433. break;
  434. case 'MeshLambertMaterial' :
  435. material = new THREE.MeshLambertMaterial( { color: 0x049EF4 } );
  436. guiMaterial( gui, mesh, material, geometry );
  437. guiMeshLambertMaterial( gui, mesh, material, geometry );
  438. return material;
  439. break;
  440. case 'MeshMatcapMaterial' :
  441. material = new THREE.MeshMatcapMaterial( { matcap: matcaps.porcelainWhite } );
  442. guiMaterial( gui, mesh, material, geometry );
  443. guiMeshMatcapMaterial( gui, mesh, material, geometry );
  444. // no need for lights
  445. light1.visible = false;
  446. light2.visible = false;
  447. light3.visible = false;
  448. return material;
  449. break;
  450. case 'MeshPhongMaterial' :
  451. material = new THREE.MeshPhongMaterial( { color: 0x049EF4 } );
  452. guiMaterial( gui, mesh, material, geometry );
  453. guiMeshPhongMaterial( gui, mesh, material, geometry );
  454. return material;
  455. break;
  456. case 'MeshToonMaterial' :
  457. material = new THREE.MeshToonMaterial( { color: 0x049EF4, gradientMap: gradientMaps.threeTone } );
  458. guiMaterial( gui, mesh, material, geometry );
  459. guiMeshToonMaterial( gui, mesh, material );
  460. // only use a single point light
  461. light1.visible = false;
  462. light3.visible = false;
  463. return material;
  464. break;
  465. case 'MeshStandardMaterial' :
  466. material = new THREE.MeshStandardMaterial( { color: 0x049EF4 } );
  467. guiMaterial( gui, mesh, material, geometry );
  468. guiMeshStandardMaterial( gui, mesh, material, geometry );
  469. // only use scene environment
  470. light1.visible = false;
  471. light2.visible = false;
  472. light3.visible = false;
  473. return material;
  474. break;
  475. case 'MeshPhysicalMaterial' :
  476. material = new THREE.MeshPhysicalMaterial( { color: 0x049EF4 } );
  477. guiMaterial( gui, mesh, material, geometry );
  478. guiMeshPhysicalMaterial( gui, mesh, material, geometry );
  479. // only use scene environment
  480. light1.visible = false;
  481. light2.visible = false;
  482. light3.visible = false;
  483. return material;
  484. break;
  485. case 'MeshDepthMaterial' :
  486. material = new THREE.MeshDepthMaterial();
  487. guiMaterial( gui, mesh, material, geometry );
  488. guiMeshDepthMaterial( gui, mesh, material );
  489. return material;
  490. break;
  491. case 'MeshNormalMaterial' :
  492. material = new THREE.MeshNormalMaterial();
  493. guiMaterial( gui, mesh, material, geometry );
  494. guiMeshNormalMaterial( gui, mesh, material, geometry );
  495. return material;
  496. break;
  497. case 'LineBasicMaterial' :
  498. material = new THREE.LineBasicMaterial( { color: 0x2194CE } );
  499. guiMaterial( gui, mesh, material, geometry );
  500. guiLineBasicMaterial( gui, mesh, material, geometry );
  501. return material;
  502. break;
  503. }
  504. }
  505. //
  506. const selectedMaterial = window.location.hash.substring( 1 );
  507. if ( THREE[ selectedMaterial ] !== undefined ) {
  508. document.getElementById( 'newWindow' ).href += '#' + selectedMaterial;
  509. }
  510. const gui = new GUI();
  511. const renderer = new THREE.WebGLRenderer( { antialias: true } );
  512. renderer.setPixelRatio( window.devicePixelRatio );
  513. renderer.setSize( window.innerWidth, window.innerHeight );
  514. document.body.appendChild( renderer.domElement );
  515. const pmremGenerator = new THREE.PMREMGenerator( renderer );
  516. const scene = new THREE.Scene();
  517. scene.background = new THREE.Color( 0x444444 );
  518. scene.environment = pmremGenerator.fromScene( new RoomEnvironment(), 0.04 ).texture;
  519. const camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 10, 100 );
  520. camera.position.z = 35;
  521. const ambientLight = new THREE.AmbientLight( 0x000000 );
  522. scene.add( ambientLight );
  523. const light1 = new THREE.DirectionalLight( 0xffffff, 3 );
  524. light1.position.set( 0, 200, 0 );
  525. scene.add( light1 );
  526. const light2 = new THREE.DirectionalLight( 0xffffff, 3 );
  527. light2.position.set( 100, 200, 100 );
  528. scene.add( light2 );
  529. const light3 = new THREE.DirectionalLight( 0xffffff, 3 );
  530. light3.position.set( - 100, - 200, - 100 );
  531. scene.add( light3 );
  532. guiScene( gui, scene );
  533. const geometry = new THREE.TorusKnotGeometry( 10, 3, 200, 32 ).toNonIndexed();
  534. generateVertexColors( geometry );
  535. const mesh = new THREE.Mesh( geometry );
  536. mesh.material = chooseFromHash( gui, mesh, geometry );
  537. scene.add( mesh );
  538. let prevFog = false;
  539. function render() {
  540. requestAnimationFrame( render );
  541. mesh.rotation.x += 0.005;
  542. mesh.rotation.y += 0.005;
  543. if ( prevFog !== scene.fog ) {
  544. mesh.material.needsUpdate = true;
  545. prevFog = scene.fog;
  546. }
  547. renderer.render( scene, camera );
  548. }
  549. window.addEventListener( 'resize', function () {
  550. camera.aspect = window.innerWidth / window.innerHeight;
  551. camera.updateProjectionMatrix();
  552. renderer.setSize( window.innerWidth, window.innerHeight );
  553. }, false );
  554. render();
  555. </script>
  556. </body>
  557. </html>