2
0

material-browser.html 23 KB

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