webgl_materials_variations_standard.html 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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 {
  15. AmbientLight,
  16. Color,
  17. DirectionalLight,
  18. FontLoader,
  19. LinearFilter,
  20. Mesh,
  21. MeshBasicMaterial,
  22. MeshStandardMaterial,
  23. PerspectiveCamera,
  24. PointLight,
  25. RepeatWrapping,
  26. Scene,
  27. SphereBufferGeometry,
  28. TextureLoader,
  29. TextBufferGeometry,
  30. UnsignedByteType,
  31. Uncharted2ToneMapping,
  32. Vector3,
  33. WebGLRenderer,
  34. } from "../build/three.module.js";
  35. import Stats from './jsm/libs/stats.module.js';
  36. import { OrbitControls } from './jsm/controls/OrbitControls.js';
  37. import { HDRCubeTextureLoader } from './jsm/loaders/HDRCubeTextureLoader.js';
  38. import { PMREMGenerator } from './jsm/pmrem/PMREMGenerator.js';
  39. import { PMREMCubeUVPacker } from './jsm/pmrem/PMREMCubeUVPacker.js';
  40. var container, stats;
  41. var camera, scene, renderer;
  42. var particleLight;
  43. var loader = new FontLoader();
  44. loader.load( 'fonts/gentilis_regular.typeface.json', function ( font ) {
  45. init( font );
  46. animate();
  47. } );
  48. function init( font ) {
  49. container = document.createElement( 'div' );
  50. document.body.appendChild( container );
  51. camera = new PerspectiveCamera( 40, window.innerWidth / window.innerHeight, 1, 2000 );
  52. camera.position.set( 0.0, 400, 400 * 3.5 );
  53. //
  54. var genCubeUrls = function ( prefix, postfix ) {
  55. return [
  56. prefix + 'px' + postfix, prefix + 'nx' + postfix,
  57. prefix + 'py' + postfix, prefix + 'ny' + postfix,
  58. prefix + 'pz' + postfix, prefix + 'nz' + postfix
  59. ];
  60. };
  61. scene = new Scene();
  62. var hdrUrls = genCubeUrls( './textures/cube/pisaHDR/', '.hdr' );
  63. var hdrCubeRenderTarget = null;
  64. // Materials
  65. var imgTexture = new TextureLoader().load( "textures/planets/moon_1024.jpg" );
  66. imgTexture.wrapS = imgTexture.wrapT = RepeatWrapping;
  67. imgTexture.anisotropy = 16;
  68. imgTexture = null;
  69. new HDRCubeTextureLoader()
  70. .setType( UnsignedByteType )
  71. .load( hdrUrls, function ( hdrCubeMap ) {
  72. var pmremGenerator = new PMREMGenerator( hdrCubeMap );
  73. pmremGenerator.update( renderer );
  74. var pmremCubeUVPacker = new PMREMCubeUVPacker( pmremGenerator.cubeLods );
  75. pmremCubeUVPacker.update( renderer );
  76. hdrCubeRenderTarget = pmremCubeUVPacker.CubeUVRenderTarget;
  77. var bumpScale = 1;
  78. var cubeWidth = 400;
  79. var numberOfSphersPerSide = 5;
  80. var sphereRadius = ( cubeWidth / numberOfSphersPerSide ) * 0.8 * 0.5;
  81. var stepSize = 1.0 / numberOfSphersPerSide;
  82. var geometry = new SphereBufferGeometry( sphereRadius, 32, 16 );
  83. var index = 0;
  84. for ( var alpha = 0; alpha <= 1.0; alpha += stepSize ) {
  85. for ( var beta = 0; beta <= 1.0; beta += stepSize ) {
  86. for ( var gamma = 0; gamma <= 1.0; gamma += stepSize ) {
  87. // basic monochromatic energy preservation
  88. var diffuseColor = new Color().setHSL( alpha, 0.5, gamma * 0.5 + 0.1 );
  89. var material = new MeshStandardMaterial( {
  90. map: imgTexture,
  91. bumpMap: imgTexture,
  92. bumpScale: bumpScale,
  93. color: diffuseColor,
  94. metalness: beta,
  95. roughness: 1.0 - alpha,
  96. envMap: index % 2 === 0 ? null : hdrCubeRenderTarget.texture
  97. } );
  98. index ++;
  99. var mesh = new Mesh( geometry, material );
  100. mesh.position.x = alpha * 400 - 200;
  101. mesh.position.y = beta * 400 - 200;
  102. mesh.position.z = gamma * 400 - 200;
  103. scene.add( mesh );
  104. }
  105. }
  106. index ++;
  107. }
  108. hdrCubeMap.magFilter = LinearFilter;
  109. hdrCubeMap.needsUpdate = true;
  110. scene.background = hdrCubeMap;
  111. pmremGenerator.dispose();
  112. pmremCubeUVPacker.dispose();
  113. } );
  114. function addLabel( name, location ) {
  115. var textGeo = new TextBufferGeometry( name, {
  116. font: font,
  117. size: 20,
  118. height: 1,
  119. curveSegments: 1
  120. } );
  121. var textMaterial = new MeshBasicMaterial( { color: 0xffffff } );
  122. var textMesh = new Mesh( textGeo, textMaterial );
  123. textMesh.position.copy( location );
  124. scene.add( textMesh );
  125. }
  126. addLabel( "+roughness", new Vector3( - 350, 0, 0 ) );
  127. addLabel( "-roughness", new Vector3( 350, 0, 0 ) );
  128. addLabel( "-metalness", new Vector3( 0, - 300, 0 ) );
  129. addLabel( "+metalness", new Vector3( 0, 300, 0 ) );
  130. addLabel( "-diffuse", new Vector3( 0, 0, - 300 ) );
  131. addLabel( "+diffuse", new Vector3( 0, 0, 300 ) );
  132. particleLight = new Mesh( new SphereBufferGeometry( 4, 8, 8 ), new MeshBasicMaterial( { color: 0xffffff } ) );
  133. scene.add( particleLight );
  134. // Lights
  135. scene.add( new AmbientLight( 0x222222 ) );
  136. var directionalLight = new DirectionalLight( 0xffffff, 1 );
  137. directionalLight.position.set( 1, 1, 1 ).normalize();
  138. scene.add( directionalLight );
  139. var pointLight = new PointLight( 0xffffff, 2, 800 );
  140. particleLight.add( pointLight );
  141. //
  142. renderer = new WebGLRenderer( { antialias: true } );
  143. renderer.setPixelRatio( window.devicePixelRatio );
  144. renderer.setSize( window.innerWidth, window.innerHeight );
  145. container.appendChild( renderer.domElement );
  146. renderer.gammaOutput = true;
  147. renderer.toneMapping = Uncharted2ToneMapping;
  148. renderer.toneMappingExposure = 0.75;
  149. //
  150. stats = new Stats();
  151. container.appendChild( stats.dom );
  152. var controls = new OrbitControls( camera, renderer.domElement );
  153. window.addEventListener( 'resize', onWindowResize, false );
  154. }
  155. function onWindowResize() {
  156. camera.aspect = window.innerWidth / window.innerHeight;
  157. camera.updateProjectionMatrix();
  158. renderer.setSize( window.innerWidth, window.innerHeight );
  159. }
  160. //
  161. function animate() {
  162. requestAnimationFrame( animate );
  163. render();
  164. stats.update();
  165. }
  166. function render() {
  167. var timer = Date.now() * 0.00025;
  168. //camera.position.x = Math.cos( timer ) * 800;
  169. //camera.position.z = Math.sin( timer ) * 800;
  170. camera.lookAt( scene.position );
  171. particleLight.position.x = Math.sin( timer * 7 ) * 300;
  172. particleLight.position.y = Math.cos( timer * 5 ) * 400;
  173. particleLight.position.z = Math.cos( timer * 3 ) * 300;
  174. renderer.render( scene, camera );
  175. }
  176. </script>
  177. </body>
  178. </html>