webgl_materials_physical_clearcoat.html 6.2 KB

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