webgl_materials_clearcoat_normalmap.html 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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, loader;
  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. var windowHalfX = window.innerWidth / 2;
  55. var windowHalfY = window.innerHeight / 2;
  56. const sphereSize = 80;
  57. const sphereSpacing = 1.25 * sphereSize * 2;
  58. const variationsX = [
  59. (mat, scale, map) => { mat.clearCoatNormalScale.copy(mat.normalScale); },
  60. (mat, scale, map) => {
  61. mat.clearCoatNormalScale = new THREE.Vector2(scale, scale);
  62. if (mat.clearCoatNormalMap !== map) {
  63. mat.clearCoatNormalMap = map;
  64. mat.needsUpdate = true;
  65. }
  66. },
  67. ];
  68. const variationsY = [
  69. (mat, scale, map) => { },
  70. (mat, scale, map) => {
  71. mat.normalScale = new THREE.Vector2(scale, scale);
  72. if (mat.normalMap !== map) {
  73. mat.normalMap = map;
  74. mat.needsUpdate = true;
  75. }
  76. },
  77. ];
  78. const maxI = variationsX.length;
  79. const maxJ = variationsY.length;
  80. new THREE.FontLoader()
  81. .load('fonts/gentilis_regular.typeface.json', function (font) {
  82. init(font);
  83. animate();
  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, function (hdrCubeMap) {
  102. var pmremGenerator = new PMREMGenerator(hdrCubeMap);
  103. pmremGenerator.update(renderer);
  104. var pmremCubeUVPacker = new PMREMCubeUVPacker(pmremGenerator.cubeLods);
  105. pmremCubeUVPacker.update(renderer);
  106. var hdrCubeRenderTarget = pmremCubeUVPacker.CubeUVRenderTarget;
  107. var geometry = new THREE.SphereBufferGeometry(80, 64, 32);
  108. var textureLoader = new THREE.TextureLoader();
  109. normalMaps.tentacle = textureLoader
  110. .load("textures/nvidia_tentacle/tentacle_tangent_space.png");
  111. normalMaps.face = textureLoader
  112. .load("models/gltf/LeePerrySmith/Infinite-Level_02_Tangent_SmoothUV.jpg");
  113. // objectNormalMap = textureLoader
  114. // .load("textures/nvidia_tentacle/tentacle_object_space.png");
  115. var matParams = {
  116. envMap: hdrCubeRenderTarget.texture,
  117. };
  118. for (var ii = 0; ii < maxI; ii++) {
  119. if (!mats[ii]) {
  120. mats[ii] = [];
  121. }
  122. for (var jj = 0; jj < maxJ; jj++) {
  123. var mat = mats[ii][jj] = new THREE.MeshPhysicalMaterial(matParams);
  124. variationsX[ii](mat, 1, normalMaps.tentacle);
  125. variationsY[jj](mat, 1, normalMaps.face);
  126. var mesh = new THREE.Mesh(geometry, mat);
  127. meshes.push(mesh);
  128. mesh.position.x = (ii - (maxI - 1) / 2) * sphereSpacing;
  129. mesh.position.y = (jj - (maxJ - 1) / 2) * sphereSpacing;
  130. scene.add(mesh);
  131. }
  132. }
  133. hdrCubeMap.magFilter = THREE.LinearFilter;
  134. hdrCubeMap.needsUpdate = true;
  135. scene.background = hdrCubeMap;
  136. pmremGenerator.dispose();
  137. pmremCubeUVPacker.dispose();
  138. });
  139. function addLabel(name, location, fontSize) {
  140. fontSize = fontSize | 20;
  141. var textGeo = new THREE.TextBufferGeometry(name, {
  142. font: font,
  143. size: fontSize,
  144. height: 1,
  145. curveSegments: 1
  146. });
  147. var textMaterial = new THREE.MeshBasicMaterial({ color: 0xffffff });
  148. var textMesh = new THREE.Mesh(textGeo, textMaterial);
  149. textGeo.computeBoundingBox();
  150. var bb = textGeo.boundingBox.clone();
  151. var displace = bb.max.sub(bb.min).divide(new THREE.Vector3(2, 2, 2));
  152. textMesh.position.copy(location.sub(displace));
  153. scene.add(textMesh);
  154. }
  155. addLabel("Normal Map", new THREE.Vector3(-350, 0, 0), 30);
  156. addLabel("None", new THREE.Vector3(-250, -100, 0));
  157. addLabel("Map", new THREE.Vector3(-300, 100, 0));
  158. addLabel("Clearcoat Normal Map", new THREE.Vector3(0, 260, 0), 30);
  159. addLabel("None", new THREE.Vector3(-100, 200, 0));
  160. addLabel("Map", new THREE.Vector3(100, 200, 0));
  161. // LIGHTS
  162. particleLight = new THREE.Mesh(new THREE.SphereBufferGeometry(4, 8, 8), new THREE.MeshBasicMaterial({ color: 0xffffff }));
  163. scene.add(particleLight);
  164. {
  165. var ambientLight = new THREE.AmbientLight(0x444444);
  166. scene.add(ambientLight);
  167. }
  168. {
  169. var pointLight = new THREE.PointLight(0xffffff, 1.25, 1000);
  170. particleLight.add(pointLight);
  171. }
  172. {
  173. var directionalLight = new THREE.DirectionalLight(0xffffff);
  174. directionalLight.position.set(1, - 0.5, - 1);
  175. scene.add(directionalLight);
  176. }
  177. renderer = new THREE.WebGLRenderer();
  178. renderer.setSize(window.innerWidth, window.innerHeight);
  179. container.appendChild(renderer.domElement);
  180. //
  181. renderer.gammaInput = true;
  182. renderer.gammaOutput = true;
  183. //
  184. stats = new Stats();
  185. container.appendChild(stats.dom);
  186. // EVENTS
  187. var controls = new OrbitControls(camera, renderer.domElement);
  188. window.addEventListener('resize', onWindowResize, false);
  189. var gui = new GUI();
  190. {
  191. var materialGui = gui.addFolder( 'Physical Material' );
  192. materialGui.add(guiParams, 'metalness', 0, 1, 0.01);
  193. materialGui.add(guiParams, 'roughness', 0, 1, 0.01);
  194. materialGui.add(guiParams, 'reflectivity', 0, 1, 0.01);
  195. materialGui.open();
  196. }
  197. {
  198. var clearCoatGui = gui.addFolder( 'ClearCoat' );
  199. clearCoatGui.add(guiParams, 'clearCoat', 0, 1, 0.01);
  200. clearCoatGui.add(guiParams, 'clearCoatRoughness', 0, 1, 0.01);
  201. clearCoatGui.open();
  202. }
  203. {
  204. var normalGui = gui.addFolder( 'Normal Maps' );
  205. normalGui.add(guiMatParams, 'normalMap', guiNormalMapNames);
  206. normalGui.add(guiMatParams, 'normalScale', 0, 1, 0.01);
  207. normalGui.add(guiMatParams, 'clearCoatNormalMap', guiNormalMapNames);
  208. normalGui.add(guiMatParams, 'clearCoatNormalScale', 0, 1, 0.01);
  209. normalGui.open();
  210. }
  211. gui.open();
  212. }
  213. //
  214. function onWindowResize() {
  215. var width = window.innerWidth;
  216. var height = window.innerHeight;
  217. camera.aspect = width / height;
  218. camera.updateProjectionMatrix();
  219. renderer.setSize(width, height);
  220. }
  221. //
  222. function animate() {
  223. requestAnimationFrame(animate);
  224. render();
  225. stats.update();
  226. }
  227. function render() {
  228. var matColor = new THREE.Color().setHSL(0.0, 0.5, 0.25);
  229. for (var ii = 0; ii < maxI; ii++) {
  230. if (mats[ii]) {
  231. for (var jj = 0; jj < maxJ; jj++) {
  232. var mat = mats[ii][jj];
  233. mat.color = matColor;
  234. for (var matParam in guiParams) {
  235. mat[matParam] = guiParams[matParam];
  236. }
  237. var normalMap = normalMaps[guiMatParams.normalMap];
  238. var clearCoatNormalMap = normalMaps[guiMatParams.clearCoatNormalMap];
  239. variationsX[ii](mat, guiMatParams.clearCoatNormalScale, clearCoatNormalMap);
  240. variationsY[jj](mat, guiMatParams.normalScale, normalMap);
  241. }
  242. }
  243. }
  244. var timer = Date.now() * 0.00025;
  245. particleLight.position.x = Math.sin(timer * 7) * 300;
  246. particleLight.position.y = Math.cos(timer * 5) * 400;
  247. particleLight.position.z = Math.cos(timer * 3) * 300;
  248. for (var mesh of meshes) {
  249. mesh.rotation.y += 0.01;
  250. }
  251. renderer.render(scene, camera);
  252. }
  253. </script>
  254. </body>
  255. </html>
粤ICP备19079148号