webgl_materials_variations_standard.html 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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> - 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. <!-- 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. THREE.ColorManagement.enabled = false; // TODO: Consider enabling color management.
  32. let container, stats;
  33. let camera, scene, renderer;
  34. let particleLight;
  35. const loader = new FontLoader();
  36. loader.load( 'fonts/gentilis_regular.typeface.json', function ( font ) {
  37. init( font );
  38. animate();
  39. } );
  40. function init( font ) {
  41. container = document.createElement( 'div' );
  42. document.body.appendChild( container );
  43. camera = new THREE.PerspectiveCamera( 40, window.innerWidth / window.innerHeight, 1, 2500 );
  44. camera.position.set( 0.0, 400, 400 * 3.5 );
  45. scene = new THREE.Scene();
  46. // Materials
  47. let imgTexture = new THREE.TextureLoader().load( 'textures/planets/moon_1024.jpg' );
  48. imgTexture.wrapS = imgTexture.wrapT = THREE.RepeatWrapping;
  49. imgTexture.anisotropy = 16;
  50. imgTexture = null;
  51. new RGBELoader()
  52. .setPath( 'textures/equirectangular/' )
  53. .load( 'pedestrian_overpass_1k.hdr', function ( texture ) {
  54. texture.mapping = THREE.EquirectangularReflectionMapping;
  55. const bumpScale = 1;
  56. const cubeWidth = 400;
  57. const numberOfSphersPerSide = 5;
  58. const sphereRadius = ( cubeWidth / numberOfSphersPerSide ) * 0.8 * 0.5;
  59. const stepSize = 1.0 / numberOfSphersPerSide;
  60. const geometry = new THREE.SphereGeometry( sphereRadius, 32, 16 );
  61. let index = 0;
  62. for ( let alpha = 0; alpha <= 1.0; alpha += stepSize ) {
  63. for ( let beta = 0; beta <= 1.0; beta += stepSize ) {
  64. for ( let gamma = 0; gamma <= 1.0; gamma += stepSize ) {
  65. // basic monochromatic energy preservation
  66. const diffuseColor = new THREE.Color().setHSL( alpha, 0.5, gamma * 0.5 + 0.1 );
  67. const material = new THREE.MeshStandardMaterial( {
  68. map: imgTexture,
  69. bumpMap: imgTexture,
  70. bumpScale: bumpScale,
  71. color: diffuseColor,
  72. metalness: beta,
  73. roughness: 1.0 - alpha,
  74. envMap: index % 2 === 0 ? null : texture
  75. } );
  76. index ++;
  77. const 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. }
  84. index ++;
  85. }
  86. scene.background = texture;
  87. } );
  88. function addLabel( name, location ) {
  89. const textGeo = new TextGeometry( name, {
  90. font: font,
  91. size: 20,
  92. height: 1,
  93. curveSegments: 1
  94. } );
  95. const textMaterial = new THREE.MeshBasicMaterial( { color: 0xffffff } );
  96. const textMesh = new THREE.Mesh( textGeo, textMaterial );
  97. textMesh.position.copy( location );
  98. scene.add( textMesh );
  99. }
  100. addLabel( '+roughness', new THREE.Vector3( - 350, 0, 0 ) );
  101. addLabel( '-roughness', new THREE.Vector3( 350, 0, 0 ) );
  102. addLabel( '-metalness', new THREE.Vector3( 0, - 300, 0 ) );
  103. addLabel( '+metalness', new THREE.Vector3( 0, 300, 0 ) );
  104. addLabel( '-diffuse', new THREE.Vector3( 0, 0, - 300 ) );
  105. addLabel( '+diffuse', new THREE.Vector3( 0, 0, 300 ) );
  106. particleLight = new THREE.Mesh( new THREE.SphereGeometry( 4, 8, 8 ), new THREE.MeshBasicMaterial( { color: 0xffffff } ) );
  107. scene.add( particleLight );
  108. // Lights
  109. scene.add( new THREE.AmbientLight( 0x222222 ) );
  110. const directionalLight = new THREE.DirectionalLight( 0xffffff, 1 );
  111. directionalLight.position.set( 1, 1, 1 ).normalize();
  112. scene.add( directionalLight );
  113. const pointLight = new THREE.PointLight( 0xffffff, 2, 800 );
  114. particleLight.add( pointLight );
  115. //
  116. renderer = new THREE.WebGLRenderer( { antialias: true } );
  117. renderer.setPixelRatio( window.devicePixelRatio );
  118. renderer.setSize( window.innerWidth, window.innerHeight );
  119. container.appendChild( renderer.domElement );
  120. renderer.toneMapping = THREE.ACESFilmicToneMapping;
  121. renderer.toneMappingExposure = 0.75;
  122. //
  123. stats = new Stats();
  124. container.appendChild( stats.dom );
  125. const controls = new OrbitControls( camera, renderer.domElement );
  126. controls.minDistance = 200;
  127. controls.maxDistance = 2000;
  128. window.addEventListener( 'resize', onWindowResize );
  129. }
  130. function onWindowResize() {
  131. camera.aspect = window.innerWidth / window.innerHeight;
  132. camera.updateProjectionMatrix();
  133. renderer.setSize( window.innerWidth, window.innerHeight );
  134. }
  135. //
  136. function animate() {
  137. requestAnimationFrame( animate );
  138. render();
  139. stats.update();
  140. }
  141. function render() {
  142. const timer = Date.now() * 0.00025;
  143. //camera.position.x = Math.cos( timer ) * 800;
  144. //camera.position.z = Math.sin( timer ) * 800;
  145. camera.lookAt( scene.position );
  146. particleLight.position.x = Math.sin( timer * 7 ) * 300;
  147. particleLight.position.y = Math.cos( timer * 5 ) * 400;
  148. particleLight.position.z = Math.cos( timer * 3 ) * 300;
  149. renderer.render( scene, camera );
  150. }
  151. </script>
  152. </body>
  153. </html>