webgl_modifier_tessellation.html 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - modifier - tessellation</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"><a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> - modifier tessellation</div>
  11. <div id="container"></div>
  12. <script type="x-shader/x-vertex" id="vertexshader">
  13. uniform float amplitude;
  14. attribute vec3 customColor;
  15. attribute vec3 displacement;
  16. varying vec3 vNormal;
  17. varying vec3 vColor;
  18. void main() {
  19. vNormal = normal;
  20. vColor = customColor;
  21. vec3 newPosition = position + normal * amplitude * displacement;
  22. gl_Position = projectionMatrix * modelViewMatrix * vec4( newPosition, 1.0 );
  23. }
  24. </script>
  25. <script type="x-shader/x-fragment" id="fragmentshader">
  26. varying vec3 vNormal;
  27. varying vec3 vColor;
  28. void main() {
  29. const float ambient = 0.4;
  30. vec3 light = vec3( 1.0 );
  31. light = normalize( light );
  32. float directional = max( dot( vNormal, light ), 0.0 );
  33. gl_FragColor = vec4( ( directional + ambient ) * vColor, 1.0 );
  34. }
  35. </script>
  36. <script type="module">
  37. import * as THREE from '../build/three.module.js';
  38. import Stats from './jsm/libs/stats.module.js';
  39. import { TrackballControls } from './jsm/controls/TrackballControls.js';
  40. import { TessellateModifier } from './jsm/modifiers/TessellateModifier.js';
  41. let renderer, scene, camera, stats;
  42. let controls;
  43. let mesh, uniforms;
  44. const WIDTH = window.innerWidth;
  45. const HEIGHT = window.innerHeight;
  46. const loader = new THREE.FontLoader();
  47. loader.load( 'fonts/helvetiker_bold.typeface.json', function ( font ) {
  48. init( font );
  49. animate();
  50. } );
  51. function init( font ) {
  52. camera = new THREE.PerspectiveCamera( 40, WIDTH / HEIGHT, 1, 10000 );
  53. camera.position.set( - 100, 100, 200 );
  54. scene = new THREE.Scene();
  55. scene.background = new THREE.Color( 0x050505 );
  56. //
  57. let geometry = new THREE.TextGeometry( 'THREE.JS', {
  58. font: font,
  59. size: 40,
  60. height: 5,
  61. curveSegments: 3,
  62. bevelThickness: 2,
  63. bevelSize: 1,
  64. bevelEnabled: true
  65. } );
  66. geometry.center();
  67. const tessellateModifier = new TessellateModifier( 8, 6 );
  68. geometry = tessellateModifier.modify( geometry );
  69. //
  70. const numFaces = geometry.attributes.position.count / 3;
  71. const colors = new Float32Array( numFaces * 3 * 3 );
  72. const displacement = new Float32Array( numFaces * 3 * 3 );
  73. const color = new THREE.Color();
  74. for ( let f = 0; f < numFaces; f ++ ) {
  75. const index = 9 * f;
  76. const h = 0.2 * Math.random();
  77. const s = 0.5 + 0.5 * Math.random();
  78. const l = 0.5 + 0.5 * Math.random();
  79. color.setHSL( h, s, l );
  80. const d = 10 * ( 0.5 - Math.random() );
  81. for ( let i = 0; i < 3; i ++ ) {
  82. colors[ index + ( 3 * i ) ] = color.r;
  83. colors[ index + ( 3 * i ) + 1 ] = color.g;
  84. colors[ index + ( 3 * i ) + 2 ] = color.b;
  85. displacement[ index + ( 3 * i ) ] = d;
  86. displacement[ index + ( 3 * i ) + 1 ] = d;
  87. displacement[ index + ( 3 * i ) + 2 ] = d;
  88. }
  89. }
  90. geometry.setAttribute( 'customColor', new THREE.BufferAttribute( colors, 3 ) );
  91. geometry.setAttribute( 'displacement', new THREE.BufferAttribute( displacement, 3 ) );
  92. //
  93. uniforms = {
  94. amplitude: { value: 0.0 }
  95. };
  96. const shaderMaterial = new THREE.ShaderMaterial( {
  97. uniforms: uniforms,
  98. vertexShader: document.getElementById( 'vertexshader' ).textContent,
  99. fragmentShader: document.getElementById( 'fragmentshader' ).textContent
  100. } );
  101. //
  102. mesh = new THREE.Mesh( geometry, shaderMaterial );
  103. scene.add( mesh );
  104. renderer = new THREE.WebGLRenderer( { antialias: true } );
  105. renderer.setPixelRatio( window.devicePixelRatio );
  106. renderer.setSize( WIDTH, HEIGHT );
  107. const container = document.getElementById( 'container' );
  108. container.appendChild( renderer.domElement );
  109. controls = new TrackballControls( camera, renderer.domElement );
  110. stats = new Stats();
  111. container.appendChild( stats.dom );
  112. //
  113. window.addEventListener( 'resize', onWindowResize );
  114. }
  115. function onWindowResize() {
  116. camera.aspect = window.innerWidth / window.innerHeight;
  117. camera.updateProjectionMatrix();
  118. renderer.setSize( window.innerWidth, window.innerHeight );
  119. }
  120. function animate() {
  121. requestAnimationFrame( animate );
  122. render();
  123. stats.update();
  124. }
  125. function render() {
  126. const time = Date.now() * 0.001;
  127. uniforms.amplitude.value = 1.0 + Math.sin( time * 0.5 );
  128. controls.update();
  129. renderer.render( scene, camera );
  130. }
  131. </script>
  132. </body>
  133. </html>