webgl_materials_standard.html 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - materials - standard</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="info">
  11. <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> - webgl physically based material<br/>
  12. <a href="http://www.polycount.com/forum/showthread.php?t=130641" target="_blank" rel="noopener">Cerberus(FFVII Gun) model</a> by Andrew Maximov.
  13. </div>
  14. <script type="module">
  15. import * as THREE from '../build/three.module.js';
  16. import Stats from './jsm/libs/stats.module.js';
  17. import { TrackballControls } from './jsm/controls/TrackballControls.js';
  18. import { OBJLoader } from './jsm/loaders/OBJLoader.js';
  19. import { RGBELoader } from './jsm/loaders/RGBELoader.js';
  20. var statsEnabled = true;
  21. var container, stats;
  22. var camera, scene, renderer, controls;
  23. init();
  24. animate();
  25. function init() {
  26. container = document.createElement( 'div' );
  27. document.body.appendChild( container );
  28. renderer = new THREE.WebGLRenderer( { antialias: true } );
  29. renderer.setPixelRatio( window.devicePixelRatio );
  30. renderer.setSize( window.innerWidth, window.innerHeight );
  31. container.appendChild( renderer.domElement );
  32. renderer.outputEncoding = THREE.sRGBEncoding;
  33. renderer.toneMapping = THREE.ReinhardToneMapping;
  34. renderer.toneMappingExposure = 3;
  35. //
  36. scene = new THREE.Scene();
  37. camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 0.01, 1000 );
  38. camera.position.z = 2;
  39. controls = new TrackballControls( camera, renderer.domElement );
  40. //
  41. scene.add( new THREE.HemisphereLight( 0x443333, 0x222233, 4 ) );
  42. //
  43. var material = new THREE.MeshStandardMaterial();
  44. new OBJLoader()
  45. .setPath( 'models/obj/cerberus/' )
  46. .load( 'Cerberus.obj', function ( group ) {
  47. var loader = new THREE.TextureLoader()
  48. .setPath( 'models/obj/cerberus/' );
  49. material.roughness = 1; // attenuates roughnessMap
  50. material.metalness = 1; // attenuates metalnessMap
  51. var diffuseMap = loader.load( 'Cerberus_A.jpg' );
  52. diffuseMap.encoding = THREE.sRGBEncoding;
  53. material.map = diffuseMap;
  54. // roughness is in G channel, metalness is in B channel
  55. material.metalnessMap = material.roughnessMap = loader.load( 'Cerberus_RM.jpg' );
  56. material.normalMap = loader.load( 'Cerberus_N.jpg' );
  57. material.map.wrapS = THREE.RepeatWrapping;
  58. material.roughnessMap.wrapS = THREE.RepeatWrapping;
  59. material.metalnessMap.wrapS = THREE.RepeatWrapping;
  60. material.normalMap.wrapS = THREE.RepeatWrapping;
  61. group.traverse( function ( child ) {
  62. if ( child.isMesh ) {
  63. child.material = material;
  64. }
  65. } );
  66. group.position.x = - 0.45;
  67. group.rotation.y = - Math.PI / 2;
  68. scene.add( group );
  69. } );
  70. new RGBELoader()
  71. .setDataType( THREE.UnsignedByteType )
  72. .setPath( 'textures/equirectangular/' )
  73. .load( 'venice_sunset_1k.hdr', function ( hdrEquirect ) {
  74. var hdrCubeRenderTarget = pmremGenerator.fromEquirectangular( hdrEquirect );
  75. hdrEquirect.dispose();
  76. pmremGenerator.dispose();
  77. scene.background = hdrCubeRenderTarget.texture;
  78. scene.environment = hdrCubeRenderTarget.texture;
  79. } );
  80. var pmremGenerator = new THREE.PMREMGenerator( renderer );
  81. pmremGenerator.compileEquirectangularShader();
  82. //
  83. if ( statsEnabled ) {
  84. stats = new Stats();
  85. container.appendChild( stats.dom );
  86. }
  87. window.addEventListener( 'resize', onWindowResize, false );
  88. }
  89. //
  90. function onWindowResize() {
  91. renderer.setSize( window.innerWidth, window.innerHeight );
  92. camera.aspect = window.innerWidth / window.innerHeight;
  93. camera.updateProjectionMatrix();
  94. }
  95. //
  96. function animate() {
  97. requestAnimationFrame( animate );
  98. controls.update();
  99. renderer.render( scene, camera );
  100. if ( statsEnabled ) stats.update();
  101. }
  102. </script>
  103. </body>
  104. </html>