webgl_materials_variations_standard.html 5.9 KB

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