_.html 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - materials - normal map [Lee Perry-Smith]</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 clearcoat geometry 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) => { mat.clearCoatNormalScale = new THREE.Vector2(scale, scale); },
  61. (mat, scale, map) => {
  62. mat.clearCoatNormalScale = new THREE.Vector2(scale, scale);
  63. if (mat.clearCoatNormalMap !== map) {
  64. mat.clearCoatNormalMap = map;
  65. mat.needsUpdate = true;
  66. }
  67. },
  68. ];
  69. const variationsY = [
  70. (mat, scale, map) => { },
  71. (mat, scale, map) => {
  72. mat.normalScale = new THREE.Vector2(scale, scale);
  73. if (mat.normalMap !== map) {
  74. mat.normalMap = map;
  75. mat.needsUpdate = true;
  76. }
  77. },
  78. ];
  79. const maxI = variationsX.length;
  80. const maxJ = variationsY.length;
  81. new THREE.FontLoader()
  82. .load('fonts/gentilis_regular.typeface.json', function (font) {
  83. init(font);
  84. animate();
  85. });
  86. function init(font) {
  87. container = document.createElement('div');
  88. document.body.appendChild(container);
  89. camera = new THREE.PerspectiveCamera(27, window.innerWidth / window.innerHeight, 1, 10000);
  90. camera.position.z = 1500;
  91. var genCubeUrls = function (prefix, postfix) {
  92. return [
  93. prefix + 'px' + postfix, prefix + 'nx' + postfix,
  94. prefix + 'py' + postfix, prefix + 'ny' + postfix,
  95. prefix + 'pz' + postfix, prefix + 'nz' + postfix
  96. ];
  97. };
  98. scene = new THREE.Scene();
  99. var hdrUrls = genCubeUrls('./textures/cube/pisaHDR/', '.hdr');
  100. new HDRCubeTextureLoader()
  101. .setType(THREE.UnsignedByteType)
  102. .load(hdrUrls, 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. // objectNormalMap = textureLoader
  115. // .load("textures/nvidia_tentacle/tentacle_object_space.png");
  116. var matParams = {
  117. envMap: hdrCubeRenderTarget.texture,
  118. };
  119. for (var ii = 0; ii < maxI; ii++) {
  120. if (!mats[ii]) {
  121. mats[ii] = [];
  122. }
  123. for (var jj = 0; jj < maxJ; jj++) {
  124. var mat = mats[ii][jj] = new THREE.MeshPhysicalMaterial(matParams);
  125. variationsX[ii](mat, 1, normalMaps.tentacle);
  126. variationsY[jj](mat, 1, normalMaps.face);
  127. var mesh = new THREE.Mesh(geometry, mat);
  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(-450, 0, 0), 30);
  156. addLabel("None", new THREE.Vector3(-350, -100, 0));
  157. addLabel("Scaling + Map", new THREE.Vector3(-400, 100, 0));
  158. addLabel("Clearcoat Normal Map", new THREE.Vector3(0, 260, 0), 30);
  159. addLabel("None", new THREE.Vector3(- 200, 200, 0));
  160. addLabel("Scaling", new THREE.Vector3(0, 200, 0));
  161. addLabel("Scaling + Map", new THREE.Vector3(200, 200, 0));
  162. // LIGHTS
  163. particleLight = new THREE.Mesh(new THREE.SphereBufferGeometry(4, 8, 8), new THREE.MeshBasicMaterial({ color: 0xffffff }));
  164. scene.add(particleLight);
  165. {
  166. var ambientLight = new THREE.AmbientLight(0x444444);
  167. scene.add(ambientLight);
  168. }
  169. {
  170. var pointLight = new THREE.PointLight(0xffffff, 1.25, 1000);
  171. particleLight.add(pointLight);
  172. }
  173. {
  174. var directionalLight = new THREE.DirectionalLight(0xffffff);
  175. directionalLight.position.set(1, - 0.5, - 1);
  176. scene.add(directionalLight);
  177. }
  178. renderer = new THREE.WebGLRenderer();
  179. renderer.setSize(window.innerWidth, window.innerHeight);
  180. container.appendChild(renderer.domElement);
  181. //
  182. renderer.gammaInput = true;
  183. renderer.gammaOutput = true;
  184. //
  185. stats = new Stats();
  186. container.appendChild(stats.dom);
  187. // EVENTS
  188. var controls = new OrbitControls(camera, renderer.domElement);
  189. window.addEventListener('resize', onWindowResize, false);
  190. var gui = new GUI();
  191. gui.add(guiParams, 'metalness', 0, 1, 0.01);
  192. gui.add(guiParams, 'roughness', 0, 1, 0.01);
  193. gui.add(guiParams, 'reflectivity', 0, 1, 0.01);
  194. gui.add(guiParams, 'clearCoat', 0, 1, 0.01);
  195. gui.add(guiParams, 'clearCoatRoughness', 0, 1, 0.01);
  196. gui.add(guiMatParams, 'normalMap', guiNormalMapNames);
  197. gui.add(guiMatParams, 'normalScale', 0, 10, 0.01);
  198. gui.add(guiMatParams, 'clearCoatNormalMap', guiNormalMapNames);
  199. gui.add(guiMatParams, 'clearCoatNormalScale', 0, 10, 0.01);
  200. gui.open();
  201. }
  202. //
  203. function onWindowResize() {
  204. var width = window.innerWidth;
  205. var height = window.innerHeight;
  206. camera.aspect = width / height;
  207. camera.updateProjectionMatrix();
  208. renderer.setSize(width, height);
  209. }
  210. //
  211. function animate() {
  212. requestAnimationFrame(animate);
  213. render();
  214. stats.update();
  215. }
  216. function render() {
  217. var matColor = new THREE.Color().setHSL(0.0, 0.5, 0.25);
  218. for (var ii = 0; ii < maxI; ii++) {
  219. if (mats[ii]) {
  220. for (var jj = 0; jj < maxJ; jj++) {
  221. var mat = mats[ii][jj];
  222. mat.color = matColor;
  223. for (var matParam in guiParams) {
  224. mat[matParam] = guiParams[matParam];
  225. }
  226. var normalMap = normalMaps[guiMatParams.normalMap];
  227. var clearCoatNormalMap = normalMaps[guiMatParams.clearCoatNormalMap];
  228. variationsX[ii](mat, guiMatParams.clearCoatNormalScale, clearCoatNormalMap);
  229. variationsY[jj](mat, guiMatParams.normalScale, normalMap);
  230. }
  231. }
  232. }
  233. var timer = Date.now() * 0.00025;
  234. particleLight.position.x = Math.sin(timer * 7) * 300;
  235. particleLight.position.y = Math.cos(timer * 5) * 400;
  236. particleLight.position.z = Math.cos(timer * 3) * 300;
  237. for (var mesh of meshes) {
  238. mesh.rotation.y += 0.01;
  239. }
  240. renderer.render(scene, camera);
  241. }
  242. </script>
  243. </body>
  244. </html>
粤ICP备19079148号