webgl_materials_variations_standard.html 6.0 KB

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