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