webgl_shaders_vector.html 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  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="http://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 {
  49. BufferGeometry,
  50. Color,
  51. DoubleSide,
  52. Float32BufferAttribute,
  53. FontLoader,
  54. Group,
  55. LineCurve,
  56. Mesh,
  57. MeshBasicMaterial,
  58. PerspectiveCamera,
  59. QuadraticBezierCurve,
  60. Scene,
  61. Shape,
  62. ShapeBufferGeometry,
  63. ShaderMaterial,
  64. Vector2,
  65. WebGLRenderer
  66. } from "../build/three.module.js";
  67. import Stats from './jsm/libs/stats.module.js';
  68. import { OrbitControls } from './jsm/controls/OrbitControls.js';
  69. var stats;
  70. var camera, scene, renderer, controls;
  71. var group;
  72. var loader = new FontLoader();
  73. loader.load( 'fonts/helvetiker_regular.typeface.json', function ( font ) {
  74. init( font );
  75. animate();
  76. } );
  77. function init( font ) {
  78. camera = new PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 1, 10000 );
  79. camera.position.set( 50, 100, 500 );
  80. scene = new Scene();
  81. scene.background = new Color( 0xf0f0f0 );
  82. var theText = '&'; // i % & j b 8
  83. group = new Group();
  84. scene.add( group );
  85. var textMaterial = new MeshBasicMaterial( {
  86. color: new Color( 0, 0, 1 ),
  87. side: DoubleSide,
  88. wireframe: true
  89. } );
  90. var textShapes = font.generateShapes( theText, 180 );
  91. var geometry = new ShapeBufferGeometry( textShapes );
  92. var text = new Mesh( geometry, textMaterial );
  93. text.position.x = - 200;
  94. group.add( text );
  95. //
  96. var vA = new Vector2();
  97. var vB = new Vector2();
  98. function processShape( path ) {
  99. var pts = []; // bigger area (convex hull)
  100. var pts2 = []; // smaller area (full solid shapes)
  101. var beziers = []; // quad bezier points
  102. var invert = [];
  103. var z;
  104. var wind;
  105. pts.push( path[ 0 ].getPoint( 0 ) );
  106. pts2.push( path[ 0 ].getPoint( 0 ) );
  107. for ( var i = 0; i < path.length; i ++ ) {
  108. var curve = path[ i ];
  109. if ( curve instanceof LineCurve ) {
  110. pts.push( curve.v2 );
  111. pts2.push( curve.v2 );
  112. } else if ( curve instanceof QuadraticBezierCurve ) {
  113. vA = vA.subVectors( curve.v1, curve.v0 );
  114. vB = vB.subVectors( curve.v2, curve.v1 );
  115. z = vA.x * vB.y - vA.y * vB.x; // z component of cross Production
  116. wind = z < 0; // clockwise/anticlock wind
  117. if ( wind ) {
  118. pts.push( curve.v1 );
  119. pts.push( curve.v2 );
  120. pts2.push( curve.v2 );
  121. } else {
  122. pts.push( curve.v2 );
  123. pts2.push( curve.v1 );
  124. pts2.push( curve.v2 );
  125. }
  126. var flip = wind ? 1 : - 1;
  127. // if (reverse) flip *= -1;
  128. invert.push( flip, flip, flip );
  129. beziers.push( curve.v0, curve.v1, curve.v2 );
  130. }
  131. }
  132. return {
  133. pts: pts,
  134. pts2: pts2,
  135. beziers: beziers,
  136. invert: invert
  137. };
  138. }
  139. var pts, pts2;
  140. var subshape;
  141. var convexhullShape;
  142. var solidShape;
  143. var convexhullShapeGroup = [];
  144. var solidShapeGroup = [];
  145. var beziers = [], invert = [];
  146. var process;
  147. var hole;
  148. for ( var s = 0; s < textShapes.length; s ++ ) {
  149. subshape = textShapes[ s ];
  150. process = processShape( subshape.curves );
  151. pts = process.pts;
  152. pts2 = process.pts2;
  153. beziers = beziers.concat( process.beziers );
  154. invert = invert.concat( process.invert );
  155. convexhullShape = new Shape( pts );
  156. solidShape = new Shape( pts2 );
  157. convexhullShapeGroup.push( convexhullShape );
  158. solidShapeGroup.push( solidShape );
  159. for ( var i = 0; i < subshape.holes.length; i ++ ) {
  160. hole = subshape.holes[ i ];
  161. process = processShape( hole.curves, true );
  162. pts = process.pts;
  163. pts2 = process.pts2;
  164. beziers = beziers.concat( process.beziers );
  165. invert = invert.concat( process.invert );
  166. convexhullShape.holes.push( new Shape( pts ) );
  167. solidShape.holes.push( new Shape( pts2 ) );
  168. }
  169. } // end of subshape
  170. var bezierGeometry = new BufferGeometry();
  171. var vertices = [];
  172. var uvs = [];
  173. for ( var i = 0; i < beziers.length; i ++ ) {
  174. var p = beziers[ i ];
  175. vertices.push( p.x, p.y, 0 );
  176. }
  177. for ( var i = 0; i < beziers.length; i += 3 ) {
  178. uvs.push( 0, 0, 0.5, 0, 1, 1 );
  179. }
  180. bezierGeometry.addAttribute( 'position', new Float32BufferAttribute( vertices, 3 ) );
  181. bezierGeometry.addAttribute( 'uv', new Float32BufferAttribute( uvs, 2 ) );
  182. bezierGeometry.addAttribute( 'invert', new Float32BufferAttribute( invert, 1 ) );
  183. geometry = new ShapeBufferGeometry( convexhullShapeGroup );
  184. text = new Mesh( geometry, textMaterial );
  185. text.position.x = 200;
  186. group.add( text );
  187. geometry = new ShapeBufferGeometry( solidShapeGroup );
  188. text = new Mesh( geometry, new MeshBasicMaterial( { color: new Color( 1, 0, 0 ), side: DoubleSide, wireframe: true } ) );
  189. text.position.x = 200;
  190. group.add( text );
  191. //
  192. var newMaterial = new ShaderMaterial( {
  193. uniforms: {
  194. color: { value: new Color( 0.45 * 0xffffff ) }
  195. },
  196. vertexShader: document.getElementById( 'vs' ).textContent,
  197. fragmentShader: document.getElementById( 'fs' ).textContent,
  198. side: DoubleSide
  199. } );
  200. text = new Mesh( bezierGeometry, newMaterial );
  201. text.rotation.y = Math.PI * 2;
  202. group.add( text );
  203. //
  204. geometry = new ShapeBufferGeometry( solidShapeGroup );
  205. text = new Mesh( geometry, new MeshBasicMaterial( { color: 0.45 * 0xffffff, side: DoubleSide } ) );
  206. text.rotation.y = Math.PI * 2;
  207. group.add( text );
  208. //
  209. renderer = new WebGLRenderer( { antialias: true } );
  210. renderer.setPixelRatio( window.devicePixelRatio );
  211. renderer.setSize( window.innerWidth, window.innerHeight );
  212. document.body.appendChild( renderer.domElement );
  213. controls = new OrbitControls( camera, renderer.domElement );
  214. controls.target.set( 50, 100, 0 );
  215. controls.update();
  216. stats = new Stats();
  217. document.body.appendChild( stats.dom );
  218. window.addEventListener( 'resize', onWindowResize, false );
  219. }
  220. function onWindowResize() {
  221. camera.aspect = window.innerWidth / window.innerHeight;
  222. camera.updateProjectionMatrix();
  223. renderer.setSize( window.innerWidth, window.innerHeight );
  224. }
  225. //
  226. function animate() {
  227. requestAnimationFrame( animate );
  228. render();
  229. stats.update();
  230. }
  231. function render() {
  232. renderer.render( scene, camera );
  233. }
  234. </script>
  235. </body>
  236. </html>