webgl_materials_physical_clearcoat.html 6.0 KB

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