webgl_materials_physical_clearcoat.html 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - materials - clearcoat</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 - materials - clearcoat
  12. </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 { HDRCubeTextureLoader } from './jsm/loaders/HDRCubeTextureLoader.js';
  18. import { PMREMGenerator } from './jsm/pmrem/PMREMGenerator.js';
  19. import { PMREMCubeUVPacker } from './jsm/pmrem/PMREMCubeUVPacker.js';
  20. var container, stats;
  21. var camera, scene, renderer;
  22. var particleLight;
  23. var group;
  24. init();
  25. animate();
  26. function init() {
  27. container = document.createElement( 'div' );
  28. document.body.appendChild( container );
  29. camera = new THREE.PerspectiveCamera( 27, window.innerWidth / window.innerHeight, 1, 10000 );
  30. camera.position.z = 1000;
  31. scene = new THREE.Scene();
  32. group = new THREE.Group();
  33. scene.add( group );
  34. new HDRCubeTextureLoader()
  35. .setDataType( THREE.UnsignedByteType )
  36. .setPath( './textures/cube/pisaHDR/' )
  37. .load( [ 'px.hdr', 'nx.hdr', 'py.hdr', 'ny.hdr', 'pz.hdr', 'nz.hdr' ],
  38. function ( hdrCubeMap ) {
  39. var pmremGenerator = new PMREMGenerator( hdrCubeMap );
  40. pmremGenerator.update( renderer );
  41. var pmremCubeUVPacker = new PMREMCubeUVPacker( pmremGenerator.cubeLods );
  42. pmremCubeUVPacker.update( renderer );
  43. var hdrCubeRenderTarget = pmremCubeUVPacker.CubeUVRenderTarget;
  44. var geometry = new THREE.SphereBufferGeometry( 80, 64, 32 );
  45. var textureLoader = new THREE.TextureLoader();
  46. var diffuse = textureLoader.load( "textures/carbon/Carbon.png" );
  47. diffuse.wrapS = THREE.RepeatWrapping;
  48. diffuse.wrapT = THREE.RepeatWrapping;
  49. diffuse.repeat.x = 10;
  50. diffuse.repeat.y = 10;
  51. var normalMap = textureLoader.load( "textures/carbon/Carbon_Normal.png" );
  52. normalMap.wrapS = THREE.RepeatWrapping;
  53. normalMap.wrapT = THREE.RepeatWrapping;
  54. var normalMap2 = textureLoader.load( "textures/water/Water_1_M_Normal.jpg" );
  55. var normalMap3 = textureLoader.load( "textures/flakes.png" );
  56. normalMap3.wrapS = THREE.RepeatWrapping;
  57. normalMap3.wrapT = THREE.RepeatWrapping;
  58. normalMap3.repeat.x = 10;
  59. normalMap3.repeat.y = 10;
  60. normalMap3.anisotropy = 16;
  61. var normalMap4 = textureLoader.load( "textures/golfball.jpg" );
  62. var clearcoatNormaMap = textureLoader.load( "textures/pbr/Scratched_gold/Scratched_gold_01_1K_Normal.png" );
  63. // car paint
  64. var material = new THREE.MeshPhysicalMaterial( {
  65. clearcoat: 1.0,
  66. clearcoatRoughness: 0.1,
  67. metalness: 0.9,
  68. roughness: 0.5,
  69. color: 0x0000ff,
  70. envMap: hdrCubeRenderTarget.texture,
  71. normalMap: normalMap3,
  72. normalScale: new THREE.Vector2( 0.1, 0.1 )
  73. } );
  74. var mesh = new THREE.Mesh( geometry, material );
  75. mesh.position.x = - 100;
  76. mesh.position.y = 100;
  77. group.add( mesh );
  78. // fibers
  79. var material = new THREE.MeshPhysicalMaterial( {
  80. clearcoat: 1.0,
  81. clearcoatRoughness: 0.1,
  82. envMap: hdrCubeRenderTarget.texture,
  83. map: diffuse,
  84. normalMap: normalMap
  85. } );
  86. var mesh = new THREE.Mesh( geometry, material );
  87. mesh.position.x = 100;
  88. mesh.position.y = 100;
  89. group.add( mesh );
  90. // golf
  91. var material = new THREE.MeshPhysicalMaterial( {
  92. metalness: 0.0,
  93. roughness: 0.1,
  94. clearcoat: 1.0,
  95. normalMap: normalMap4,
  96. envMap: hdrCubeRenderTarget.texture,
  97. clearcoatNormalMap: clearcoatNormaMap,
  98. clearcoatNormalScale: new THREE.Vector2( 2.0, 2.0 )
  99. } );
  100. var mesh = new THREE.Mesh( geometry, material );
  101. mesh.position.x = - 100;
  102. mesh.position.y = - 100;
  103. group.add( mesh );
  104. // clearcoat + normalmap
  105. var material = new THREE.MeshPhysicalMaterial( {
  106. clearcoat: 1.0,
  107. metalness: 1.0,
  108. color: 0xff0000,
  109. envMap: hdrCubeRenderTarget.texture,
  110. normalMap: normalMap2,
  111. normalScale: new THREE.Vector2( 0.15, 0.15 ),
  112. clearcoatNormalMap: clearcoatNormaMap,
  113. clearcoatNormalScale: new THREE.Vector2( 2.0, 2.0 )
  114. } );
  115. var mesh = new THREE.Mesh( geometry, material );
  116. mesh.position.x = 100;
  117. mesh.position.y = - 100;
  118. group.add( mesh );
  119. //
  120. hdrCubeMap.magFilter = THREE.LinearFilter;
  121. hdrCubeMap.needsUpdate = true;
  122. scene.background = hdrCubeMap;
  123. pmremGenerator.dispose();
  124. pmremCubeUVPacker.dispose();
  125. }
  126. );
  127. // LIGHTS
  128. particleLight = new THREE.Mesh(
  129. new THREE.SphereBufferGeometry( 4, 8, 8 ),
  130. new THREE.MeshBasicMaterial( { color: 0xffffff } )
  131. );
  132. scene.add( particleLight );
  133. particleLight.add( new THREE.PointLight( 0xffffff, 1 ) );
  134. renderer = new THREE.WebGLRenderer();
  135. renderer.setPixelRatio( window.devicePixelRatio );
  136. renderer.setSize( window.innerWidth, window.innerHeight );
  137. container.appendChild( renderer.domElement );
  138. //
  139. renderer.gammaInput = true;
  140. renderer.gammaOutput = true;
  141. //
  142. stats = new Stats();
  143. container.appendChild( stats.dom );
  144. // EVENTS
  145. var controls = new OrbitControls( camera, renderer.domElement );
  146. window.addEventListener( 'resize', onWindowResize, false );
  147. }
  148. //
  149. function onWindowResize() {
  150. var width = window.innerWidth;
  151. var height = window.innerHeight;
  152. camera.aspect = width / height;
  153. camera.updateProjectionMatrix();
  154. renderer.setSize( width, height );
  155. }
  156. //
  157. function animate() {
  158. requestAnimationFrame( animate );
  159. render();
  160. stats.update();
  161. }
  162. function render() {
  163. var timer = Date.now() * 0.00025;
  164. particleLight.position.x = Math.sin( timer * 7 ) * 300;
  165. particleLight.position.y = Math.cos( timer * 5 ) * 400;
  166. particleLight.position.z = Math.cos( timer * 3 ) * 300;
  167. for ( var i = 0; i < group.children.length; i ++ ) {
  168. var child = group.children[ i ];
  169. child.rotation.y += 0.005;
  170. }
  171. renderer.render( scene, camera );
  172. }
  173. </script>
  174. </body>
  175. </html>