webgl_modifier_tessellation.html 5.2 KB

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