2
0

webgl_materials_variations_physical.html 6.5 KB

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