webgl_materials_variations_physical.html 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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="https://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 type="module">
  14. import * as THREE from '../build/three.module.js';
  15. import Stats from './jsm/libs/stats.module.js';
  16. import { OrbitControls } from './jsm/controls/OrbitControls.js';
  17. import { RGBELoader } from './jsm/loaders/RGBELoader.js';
  18. var container, stats;
  19. var camera, scene, renderer;
  20. var particleLight;
  21. var loader = new THREE.FontLoader();
  22. loader.load( 'fonts/gentilis_regular.typeface.json', function ( font ) {
  23. init( font );
  24. animate();
  25. } );
  26. function init( font ) {
  27. container = document.createElement( 'div' );
  28. document.body.appendChild( container );
  29. camera = new THREE.PerspectiveCamera( 40, window.innerWidth / window.innerHeight, 1, 2000 );
  30. camera.position.set( 0.0, 400, 400 * 3.5 );
  31. //
  32. scene = new THREE.Scene();
  33. var hdrCubeRenderTarget = null;
  34. new RGBELoader()
  35. .setDataType( THREE.UnsignedByteType )
  36. .setPath( 'textures/equirectangular/' )
  37. .load( 'pedestrian_overpass_1k.hdr', function ( hdrEquirect ) {
  38. hdrCubeRenderTarget = pmremGenerator.fromEquirectangular( hdrEquirect );
  39. hdrEquirect.dispose();
  40. pmremGenerator.dispose();
  41. // Materials
  42. var cubeWidth = 400;
  43. var numberOfSphersPerSide = 5;
  44. var sphereRadius = ( cubeWidth / numberOfSphersPerSide ) * 0.8 * 0.5;
  45. var stepSize = 1.0 / numberOfSphersPerSide;
  46. var geometry = new THREE.SphereBufferGeometry( sphereRadius, 32, 16 );
  47. var index = 0;
  48. for ( var alpha = 0; alpha <= 1.0; alpha += stepSize ) {
  49. for ( var beta = 0; beta <= 1.0; beta += stepSize ) {
  50. for ( var gamma = 0; gamma <= 1.0; gamma += stepSize ) {
  51. var diffuseColor = new THREE.Color().setHSL( alpha, 0.5, 0.25 );
  52. var material = new THREE.MeshPhysicalMaterial( {
  53. color: diffuseColor,
  54. metalness: 0,
  55. roughness: 0.5,
  56. clearcoat: 1.0 - alpha,
  57. clearcoatRoughness: 1.0 - beta,
  58. reflectivity: 1.0 - gamma,
  59. envMap: ( index % 2 ) == 1 ? hdrCubeRenderTarget.texture : null
  60. } );
  61. index ++;
  62. var mesh = new THREE.Mesh( geometry, material );
  63. mesh.position.x = alpha * 400 - 200;
  64. mesh.position.y = beta * 400 - 200;
  65. mesh.position.z = gamma * 400 - 200;
  66. scene.add( mesh );
  67. }
  68. index ++;
  69. }
  70. index ++;
  71. }
  72. scene.background = hdrCubeRenderTarget.texture;
  73. } );
  74. function addLabel( name, location ) {
  75. var textGeo = new THREE.TextBufferGeometry( name, {
  76. font: font,
  77. size: 20,
  78. height: 1,
  79. curveSegments: 1
  80. } );
  81. var textMaterial = new THREE.MeshBasicMaterial( { color: 0xffffff } );
  82. var textMesh = new THREE.Mesh( textGeo, textMaterial );
  83. textMesh.position.copy( location );
  84. scene.add( textMesh );
  85. }
  86. addLabel( "+clearcoat", new THREE.Vector3( - 350, 0, 0 ) );
  87. addLabel( "-clearcoat", new THREE.Vector3( 350, 0, 0 ) );
  88. addLabel( "+clearcoatRoughness", new THREE.Vector3( 0, - 300, 0 ) );
  89. addLabel( "-clearcoatRoughness", new THREE.Vector3( 0, 300, 0 ) );
  90. addLabel( "+reflectivity", new THREE.Vector3( 0, 0, - 300 ) );
  91. addLabel( "-reflectivity", new THREE.Vector3( 0, 0, 300 ) );
  92. particleLight = new THREE.Mesh( new THREE.SphereBufferGeometry( 4, 8, 8 ), new THREE.MeshBasicMaterial( { color: 0xffffff } ) );
  93. scene.add( particleLight );
  94. // Lights
  95. scene.add( new THREE.AmbientLight( 0x222222 ) );
  96. var directionalLight = new THREE.DirectionalLight( 0xffffff, 1 );
  97. directionalLight.position.set( 1, 1, 1 ).normalize();
  98. scene.add( directionalLight );
  99. var pointLight = new THREE.PointLight( 0xffffff, 2, 800 );
  100. particleLight.add( pointLight );
  101. //
  102. renderer = new THREE.WebGLRenderer( { antialias: true } );
  103. renderer.setPixelRatio( window.devicePixelRatio );
  104. renderer.setSize( window.innerWidth, window.innerHeight );
  105. container.appendChild( renderer.domElement );
  106. renderer.outputEncoding = THREE.sRGBEncoding;
  107. renderer.toneMapping = THREE.Uncharted2ToneMapping;
  108. renderer.toneMappingExposure = 0.75;
  109. //
  110. var pmremGenerator = new THREE.PMREMGenerator( renderer );
  111. pmremGenerator.compileEquirectangularShader();
  112. //
  113. stats = new Stats();
  114. container.appendChild( stats.dom );
  115. var controls = new OrbitControls( camera, renderer.domElement );
  116. window.addEventListener( 'resize', onWindowResize, false );
  117. }
  118. function onWindowResize() {
  119. camera.aspect = window.innerWidth / window.innerHeight;
  120. camera.updateProjectionMatrix();
  121. renderer.setSize( window.innerWidth, window.innerHeight );
  122. }
  123. //
  124. function animate() {
  125. requestAnimationFrame( animate );
  126. render();
  127. stats.update();
  128. }
  129. function render() {
  130. var timer = Date.now() * 0.00025;
  131. //camera.position.x = Math.cos( timer ) * 800;
  132. //camera.position.z = Math.sin( timer ) * 800;
  133. camera.lookAt( scene.position );
  134. particleLight.position.x = Math.sin( timer * 7 ) * 300;
  135. particleLight.position.y = Math.cos( timer * 5 ) * 400;
  136. particleLight.position.z = Math.cos( timer * 3 ) * 300;
  137. renderer.render( scene, camera );
  138. }
  139. </script>
  140. </body>
  141. </html>