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