webgpu_clearcoat.html 6.6 KB

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