webgpu_clearcoat.html 6.5 KB

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