webgl_materials_variations_physical.html 6.0 KB

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