webgl_modifier_tessellation.html 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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. var renderer, scene, camera, stats;
  42. var controls;
  43. var mesh, uniforms;
  44. var WIDTH = window.innerWidth,
  45. HEIGHT = window.innerHeight;
  46. var 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. var 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. var tessellateModifier = new TessellateModifier( 8 );
  68. for ( var i = 0; i < 6; i ++ ) {
  69. tessellateModifier.modify( geometry );
  70. }
  71. //
  72. geometry = new THREE.BufferGeometry().fromGeometry( geometry );
  73. var numFaces = geometry.attributes.position.count / 3;
  74. var colors = new Float32Array( numFaces * 3 * 3 );
  75. var displacement = new Float32Array( numFaces * 3 * 3 );
  76. var color = new THREE.Color();
  77. for ( var f = 0; f < numFaces; f ++ ) {
  78. var index = 9 * f;
  79. var h = 0.2 * Math.random();
  80. var s = 0.5 + 0.5 * Math.random();
  81. var l = 0.5 + 0.5 * Math.random();
  82. color.setHSL( h, s, l );
  83. var d = 10 * ( 0.5 - Math.random() );
  84. for ( var i = 0; i < 3; i ++ ) {
  85. colors[ index + ( 3 * i ) ] = color.r;
  86. colors[ index + ( 3 * i ) + 1 ] = color.g;
  87. colors[ index + ( 3 * i ) + 2 ] = color.b;
  88. displacement[ index + ( 3 * i ) ] = d;
  89. displacement[ index + ( 3 * i ) + 1 ] = d;
  90. displacement[ index + ( 3 * i ) + 2 ] = d;
  91. }
  92. }
  93. geometry.setAttribute( 'customColor', new THREE.BufferAttribute( colors, 3 ) );
  94. geometry.setAttribute( 'displacement', new THREE.BufferAttribute( displacement, 3 ) );
  95. //
  96. uniforms = {
  97. amplitude: { value: 0.0 }
  98. };
  99. var shaderMaterial = new THREE.ShaderMaterial( {
  100. uniforms: uniforms,
  101. vertexShader: document.getElementById( 'vertexshader' ).textContent,
  102. fragmentShader: document.getElementById( 'fragmentshader' ).textContent
  103. } );
  104. //
  105. mesh = new THREE.Mesh( geometry, shaderMaterial );
  106. scene.add( mesh );
  107. renderer = new THREE.WebGLRenderer( { antialias: true } );
  108. renderer.setPixelRatio( window.devicePixelRatio );
  109. renderer.setSize( WIDTH, HEIGHT );
  110. var container = document.getElementById( 'container' );
  111. container.appendChild( renderer.domElement );
  112. controls = new TrackballControls( camera, renderer.domElement );
  113. stats = new Stats();
  114. container.appendChild( stats.dom );
  115. //
  116. window.addEventListener( 'resize', onWindowResize, false );
  117. }
  118. function onWindowResize() {
  119. camera.aspect = window.innerWidth / window.innerHeight;
  120. camera.updateProjectionMatrix();
  121. renderer.setSize( window.innerWidth, window.innerHeight );
  122. }
  123. function animate() {
  124. requestAnimationFrame( animate );
  125. render();
  126. stats.update();
  127. }
  128. function render() {
  129. var time = Date.now() * 0.001;
  130. uniforms.amplitude.value = 1.0 + Math.sin( time * 0.5 );
  131. controls.update();
  132. renderer.render( scene, camera );
  133. }
  134. </script>
  135. </body>
  136. </html>