webgl_modifier_tessellation.html 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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. <!-- Import maps polyfill -->
  37. <!-- Remove this when import maps will be widely supported -->
  38. <script async src="https://unpkg.com/[email protected]/dist/es-module-shims.js"></script>
  39. <script type="importmap">
  40. {
  41. "imports": {
  42. "three": "../build/three.module.js"
  43. }
  44. }
  45. </script>
  46. <script type="module">
  47. import * as THREE from 'three';
  48. import Stats from './jsm/libs/stats.module.js';
  49. import { TrackballControls } from './jsm/controls/TrackballControls.js';
  50. import { TessellateModifier } from './jsm/modifiers/TessellateModifier.js';
  51. import { FontLoader } from './jsm/loaders/FontLoader.js';
  52. import { TextGeometry } from './jsm/geometries/TextGeometry.js';
  53. let renderer, scene, camera, stats;
  54. let controls;
  55. let mesh, uniforms;
  56. const WIDTH = window.innerWidth;
  57. const HEIGHT = window.innerHeight;
  58. const loader = new FontLoader();
  59. loader.load( 'fonts/helvetiker_bold.typeface.json', function ( font ) {
  60. init( font );
  61. animate();
  62. } );
  63. function init( font ) {
  64. camera = new THREE.PerspectiveCamera( 40, WIDTH / HEIGHT, 1, 10000 );
  65. camera.position.set( - 100, 100, 200 );
  66. scene = new THREE.Scene();
  67. scene.background = new THREE.Color( 0x050505 );
  68. //
  69. let geometry = new TextGeometry( 'THREE.JS', {
  70. font: font,
  71. size: 40,
  72. height: 5,
  73. curveSegments: 3,
  74. bevelThickness: 2,
  75. bevelSize: 1,
  76. bevelEnabled: true
  77. } );
  78. geometry.center();
  79. const tessellateModifier = new TessellateModifier( 8, 6 );
  80. geometry = tessellateModifier.modify( geometry );
  81. //
  82. const numFaces = geometry.attributes.position.count / 3;
  83. const colors = new Float32Array( numFaces * 3 * 3 );
  84. const displacement = new Float32Array( numFaces * 3 * 3 );
  85. const color = new THREE.Color();
  86. for ( let f = 0; f < numFaces; f ++ ) {
  87. const index = 9 * f;
  88. const h = 0.2 * Math.random();
  89. const s = 0.5 + 0.5 * Math.random();
  90. const l = 0.5 + 0.5 * Math.random();
  91. color.setHSL( h, s, l );
  92. const d = 10 * ( 0.5 - Math.random() );
  93. for ( let i = 0; i < 3; i ++ ) {
  94. colors[ index + ( 3 * i ) ] = color.r;
  95. colors[ index + ( 3 * i ) + 1 ] = color.g;
  96. colors[ index + ( 3 * i ) + 2 ] = color.b;
  97. displacement[ index + ( 3 * i ) ] = d;
  98. displacement[ index + ( 3 * i ) + 1 ] = d;
  99. displacement[ index + ( 3 * i ) + 2 ] = d;
  100. }
  101. }
  102. geometry.setAttribute( 'customColor', new THREE.BufferAttribute( colors, 3 ) );
  103. geometry.setAttribute( 'displacement', new THREE.BufferAttribute( displacement, 3 ) );
  104. //
  105. uniforms = {
  106. amplitude: { value: 0.0 }
  107. };
  108. const shaderMaterial = new THREE.ShaderMaterial( {
  109. uniforms: uniforms,
  110. vertexShader: document.getElementById( 'vertexshader' ).textContent,
  111. fragmentShader: document.getElementById( 'fragmentshader' ).textContent
  112. } );
  113. //
  114. mesh = new THREE.Mesh( geometry, shaderMaterial );
  115. scene.add( mesh );
  116. renderer = new THREE.WebGLRenderer( { antialias: true } );
  117. renderer.setPixelRatio( window.devicePixelRatio );
  118. renderer.setSize( WIDTH, HEIGHT );
  119. const container = document.getElementById( 'container' );
  120. container.appendChild( renderer.domElement );
  121. controls = new TrackballControls( camera, renderer.domElement );
  122. stats = new Stats();
  123. container.appendChild( stats.dom );
  124. //
  125. window.addEventListener( 'resize', onWindowResize );
  126. }
  127. function onWindowResize() {
  128. camera.aspect = window.innerWidth / window.innerHeight;
  129. camera.updateProjectionMatrix();
  130. renderer.setSize( window.innerWidth, window.innerHeight );
  131. }
  132. function animate() {
  133. requestAnimationFrame( animate );
  134. render();
  135. stats.update();
  136. }
  137. function render() {
  138. const time = Date.now() * 0.001;
  139. uniforms.amplitude.value = 1.0 + Math.sin( time * 0.5 );
  140. controls.update();
  141. renderer.render( scene, camera );
  142. }
  143. </script>
  144. </body>
  145. </html>