webgl_materials_variations_standard.html 6.0 KB

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