material-browser.html 23 KB

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