webgpu_clearcoat.html 6.7 KB

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