material.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588
  1. /**
  2. * @author TatumCreative (Greg Tatum) / http://gregtatum.com/
  3. */
  4. var constants = {
  5. combine: {
  6. 'THREE.MultiplyOperation': THREE.MultiplyOperation,
  7. 'THREE.MixOperation': THREE.MixOperation,
  8. 'THREE.AddOperation': THREE.AddOperation
  9. },
  10. side: {
  11. 'THREE.FrontSide': THREE.FrontSide,
  12. 'THREE.BackSide': THREE.BackSide,
  13. 'THREE.DoubleSide': THREE.DoubleSide
  14. },
  15. colors: {
  16. 'THREE.NoColors': THREE.NoColors,
  17. 'THREE.VertexColors': THREE.VertexColors
  18. },
  19. blendingMode: {
  20. 'THREE.NoBlending': THREE.NoBlending,
  21. 'THREE.NormalBlending': THREE.NormalBlending,
  22. 'THREE.AdditiveBlending': THREE.AdditiveBlending,
  23. 'THREE.SubtractiveBlending': THREE.SubtractiveBlending,
  24. 'THREE.MultiplyBlending': THREE.MultiplyBlending,
  25. 'THREE.CustomBlending': THREE.CustomBlending
  26. },
  27. equations: {
  28. 'THREE.AddEquation': THREE.AddEquation,
  29. 'THREE.SubtractEquation': THREE.SubtractEquation,
  30. 'THREE.ReverseSubtractEquation': THREE.ReverseSubtractEquation
  31. },
  32. destinationFactors: {
  33. 'THREE.ZeroFactor': THREE.ZeroFactor,
  34. 'THREE.OneFactor': THREE.OneFactor,
  35. 'THREE.SrcColorFactor': THREE.SrcColorFactor,
  36. 'THREE.OneMinusSrcColorFactor': THREE.OneMinusSrcColorFactor,
  37. 'THREE.SrcAlphaFactor': THREE.SrcAlphaFactor,
  38. 'THREE.OneMinusSrcAlphaFactor': THREE.OneMinusSrcAlphaFactor,
  39. 'THREE.DstAlphaFactor': THREE.DstAlphaFactor,
  40. 'THREE.OneMinusDstAlphaFactor': THREE.OneMinusDstAlphaFactor
  41. },
  42. sourceFactors: {
  43. 'THREE.DstColorFactor': THREE.DstColorFactor,
  44. 'THREE.OneMinusDstColorFactor': THREE.OneMinusDstColorFactor,
  45. 'THREE.SrcAlphaSaturateFactor': THREE.SrcAlphaSaturateFactor
  46. }
  47. };
  48. function getObjectsKeys( obj ) {
  49. var keys = [];
  50. for ( var key in obj ) {
  51. if ( obj.hasOwnProperty( key ) ) {
  52. keys.push( key );
  53. }
  54. }
  55. return keys;
  56. }
  57. var envMaps = ( function () {
  58. var path = '../../examples/textures/cube/SwedishRoyalCastle/';
  59. var format = '.jpg';
  60. var urls = [
  61. path + 'px' + format, path + 'nx' + format,
  62. path + 'py' + format, path + 'ny' + format,
  63. path + 'pz' + format, path + 'nz' + format
  64. ];
  65. var reflectionCube = new THREE.CubeTextureLoader().load( urls );
  66. reflectionCube.format = THREE.RGBFormat;
  67. var refractionCube = new THREE.CubeTextureLoader().load( urls );
  68. refractionCube.mapping = THREE.CubeRefractionMapping;
  69. refractionCube.format = THREE.RGBFormat;
  70. return {
  71. none: null,
  72. reflection: reflectionCube,
  73. refraction: refractionCube
  74. };
  75. } )();
  76. var envMapKeys = getObjectsKeys( envMaps );
  77. var textureMaps = ( function () {
  78. return {
  79. none: null,
  80. grass: new THREE.TextureLoader().load( '../../examples/textures/terrain/grasslight-thin.jpg' )
  81. };
  82. } )();
  83. var textureMapKeys = getObjectsKeys( textureMaps );
  84. var matcaps = ( function () {
  85. return {
  86. none: null,
  87. porcelainWhite: new THREE.TextureLoader().load( '../../examples/textures/matcaps/matcap-porcelain-white.jpg' )
  88. };
  89. } )();
  90. var matcapKeys = getObjectsKeys( matcaps );
  91. function generateVertexColors( geometry ) {
  92. var positionAttribute = geometry.attributes.position;
  93. var colors = [];
  94. var color = new THREE.Color();
  95. for ( var i = 0, il = positionAttribute.count; i < il; i ++ ) {
  96. color.setHSL( i / il * Math.random(), 0.5, 0.5 );
  97. colors.push( color.r, color.g, color.b );
  98. }
  99. geometry.addAttribute( 'color', new THREE.Float32BufferAttribute( colors, 3 ) );
  100. }
  101. function handleColorChange( color ) {
  102. return function ( value ) {
  103. if ( typeof value === 'string' ) {
  104. value = value.replace( '#', '0x' );
  105. }
  106. color.setHex( value );
  107. };
  108. }
  109. function needsUpdate( material, geometry ) {
  110. return function () {
  111. material.vertexColors = parseInt( material.vertexColors ); //Ensure number
  112. material.side = parseInt( material.side ); //Ensure number
  113. material.needsUpdate = true;
  114. geometry.attributes.position.needsUpdate = true;
  115. geometry.attributes.normal.needsUpdate = true;
  116. geometry.attributes.color.needsUpdate = true;
  117. };
  118. }
  119. function updateTexture( material, materialKey, textures ) {
  120. return function ( key ) {
  121. material[ materialKey ] = textures[ key ];
  122. material.needsUpdate = true;
  123. };
  124. }
  125. function guiScene( gui, scene ) {
  126. var folder = gui.addFolder( 'Scene' );
  127. var data = {
  128. background: '#000000',
  129. 'ambient light': ambientLight.color.getHex()
  130. };
  131. var color = new THREE.Color();
  132. var colorConvert = handleColorChange( color );
  133. folder.addColor( data, 'background' ).onChange( function ( value ) {
  134. colorConvert( value );
  135. renderer.setClearColor( color.getHex() );
  136. } );
  137. folder.addColor( data, 'ambient light' ).onChange( handleColorChange( ambientLight.color ) );
  138. guiSceneFog( folder, scene );
  139. }
  140. function guiSceneFog( folder, scene ) {
  141. var fogFolder = folder.addFolder( 'scene.fog' );
  142. var fog = new THREE.Fog( 0x3f7b9d, 0, 60 );
  143. var data = {
  144. fog: {
  145. 'THREE.Fog()': false,
  146. 'scene.fog.color': fog.color.getHex()
  147. }
  148. };
  149. fogFolder.add( data.fog, 'THREE.Fog()' ).onChange( function ( useFog ) {
  150. if ( useFog ) {
  151. scene.fog = fog;
  152. } else {
  153. scene.fog = null;
  154. }
  155. } );
  156. fogFolder.addColor( data.fog, 'scene.fog.color' ).onChange( handleColorChange( fog.color ) );
  157. }
  158. function guiMaterial( gui, mesh, material, geometry ) {
  159. var folder = gui.addFolder( 'THREE.Material' );
  160. folder.add( material, 'transparent' );
  161. folder.add( material, 'opacity', 0, 1 );
  162. // folder.add( material, 'blending', constants.blendingMode );
  163. // folder.add( material, 'blendSrc', constants.destinationFactors );
  164. // folder.add( material, 'blendDst', constants.destinationFactors );
  165. // folder.add( material, 'blendEquation', constants.equations );
  166. folder.add( material, 'depthTest' );
  167. folder.add( material, 'depthWrite' );
  168. // folder.add( material, 'polygonOffset' );
  169. // folder.add( material, 'polygonOffsetFactor' );
  170. // folder.add( material, 'polygonOffsetUnits' );
  171. folder.add( material, 'alphaTest', 0, 1 );
  172. folder.add( material, 'visible' );
  173. folder.add( material, 'side', constants.side ).onChange( needsUpdate( material, geometry ) );
  174. }
  175. function guiMeshBasicMaterial( gui, mesh, material, geometry ) {
  176. var data = {
  177. color: material.color.getHex(),
  178. envMaps: envMapKeys,
  179. map: textureMapKeys,
  180. specularMap: textureMapKeys,
  181. alphaMap: textureMapKeys
  182. };
  183. var folder = gui.addFolder( 'THREE.MeshBasicMaterial' );
  184. folder.addColor( data, 'color' ).onChange( handleColorChange( material.color ) );
  185. folder.add( material, 'wireframe' );
  186. folder.add( material, 'wireframeLinewidth', 0, 10 );
  187. folder.add( material, 'vertexColors', constants.colors ).onChange( needsUpdate( material, geometry ) );
  188. folder.add( material, 'fog' );
  189. folder.add( data, 'envMaps', envMapKeys ).onChange( updateTexture( material, 'envMap', envMaps ) );
  190. folder.add( data, 'map', textureMapKeys ).onChange( updateTexture( material, 'map', textureMaps ) );
  191. folder.add( data, 'specularMap', textureMapKeys ).onChange( updateTexture( material, 'specularMap', textureMaps ) );
  192. folder.add( data, 'alphaMap', textureMapKeys ).onChange( updateTexture( material, 'alphaMap', textureMaps ) );
  193. folder.add( material, 'combine', constants.combine );
  194. folder.add( material, 'reflectivity', 0, 1 );
  195. folder.add( material, 'refractionRatio', 0, 1 );
  196. }
  197. function guiMeshDepthMaterial( gui, mesh, material, geometry ) {
  198. var folder = gui.addFolder( 'THREE.MeshDepthMaterial' );
  199. folder.add( material, 'wireframe' );
  200. folder.add( material, 'wireframeLinewidth', 0, 10 );
  201. }
  202. function guiMeshNormalMaterial( gui, mesh, material, geometry ) {
  203. var folder = gui.addFolder( 'THREE.MeshNormalMaterial' );
  204. folder.add( material, 'flatShading' ).onChange( needsUpdate( material, geometry ) );
  205. folder.add( material, 'wireframe' );
  206. folder.add( material, 'wireframeLinewidth', 0, 10 );
  207. }
  208. function guiLineBasicMaterial( gui, mesh, material, geometry ) {
  209. var data = {
  210. color: material.color.getHex()
  211. };
  212. var folder = gui.addFolder( 'THREE.LineBasicMaterial' );
  213. folder.addColor( data, 'color' ).onChange( handleColorChange( material.color ) );
  214. folder.add( material, 'linewidth', 0, 10 );
  215. folder.add( material, 'linecap', [ 'butt', 'round', 'square' ] );
  216. folder.add( material, 'linejoin', [ 'round', 'bevel', 'miter' ] );
  217. folder.add( material, 'vertexColors', constants.colors ).onChange( needsUpdate( material, geometry ) );
  218. folder.add( material, 'fog' );
  219. }
  220. function guiMeshLambertMaterial( gui, mesh, material, geometry ) {
  221. var data = {
  222. color: material.color.getHex(),
  223. emissive: material.emissive.getHex(),
  224. envMaps: envMapKeys,
  225. map: textureMapKeys,
  226. specularMap: textureMapKeys,
  227. alphaMap: textureMapKeys
  228. };
  229. var folder = gui.addFolder( 'THREE.MeshLambertMaterial' );
  230. folder.addColor( data, 'color' ).onChange( handleColorChange( material.color ) );
  231. folder.addColor( data, 'emissive' ).onChange( handleColorChange( material.emissive ) );
  232. folder.add( material, 'wireframe' );
  233. folder.add( material, 'wireframeLinewidth', 0, 10 );
  234. folder.add( material, 'vertexColors', constants.colors ).onChange( needsUpdate( material, geometry ) );
  235. folder.add( material, 'fog' );
  236. folder.add( data, 'envMaps', envMapKeys ).onChange( updateTexture( material, 'envMap', envMaps ) );
  237. folder.add( data, 'map', textureMapKeys ).onChange( updateTexture( material, 'map', textureMaps ) );
  238. folder.add( data, 'specularMap', textureMapKeys ).onChange( updateTexture( material, 'specularMap', textureMaps ) );
  239. folder.add( data, 'alphaMap', textureMapKeys ).onChange( updateTexture( material, 'alphaMap', textureMaps ) );
  240. folder.add( material, 'combine', constants.combine );
  241. folder.add( material, 'reflectivity', 0, 1 );
  242. folder.add( material, 'refractionRatio', 0, 1 );
  243. }
  244. function guiMeshMatcapMaterial( gui, mesh, material ) {
  245. var data = {
  246. color: material.color.getHex(),
  247. matcap: matcapKeys[ 1 ]
  248. };
  249. var folder = gui.addFolder( 'THREE.MeshMatcapMaterial' );
  250. folder.addColor( data, 'color' ).onChange( handleColorChange( material.color ) );
  251. folder.add( data, 'matcap', matcapKeys ).onChange( updateTexture( material, 'matcap', matcaps ) );
  252. }
  253. function guiMeshPhongMaterial( gui, mesh, material, geometry ) {
  254. var data = {
  255. color: material.color.getHex(),
  256. emissive: material.emissive.getHex(),
  257. specular: material.specular.getHex(),
  258. envMaps: envMapKeys,
  259. map: textureMapKeys,
  260. lightMap: textureMapKeys,
  261. specularMap: textureMapKeys,
  262. alphaMap: textureMapKeys
  263. };
  264. var folder = gui.addFolder( 'THREE.MeshPhongMaterial' );
  265. folder.addColor( data, 'color' ).onChange( handleColorChange( material.color ) );
  266. folder.addColor( data, 'emissive' ).onChange( handleColorChange( material.emissive ) );
  267. folder.addColor( data, 'specular' ).onChange( handleColorChange( material.specular ) );
  268. folder.add( material, 'shininess', 0, 100 );
  269. folder.add( material, 'flatShading' ).onChange( needsUpdate( material, geometry ) );
  270. folder.add( material, 'wireframe' );
  271. folder.add( material, 'wireframeLinewidth', 0, 10 );
  272. folder.add( material, 'vertexColors', constants.colors ).onChange( needsUpdate( material, geometry ) );
  273. folder.add( material, 'fog' );
  274. folder.add( data, 'envMaps', envMapKeys ).onChange( updateTexture( material, 'envMap', envMaps ) );
  275. folder.add( data, 'map', textureMapKeys ).onChange( updateTexture( material, 'map', textureMaps ) );
  276. folder.add( data, 'lightMap', textureMapKeys ).onChange( updateTexture( material, 'lightMap', textureMaps ) );
  277. folder.add( data, 'specularMap', textureMapKeys ).onChange( updateTexture( material, 'specularMap', textureMaps ) );
  278. folder.add( data, 'alphaMap', textureMapKeys ).onChange( updateTexture( material, 'alphaMap', textureMaps ) );
  279. }
  280. function guiMeshStandardMaterial( gui, mesh, material, geometry ) {
  281. var data = {
  282. color: material.color.getHex(),
  283. emissive: material.emissive.getHex(),
  284. envMaps: envMapKeys,
  285. map: textureMapKeys,
  286. lightMap: textureMapKeys,
  287. specularMap: textureMapKeys,
  288. alphaMap: textureMapKeys
  289. };
  290. var folder = gui.addFolder( 'THREE.MeshStandardMaterial' );
  291. folder.addColor( data, 'color' ).onChange( handleColorChange( material.color ) );
  292. folder.addColor( data, 'emissive' ).onChange( handleColorChange( material.emissive ) );
  293. folder.add( material, 'roughness', 0, 1 );
  294. folder.add( material, 'metalness', 0, 1 );
  295. folder.add( material, 'flatShading' ).onChange( needsUpdate( material, geometry ) );
  296. folder.add( material, 'wireframe' );
  297. folder.add( material, 'wireframeLinewidth', 0, 10 );
  298. folder.add( material, 'vertexColors', constants.colors ).onChange( needsUpdate( material, geometry ) );
  299. folder.add( material, 'fog' );
  300. folder.add( data, 'envMaps', envMapKeys ).onChange( updateTexture( material, 'envMap', envMaps ) );
  301. folder.add( data, 'map', textureMapKeys ).onChange( updateTexture( material, 'map', textureMaps ) );
  302. folder.add( data, 'lightMap', textureMapKeys ).onChange( updateTexture( material, 'lightMap', textureMaps ) );
  303. folder.add( data, 'alphaMap', textureMapKeys ).onChange( updateTexture( material, 'alphaMap', textureMaps ) );
  304. // TODO roughnessMap and metalnessMap
  305. }
  306. function guiMeshPhysicalMaterial( gui, mesh, material, geometry ) {
  307. var data = {
  308. color: material.color.getHex(),
  309. emissive: material.emissive.getHex(),
  310. envMaps: envMapKeys,
  311. map: textureMapKeys,
  312. lightMap: textureMapKeys,
  313. specularMap: textureMapKeys,
  314. alphaMap: textureMapKeys
  315. };
  316. var folder = gui.addFolder( 'THREE.MeshPhysicalMaterial' );
  317. folder.addColor( data, 'color' ).onChange( handleColorChange( material.color ) );
  318. folder.addColor( data, 'emissive' ).onChange( handleColorChange( material.emissive ) );
  319. folder.add( material, 'roughness', 0, 1 );
  320. folder.add( material, 'metalness', 0, 1 );
  321. folder.add( material, 'reflectivity', 0, 1 );
  322. folder.add( material, 'clearCoat', 0, 1 ).step( 0.01 );
  323. folder.add( material, 'clearCoatRoughness', 0, 1 ).step( 0.01 );
  324. folder.add( material, 'flatShading' ).onChange( needsUpdate( material, geometry ) );
  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', textureMapKeys ).onChange( updateTexture( material, 'map', textureMaps ) );
  331. folder.add( data, 'lightMap', textureMapKeys ).onChange( updateTexture( material, 'lightMap', textureMaps ) );
  332. folder.add( data, 'alphaMap', textureMapKeys ).onChange( updateTexture( material, 'alphaMap', textureMaps ) );
  333. // TODO roughnessMap and metalnessMap
  334. }
  335. function chooseFromHash( gui, mesh, geometry ) {
  336. var selectedMaterial = window.location.hash.substring( 1 ) || 'MeshBasicMaterial';
  337. var material;
  338. switch ( selectedMaterial ) {
  339. case 'MeshBasicMaterial' :
  340. material = new THREE.MeshBasicMaterial( { color: 0x2194CE } );
  341. guiMaterial( gui, mesh, material, geometry );
  342. guiMeshBasicMaterial( gui, mesh, material, geometry );
  343. return material;
  344. break;
  345. case 'MeshLambertMaterial' :
  346. material = new THREE.MeshLambertMaterial( { color: 0x2194CE } );
  347. guiMaterial( gui, mesh, material, geometry );
  348. guiMeshLambertMaterial( gui, mesh, material, geometry );
  349. return material;
  350. break;
  351. case 'MeshMatcapMaterial' :
  352. material = new THREE.MeshMatcapMaterial( { matcap: matcaps.porcelainWhite } );
  353. guiMaterial( gui, mesh, material, geometry );
  354. guiMeshMatcapMaterial( gui, mesh, material, geometry );
  355. return material;
  356. break;
  357. case 'MeshPhongMaterial' :
  358. material = new THREE.MeshPhongMaterial( { color: 0x2194CE } );
  359. guiMaterial( gui, mesh, material, geometry );
  360. guiMeshPhongMaterial( gui, mesh, material, geometry );
  361. return material;
  362. break;
  363. case 'MeshStandardMaterial' :
  364. material = new THREE.MeshStandardMaterial( { color: 0x2194CE } );
  365. guiMaterial( gui, mesh, material, geometry );
  366. guiMeshStandardMaterial( gui, mesh, material, geometry );
  367. return material;
  368. break;
  369. case 'MeshPhysicalMaterial' :
  370. material = new THREE.MeshPhysicalMaterial( { color: 0x2194CE } );
  371. guiMaterial( gui, mesh, material, geometry );
  372. guiMeshPhysicalMaterial( gui, mesh, material, geometry );
  373. return material;
  374. break;
  375. case 'MeshDepthMaterial' :
  376. material = new THREE.MeshDepthMaterial();
  377. guiMaterial( gui, mesh, material, geometry );
  378. guiMeshDepthMaterial( gui, mesh, material, geometry );
  379. return material;
  380. break;
  381. case 'MeshNormalMaterial' :
  382. material = new THREE.MeshNormalMaterial();
  383. guiMaterial( gui, mesh, material, geometry );
  384. guiMeshNormalMaterial( gui, mesh, material, geometry );
  385. return material;
  386. break;
  387. case 'LineBasicMaterial' :
  388. material = new THREE.LineBasicMaterial( { color: 0x2194CE } );
  389. guiMaterial( gui, mesh, material, geometry );
  390. guiLineBasicMaterial( gui, mesh, material, geometry );
  391. return material;
  392. break;
  393. }
  394. }