webgl_shaders_vector.html 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  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, 10000 );
  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( {
  90. color: new THREE.Color(0, 0, 1 ),
  91. side: THREE.DoubleSide,
  92. wireframe: true
  93. } );
  94. textShapes = THREE.FontUtils.generateShapes( theText, options );
  95. text3d = new THREE.ShapeGeometry( textShapes );
  96. text3d.computeBoundingBox();
  97. var centerOffset = -0.5 * ( text3d.boundingBox.max.x - text3d.boundingBox.min.x );
  98. text = new THREE.Mesh( text3d, textMaterial );
  99. text.position.x = centerOffset - 150;
  100. group.add( text );
  101. //
  102. vA = new THREE.Vector2();
  103. vB = new THREE.Vector2();
  104. vDot = new THREE.Vector2();
  105. function processShape(path, reverse) {
  106. var pts = []; // bigger area (convex hull)
  107. var pts2 = []; // smaller area (full solid shapes)
  108. var beziers = []; // quad bezier points
  109. var invert = [];
  110. var z;
  111. var wind;
  112. pts.push( path[0].getPoint(0) );
  113. pts2.push( path[0].getPoint(0) );
  114. for (var i=0; i < path.length; i++) {
  115. curve = path[i];
  116. if (curve instanceof THREE.LineCurve) {
  117. pts.push( curve.v2 );
  118. pts2.push( curve.v2 );
  119. } else if (curve instanceof THREE.QuadraticBezierCurve) {
  120. vA = vA.subVectors( curve.v1, curve.v0 ); // .normalize()
  121. vB = vB.subVectors( curve.v2, curve.v1 );
  122. z = vA.x * vB.y - vA.y * vB.x; // z component of cross Production
  123. wind = z < 0; // clockwise/anticlock wind
  124. // if (reverse) wind = !wind;
  125. // console.log(z, wind , wind ? 'clockwise' : 'anti');
  126. if (wind) {
  127. pts.push( curve.v1 );
  128. pts.push( curve.v2 );
  129. pts2.push( curve.v2 );
  130. } else {
  131. pts.push( curve.v2 );
  132. pts2.push( curve.v1 );
  133. pts2.push( curve.v2 );
  134. }
  135. var flip = wind ? 1 : -1;
  136. // if (reverse) flip *= -1;
  137. invert.push(flip, flip, flip);
  138. beziers.push( curve.v0, curve.v1, curve.v2);
  139. }
  140. }
  141. return {
  142. pts: pts,
  143. pts2: pts2,
  144. beziers: beziers,
  145. invert: invert
  146. };
  147. }
  148. var subshape;
  149. var convexhullShapeGroup = [];
  150. var solidShapeGroup = [];
  151. var beziers = [], invert = [];
  152. for (var s=0;s<textShapes.length;s++) {
  153. subshape = textShapes[s];
  154. var process = processShape(subshape.curves);
  155. pts = process.pts;
  156. pts2 = process.pts2;
  157. beziers = beziers.concat(process.beziers);
  158. invert = invert.concat(process.invert);
  159. convexhullShape = new THREE.Shape( pts );
  160. solidShape = new THREE.Shape( pts2 );
  161. convexhullShapeGroup.push( convexhullShape );
  162. solidShapeGroup.push( solidShape );
  163. for (var i=0; i<subshape.holes.length;i++) {
  164. hole = subshape.holes[i];
  165. // console.log('hole', hole);
  166. process = processShape(hole.curves, true);
  167. pts = process.pts;
  168. pts2 = process.pts2;
  169. beziers = beziers.concat(process.beziers);
  170. invert = invert.concat(process.invert);
  171. convexhullShape.holes.push(new THREE.Shape(pts));
  172. solidShape.holes.push(new THREE.Shape(pts2));
  173. }
  174. } // end of subshape
  175. bezierGeometry = new THREE.Geometry();
  176. for (var i=0;i<beziers.length;i++) {
  177. p = beziers[i];
  178. bezierGeometry.vertices.push( new THREE.Vector3(p.x, p.y, 0) );
  179. }
  180. for (i=0;i<beziers.length;i+=3) {
  181. bezierGeometry.faces.push( new THREE.Face3(i, i+1, i+2) );
  182. bezierGeometry.faceVertexUvs[0].push( [
  183. new THREE.Vector2(0, 0),
  184. new THREE.Vector2(0.5, 0),
  185. new THREE.Vector2(1, 1)
  186. ] );
  187. }
  188. text3d = new THREE.ShapeGeometry( convexhullShapeGroup );
  189. text3d.computeBoundingBox();
  190. var centerOffset = -0.5 * ( text3d.boundingBox.max.x - text3d.boundingBox.min.x );
  191. text1 = new THREE.Mesh( text3d, textMaterial );
  192. text1.position.x = centerOffset + 150;
  193. group.add( text1 );
  194. text3d = new THREE.ShapeGeometry( solidShapeGroup );
  195. text3d.computeBoundingBox();
  196. var centerOffset = -0.5 * ( text3d.boundingBox.max.x - text3d.boundingBox.min.x );
  197. text2 = new THREE.Mesh( text3d, new THREE.MeshBasicMaterial( { color: new THREE.Color(1, 0, 0 ), side: THREE.DoubleSide, wireframe: true } ) );
  198. text2.position.x = centerOffset + 150;
  199. group.add( text2 );
  200. //
  201. bezierGeometry.computeBoundingBox();
  202. bezierGeometry.computeFaceNormals();
  203. bezierGeometry.computeVertexNormals();
  204. bezierGeometry = new THREE.BufferGeometry().fromGeometry( bezierGeometry );
  205. bezierGeometry.addAttribute( 'invert', new THREE.Float32Attribute( invert, 1 ) );
  206. //
  207. newMaterial = new THREE.ShaderMaterial({
  208. uniforms: {
  209. color: { type: 'c', value: new THREE.Color(0.45 * 0xffffff) }
  210. },
  211. vertexShader: document.getElementById( 'vs' ).textContent,
  212. fragmentShader: document.getElementById( 'fs' ).textContent,
  213. side: THREE.DoubleSide
  214. });
  215. text = new THREE.Mesh( bezierGeometry, newMaterial );
  216. text.position.x = centerOffset;
  217. text.position.y = 0;
  218. text.position.z = 0;
  219. text.rotation.x = 0;
  220. text.rotation.y = Math.PI * 2;
  221. group.add( text );
  222. //
  223. text3d = new THREE.ShapeGeometry( solidShapeGroup );
  224. text3d.computeBoundingBox();
  225. text = new THREE.Mesh( text3d, new THREE.MeshBasicMaterial( { color: 0.45 * 0xffffff, side: THREE.DoubleSide } ) );
  226. text.position.x = centerOffset;
  227. text.position.y = 0;
  228. text.position.z = 0;
  229. text.rotation.x = 0;
  230. text.rotation.y = Math.PI * 2;
  231. group.add( text );
  232. //
  233. renderer = new THREE.WebGLRenderer( { antialias: true } );
  234. renderer.setClearColor( 0xf0f0f0 );
  235. renderer.setPixelRatio( window.devicePixelRatio );
  236. renderer.setSize( window.innerWidth, window.innerHeight );
  237. document.body.appendChild( renderer.domElement );
  238. stats = new Stats();
  239. stats.domElement.style.position = 'absolute';
  240. stats.domElement.style.top = '0px';
  241. document.body.appendChild( stats.domElement );
  242. document.addEventListener( 'mousedown', toggle, false );
  243. window.addEventListener( 'resize', onWindowResize, false );
  244. }
  245. function onWindowResize() {
  246. camera.aspect = window.innerWidth / window.innerHeight;
  247. camera.updateProjectionMatrix();
  248. renderer.setSize( window.innerWidth, window.innerHeight );
  249. }
  250. //
  251. function animate() {
  252. requestAnimationFrame( animate );
  253. render();
  254. stats.update();
  255. }
  256. function render() {
  257. controls.update();
  258. renderer.render( scene, camera );
  259. }
  260. </script>
  261. </body>
  262. </html>