webgl_materials_modified.html 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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="http://graphics.cs.williams.edu/data/meshes.xml#14" target="_blank" rel="noopener">Lee Perry-Smith</a> head.
  13. </div>
  14. <script type="module">
  15. import * as THREE from '../build/three.module.js';
  16. import Stats from './jsm/libs/stats.module.js';
  17. import { OrbitControls } from './jsm/controls/OrbitControls.js';
  18. import { GLTFLoader } from './jsm/loaders/GLTFLoader.js';
  19. var camera, scene, renderer, stats;
  20. var materialShader;
  21. init();
  22. animate();
  23. function init() {
  24. camera = new THREE.PerspectiveCamera( 27, window.innerWidth / window.innerHeight, 1, 10000 );
  25. camera.position.z = 1500;
  26. scene = new THREE.Scene();
  27. var material = new THREE.MeshNormalMaterial();
  28. material.onBeforeCompile = function ( shader ) {
  29. shader.uniforms.time = { value: 0 };
  30. shader.vertexShader = 'uniform float time;\n' + shader.vertexShader;
  31. shader.vertexShader = shader.vertexShader.replace(
  32. '#include <begin_vertex>',
  33. [
  34. 'float theta = sin( time + position.y ) / 2.0;',
  35. 'float c = cos( theta );',
  36. 'float s = sin( theta );',
  37. 'mat3 m = mat3( c, 0, s, 0, 1, 0, -s, 0, c );',
  38. 'vec3 transformed = vec3( position ) * m;',
  39. 'vNormal = vNormal * m;'
  40. ].join( '\n' )
  41. );
  42. materialShader = shader;
  43. };
  44. var loader = new GLTFLoader();
  45. loader.load( 'models/gltf/LeePerrySmith/LeePerrySmith.glb', function ( gltf ) {
  46. var mesh = new THREE.Mesh( gltf.scene.children[ 0 ].geometry, material );
  47. mesh.position.y = - 50;
  48. mesh.scale.setScalar( 100 );
  49. scene.add( mesh );
  50. } );
  51. renderer = new THREE.WebGLRenderer( { antialias: true } );
  52. renderer.setPixelRatio( window.devicePixelRatio );
  53. renderer.setSize( window.innerWidth, window.innerHeight );
  54. document.body.appendChild( renderer.domElement );
  55. var controls = new OrbitControls( camera, renderer.domElement );
  56. //
  57. stats = new Stats();
  58. document.body.appendChild( stats.dom );
  59. // EVENTS
  60. window.addEventListener( 'resize', onWindowResize, false );
  61. }
  62. //
  63. function onWindowResize() {
  64. var width = window.innerWidth;
  65. var height = window.innerHeight;
  66. camera.aspect = width / height;
  67. camera.updateProjectionMatrix();
  68. renderer.setSize( width, height );
  69. }
  70. //
  71. function animate() {
  72. requestAnimationFrame( animate );
  73. render();
  74. stats.update();
  75. }
  76. function render() {
  77. if ( materialShader ) {
  78. materialShader.uniforms.time.value = performance.now() / 1000;
  79. }
  80. renderer.render( scene, camera );
  81. }
  82. </script>
  83. </body>
  84. </html>