webgl_nodes_materials_standard.html 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - materials - standard (nodes)</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="importmap">
  15. {
  16. "imports": {
  17. "three": "../build/three.module.js",
  18. "three/addons/": "./jsm/",
  19. "three/nodes": "./jsm/nodes/Nodes.js"
  20. }
  21. }
  22. </script>
  23. <script type="module">
  24. import * as THREE from 'three';
  25. import { texture, uniform, normalMap, MeshStandardNodeMaterial, NodeObjectLoader } from 'three/nodes';
  26. import Stats from 'three/addons/libs/stats.module.js';
  27. import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
  28. import { TrackballControls } from 'three/addons/controls/TrackballControls.js';
  29. import { OBJLoader } from 'three/addons/loaders/OBJLoader.js';
  30. import { RGBELoader } from 'three/addons/loaders/RGBELoader.js';
  31. import { nodeFrame } from 'three/addons/renderers/webgl-legacy/nodes/WebGLNodes.js';
  32. let container, stats;
  33. let camera, scene, renderer, controls;
  34. init();
  35. animate();
  36. function init() {
  37. container = document.createElement( 'div' );
  38. document.body.appendChild( container );
  39. renderer = new THREE.WebGLRenderer( { antialias: true } );
  40. renderer.setPixelRatio( window.devicePixelRatio );
  41. renderer.setSize( window.innerWidth, window.innerHeight );
  42. container.appendChild( renderer.domElement );
  43. renderer.toneMapping = THREE.ReinhardToneMapping;
  44. renderer.toneMappingExposure = 3;
  45. //
  46. scene = new THREE.Scene();
  47. camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 0.01, 1000 );
  48. camera.position.z = 2;
  49. controls = new TrackballControls( camera, renderer.domElement );
  50. // Test Extended Material
  51. class MeshCustomNodeMaterial extends MeshStandardNodeMaterial {
  52. constructor() {
  53. super();
  54. }
  55. }
  56. // Extends Serialization Material
  57. const superCreateMaterialFromType = THREE.MaterialLoader.createMaterialFromType;
  58. THREE.MaterialLoader.createMaterialFromType = function ( type ) {
  59. const materialLib = {
  60. MeshCustomNodeMaterial
  61. };
  62. if ( materialLib[ type ] !== undefined ) {
  63. return new materialLib[ type ]();
  64. }
  65. return superCreateMaterialFromType.call( this, type );
  66. };
  67. //
  68. const material = new MeshCustomNodeMaterial();
  69. new OBJLoader()
  70. .setPath( 'models/obj/cerberus/' )
  71. .load( 'Cerberus.obj', function ( group ) {
  72. const loaderManager = new THREE.LoadingManager();
  73. const loader = new THREE.TextureLoader( loaderManager )
  74. .setPath( 'models/obj/cerberus/' );
  75. const diffuseMap = loader.load( 'Cerberus_A.jpg' );
  76. diffuseMap.wrapS = THREE.RepeatWrapping;
  77. diffuseMap.colorSpace = THREE.SRGBColorSpace;
  78. const rmMap = loader.load( 'Cerberus_RM.jpg' );
  79. rmMap.wrapS = THREE.RepeatWrapping;
  80. const normalMapTexture = loader.load( 'Cerberus_N.jpg' );
  81. normalMapTexture.wrapS = THREE.RepeatWrapping;
  82. const mgMapNode = texture( rmMap );
  83. material.colorNode = texture( diffuseMap ).mul( uniform( material.color ) );
  84. // roughness is in G channel, metalness is in B channel
  85. material.roughnessNode = mgMapNode.g;
  86. material.metalnessNode = mgMapNode.b;
  87. material.normalNode = normalMap( texture( normalMapTexture ) );
  88. group.traverse( function ( child ) {
  89. if ( child.isMesh ) {
  90. child.material = material;
  91. }
  92. } );
  93. group.position.x = - 0.45;
  94. group.rotation.y = - Math.PI / 2;
  95. //scene.add( group );
  96. // TODO: Serialization test
  97. loaderManager.onLoad = () => {
  98. const groupJSON = JSON.stringify( group.toJSON() );
  99. const objectLoader = new NodeObjectLoader();
  100. objectLoader.parse( JSON.parse( groupJSON ), ( newGroup ) => {
  101. //scene.remove( group );
  102. newGroup.position.copy( group.position );
  103. newGroup.rotation.copy( group.rotation );
  104. scene.add( newGroup );
  105. console.log( 'Serialized!' );
  106. } );
  107. };
  108. } );
  109. const environments = {
  110. 'Venice Sunset': { filename: 'venice_sunset_1k.hdr' },
  111. 'Overpass': { filename: 'pedestrian_overpass_1k.hdr' }
  112. };
  113. function loadEnvironment( name ) {
  114. if ( environments[ name ].texture !== undefined ) {
  115. scene.background = environments[ name ].texture;
  116. scene.environment = environments[ name ].texture;
  117. return;
  118. }
  119. const filename = environments[ name ].filename;
  120. new RGBELoader()
  121. .setPath( 'textures/equirectangular/' )
  122. .load( filename, function ( hdrEquirect ) {
  123. const hdrCubeRenderTarget = pmremGenerator.fromEquirectangular( hdrEquirect );
  124. hdrEquirect.dispose();
  125. scene.background = hdrCubeRenderTarget.texture;
  126. scene.environment = hdrCubeRenderTarget.texture;
  127. environments[ name ].texture = hdrCubeRenderTarget.texture;
  128. } );
  129. }
  130. const params = {
  131. environment: Object.keys( environments )[ 0 ]
  132. };
  133. loadEnvironment( params.environment );
  134. const gui = new GUI();
  135. gui.add( params, 'environment', Object.keys( environments ) ).onChange( function ( value ) {
  136. loadEnvironment( value );
  137. } );
  138. gui.open();
  139. const pmremGenerator = new THREE.PMREMGenerator( renderer );
  140. pmremGenerator.compileEquirectangularShader();
  141. //
  142. stats = new Stats();
  143. container.appendChild( stats.dom );
  144. window.addEventListener( 'resize', onWindowResize );
  145. }
  146. //
  147. function onWindowResize() {
  148. renderer.setSize( window.innerWidth, window.innerHeight );
  149. camera.aspect = window.innerWidth / window.innerHeight;
  150. camera.updateProjectionMatrix();
  151. }
  152. //
  153. function animate() {
  154. requestAnimationFrame( animate );
  155. nodeFrame.update();
  156. controls.update();
  157. renderer.render( scene, camera );
  158. stats.update();
  159. }
  160. </script>
  161. </body>
  162. </html>