material-browser.html 23 KB

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