webgl_shaders_vector.html 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  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. </style>
  15. </head>
  16. <body>
  17. <script src="../build/three.min.js"></script>
  18. <script src="./js/controls/OrbitControls.js"></script>
  19. <script src="js/libs/stats.min.js"></script>
  20. <!-- load the font file from canvas-text -->
  21. <script src="fonts/helvetiker_regular.typeface.js"></script>
  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>
  48. var container, stats;
  49. var camera, scene, renderer, controls;
  50. var group, text;
  51. var targetRotation = 0;
  52. var targetRotationOnMouseDown = 0;
  53. var mouseX = 0;
  54. var mouseXOnMouseDown = 0;
  55. var windowHalfX = window.innerWidth / 2;
  56. var windowHalfY = window.innerHeight / 2;
  57. var t = false;
  58. function toggle() {
  59. if ( t ) {
  60. text2.visible = 0;
  61. text1.visible = 1;
  62. } else {
  63. text2.visible = 1;
  64. text1.visible = 0;
  65. }
  66. t = !t;
  67. }
  68. init();
  69. animate();
  70. function init() {
  71. container = document.createElement( 'div' );
  72. document.body.appendChild( container );
  73. var info = document.createElement( 'div' );
  74. info.style.position = 'absolute';
  75. info.style.top = '10px';
  76. info.style.width = '100%';
  77. info.style.textAlign = 'center';
  78. info.innerHTML = 'Resolution-Independent Vector Fonts<br/><a href="https://github.com/mrdoob/three.js/issues/4746">rendered with three.js and WebGL </a>';
  79. container.appendChild( info );
  80. camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 1, 1000 );
  81. camera.position.set( 0, 150, 500 );
  82. controls = new THREE.OrbitControls( camera );
  83. scene = new THREE.Scene();
  84. var theText = "&"; // i % & j b 8
  85. var options = {
  86. size: 180,
  87. height: 20,
  88. curveSegments: 2,
  89. font: "helvetiker",
  90. bevelEnabled: false
  91. };
  92. group = new THREE.Object3D();
  93. scene.add( group );
  94. var textMaterial = new THREE.MeshBasicMaterial( { color: new THREE.Color(0, 0, 1 ), overdraw: 0.5, wireframe: true, side: THREE.DoubleSide } );
  95. textShapes = THREE.FontUtils.generateShapes( theText, options );
  96. text3d = new THREE.ShapeGeometry( textShapes );
  97. text3d.computeBoundingBox();
  98. var centerOffset = -0.5 * ( text3d.boundingBox.max.x - text3d.boundingBox.min.x );
  99. text = new THREE.Mesh( text3d, textMaterial );
  100. text.position.x = centerOffset - 150;
  101. group.add( text );
  102. //
  103. vA = new THREE.Vector2();
  104. vB = new THREE.Vector2();
  105. vDot = new THREE.Vector2();
  106. function processShape(path, reverse) {
  107. var pts = []; // bigger area (convex hull)
  108. var pts2 = []; // smaller area (full solid shapes)
  109. var beziers = []; // quad bezier points
  110. var invert = [];
  111. var z;
  112. var wind;
  113. pts.push( path[0].getPoint(0) );
  114. pts2.push( path[0].getPoint(0) );
  115. for (var i=0; i < path.length; i++) {
  116. curve = path[i];
  117. if (curve instanceof THREE.LineCurve) {
  118. pts.push( curve.v2 );
  119. pts2.push( curve.v2 );
  120. } else if (curve instanceof THREE.QuadraticBezierCurve) {
  121. vA = vA.subVectors( curve.v1, curve.v0 ); // .normalize()
  122. vB = vB.subVectors( curve.v2, curve.v1 );
  123. z = vA.x * vB.y - vA.y * vB.x; // z component of cross Production
  124. wind = z < 0; // clockwise/anticlock wind
  125. // if (reverse) wind = !wind;
  126. // console.log(z, wind , wind ? 'clockwise' : 'anti');
  127. if (wind) {
  128. pts.push( curve.v1 );
  129. pts.push( curve.v2 );
  130. pts2.push( curve.v2 );
  131. } else {
  132. pts.push( curve.v2 );
  133. pts2.push( curve.v1 );
  134. pts2.push( curve.v2 );
  135. }
  136. var flip = wind ? 1 : -1;
  137. // if (reverse) flip *= -1;
  138. invert.push(flip, flip, flip);
  139. beziers.push( curve.v0, curve.v1, curve.v2);
  140. }
  141. }
  142. return {
  143. pts: pts,
  144. pts2: pts2,
  145. beziers: beziers,
  146. invert: invert
  147. };
  148. }
  149. var subshape;
  150. var convexhullShapeGroup = [];
  151. var solidShapeGroup = [];
  152. var beziers = [], invert = [];
  153. for (var s=0;s<textShapes.length;s++) {
  154. subshape = textShapes[s];
  155. var process = processShape(subshape.curves);
  156. pts = process.pts;
  157. pts2 = process.pts2;
  158. beziers = beziers.concat(process.beziers);
  159. invert = invert.concat(process.invert);
  160. convexhullShape = new THREE.Shape( pts );
  161. solidShape = new THREE.Shape( pts2 );
  162. convexhullShapeGroup.push( convexhullShape );
  163. solidShapeGroup.push( solidShape );
  164. for (var i=0; i<subshape.holes.length;i++) {
  165. hole = subshape.holes[i];
  166. console.log('hole', hole);
  167. process = processShape(hole.curves, true);
  168. pts = process.pts;
  169. pts2 = process.pts2;
  170. beziers = beziers.concat(process.beziers);
  171. invert = invert.concat(process.invert);
  172. convexhullShape.holes.push(new THREE.Shape(pts));
  173. solidShape.holes.push(new THREE.Shape(pts2));
  174. }
  175. } // end of subshape
  176. bezierGeometry = new THREE.Geometry();
  177. for (var i=0;i<beziers.length;i++) {
  178. p = beziers[i];
  179. bezierGeometry.vertices.push( new THREE.Vector3(p.x, p.y, 0) );
  180. }
  181. for (i=0;i<beziers.length;i+=3) {
  182. bezierGeometry.faces.push( new THREE.Face3(i, i+1, i+2) );
  183. bezierGeometry.faceVertexUvs[0].push( [
  184. new THREE.Vector2(0, 0),
  185. new THREE.Vector2(0.5, 0),
  186. new THREE.Vector2(1, 1)
  187. ] );
  188. }
  189. text3d = new THREE.ShapeGeometry( convexhullShapeGroup );
  190. text3d.computeBoundingBox();
  191. var centerOffset = -0.5 * ( text3d.boundingBox.max.x - text3d.boundingBox.min.x );
  192. text1 = new THREE.Mesh( text3d, textMaterial );
  193. text1.position.x = centerOffset + 150;
  194. group.add( text1 );
  195. text3d = new THREE.ShapeGeometry( solidShapeGroup );
  196. text3d.computeBoundingBox();
  197. var centerOffset = -0.5 * ( text3d.boundingBox.max.x - text3d.boundingBox.min.x );
  198. text2 = new THREE.Mesh( text3d, new THREE.MeshBasicMaterial( { color: new THREE.Color(1, 0, 0 ), side: THREE.DoubleSide, wireframe: true } ) );
  199. text2.position.x = centerOffset + 150;
  200. group.add( text2 );
  201. //
  202. bezierGeometry.computeBoundingBox();
  203. bezierGeometry.computeFaceNormals();
  204. bezierGeometry.computeVertexNormals();
  205. //
  206. var uniforms = {
  207. color: { type: 'c', value: new THREE.Color(0.45 * 0xffffff) }
  208. };
  209. var vertexShader = document.getElementById( 'vs' ).textContent;
  210. var fragmentShader = document.getElementById( 'fs' ).textContent;
  211. newMaterial = new THREE.ShaderMaterial({
  212. attributes: { invert: { type: 'f', value: invert } },
  213. uniforms: uniforms,
  214. vertexShader: vertexShader,
  215. fragmentShader: fragmentShader,
  216. side: THREE.DoubleSide
  217. });
  218. text = new THREE.Mesh( bezierGeometry, newMaterial );
  219. text.position.x = centerOffset;
  220. text.position.y = 0;
  221. text.position.z = 0;
  222. text.rotation.x = 0;
  223. text.rotation.y = Math.PI * 2;
  224. group.add( text );
  225. //
  226. text3d = new THREE.ShapeGeometry( solidShapeGroup );
  227. text3d.computeBoundingBox();
  228. text = new THREE.Mesh( text3d, new THREE.MeshBasicMaterial( { color: 0.45 * 0xffffff, side: THREE.DoubleSide } ) );
  229. text.position.x = centerOffset;
  230. text.position.y = 0;
  231. text.position.z = 0;
  232. text.rotation.x = 0;
  233. text.rotation.y = Math.PI * 2;
  234. group.add( text );
  235. //
  236. renderer = new THREE.WebGLRenderer();
  237. renderer.setClearColor( 0xf0f0f0 );
  238. renderer.setSize( window.innerWidth, window.innerHeight );
  239. container.appendChild( renderer.domElement );
  240. stats = new Stats();
  241. stats.domElement.style.position = 'absolute';
  242. stats.domElement.style.top = '0px';
  243. container.appendChild( stats.domElement );
  244. document.addEventListener( 'mousedown', onDocumentMouseDown, false );
  245. //
  246. window.addEventListener( 'resize', onWindowResize, false );
  247. }
  248. function onWindowResize() {
  249. windowHalfX = window.innerWidth / 2;
  250. windowHalfY = window.innerHeight / 2;
  251. camera.aspect = window.innerWidth / window.innerHeight;
  252. camera.updateProjectionMatrix();
  253. renderer.setSize( window.innerWidth, window.innerHeight );
  254. }
  255. //
  256. function onDocumentMouseDown( event ) {
  257. toggle();
  258. }
  259. //
  260. function animate() {
  261. requestAnimationFrame( animate );
  262. render();
  263. stats.update();
  264. }
  265. function render() {
  266. controls.update();
  267. renderer.render( scene, camera );
  268. }
  269. </script>
  270. </body>
  271. </html>