webgl_materials_modified.html 3.7 KB

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