webgl_nodes_materials_physical_clearcoat.html 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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 { color, float, vec2, texture, normalMap, uv, MeshPhysicalNodeMaterial } from 'three/nodes';
  28. import { nodeFrame } from 'three/addons/renderers/webgl/nodes/WebGLNodes.js';
  29. import Stats from 'three/addons/libs/stats.module.js';
  30. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  31. import { HDRCubeTextureLoader } from 'three/addons/loaders/HDRCubeTextureLoader.js';
  32. import { FlakesTexture } from 'three/addons/textures/FlakesTexture.js';
  33. let container, stats;
  34. let camera, scene, renderer;
  35. let particleLight;
  36. let group;
  37. init();
  38. animate();
  39. function init() {
  40. container = document.createElement( 'div' );
  41. document.body.appendChild( container );
  42. camera = new THREE.PerspectiveCamera( 27, window.innerWidth / window.innerHeight, 1, 10000 );
  43. camera.position.z = 1000;
  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 ( hdrTexture ) {
  51. const geometry = new THREE.SphereGeometry( 80, 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. const normalMap1 = textureLoader.load( 'textures/carbon/Carbon_Normal.png' );
  58. normalMap1.wrapS = THREE.RepeatWrapping;
  59. normalMap1.wrapT = THREE.RepeatWrapping;
  60. const normalMap2 = textureLoader.load( 'textures/water/Water_1_M_Normal.jpg' );
  61. const normalMap3 = new THREE.CanvasTexture( new FlakesTexture() );
  62. normalMap3.wrapS = THREE.RepeatWrapping;
  63. normalMap3.wrapT = THREE.RepeatWrapping;
  64. normalMap3.anisotropy = 16;
  65. const normalMap4 = textureLoader.load( 'textures/golfball.jpg' );
  66. const clearcoatNormalMap = textureLoader.load( 'textures/pbr/Scratched_gold/Scratched_gold_01_1K_Normal.png' );
  67. // car paint
  68. const carPaintUV = uv().mul( vec2( 10, 6 ) );
  69. const carPaintNormalScale = vec2( 0.15 );
  70. let material = new MeshPhysicalNodeMaterial();
  71. material.clearcoatNode = float( 1 );
  72. material.clearcoatRoughnessNode = float( 0.1 );
  73. material.metalnessNode = float( 0.9 );
  74. material.roughnessNode = float( 0.5 );
  75. material.colorNode = color( 0x0000ff );
  76. material.normalNode = normalMap( texture( normalMap3, carPaintUV ), carPaintNormalScale );
  77. let mesh = new THREE.Mesh( geometry, material );
  78. mesh.position.x = - 100;
  79. mesh.position.y = 100;
  80. group.add( mesh );
  81. // fibers
  82. const fibersUV = uv().mul( 10 );
  83. material = new MeshPhysicalNodeMaterial();
  84. material.roughnessNode = float( 0.5 );
  85. material.clearcoatNode = float( 1 );
  86. material.clearcoatRoughnessNode = float( 0.1 );
  87. material.colorNode = texture( diffuse, fibersUV );
  88. material.normalNode = normalMap( texture( normalMap1, fibersUV ) );
  89. mesh = new THREE.Mesh( geometry, material );
  90. mesh.position.x = 100;
  91. mesh.position.y = 100;
  92. group.add( mesh );
  93. // golf
  94. material = new MeshPhysicalNodeMaterial();
  95. material.clearcoatNode = float( 1 );
  96. material.roughnessNode = float( 0.1 );
  97. material.metalnessNode = float( 0 );
  98. material.colorNode = color( 0xffffff );
  99. material.normalNode = normalMap( texture( normalMap4 ) );
  100. // y scale is negated to compensate for normal map handedness.
  101. material.clearcoatNormalNode = normalMap( texture( clearcoatNormalMap ), vec2( 2.0, - 2.0 ) );
  102. mesh = new THREE.Mesh( geometry, material );
  103. mesh.position.x = - 100;
  104. mesh.position.y = - 100;
  105. group.add( mesh );
  106. // clearcoat + normalmap
  107. material = new MeshPhysicalNodeMaterial();
  108. material.clearcoatNode = float( 1 );
  109. material.roughnessNode = float( 1 );
  110. material.metalnessNode = float( 1 );
  111. material.colorNode = color( 0xff0000 );
  112. material.normalNode = normalMap( texture( normalMap2 ), vec2( 0.15, 0.15 ) );
  113. // y scale is negated to compensate for normal map handedness.
  114. material.clearcoatNormalNode = normalMap( texture( clearcoatNormalMap ), vec2( 2.0, - 2.0 ) );
  115. mesh = new THREE.Mesh( geometry, material );
  116. mesh.position.x = 100;
  117. mesh.position.y = - 100;
  118. group.add( mesh );
  119. //
  120. scene.background = hdrTexture;
  121. scene.environment = hdrTexture;
  122. }
  123. );
  124. // LIGHTS
  125. particleLight = new THREE.Mesh(
  126. new THREE.SphereGeometry( 4, 8, 8 ),
  127. new THREE.MeshBasicMaterial( { color: 0xffffff } )
  128. );
  129. scene.add( particleLight );
  130. particleLight.add( new THREE.PointLight( 0xffffff, 1 ) );
  131. renderer = new THREE.WebGLRenderer();
  132. renderer.setPixelRatio( window.devicePixelRatio );
  133. renderer.setSize( window.innerWidth, window.innerHeight );
  134. container.appendChild( renderer.domElement );
  135. //
  136. renderer.toneMapping = THREE.ACESFilmicToneMapping;
  137. renderer.toneMappingExposure = 1.25;
  138. //
  139. //
  140. stats = new Stats();
  141. container.appendChild( stats.dom );
  142. // EVENTS
  143. new OrbitControls( camera, renderer.domElement );
  144. window.addEventListener( 'resize', onWindowResize );
  145. }
  146. //
  147. function onWindowResize() {
  148. const width = window.innerWidth;
  149. const height = window.innerHeight;
  150. camera.aspect = width / height;
  151. camera.updateProjectionMatrix();
  152. renderer.setSize( width, height );
  153. }
  154. //
  155. function animate() {
  156. requestAnimationFrame( animate );
  157. nodeFrame.update();
  158. render();
  159. stats.update();
  160. }
  161. function render() {
  162. const timer = Date.now() * 0.00025;
  163. particleLight.position.x = Math.sin( timer * 7 ) * 300;
  164. particleLight.position.y = Math.cos( timer * 5 ) * 400;
  165. particleLight.position.z = Math.cos( timer * 3 ) * 300;
  166. for ( let i = 0; i < group.children.length; i ++ ) {
  167. const child = group.children[ i ];
  168. child.rotation.y += 0.005;
  169. }
  170. renderer.render( scene, camera );
  171. }
  172. </script>
  173. </body>
  174. </html>