webgl_materials_clearcoat_normalmap.html 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - materials - clearcoat normal</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. <style>
  9. #vt {
  10. display: none
  11. }
  12. #vt,
  13. #vt a {
  14. color: orange;
  15. }
  16. </style>
  17. </head>
  18. <body>
  19. <div id="info">
  20. <a href="http://threejs.org" target="_blank" rel="noopener">three.js</a> - webgl - materials - clearcoat normal
  21. demo.<br />
  22. </div>
  23. <script type="module">
  24. import * as THREE from '../build/three.module.js';
  25. import Stats from './jsm/libs/stats.module.js';
  26. import { GUI } from './jsm/libs/dat.gui.module.js';
  27. import { OrbitControls } from './jsm/controls/OrbitControls.js';
  28. import { HDRCubeTextureLoader } from './jsm/loaders/HDRCubeTextureLoader.js';
  29. import { PMREMGenerator } from './jsm/pmrem/PMREMGenerator.js';
  30. import { PMREMCubeUVPacker } from './jsm/pmrem/PMREMCubeUVPacker.js';
  31. var container, stats;
  32. var camera, scene, renderer;
  33. var guiParams = {
  34. metalness: 0.0,
  35. roughness: 1.0,
  36. clearCoat: 1.0,
  37. clearCoatRoughness: 0.0,
  38. reflectivity: 0.0,
  39. };
  40. var guiNormalMapNames = [
  41. "face",
  42. "tentacle"
  43. ];
  44. var guiMatParams = {
  45. normalMap: "face",
  46. normalScale: 1.0,
  47. clearCoatNormalMap: "tentacle",
  48. clearCoatNormalScale: 1.0,
  49. };
  50. var particleLight;
  51. var meshes = [];
  52. var mats = [];
  53. var normalMaps = {};
  54. const sphereSize = 80;
  55. const sphereSpacing = 1.25 * sphereSize * 2;
  56. const variationsX = [
  57. ( mat, scale, map ) => { },
  58. ( mat, scale, map ) => {
  59. mat.clearCoatNormalScale = new THREE.Vector2( scale, scale );
  60. if ( mat.clearCoatNormalMap !== map ) {
  61. mat.clearCoatNormalMap = map;
  62. mat.needsUpdate = true;
  63. }
  64. },
  65. ];
  66. const variationsY = [
  67. ( mat, scale, map ) => { },
  68. ( mat, scale, map ) => {
  69. mat.normalScale = new THREE.Vector2( scale, scale );
  70. if ( mat.normalMap !== map ) {
  71. mat.normalMap = map;
  72. mat.needsUpdate = true;
  73. }
  74. },
  75. ];
  76. const maxI = variationsX.length;
  77. const maxJ = variationsY.length;
  78. new THREE.FontLoader()
  79. .load( 'fonts/gentilis_regular.typeface.json',
  80. function ( font ) {
  81. init( font );
  82. animate();
  83. }
  84. );
  85. function init( font ) {
  86. container = document.createElement( 'div' );
  87. document.body.appendChild( container );
  88. camera = new THREE.PerspectiveCamera( 27, window.innerWidth / window.innerHeight, 1, 10000 );
  89. camera.position.z = 1500;
  90. var genCubeUrls = function ( prefix, postfix ) {
  91. return [
  92. prefix + 'px' + postfix, prefix + 'nx' + postfix,
  93. prefix + 'py' + postfix, prefix + 'ny' + postfix,
  94. prefix + 'pz' + postfix, prefix + 'nz' + postfix
  95. ];
  96. };
  97. scene = new THREE.Scene();
  98. var hdrUrls = genCubeUrls( './textures/cube/pisaHDR/', '.hdr' );
  99. new HDRCubeTextureLoader()
  100. .setType( THREE.UnsignedByteType )
  101. .load( hdrUrls,
  102. function ( hdrCubeMap ) {
  103. var pmremGenerator = new PMREMGenerator( hdrCubeMap );
  104. pmremGenerator.update( renderer );
  105. var pmremCubeUVPacker = new PMREMCubeUVPacker( pmremGenerator.cubeLods );
  106. pmremCubeUVPacker.update( renderer );
  107. var hdrCubeRenderTarget = pmremCubeUVPacker.CubeUVRenderTarget;
  108. var geometry = new THREE.SphereBufferGeometry( 80, 64, 32 );
  109. var textureLoader = new THREE.TextureLoader();
  110. normalMaps.tentacle = textureLoader
  111. .load( "textures/nvidia_tentacle/tentacle_tangent_space.png" );
  112. normalMaps.face = textureLoader
  113. .load( "models/gltf/LeePerrySmith/Infinite-Level_02_Tangent_SmoothUV.jpg" );
  114. var matParams = {
  115. envMap: hdrCubeRenderTarget.texture,
  116. };
  117. for ( var ii = 0; ii < maxI; ii ++ ) {
  118. if ( ! mats[ ii ] ) {
  119. mats[ ii ] = [];
  120. }
  121. for ( var jj = 0; jj < maxJ; jj ++ ) {
  122. var mat = mats[ ii ][ jj ] = new THREE.MeshPhysicalMaterial( matParams );
  123. variationsX[ ii ]( mat, 1, normalMaps.tentacle );
  124. variationsY[ jj ]( mat, 1, normalMaps.face );
  125. var mesh = new THREE.Mesh( geometry, mat );
  126. meshes.push( mesh );
  127. mesh.position.x = ( ii - ( maxI - 1 ) / 2 ) * sphereSpacing;
  128. mesh.position.y = ( jj - ( maxJ - 1 ) / 2 ) * sphereSpacing;
  129. scene.add( mesh );
  130. }
  131. }
  132. hdrCubeMap.magFilter = THREE.LinearFilter;
  133. hdrCubeMap.needsUpdate = true;
  134. scene.background = hdrCubeMap;
  135. pmremGenerator.dispose();
  136. pmremCubeUVPacker.dispose();
  137. }
  138. );
  139. function addLabel( name, location, fontSize ) {
  140. fontSize = fontSize | 20;
  141. var textGeo = new THREE.TextBufferGeometry( name,
  142. {
  143. font: font,
  144. size: fontSize,
  145. height: 1,
  146. curveSegments: 1
  147. }
  148. );
  149. var textMaterial = new THREE.MeshBasicMaterial( { color: 0xffffff } );
  150. var textMesh = new THREE.Mesh( textGeo, textMaterial );
  151. // Center the bounding box on the specified position w/ translation of -half diagonal
  152. textGeo.computeBoundingBox();
  153. var bb = textGeo.boundingBox.clone();
  154. var displace = bb.max.sub( bb.min ).divide( new THREE.Vector3( 2, 2, 2 ) );
  155. textMesh.position.copy( location.sub( displace ) );
  156. scene.add( textMesh );
  157. }
  158. addLabel( "Normal Map", new THREE.Vector3( -350, 0, 0 ), 30 );
  159. addLabel( "None", new THREE.Vector3( -250, -100, 0 ) );
  160. addLabel( "Map", new THREE.Vector3( -300, 100, 0 ) );
  161. addLabel( "Clearcoat Normal Map", new THREE.Vector3( 0, 260, 0 ), 30 );
  162. addLabel( "None", new THREE.Vector3( -100, 200, 0 ) );
  163. addLabel( "Map", new THREE.Vector3( 100, 200, 0 ) );
  164. // LIGHTS
  165. particleLight = new THREE.Mesh( new THREE.SphereBufferGeometry( 4, 8, 8 ), new THREE.MeshBasicMaterial( { color: 0xffffff } ) );
  166. scene.add( particleLight );
  167. {
  168. var ambientLight = new THREE.AmbientLight( 0x444444 );
  169. scene.add( ambientLight );
  170. }
  171. {
  172. var pointLight = new THREE.PointLight( 0xffffff, 1.25, 1000 );
  173. particleLight.add( pointLight );
  174. }
  175. {
  176. var directionalLight = new THREE.DirectionalLight( 0xffffff );
  177. directionalLight.position.set( 1, - 0.5, - 1 );
  178. scene.add( directionalLight );
  179. }
  180. renderer = new THREE.WebGLRenderer();
  181. renderer.setSize( window.innerWidth, window.innerHeight );
  182. container.appendChild( renderer.domElement );
  183. //
  184. renderer.gammaInput = true;
  185. renderer.gammaOutput = true;
  186. //
  187. stats = new Stats();
  188. container.appendChild( stats.dom );
  189. // EVENTS
  190. var controls = new OrbitControls( camera, renderer.domElement );
  191. window.addEventListener( 'resize', onWindowResize, false );
  192. var gui = new GUI();
  193. {
  194. var materialGui = gui.addFolder( 'Physical Material' );
  195. materialGui.add( guiParams, 'metalness', 0, 1, 0.01 );
  196. materialGui.add( guiParams, 'roughness', 0, 1, 0.01 );
  197. materialGui.add( guiParams, 'reflectivity', 0, 1, 0.01 );
  198. materialGui.open();
  199. }
  200. {
  201. var clearCoatGui = gui.addFolder( 'ClearCoat' );
  202. clearCoatGui.add( guiParams, 'clearCoat', 0, 1, 0.01 );
  203. clearCoatGui.add( guiParams, 'clearCoatRoughness', 0, 1, 0.01 );
  204. clearCoatGui.open();
  205. }
  206. {
  207. var normalGui = gui.addFolder( 'Normal Maps' );
  208. normalGui.add( guiMatParams, 'normalMap', guiNormalMapNames );
  209. normalGui.add( guiMatParams, 'normalScale', 0, 1, 0.01 );
  210. normalGui.add( guiMatParams, 'clearCoatNormalMap', guiNormalMapNames );
  211. normalGui.add( guiMatParams, 'clearCoatNormalScale', 0, 1, 0.01 );
  212. normalGui.open();
  213. }
  214. gui.open();
  215. }
  216. //
  217. function onWindowResize() {
  218. var width = window.innerWidth;
  219. var height = window.innerHeight;
  220. camera.aspect = width / height;
  221. camera.updateProjectionMatrix();
  222. renderer.setSize( width, height );
  223. }
  224. //
  225. function animate() {
  226. requestAnimationFrame( animate );
  227. render();
  228. stats.update();
  229. }
  230. function render() {
  231. var matColor = new THREE.Color().setHSL( 0.0, 0.5, 0.25 );
  232. for ( var ii = 0; ii < maxI; ii ++ ) {
  233. if ( mats[ ii ] ) { // avoid exceptions on first render before materials are set. TODO: lauch animation only after setup
  234. for ( var jj = 0; jj < maxJ; jj ++ ) {
  235. var mat = mats[ ii ][ jj ];
  236. mat.color = matColor;
  237. for ( var matParam in guiParams ) {
  238. mat[ matParam ] = guiParams[ matParam ];
  239. }
  240. var normalMap = normalMaps[ guiMatParams.normalMap ];
  241. var clearCoatNormalMap = normalMaps[ guiMatParams.clearCoatNormalMap ];
  242. variationsX[ ii ]( mat, guiMatParams.clearCoatNormalScale, clearCoatNormalMap );
  243. variationsY[ jj ]( mat, guiMatParams.normalScale, normalMap );
  244. }
  245. }
  246. }
  247. var timer = Date.now() * 0.00025;
  248. particleLight.position.x = Math.sin( timer * 7 ) * 300;
  249. particleLight.position.y = Math.cos( timer * 5 ) * 400;
  250. particleLight.position.z = Math.cos( timer * 3 ) * 300;
  251. for ( var mesh of meshes ) {
  252. mesh.rotation.y += 0.01;
  253. }
  254. renderer.render( scene, camera );
  255. }
  256. </script>
  257. </body>
  258. </html>