webgl_materials_variations_standard.html 5.8 KB

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