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