webgl_materials_variations_standard.html 6.6 KB

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