webgl_nodes_materials_physical_clearcoat.html 6.9 KB

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