_.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. meshes.push(mesh);
  129. mesh.position.x = (ii - (maxI - 1) / 2) * sphereSpacing;
  130. mesh.position.y = (jj - (maxJ - 1) / 2) * sphereSpacing;
  131. scene.add(mesh);
  132. }
  133. }
  134. hdrCubeMap.magFilter = THREE.LinearFilter;
  135. hdrCubeMap.needsUpdate = true;
  136. scene.background = hdrCubeMap;
  137. pmremGenerator.dispose();
  138. pmremCubeUVPacker.dispose();
  139. });
  140. function addLabel(name, location, fontSize) {
  141. fontSize = fontSize | 20;
  142. var textGeo = new THREE.TextBufferGeometry(name, {
  143. font: font,
  144. size: fontSize,
  145. height: 1,
  146. curveSegments: 1
  147. });
  148. var textMaterial = new THREE.MeshBasicMaterial({ color: 0xffffff });
  149. var textMesh = new THREE.Mesh(textGeo, textMaterial);
  150. textGeo.computeBoundingBox();
  151. var bb = textGeo.boundingBox.clone();
  152. var displace = bb.max.sub(bb.min).divide(new THREE.Vector3(2, 2, 2));
  153. textMesh.position.copy(location.sub(displace));
  154. scene.add(textMesh);
  155. }
  156. addLabel("Normal Map", new THREE.Vector3(-450, 0, 0), 30);
  157. addLabel("None", new THREE.Vector3(-350, -100, 0));
  158. addLabel("Scaling + Map", new THREE.Vector3(-400, 100, 0));
  159. addLabel("Clearcoat Normal Map", new THREE.Vector3(0, 260, 0), 30);
  160. addLabel("None", new THREE.Vector3(- 200, 200, 0));
  161. addLabel("Scaling", new THREE.Vector3(0, 200, 0));
  162. addLabel("Scaling + Map", new THREE.Vector3(200, 200, 0));
  163. // LIGHTS
  164. particleLight = new THREE.Mesh(new THREE.SphereBufferGeometry(4, 8, 8), new THREE.MeshBasicMaterial({ color: 0xffffff }));
  165. scene.add(particleLight);
  166. {
  167. var ambientLight = new THREE.AmbientLight(0x444444);
  168. scene.add(ambientLight);
  169. }
  170. {
  171. var pointLight = new THREE.PointLight(0xffffff, 1.25, 1000);
  172. particleLight.add(pointLight);
  173. }
  174. {
  175. var directionalLight = new THREE.DirectionalLight(0xffffff);
  176. directionalLight.position.set(1, - 0.5, - 1);
  177. scene.add(directionalLight);
  178. }
  179. renderer = new THREE.WebGLRenderer();
  180. renderer.setSize(window.innerWidth, window.innerHeight);
  181. container.appendChild(renderer.domElement);
  182. //
  183. renderer.gammaInput = true;
  184. renderer.gammaOutput = true;
  185. //
  186. stats = new Stats();
  187. container.appendChild(stats.dom);
  188. // EVENTS
  189. var controls = new OrbitControls(camera, renderer.domElement);
  190. window.addEventListener('resize', onWindowResize, false);
  191. var gui = new GUI();
  192. gui.add(guiParams, 'metalness', 0, 1, 0.01);
  193. gui.add(guiParams, 'roughness', 0, 1, 0.01);
  194. gui.add(guiParams, 'reflectivity', 0, 1, 0.01);
  195. gui.add(guiParams, 'clearCoat', 0, 1, 0.01);
  196. gui.add(guiParams, 'clearCoatRoughness', 0, 1, 0.01);
  197. gui.add(guiMatParams, 'normalMap', guiNormalMapNames);
  198. gui.add(guiMatParams, 'normalScale', 0, 1, 0.01);
  199. gui.add(guiMatParams, 'clearCoatNormalMap', guiNormalMapNames);
  200. gui.add(guiMatParams, 'clearCoatNormalScale', 0, 1, 0.01);
  201. gui.open();
  202. }
  203. //
  204. function onWindowResize() {
  205. var width = window.innerWidth;
  206. var height = window.innerHeight;
  207. camera.aspect = width / height;
  208. camera.updateProjectionMatrix();
  209. renderer.setSize(width, height);
  210. }
  211. //
  212. function animate() {
  213. requestAnimationFrame(animate);
  214. render();
  215. stats.update();
  216. }
  217. function render() {
  218. var matColor = new THREE.Color().setHSL(0.0, 0.5, 0.25);
  219. for (var ii = 0; ii < maxI; ii++) {
  220. if (mats[ii]) {
  221. for (var jj = 0; jj < maxJ; jj++) {
  222. var mat = mats[ii][jj];
  223. mat.color = matColor;
  224. for (var matParam in guiParams) {
  225. mat[matParam] = guiParams[matParam];
  226. }
  227. var normalMap = normalMaps[guiMatParams.normalMap];
  228. var clearCoatNormalMap = normalMaps[guiMatParams.clearCoatNormalMap];
  229. variationsX[ii](mat, guiMatParams.clearCoatNormalScale, clearCoatNormalMap);
  230. variationsY[jj](mat, guiMatParams.normalScale, normalMap);
  231. }
  232. }
  233. }
  234. var timer = Date.now() * 0.00025;
  235. particleLight.position.x = Math.sin(timer * 7) * 300;
  236. particleLight.position.y = Math.cos(timer * 5) * 400;
  237. particleLight.position.z = Math.cos(timer * 3) * 300;
  238. for (var mesh of meshes) {
  239. mesh.rotation.y += 0.01;
  240. }
  241. renderer.render(scene, camera);
  242. }
  243. </script>
  244. </body>
  245. </html>
粤ICP备19079148号