webgl_materials_variations_standard.html 6.6 KB

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