webgl_shaders_vector.html 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  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.min.js"></script>
  27. <script src="./js/controls/OrbitControls.js"></script>
  28. <script src="js/libs/stats.min.js"></script>
  29. <!-- load the font file from canvas-text -->
  30. <script src="fonts/helvetiker_regular.typeface.js"></script>
  31. <script type="x-shader/x-fragment" id="fs">
  32. varying vec2 vUv;
  33. varying float flip;
  34. uniform vec3 color;
  35. float inCurve(vec2 uv) {
  36. return uv.x * uv.x - uv.y;
  37. }
  38. float delta = 0.1;
  39. void main() {
  40. float x = inCurve(vUv);
  41. if (x * flip > 0.) discard;
  42. gl_FragColor = vec4(color, 1.);
  43. }
  44. </script>
  45. <script type="x-shader/x-vertex" id="vs">
  46. varying vec2 vUv;
  47. attribute float invert;
  48. varying float flip;
  49. void main() {
  50. vUv = uv;
  51. flip = invert;
  52. vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );
  53. gl_Position = projectionMatrix * mvPosition;
  54. }
  55. </script>
  56. <script>
  57. var stats;
  58. var camera, scene, renderer, controls;
  59. var group, text;
  60. var t = false;
  61. function toggle() {
  62. if ( t ) {
  63. text2.visible = 0;
  64. text1.visible = 1;
  65. } else {
  66. text2.visible = 1;
  67. text1.visible = 0;
  68. }
  69. t = !t;
  70. }
  71. init();
  72. animate();
  73. function init() {
  74. camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 1, 1000 );
  75. camera.position.set( 0, 100, 500 );
  76. controls = new THREE.OrbitControls( camera );
  77. controls.center.set( 0, 100, 0 );
  78. scene = new THREE.Scene();
  79. var theText = "&"; // i % & j b 8
  80. var options = {
  81. size: 180,
  82. height: 20,
  83. curveSegments: 2,
  84. font: "helvetiker",
  85. bevelEnabled: false
  86. };
  87. group = new THREE.Group();
  88. scene.add( group );
  89. var textMaterial = new THREE.MeshBasicMaterial( { color: new THREE.Color(0, 0, 1 ), overdraw: 0.5, wireframe: true, side: THREE.DoubleSide } );
  90. textShapes = THREE.FontUtils.generateShapes( theText, options );
  91. text3d = new THREE.ShapeGeometry( textShapes );
  92. text3d.computeBoundingBox();
  93. var centerOffset = -0.5 * ( text3d.boundingBox.max.x - text3d.boundingBox.min.x );
  94. text = new THREE.Mesh( text3d, textMaterial );
  95. text.position.x = centerOffset - 150;
  96. group.add( text );
  97. //
  98. vA = new THREE.Vector2();
  99. vB = new THREE.Vector2();
  100. vDot = new THREE.Vector2();
  101. function processShape(path, reverse) {
  102. var pts = []; // bigger area (convex hull)
  103. var pts2 = []; // smaller area (full solid shapes)
  104. var beziers = []; // quad bezier points
  105. var invert = [];
  106. var z;
  107. var wind;
  108. pts.push( path[0].getPoint(0) );
  109. pts2.push( path[0].getPoint(0) );
  110. for (var i=0; i < path.length; i++) {
  111. curve = path[i];
  112. if (curve instanceof THREE.LineCurve) {
  113. pts.push( curve.v2 );
  114. pts2.push( curve.v2 );
  115. } else if (curve instanceof THREE.QuadraticBezierCurve) {
  116. vA = vA.subVectors( curve.v1, curve.v0 ); // .normalize()
  117. vB = vB.subVectors( curve.v2, curve.v1 );
  118. z = vA.x * vB.y - vA.y * vB.x; // z component of cross Production
  119. wind = z < 0; // clockwise/anticlock wind
  120. // if (reverse) wind = !wind;
  121. // console.log(z, wind , wind ? 'clockwise' : 'anti');
  122. if (wind) {
  123. pts.push( curve.v1 );
  124. pts.push( curve.v2 );
  125. pts2.push( curve.v2 );
  126. } else {
  127. pts.push( curve.v2 );
  128. pts2.push( curve.v1 );
  129. pts2.push( curve.v2 );
  130. }
  131. var flip = wind ? 1 : -1;
  132. // if (reverse) flip *= -1;
  133. invert.push(flip, flip, flip);
  134. beziers.push( curve.v0, curve.v1, curve.v2);
  135. }
  136. }
  137. return {
  138. pts: pts,
  139. pts2: pts2,
  140. beziers: beziers,
  141. invert: invert
  142. };
  143. }
  144. var subshape;
  145. var convexhullShapeGroup = [];
  146. var solidShapeGroup = [];
  147. var beziers = [], invert = [];
  148. for (var s=0;s<textShapes.length;s++) {
  149. subshape = textShapes[s];
  150. var 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 THREE.Shape( pts );
  156. solidShape = new THREE.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. // console.log('hole', hole);
  162. process = processShape(hole.curves, true);
  163. pts = process.pts;
  164. pts2 = process.pts2;
  165. beziers = beziers.concat(process.beziers);
  166. invert = invert.concat(process.invert);
  167. convexhullShape.holes.push(new THREE.Shape(pts));
  168. solidShape.holes.push(new THREE.Shape(pts2));
  169. }
  170. } // end of subshape
  171. bezierGeometry = new THREE.Geometry();
  172. for (var i=0;i<beziers.length;i++) {
  173. p = beziers[i];
  174. bezierGeometry.vertices.push( new THREE.Vector3(p.x, p.y, 0) );
  175. }
  176. for (i=0;i<beziers.length;i+=3) {
  177. bezierGeometry.faces.push( new THREE.Face3(i, i+1, i+2) );
  178. bezierGeometry.faceVertexUvs[0].push( [
  179. new THREE.Vector2(0, 0),
  180. new THREE.Vector2(0.5, 0),
  181. new THREE.Vector2(1, 1)
  182. ] );
  183. }
  184. text3d = new THREE.ShapeGeometry( convexhullShapeGroup );
  185. text3d.computeBoundingBox();
  186. var centerOffset = -0.5 * ( text3d.boundingBox.max.x - text3d.boundingBox.min.x );
  187. text1 = new THREE.Mesh( text3d, textMaterial );
  188. text1.position.x = centerOffset + 150;
  189. group.add( text1 );
  190. text3d = new THREE.ShapeGeometry( solidShapeGroup );
  191. text3d.computeBoundingBox();
  192. var centerOffset = -0.5 * ( text3d.boundingBox.max.x - text3d.boundingBox.min.x );
  193. text2 = new THREE.Mesh( text3d, new THREE.MeshBasicMaterial( { color: new THREE.Color(1, 0, 0 ), side: THREE.DoubleSide, wireframe: true } ) );
  194. text2.position.x = centerOffset + 150;
  195. group.add( text2 );
  196. //
  197. bezierGeometry.computeBoundingBox();
  198. bezierGeometry.computeFaceNormals();
  199. bezierGeometry.computeVertexNormals();
  200. //
  201. var uniforms = {
  202. color: { type: 'c', value: new THREE.Color(0.45 * 0xffffff) }
  203. };
  204. var vertexShader = document.getElementById( 'vs' ).textContent;
  205. var fragmentShader = document.getElementById( 'fs' ).textContent;
  206. newMaterial = new THREE.ShaderMaterial({
  207. attributes: { invert: { type: 'f', value: invert } },
  208. uniforms: uniforms,
  209. vertexShader: vertexShader,
  210. fragmentShader: fragmentShader,
  211. side: THREE.DoubleSide
  212. });
  213. text = new THREE.Mesh( bezierGeometry, newMaterial );
  214. text.position.x = centerOffset;
  215. text.position.y = 0;
  216. text.position.z = 0;
  217. text.rotation.x = 0;
  218. text.rotation.y = Math.PI * 2;
  219. group.add( text );
  220. //
  221. text3d = new THREE.ShapeGeometry( solidShapeGroup );
  222. text3d.computeBoundingBox();
  223. text = new THREE.Mesh( text3d, new THREE.MeshBasicMaterial( { color: 0.45 * 0xffffff, side: THREE.DoubleSide } ) );
  224. text.position.x = centerOffset;
  225. text.position.y = 0;
  226. text.position.z = 0;
  227. text.rotation.x = 0;
  228. text.rotation.y = Math.PI * 2;
  229. group.add( text );
  230. //
  231. renderer = new THREE.WebGLRenderer( { antialias: true } );
  232. renderer.setClearColor( 0xf0f0f0 );
  233. renderer.setSize( window.innerWidth, window.innerHeight );
  234. document.body.appendChild( renderer.domElement );
  235. stats = new Stats();
  236. stats.domElement.style.position = 'absolute';
  237. stats.domElement.style.top = '0px';
  238. document.body.appendChild( stats.domElement );
  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>