webgl_modifier_tessellation.html 5.0 KB

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