webgl_shaders_vector.html 8.5 KB

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