webgl_materials_modified.html 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - materials - modified</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> wegbl - modified material.
  12. <a href="https://casual-effects.com/data/" target="_blank" rel="noopener">Lee Perry-Smith</a> head.
  13. </div>
  14. <!-- Import maps polyfill -->
  15. <!-- Remove this when import maps will be widely supported -->
  16. <script async src="https://unpkg.com/[email protected]/dist/es-module-shims.js"></script>
  17. <script type="importmap">
  18. {
  19. "imports": {
  20. "three": "../build/three.module.js",
  21. "three/addons/": "./jsm/"
  22. }
  23. }
  24. </script>
  25. <script type="module">
  26. import * as THREE from 'three';
  27. import Stats from 'three/addons/libs/stats.module.js';
  28. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  29. import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js';
  30. let camera, scene, renderer, stats;
  31. init();
  32. animate();
  33. function init() {
  34. camera = new THREE.PerspectiveCamera( 27, window.innerWidth / window.innerHeight, 0.1, 100 );
  35. camera.position.z = 20;
  36. scene = new THREE.Scene();
  37. const loader = new GLTFLoader();
  38. loader.load( 'models/gltf/LeePerrySmith/LeePerrySmith.glb', function ( gltf ) {
  39. const geometry = gltf.scene.children[ 0 ].geometry;
  40. let mesh = new THREE.Mesh( geometry, buildTwistMaterial( 2.0 ) );
  41. mesh.position.x = - 3.5;
  42. mesh.position.y = - 0.5;
  43. scene.add( mesh );
  44. mesh = new THREE.Mesh( geometry, buildTwistMaterial( - 2.0 ) );
  45. mesh.position.x = 3.5;
  46. mesh.position.y = - 0.5;
  47. scene.add( mesh );
  48. } );
  49. renderer = new THREE.WebGLRenderer( { antialias: true } );
  50. renderer.setPixelRatio( window.devicePixelRatio );
  51. renderer.setSize( window.innerWidth, window.innerHeight );
  52. document.body.appendChild( renderer.domElement );
  53. const controls = new OrbitControls( camera, renderer.domElement );
  54. controls.minDistance = 10;
  55. controls.maxDistance = 50;
  56. //
  57. stats = new Stats();
  58. document.body.appendChild( stats.dom );
  59. // EVENTS
  60. window.addEventListener( 'resize', onWindowResize );
  61. }
  62. function buildTwistMaterial( amount ) {
  63. const material = new THREE.MeshNormalMaterial();
  64. material.onBeforeCompile = function ( shader ) {
  65. shader.uniforms.time = { value: 0 };
  66. shader.vertexShader = 'uniform float time;\n' + shader.vertexShader;
  67. shader.vertexShader = shader.vertexShader.replace(
  68. '#include <begin_vertex>',
  69. [
  70. `float theta = sin( time + position.y ) / ${ amount.toFixed( 1 ) };`,
  71. 'float c = cos( theta );',
  72. 'float s = sin( theta );',
  73. 'mat3 m = mat3( c, 0, s, 0, 1, 0, -s, 0, c );',
  74. 'vec3 transformed = vec3( position ) * m;',
  75. 'vNormal = vNormal * m;'
  76. ].join( '\n' )
  77. );
  78. material.userData.shader = shader;
  79. };
  80. // Make sure WebGLRenderer doesnt reuse a single program
  81. material.customProgramCacheKey = function () {
  82. return amount.toFixed( 1 );
  83. };
  84. return material;
  85. }
  86. //
  87. function onWindowResize() {
  88. const width = window.innerWidth;
  89. const height = window.innerHeight;
  90. camera.aspect = width / height;
  91. camera.updateProjectionMatrix();
  92. renderer.setSize( width, height );
  93. }
  94. //
  95. function animate() {
  96. requestAnimationFrame( animate );
  97. render();
  98. stats.update();
  99. }
  100. function render() {
  101. scene.traverse( function ( child ) {
  102. if ( child.isMesh ) {
  103. const shader = child.material.userData.shader;
  104. if ( shader ) {
  105. shader.uniforms.time.value = performance.now() / 1000;
  106. }
  107. }
  108. } );
  109. renderer.render( scene, camera );
  110. }
  111. </script>
  112. </body>
  113. </html>