webgpu_clearcoat.html 6.3 KB

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