webgl_tsl_clearcoat.html 6.4 KB

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