webgl_materials_variations_standard.html 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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/Detector.js"></script>
  35. <script src="js/libs/stats.min.js"></script>
  36. <script>
  37. if ( ! Detector.webgl ) Detector.addGetWebGLMessage();
  38. var container, stats;
  39. var camera, scene, renderer, controls;
  40. var particleLight;
  41. var loader = new THREE.FontLoader();
  42. loader.load( 'fonts/gentilis_regular.typeface.json', function ( font ) {
  43. init( font );
  44. animate();
  45. } );
  46. function init( font ) {
  47. container = document.createElement( 'div' );
  48. document.body.appendChild( container );
  49. camera = new THREE.PerspectiveCamera( 40, window.innerWidth / window.innerHeight, 1, 2000 );
  50. camera.position.set( 0.0, 400, 400 * 3.5 );
  51. //
  52. var genCubeUrls = function( prefix, postfix ) {
  53. return [
  54. prefix + 'px' + postfix, prefix + 'nx' + postfix,
  55. prefix + 'py' + postfix, prefix + 'ny' + postfix,
  56. prefix + 'pz' + postfix, prefix + 'nz' + postfix
  57. ];
  58. };
  59. var textureCube = new THREE.CubeTextureLoader()
  60. .setPath( 'textures/cube/pisa/' )
  61. .load( [ 'px.png', 'nx.png', 'py.png', 'ny.png', 'pz.png', 'nz.png' ] );
  62. scene = new THREE.Scene();
  63. scene.background = textureCube;
  64. var hdrUrls = genCubeUrls( './textures/cube/pisaHDR/', '.hdr' );
  65. var hdrCubeRenderTarget = null;
  66. // Materials
  67. var imgTexture = new THREE.TextureLoader().load( "textures/planets/moon_1024.jpg" );
  68. imgTexture.wrapS = imgTexture.wrapT = THREE.RepeatWrapping;
  69. imgTexture.anisotropy = 16;
  70. imgTexture = null;
  71. new THREE.HDRCubeTextureLoader().load( THREE.UnsignedByteType, hdrUrls, function ( hdrCubeMap ) {
  72. var pmremGenerator = new THREE.PMREMGenerator( hdrCubeMap );
  73. pmremGenerator.update( renderer );
  74. var pmremCubeUVPacker = new THREE.PMREMCubeUVPacker( pmremGenerator.cubeLods );
  75. pmremCubeUVPacker.update( renderer );
  76. hdrCubeRenderTarget = pmremCubeUVPacker.CubeUVRenderTarget;
  77. var shininess = 50, specular = 0x333333, bumpScale = 1;
  78. var materials = [];
  79. var cubeWidth = 400;
  80. var numberOfSphersPerSide = 5;
  81. var sphereRadius = ( cubeWidth / numberOfSphersPerSide ) * 0.8 * 0.5;
  82. var stepSize = 1.0 / numberOfSphersPerSide;
  83. var geometry = new THREE.SphereBufferGeometry( sphereRadius, 32, 16 );
  84. var index = 0;
  85. for ( var alpha = 0; alpha <= 1.0; alpha += stepSize ) {
  86. for ( var beta = 0; beta <= 1.0; beta += stepSize ) {
  87. for ( var gamma = 0; gamma <= 1.0; gamma += stepSize ) {
  88. // basic monochromatic energy preservation
  89. var diffuseColor = new THREE.Color().setHSL( alpha, 0.5, gamma * 0.5 + 0.1 );
  90. var material = new THREE.MeshStandardMaterial( {
  91. map: imgTexture,
  92. bumpMap: imgTexture,
  93. bumpScale: bumpScale,
  94. color: diffuseColor,
  95. metalness: beta,
  96. roughness: 1.0 - alpha,
  97. envMap: index % 2 === 0 ? null : hdrCubeRenderTarget.texture
  98. } );
  99. index ++;
  100. var mesh = new THREE.Mesh( geometry, material );
  101. mesh.position.x = alpha * 400 - 200;
  102. mesh.position.y = beta * 400 - 200;
  103. mesh.position.z = gamma * 400 - 200;
  104. scene.add( mesh );
  105. }
  106. }
  107. index ++;
  108. }
  109. hdrCubeMap.dispose();
  110. pmremGenerator.dispose();
  111. pmremCubeUVPacker.dispose();
  112. } );
  113. function addLabel( name, location ) {
  114. var textGeo = new THREE.TextBufferGeometry( name, {
  115. font: font,
  116. size: 20,
  117. height: 1,
  118. curveSegments: 1
  119. });
  120. var textMaterial = new THREE.MeshBasicMaterial( { color: 0xffffff } );
  121. var textMesh = new THREE.Mesh( textGeo, textMaterial );
  122. textMesh.position.copy( location );
  123. scene.add( textMesh );
  124. }
  125. addLabel( "+roughness", new THREE.Vector3( -350, 0, 0 ) );
  126. addLabel( "-roughness", new THREE.Vector3( 350, 0, 0 ) );
  127. addLabel( "-metalness", new THREE.Vector3( 0, -300, 0 ) );
  128. addLabel( "+metalness", new THREE.Vector3( 0, 300, 0 ) );
  129. addLabel( "-diffuse", new THREE.Vector3( 0, 0, -300 ) );
  130. addLabel( "+diffuse", new THREE.Vector3( 0, 0, 300 ) );
  131. particleLight = new THREE.Mesh( new THREE.SphereBufferGeometry( 4, 8, 8 ), new THREE.MeshBasicMaterial( { color: 0xffffff } ) );
  132. scene.add( particleLight );
  133. // Lights
  134. scene.add( new THREE.AmbientLight( 0x222222 ) );
  135. var directionalLight = new THREE.DirectionalLight( 0xffffff, 1 );
  136. directionalLight.position.set( 1, 1, 1 ).normalize();
  137. scene.add( directionalLight );
  138. var pointLight = new THREE.PointLight( 0xffffff, 2, 800 );
  139. particleLight.add( pointLight );
  140. //
  141. renderer = new THREE.WebGLRenderer( { antialias: true } );
  142. renderer.setPixelRatio( window.devicePixelRatio );
  143. renderer.setSize( window.innerWidth, window.innerHeight );
  144. container.appendChild( renderer.domElement );
  145. renderer.gammaInput = true;
  146. renderer.gammaOutput = true;
  147. renderer.toneMapping = THREE.Uncharted2ToneMapping;
  148. renderer.toneMappingExposure = 0.75;
  149. //
  150. stats = new Stats();
  151. container.appendChild( stats.dom );
  152. controls = new THREE.OrbitControls( camera );
  153. window.addEventListener( 'resize', onWindowResize, false );
  154. }
  155. function onWindowResize() {
  156. camera.aspect = window.innerWidth / window.innerHeight;
  157. camera.updateProjectionMatrix();
  158. renderer.setSize( window.innerWidth, window.innerHeight );
  159. }
  160. //
  161. function animate() {
  162. requestAnimationFrame( animate );
  163. render();
  164. stats.update();
  165. }
  166. function render() {
  167. var timer = Date.now() * 0.00025;
  168. //camera.position.x = Math.cos( timer ) * 800;
  169. //camera.position.z = Math.sin( timer ) * 800;
  170. camera.lookAt( scene.position );
  171. particleLight.position.x = Math.sin( timer * 7 ) * 300;
  172. particleLight.position.y = Math.cos( timer * 5 ) * 400;
  173. particleLight.position.z = Math.cos( timer * 3 ) * 300;
  174. renderer.render( scene, camera );
  175. }
  176. </script>
  177. </body>
  178. </html>