2
0

material.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552
  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. function generateVertexColors( geometry ) {
  85. var positionAttribute = geometry.attributes.position;
  86. var colors = [];
  87. var color = new THREE.Color();
  88. for ( var i = 0, il = positionAttribute.count; i < il; i ++ ) {
  89. color.setHSL( i / il * Math.random(), 0.5, 0.5 );
  90. colors.push( color.r, color.g, color.b );
  91. }
  92. geometry.addAttribute( 'color', new THREE.Float32BufferAttribute( colors, 3 ) );
  93. }
  94. function handleColorChange( color ) {
  95. return function ( value ) {
  96. if ( typeof value === 'string' ) {
  97. value = value.replace( '#', '0x' );
  98. }
  99. color.setHex( value );
  100. };
  101. }
  102. function needsUpdate( material, geometry ) {
  103. return function () {
  104. material.vertexColors = parseInt( material.vertexColors ); //Ensure number
  105. material.side = parseInt( material.side ); //Ensure number
  106. material.needsUpdate = true;
  107. geometry.attributes.position.needsUpdate = true;
  108. geometry.attributes.normal.needsUpdate = true;
  109. geometry.attributes.color.needsUpdate = true;
  110. };
  111. }
  112. function updateTexture( material, materialKey, textures ) {
  113. return function ( key ) {
  114. material[ materialKey ] = textures[ key ];
  115. material.needsUpdate = true;
  116. };
  117. }
  118. function guiScene( gui, scene ) {
  119. var folder = gui.addFolder( 'Scene' );
  120. var data = {
  121. background: '#000000',
  122. 'ambient light': ambientLight.color.getHex()
  123. };
  124. var color = new THREE.Color();
  125. var colorConvert = handleColorChange( color );
  126. folder.addColor( data, 'background' ).onChange( function ( value ) {
  127. colorConvert( value );
  128. renderer.setClearColor( color.getHex() );
  129. } );
  130. folder.addColor( data, 'ambient light' ).onChange( handleColorChange( ambientLight.color ) );
  131. guiSceneFog( folder, scene );
  132. }
  133. function guiSceneFog( folder, scene ) {
  134. var fogFolder = folder.addFolder( 'scene.fog' );
  135. var fog = new THREE.Fog( 0x3f7b9d, 0, 60 );
  136. var data = {
  137. fog: {
  138. 'THREE.Fog()': false,
  139. 'scene.fog.color': fog.color.getHex()
  140. }
  141. };
  142. fogFolder.add( data.fog, 'THREE.Fog()' ).onChange( function ( useFog ) {
  143. if ( useFog ) {
  144. scene.fog = fog;
  145. } else {
  146. scene.fog = null;
  147. }
  148. } );
  149. fogFolder.addColor( data.fog, 'scene.fog.color' ).onChange( handleColorChange( fog.color ) );
  150. }
  151. function guiMaterial( gui, mesh, material, geometry ) {
  152. var folder = gui.addFolder( 'THREE.Material' );
  153. folder.add( material, 'transparent' );
  154. folder.add( material, 'opacity', 0, 1 );
  155. // folder.add( material, 'blending', constants.blendingMode );
  156. // folder.add( material, 'blendSrc', constants.destinationFactors );
  157. // folder.add( material, 'blendDst', constants.destinationFactors );
  158. // folder.add( material, 'blendEquation', constants.equations );
  159. folder.add( material, 'depthTest' );
  160. folder.add( material, 'depthWrite' );
  161. // folder.add( material, 'polygonOffset' );
  162. // folder.add( material, 'polygonOffsetFactor' );
  163. // folder.add( material, 'polygonOffsetUnits' );
  164. folder.add( material, 'alphaTest', 0, 1 );
  165. folder.add( material, 'visible' );
  166. folder.add( material, 'side', constants.side ).onChange( needsUpdate( material, geometry ) );
  167. }
  168. function guiMeshBasicMaterial( gui, mesh, material, geometry ) {
  169. var data = {
  170. color: material.color.getHex(),
  171. envMaps: envMapKeys,
  172. map: textureMapKeys,
  173. specularMap: textureMapKeys,
  174. alphaMap: textureMapKeys
  175. };
  176. var folder = gui.addFolder( 'THREE.MeshBasicMaterial' );
  177. folder.addColor( data, 'color' ).onChange( handleColorChange( material.color ) );
  178. folder.add( material, 'wireframe' );
  179. folder.add( material, 'wireframeLinewidth', 0, 10 );
  180. folder.add( material, 'vertexColors', constants.colors ).onChange( needsUpdate( material, geometry ) );
  181. folder.add( material, 'fog' );
  182. folder.add( data, 'envMaps', envMapKeys ).onChange( updateTexture( material, 'envMap', envMaps ) );
  183. folder.add( data, 'map', textureMapKeys ).onChange( updateTexture( material, 'map', textureMaps ) );
  184. folder.add( data, 'specularMap', textureMapKeys ).onChange( updateTexture( material, 'specularMap', textureMaps ) );
  185. folder.add( data, 'alphaMap', textureMapKeys ).onChange( updateTexture( material, 'alphaMap', textureMaps ) );
  186. folder.add( material, 'combine', constants.combine );
  187. folder.add( material, 'reflectivity', 0, 1 );
  188. folder.add( material, 'refractionRatio', 0, 1 );
  189. }
  190. function guiMeshDepthMaterial( gui, mesh, material, geometry ) {
  191. var folder = gui.addFolder( 'THREE.MeshDepthMaterial' );
  192. folder.add( material, 'wireframe' );
  193. folder.add( material, 'wireframeLinewidth', 0, 10 );
  194. }
  195. function guiMeshNormalMaterial( gui, mesh, material, geometry ) {
  196. var folder = gui.addFolder( 'THREE.MeshNormalMaterial' );
  197. folder.add( material, 'flatShading' ).onChange( needsUpdate( material, geometry ) );
  198. folder.add( material, 'wireframe' );
  199. folder.add( material, 'wireframeLinewidth', 0, 10 );
  200. }
  201. function guiLineBasicMaterial( gui, mesh, material, geometry ) {
  202. var data = {
  203. color: material.color.getHex()
  204. };
  205. var folder = gui.addFolder( 'THREE.LineBasicMaterial' );
  206. folder.addColor( data, 'color' ).onChange( handleColorChange( material.color ) );
  207. folder.add( material, 'linewidth', 0, 10 );
  208. folder.add( material, 'linecap', [ 'butt', 'round', 'square' ] );
  209. folder.add( material, 'linejoin', [ 'round', 'bevel', 'miter' ] );
  210. folder.add( material, 'vertexColors', constants.colors ).onChange( needsUpdate( material, geometry ) );
  211. folder.add( material, 'fog' );
  212. }
  213. function guiMeshLambertMaterial( gui, mesh, material, geometry ) {
  214. var data = {
  215. color: material.color.getHex(),
  216. emissive: material.emissive.getHex(),
  217. envMaps: envMapKeys,
  218. map: textureMapKeys,
  219. specularMap: textureMapKeys,
  220. alphaMap: textureMapKeys
  221. };
  222. var folder = gui.addFolder( 'THREE.MeshLambertMaterial' );
  223. folder.addColor( data, 'color' ).onChange( handleColorChange( material.color ) );
  224. folder.addColor( data, 'emissive' ).onChange( handleColorChange( material.emissive ) );
  225. folder.add( material, 'wireframe' );
  226. folder.add( material, 'wireframeLinewidth', 0, 10 );
  227. folder.add( material, 'vertexColors', constants.colors ).onChange( needsUpdate( material, geometry ) );
  228. folder.add( material, 'fog' );
  229. folder.add( data, 'envMaps', envMapKeys ).onChange( updateTexture( material, 'envMap', envMaps ) );
  230. folder.add( data, 'map', textureMapKeys ).onChange( updateTexture( material, 'map', textureMaps ) );
  231. folder.add( data, 'specularMap', textureMapKeys ).onChange( updateTexture( material, 'specularMap', textureMaps ) );
  232. folder.add( data, 'alphaMap', textureMapKeys ).onChange( updateTexture( material, 'alphaMap', textureMaps ) );
  233. folder.add( material, 'combine', constants.combine );
  234. folder.add( material, 'reflectivity', 0, 1 );
  235. folder.add( material, 'refractionRatio', 0, 1 );
  236. }
  237. function guiMeshPhongMaterial( gui, mesh, material, geometry ) {
  238. var data = {
  239. color: material.color.getHex(),
  240. emissive: material.emissive.getHex(),
  241. specular: material.specular.getHex(),
  242. envMaps: envMapKeys,
  243. map: textureMapKeys,
  244. lightMap: textureMapKeys,
  245. specularMap: textureMapKeys,
  246. alphaMap: textureMapKeys
  247. };
  248. var folder = gui.addFolder( 'THREE.MeshPhongMaterial' );
  249. folder.addColor( data, 'color' ).onChange( handleColorChange( material.color ) );
  250. folder.addColor( data, 'emissive' ).onChange( handleColorChange( material.emissive ) );
  251. folder.addColor( data, 'specular' ).onChange( handleColorChange( material.specular ) );
  252. folder.add( material, 'shininess', 0, 100 );
  253. folder.add( material, 'flatShading' ).onChange( needsUpdate( material, geometry ) );
  254. folder.add( material, 'wireframe' );
  255. folder.add( material, 'wireframeLinewidth', 0, 10 );
  256. folder.add( material, 'vertexColors', constants.colors ).onChange( needsUpdate( material, geometry ) );
  257. folder.add( material, 'fog' );
  258. folder.add( data, 'envMaps', envMapKeys ).onChange( updateTexture( material, 'envMap', envMaps ) );
  259. folder.add( data, 'map', textureMapKeys ).onChange( updateTexture( material, 'map', textureMaps ) );
  260. folder.add( data, 'lightMap', textureMapKeys ).onChange( updateTexture( material, 'lightMap', textureMaps ) );
  261. folder.add( data, 'specularMap', textureMapKeys ).onChange( updateTexture( material, 'specularMap', textureMaps ) );
  262. folder.add( data, 'alphaMap', textureMapKeys ).onChange( updateTexture( material, 'alphaMap', textureMaps ) );
  263. }
  264. function guiMeshStandardMaterial( gui, mesh, material, geometry ) {
  265. var data = {
  266. color: material.color.getHex(),
  267. emissive: material.emissive.getHex(),
  268. envMaps: envMapKeys,
  269. map: textureMapKeys,
  270. lightMap: textureMapKeys,
  271. specularMap: textureMapKeys,
  272. alphaMap: textureMapKeys
  273. };
  274. var folder = gui.addFolder( 'THREE.MeshStandardMaterial' );
  275. folder.addColor( data, 'color' ).onChange( handleColorChange( material.color ) );
  276. folder.addColor( data, 'emissive' ).onChange( handleColorChange( material.emissive ) );
  277. folder.add( material, 'roughness', 0, 1 );
  278. folder.add( material, 'metalness', 0, 1 );
  279. folder.add( material, 'flatShading' ).onChange( needsUpdate( material, geometry ) );
  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', textureMapKeys ).onChange( updateTexture( material, 'map', textureMaps ) );
  286. folder.add( data, 'lightMap', textureMapKeys ).onChange( updateTexture( material, 'lightMap', textureMaps ) );
  287. folder.add( data, 'alphaMap', textureMapKeys ).onChange( updateTexture( material, 'alphaMap', textureMaps ) );
  288. // TODO roughnessMap and metalnessMap
  289. }
  290. function guiMeshPhysicalMaterial( gui, mesh, material, geometry ) {
  291. var data = {
  292. color: material.color.getHex(),
  293. emissive: material.emissive.getHex(),
  294. envMaps: envMapKeys,
  295. map: textureMapKeys,
  296. lightMap: textureMapKeys,
  297. specularMap: textureMapKeys,
  298. alphaMap: textureMapKeys
  299. };
  300. var folder = gui.addFolder( 'THREE.MeshPhysicalMaterial' );
  301. folder.addColor( data, 'color' ).onChange( handleColorChange( material.color ) );
  302. folder.addColor( data, 'emissive' ).onChange( handleColorChange( material.emissive ) );
  303. folder.add( material, 'roughness', 0, 1 );
  304. folder.add( material, 'metalness', 0, 1 );
  305. folder.add( material, 'reflectivity', 0, 1 );
  306. folder.add( material, 'clearCoat', 0, 1 ).step( 0.01 );
  307. folder.add( material, 'clearCoatRoughness', 0, 1 ).step( 0.01 );
  308. folder.add( material, 'flatShading' ).onChange( needsUpdate( material, geometry ) );
  309. folder.add( material, 'wireframe' );
  310. folder.add( material, 'wireframeLinewidth', 0, 10 );
  311. folder.add( material, 'vertexColors', constants.colors ).onChange( needsUpdate( material, geometry ) );
  312. folder.add( material, 'fog' );
  313. folder.add( data, 'envMaps', envMapKeys ).onChange( updateTexture( material, 'envMap', envMaps ) );
  314. folder.add( data, 'map', textureMapKeys ).onChange( updateTexture( material, 'map', textureMaps ) );
  315. folder.add( data, 'lightMap', textureMapKeys ).onChange( updateTexture( material, 'lightMap', textureMaps ) );
  316. folder.add( data, 'alphaMap', textureMapKeys ).onChange( updateTexture( material, 'alphaMap', textureMaps ) );
  317. // TODO roughnessMap and metalnessMap
  318. }
  319. function chooseFromHash( gui, mesh, geometry ) {
  320. var selectedMaterial = window.location.hash.substring( 1 ) || 'MeshBasicMaterial';
  321. var material;
  322. switch ( selectedMaterial ) {
  323. case 'MeshBasicMaterial' :
  324. material = new THREE.MeshBasicMaterial( { color: 0x2194CE } );
  325. guiMaterial( gui, mesh, material, geometry );
  326. guiMeshBasicMaterial( gui, mesh, material, geometry );
  327. return material;
  328. break;
  329. case 'MeshLambertMaterial' :
  330. material = new THREE.MeshLambertMaterial( { color: 0x2194CE } );
  331. guiMaterial( gui, mesh, material, geometry );
  332. guiMeshLambertMaterial( gui, mesh, material, geometry );
  333. return material;
  334. break;
  335. case 'MeshPhongMaterial' :
  336. material = new THREE.MeshPhongMaterial( { color: 0x2194CE } );
  337. guiMaterial( gui, mesh, material, geometry );
  338. guiMeshPhongMaterial( gui, mesh, material, geometry );
  339. return material;
  340. break;
  341. case 'MeshStandardMaterial' :
  342. material = new THREE.MeshStandardMaterial( { color: 0x2194CE } );
  343. guiMaterial( gui, mesh, material, geometry );
  344. guiMeshStandardMaterial( gui, mesh, material, geometry );
  345. return material;
  346. break;
  347. case 'MeshPhysicalMaterial' :
  348. material = new THREE.MeshPhysicalMaterial( { color: 0x2194CE } );
  349. guiMaterial( gui, mesh, material, geometry );
  350. guiMeshPhysicalMaterial( gui, mesh, material, geometry );
  351. return material;
  352. break;
  353. case 'MeshDepthMaterial' :
  354. material = new THREE.MeshDepthMaterial();
  355. guiMaterial( gui, mesh, material, geometry );
  356. guiMeshDepthMaterial( gui, mesh, material, geometry );
  357. return material;
  358. break;
  359. case 'MeshNormalMaterial' :
  360. material = new THREE.MeshNormalMaterial();
  361. guiMaterial( gui, mesh, material, geometry );
  362. guiMeshNormalMaterial( gui, mesh, material, geometry );
  363. return material;
  364. break;
  365. case 'LineBasicMaterial' :
  366. material = new THREE.LineBasicMaterial( { color: 0x2194CE } );
  367. guiMaterial( gui, mesh, material, geometry );
  368. guiLineBasicMaterial( gui, mesh, material, geometry );
  369. return material;
  370. break;
  371. }
  372. }