webgl_tsl_clearcoat.html 6.8 KB

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