webgl_nodes_materials_physical_clearcoat.html 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - materials - clearcoat nodes</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> webgl - materials - clearcoat nodes
  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. "three/nodes": "./jsm/nodes/Nodes.js"
  22. }
  23. }
  24. </script>
  25. <script type="module">
  26. import * as THREE from 'three';
  27. import * as Nodes from 'three/nodes';
  28. import { color, float, vec2, texture, normalMap, uv, mul } from 'three/nodes';
  29. import { nodeFrame } from 'three/addons/renderers/webgl/nodes/WebGLNodes.js';
  30. import Stats from 'three/addons/libs/stats.module.js';
  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 container, stats;
  35. let camera, scene, renderer;
  36. let particleLight;
  37. let group;
  38. init();
  39. animate();
  40. function init() {
  41. container = document.createElement( 'div' );
  42. document.body.appendChild( container );
  43. camera = new THREE.PerspectiveCamera( 27, window.innerWidth / window.innerHeight, 1, 10000 );
  44. camera.position.z = 1000;
  45. scene = new THREE.Scene();
  46. group = new THREE.Group();
  47. scene.add( group );
  48. new HDRCubeTextureLoader()
  49. .setPath( 'textures/cube/pisaHDR/' )
  50. .load( [ 'px.hdr', 'nx.hdr', 'py.hdr', 'ny.hdr', 'pz.hdr', 'nz.hdr' ],
  51. function ( hdrTexture ) {
  52. const geometry = new THREE.SphereGeometry( 80, 64, 32 );
  53. const textureLoader = new THREE.TextureLoader();
  54. const diffuse = textureLoader.load( 'textures/carbon/Carbon.png' );
  55. diffuse.encoding = THREE.sRGBEncoding;
  56. diffuse.wrapS = THREE.RepeatWrapping;
  57. diffuse.wrapT = THREE.RepeatWrapping;
  58. const normalMap1 = textureLoader.load( 'textures/carbon/Carbon_Normal.png' );
  59. normalMap1.wrapS = THREE.RepeatWrapping;
  60. normalMap1.wrapT = THREE.RepeatWrapping;
  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.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. const carPaintUV = mul( uv(), vec2( 10, 6 ) );
  70. const carPaintNormalScale = vec2( 0.15 );
  71. let material = new Nodes.MeshPhysicalNodeMaterial();
  72. material.clearcoatNode = float( 1 );
  73. material.clearcoatRoughnessNode = float( 0.1 );
  74. material.metalnessNode = float( 0.9 );
  75. material.roughnessNode = float( 0.5 );
  76. material.colorNode = color( 0x0000ff );
  77. material.normalNode = normalMap( texture( normalMap3, carPaintUV ), carPaintNormalScale );
  78. let mesh = new THREE.Mesh( geometry, material );
  79. mesh.position.x = - 100;
  80. mesh.position.y = 100;
  81. group.add( mesh );
  82. // fibers
  83. const fibersUV = mul( uv(), 10 );
  84. material = new Nodes.MeshPhysicalNodeMaterial();
  85. material.roughnessNode = float( 0.5 );
  86. material.clearcoatNode = float( 1 );
  87. material.clearcoatRoughnessNode = float( 0.1 );
  88. material.colorNode = texture( diffuse, fibersUV );
  89. material.normalNode = normalMap( texture( normalMap1, fibersUV ) );
  90. mesh = new THREE.Mesh( geometry, material );
  91. mesh.position.x = 100;
  92. mesh.position.y = 100;
  93. group.add( mesh );
  94. // golf
  95. material = new Nodes.MeshPhysicalNodeMaterial();
  96. material.clearcoatNode = float( 1 );
  97. material.roughnessNode = float( 0.1 );
  98. material.metalnessNode = float( 0 );
  99. material.colorNode = color( 0xffffff );
  100. material.normalNode = normalMap( texture( normalMap4 ) );
  101. // y scale is negated to compensate for normal map handedness.
  102. material.clearcoatNormalNode = normalMap( texture( clearcoatNormalMap ), vec2( 2.0, - 2.0 ) );
  103. mesh = new THREE.Mesh( geometry, material );
  104. mesh.position.x = - 100;
  105. mesh.position.y = - 100;
  106. group.add( mesh );
  107. // clearcoat + normalmap
  108. material = new Nodes.MeshPhysicalNodeMaterial();
  109. material.clearcoatNode = float( 1 );
  110. material.roughnessNode = float( 1 );
  111. material.metalnessNode = float( 1 );
  112. material.colorNode = color( 0xff0000 );
  113. material.normalNode = normalMap( texture( normalMap2 ), vec2( 0.15, 0.15 ) );
  114. // y scale is negated to compensate for normal map handedness.
  115. material.clearcoatNormalNode = normalMap( texture( clearcoatNormalMap ), vec2( 2.0, - 2.0 ) );
  116. mesh = new THREE.Mesh( geometry, material );
  117. mesh.position.x = 100;
  118. mesh.position.y = - 100;
  119. group.add( mesh );
  120. //
  121. scene.background = hdrTexture;
  122. scene.environment = hdrTexture;
  123. }
  124. );
  125. // LIGHTS
  126. particleLight = new THREE.Mesh(
  127. new THREE.SphereGeometry( 4, 8, 8 ),
  128. new THREE.MeshBasicMaterial( { color: 0xffffff } )
  129. );
  130. scene.add( particleLight );
  131. particleLight.add( new THREE.PointLight( 0xffffff, 1 ) );
  132. renderer = new THREE.WebGLRenderer();
  133. renderer.setPixelRatio( window.devicePixelRatio );
  134. renderer.setSize( window.innerWidth, window.innerHeight );
  135. container.appendChild( renderer.domElement );
  136. //
  137. renderer.toneMapping = THREE.ACESFilmicToneMapping;
  138. renderer.toneMappingExposure = 1.25;
  139. //
  140. renderer.outputEncoding = THREE.sRGBEncoding;
  141. //
  142. stats = new Stats();
  143. container.appendChild( stats.dom );
  144. // EVENTS
  145. new OrbitControls( camera, renderer.domElement );
  146. window.addEventListener( 'resize', onWindowResize );
  147. }
  148. //
  149. function onWindowResize() {
  150. const width = window.innerWidth;
  151. const height = window.innerHeight;
  152. camera.aspect = width / height;
  153. camera.updateProjectionMatrix();
  154. renderer.setSize( width, height );
  155. }
  156. //
  157. function animate() {
  158. requestAnimationFrame( animate );
  159. nodeFrame.update();
  160. render();
  161. stats.update();
  162. }
  163. function render() {
  164. const timer = Date.now() * 0.00025;
  165. particleLight.position.x = Math.sin( timer * 7 ) * 300;
  166. particleLight.position.y = Math.cos( timer * 5 ) * 400;
  167. particleLight.position.z = Math.cos( timer * 3 ) * 300;
  168. for ( let i = 0; i < group.children.length; i ++ ) {
  169. const child = group.children[ i ];
  170. child.rotation.y += 0.005;
  171. }
  172. renderer.render( scene, camera );
  173. }
  174. </script>
  175. </body>
  176. </html>