webgpu_clearcoat.html 6.4 KB

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