material.js 17 KB

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