webgl_materials_variations_standard.html 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - materials</title>
  5. <meta charset="utf-8">
  6. <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
  7. <style>
  8. body {
  9. color: #fff;
  10. font-family: Monospace;
  11. font-size: 13px;
  12. text-align: center;
  13. background-color: #000;
  14. margin: 0px;
  15. overflow: hidden;
  16. }
  17. #info {
  18. position: absolute;
  19. top: 0px; width: 100%;
  20. padding: 5px;
  21. }
  22. </style>
  23. </head>
  24. <body>
  25. <div id="container"></div>
  26. <div id="info"><a href="http://threejs.org" target="_blank" rel="noopener">three.js</a> - Standard Material Variations by <a href="http://clara.io/" target="_blank" rel="noopener">Ben Houston</a>.<br/><br/>
  27. Note: Every second sphere has an IBL environment map on it.</div>
  28. <script src="../build/three.js"></script>
  29. <script src="js/controls/OrbitControls.js"></script>
  30. <script src="js/loaders/RGBELoader.js"></script>
  31. <script src="js/loaders/HDRCubeTextureLoader.js"></script>
  32. <script src="js/pmrem/PMREMGenerator.js"></script>
  33. <script src="js/pmrem/PMREMCubeUVPacker.js"></script>
  34. <script src="js/WebGL.js"></script>
  35. <script src="js/libs/stats.min.js"></script>
  36. <script>
  37. if ( WEBGL.isWebGLAvailable() === false ) {
  38. document.body.appendChild( WEBGL.getWebGLErrorMessage() );
  39. }
  40. var container, stats;
  41. var camera, scene, renderer, controls;
  42. var particleLight;
  43. var loader = new THREE.FontLoader();
  44. loader.load( 'fonts/gentilis_regular.typeface.json', function ( font ) {
  45. init( font );
  46. animate();
  47. } );
  48. function init( font ) {
  49. container = document.createElement( 'div' );
  50. document.body.appendChild( container );
  51. camera = new THREE.PerspectiveCamera( 40, window.innerWidth / window.innerHeight, 1, 2000 );
  52. camera.position.set( 0.0, 400, 400 * 3.5 );
  53. //
  54. var genCubeUrls = function ( prefix, postfix ) {
  55. return [
  56. prefix + 'px' + postfix, prefix + 'nx' + postfix,
  57. prefix + 'py' + postfix, prefix + 'ny' + postfix,
  58. prefix + 'pz' + postfix, prefix + 'nz' + postfix
  59. ];
  60. };
  61. scene = new THREE.Scene();
  62. var hdrUrls = genCubeUrls( './textures/cube/pisaHDR/', '.hdr' );
  63. var hdrCubeRenderTarget = null;
  64. // Materials
  65. var imgTexture = new THREE.TextureLoader().load( "textures/planets/moon_1024.jpg" );
  66. imgTexture.wrapS = imgTexture.wrapT = THREE.RepeatWrapping;
  67. imgTexture.anisotropy = 16;
  68. imgTexture = null;
  69. new THREE.HDRCubeTextureLoader().load( THREE.UnsignedByteType, hdrUrls, function ( hdrCubeMap ) {
  70. var pmremGenerator = new THREE.PMREMGenerator( hdrCubeMap );
  71. pmremGenerator.update( renderer );
  72. var pmremCubeUVPacker = new THREE.PMREMCubeUVPacker( pmremGenerator.cubeLods );
  73. pmremCubeUVPacker.update( renderer );
  74. hdrCubeRenderTarget = pmremCubeUVPacker.CubeUVRenderTarget;
  75. var bumpScale = 1;
  76. var cubeWidth = 400;
  77. var numberOfSphersPerSide = 5;
  78. var sphereRadius = ( cubeWidth / numberOfSphersPerSide ) * 0.8 * 0.5;
  79. var stepSize = 1.0 / numberOfSphersPerSide;
  80. var geometry = new THREE.SphereBufferGeometry( sphereRadius, 32, 16 );
  81. var index = 0;
  82. for ( var alpha = 0; alpha <= 1.0; alpha += stepSize ) {
  83. for ( var beta = 0; beta <= 1.0; beta += stepSize ) {
  84. for ( var gamma = 0; gamma <= 1.0; gamma += stepSize ) {
  85. // basic monochromatic energy preservation
  86. var diffuseColor = new THREE.Color().setHSL( alpha, 0.5, gamma * 0.5 + 0.1 );
  87. var material = new THREE.MeshStandardMaterial( {
  88. map: imgTexture,
  89. bumpMap: imgTexture,
  90. bumpScale: bumpScale,
  91. color: diffuseColor,
  92. metalness: beta,
  93. roughness: 1.0 - alpha,
  94. envMap: index % 2 === 0 ? null : hdrCubeRenderTarget.texture
  95. } );
  96. index ++;
  97. var mesh = new THREE.Mesh( geometry, material );
  98. mesh.position.x = alpha * 400 - 200;
  99. mesh.position.y = beta * 400 - 200;
  100. mesh.position.z = gamma * 400 - 200;
  101. scene.add( mesh );
  102. }
  103. }
  104. index ++;
  105. }
  106. hdrCubeMap.magFilter = THREE.LinearFilter;
  107. hdrCubeMap.needsUpdate = true;
  108. scene.background = hdrCubeMap;
  109. pmremGenerator.dispose();
  110. pmremCubeUVPacker.dispose();
  111. } );
  112. function addLabel( name, location ) {
  113. var textGeo = new THREE.TextBufferGeometry( name, {
  114. font: font,
  115. size: 20,
  116. height: 1,
  117. curveSegments: 1
  118. } );
  119. var textMaterial = new THREE.MeshBasicMaterial( { color: 0xffffff } );
  120. var textMesh = new THREE.Mesh( textGeo, textMaterial );
  121. textMesh.position.copy( location );
  122. scene.add( textMesh );
  123. }
  124. addLabel( "+roughness", new THREE.Vector3( - 350, 0, 0 ) );
  125. addLabel( "-roughness", new THREE.Vector3( 350, 0, 0 ) );
  126. addLabel( "-metalness", new THREE.Vector3( 0, - 300, 0 ) );
  127. addLabel( "+metalness", new THREE.Vector3( 0, 300, 0 ) );
  128. addLabel( "-diffuse", new THREE.Vector3( 0, 0, - 300 ) );
  129. addLabel( "+diffuse", new THREE.Vector3( 0, 0, 300 ) );
  130. particleLight = new THREE.Mesh( new THREE.SphereBufferGeometry( 4, 8, 8 ), new THREE.MeshBasicMaterial( { color: 0xffffff } ) );
  131. scene.add( particleLight );
  132. // Lights
  133. scene.add( new THREE.AmbientLight( 0x222222 ) );
  134. var directionalLight = new THREE.DirectionalLight( 0xffffff, 1 );
  135. directionalLight.position.set( 1, 1, 1 ).normalize();
  136. scene.add( directionalLight );
  137. var pointLight = new THREE.PointLight( 0xffffff, 2, 800 );
  138. particleLight.add( pointLight );
  139. //
  140. renderer = new THREE.WebGLRenderer( { antialias: true } );
  141. renderer.setPixelRatio( window.devicePixelRatio );
  142. renderer.setSize( window.innerWidth, window.innerHeight );
  143. container.appendChild( renderer.domElement );
  144. renderer.gammaInput = true;
  145. renderer.gammaOutput = true;
  146. renderer.toneMapping = THREE.Uncharted2ToneMapping;
  147. renderer.toneMappingExposure = 0.75;
  148. //
  149. stats = new Stats();
  150. container.appendChild( stats.dom );
  151. controls = new THREE.OrbitControls( camera );
  152. window.addEventListener( 'resize', onWindowResize, false );
  153. }
  154. function onWindowResize() {
  155. camera.aspect = window.innerWidth / window.innerHeight;
  156. camera.updateProjectionMatrix();
  157. renderer.setSize( window.innerWidth, window.innerHeight );
  158. }
  159. //
  160. function animate() {
  161. requestAnimationFrame( animate );
  162. render();
  163. stats.update();
  164. }
  165. function render() {
  166. var timer = Date.now() * 0.00025;
  167. //camera.position.x = Math.cos( timer ) * 800;
  168. //camera.position.z = Math.sin( timer ) * 800;
  169. camera.lookAt( scene.position );
  170. particleLight.position.x = Math.sin( timer * 7 ) * 300;
  171. particleLight.position.y = Math.cos( timer * 5 ) * 400;
  172. particleLight.position.z = Math.cos( timer * 3 ) * 300;
  173. renderer.render( scene, camera );
  174. }
  175. </script>
  176. </body>
  177. </html>