material-browser.html 23 KB

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