webgl_shaders_vector.html 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - vector - text</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. <style>
  9. body {
  10. background-color: #f0f0f0;
  11. color: #444;
  12. }
  13. a {
  14. color: #08f;
  15. }
  16. </style>
  17. </head>
  18. <body>
  19. <div id="info">
  20. <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> webgl - Resolution-Independent Vector Fonts. <a href="https://github.com/mrdoob/three.js/issues/4746">info</a>.
  21. </div>
  22. <script type="x-shader/x-fragment" id="fs">
  23. varying vec2 vUv;
  24. varying float flip;
  25. uniform vec3 color;
  26. float inCurve(vec2 uv) {
  27. return uv.x * uv.x - uv.y;
  28. }
  29. float delta = 0.1;
  30. void main() {
  31. float x = inCurve(vUv);
  32. if (x * flip > 0.) discard;
  33. gl_FragColor = vec4(color, 1.);
  34. }
  35. </script>
  36. <script type="x-shader/x-vertex" id="vs">
  37. varying vec2 vUv;
  38. attribute float invert;
  39. varying float flip;
  40. void main() {
  41. vUv = uv;
  42. flip = invert;
  43. vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );
  44. gl_Position = projectionMatrix * mvPosition;
  45. }
  46. </script>
  47. <script type="module">
  48. import * as THREE from '../build/three.module.js';
  49. import Stats from './jsm/libs/stats.module.js';
  50. import { OrbitControls } from './jsm/controls/OrbitControls.js';
  51. var stats;
  52. var camera, scene, renderer, controls;
  53. var group;
  54. var loader = new THREE.FontLoader();
  55. loader.load( 'fonts/helvetiker_regular.typeface.json', function ( font ) {
  56. init( font );
  57. animate();
  58. } );
  59. function init( font ) {
  60. camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 1, 10000 );
  61. camera.position.set( 50, 100, 500 );
  62. scene = new THREE.Scene();
  63. scene.background = new THREE.Color( 0xf0f0f0 );
  64. var theText = '&'; // i % & j b 8
  65. group = new THREE.Group();
  66. scene.add( group );
  67. var textMaterial = new THREE.MeshBasicMaterial( {
  68. color: new THREE.Color( 0, 0, 1 ),
  69. side: THREE.DoubleSide,
  70. wireframe: true
  71. } );
  72. var textShapes = font.generateShapes( theText, 180 );
  73. var geometry = new THREE.ShapeBufferGeometry( textShapes );
  74. var text = new THREE.Mesh( geometry, textMaterial );
  75. text.position.x = - 200;
  76. group.add( text );
  77. //
  78. var vA = new THREE.Vector2();
  79. var vB = new THREE.Vector2();
  80. function processShape( path ) {
  81. var pts = []; // bigger area (convex hull)
  82. var pts2 = []; // smaller area (full solid shapes)
  83. var beziers = []; // quad bezier points
  84. var invert = [];
  85. var z;
  86. var wind;
  87. pts.push( path[ 0 ].getPoint( 0 ) );
  88. pts2.push( path[ 0 ].getPoint( 0 ) );
  89. for ( var i = 0; i < path.length; i ++ ) {
  90. var curve = path[ i ];
  91. if ( curve instanceof THREE.LineCurve ) {
  92. pts.push( curve.v2 );
  93. pts2.push( curve.v2 );
  94. } else if ( curve instanceof THREE.QuadraticBezierCurve ) {
  95. vA = vA.subVectors( curve.v1, curve.v0 );
  96. vB = vB.subVectors( curve.v2, curve.v1 );
  97. z = vA.x * vB.y - vA.y * vB.x; // z component of cross Production
  98. wind = z < 0; // clockwise/anticlock wind
  99. if ( wind ) {
  100. pts.push( curve.v1 );
  101. pts.push( curve.v2 );
  102. pts2.push( curve.v2 );
  103. } else {
  104. pts.push( curve.v2 );
  105. pts2.push( curve.v1 );
  106. pts2.push( curve.v2 );
  107. }
  108. var flip = wind ? 1 : - 1;
  109. // if (reverse) flip *= -1;
  110. invert.push( flip, flip, flip );
  111. beziers.push( curve.v0, curve.v1, curve.v2 );
  112. }
  113. }
  114. return {
  115. pts: pts,
  116. pts2: pts2,
  117. beziers: beziers,
  118. invert: invert
  119. };
  120. }
  121. var pts, pts2;
  122. var subshape;
  123. var convexhullShape;
  124. var solidShape;
  125. var convexhullShapeGroup = [];
  126. var solidShapeGroup = [];
  127. var beziers = [], invert = [];
  128. var process;
  129. var hole;
  130. for ( var s = 0; s < textShapes.length; s ++ ) {
  131. subshape = textShapes[ s ];
  132. process = processShape( subshape.curves );
  133. pts = process.pts;
  134. pts2 = process.pts2;
  135. beziers = beziers.concat( process.beziers );
  136. invert = invert.concat( process.invert );
  137. convexhullShape = new THREE.Shape( pts );
  138. solidShape = new THREE.Shape( pts2 );
  139. convexhullShapeGroup.push( convexhullShape );
  140. solidShapeGroup.push( solidShape );
  141. for ( var i = 0; i < subshape.holes.length; i ++ ) {
  142. hole = subshape.holes[ i ];
  143. process = processShape( hole.curves );
  144. pts = process.pts;
  145. pts2 = process.pts2;
  146. beziers = beziers.concat( process.beziers );
  147. invert = invert.concat( process.invert );
  148. convexhullShape.holes.push( new THREE.Shape( pts ) );
  149. solidShape.holes.push( new THREE.Shape( pts2 ) );
  150. }
  151. } // end of subshape
  152. var bezierGeometry = new THREE.BufferGeometry();
  153. var vertices = [];
  154. var uvs = [];
  155. for ( var i = 0; i < beziers.length; i ++ ) {
  156. var p = beziers[ i ];
  157. vertices.push( p.x, p.y, 0 );
  158. }
  159. for ( var i = 0; i < beziers.length; i += 3 ) {
  160. uvs.push( 0, 0, 0.5, 0, 1, 1 );
  161. }
  162. bezierGeometry.setAttribute( 'position', new THREE.Float32BufferAttribute( vertices, 3 ) );
  163. bezierGeometry.setAttribute( 'uv', new THREE.Float32BufferAttribute( uvs, 2 ) );
  164. bezierGeometry.setAttribute( 'invert', new THREE.Float32BufferAttribute( invert, 1 ) );
  165. geometry = new THREE.ShapeBufferGeometry( convexhullShapeGroup );
  166. text = new THREE.Mesh( geometry, textMaterial );
  167. text.position.x = 200;
  168. group.add( text );
  169. geometry = new THREE.ShapeBufferGeometry( solidShapeGroup );
  170. text = new THREE.Mesh( geometry, new THREE.MeshBasicMaterial( { color: new THREE.Color( 1, 0, 0 ), side: THREE.DoubleSide, wireframe: true } ) );
  171. text.position.x = 200;
  172. group.add( text );
  173. //
  174. var newMaterial = new THREE.ShaderMaterial( {
  175. uniforms: {
  176. color: { value: new THREE.Color( 0.45 * 0xffffff ) }
  177. },
  178. vertexShader: document.getElementById( 'vs' ).textContent,
  179. fragmentShader: document.getElementById( 'fs' ).textContent,
  180. side: THREE.DoubleSide
  181. } );
  182. text = new THREE.Mesh( bezierGeometry, newMaterial );
  183. text.rotation.y = Math.PI * 2;
  184. group.add( text );
  185. //
  186. geometry = new THREE.ShapeBufferGeometry( solidShapeGroup );
  187. text = new THREE.Mesh( geometry, new THREE.MeshBasicMaterial( { color: 0.45 * 0xffffff, side: THREE.DoubleSide } ) );
  188. text.rotation.y = Math.PI * 2;
  189. group.add( text );
  190. //
  191. renderer = new THREE.WebGLRenderer( { antialias: true } );
  192. renderer.setPixelRatio( window.devicePixelRatio );
  193. renderer.setSize( window.innerWidth, window.innerHeight );
  194. document.body.appendChild( renderer.domElement );
  195. controls = new OrbitControls( camera, renderer.domElement );
  196. controls.target.set( 50, 100, 0 );
  197. controls.update();
  198. stats = new Stats();
  199. document.body.appendChild( stats.dom );
  200. window.addEventListener( 'resize', onWindowResize, false );
  201. }
  202. function onWindowResize() {
  203. camera.aspect = window.innerWidth / window.innerHeight;
  204. camera.updateProjectionMatrix();
  205. renderer.setSize( window.innerWidth, window.innerHeight );
  206. }
  207. //
  208. function animate() {
  209. requestAnimationFrame( animate );
  210. render();
  211. stats.update();
  212. }
  213. function render() {
  214. renderer.render( scene, camera );
  215. }
  216. </script>
  217. </body>
  218. </html>