webgl_geometry_extrude_splines.html 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - geometry - shapes</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. <canvas id="debug" style="position:absolute; left:100px"></canvas>
  18. <script src="../build/Three.js"></script>
  19. <script src="../src/extras/core/Curve.js"></script>
  20. <script src="../src/extras/geometries/TubeGeometry.js"></script>
  21. <script src="js/Stats.js"></script>
  22. <script>
  23. // Lets define some curves
  24. // Formula from http://mathworld.wolfram.com/HeartCurve.html
  25. THREE.HeartCurve = THREE.Curve.create(
  26. function(s) {
  27. this.scale = (s === undefined) ? 5 : s ;
  28. },
  29. function(t) {
  30. t *= 2 * Math.PI;
  31. var tx = 16 * Math.pow(Math.sin(t), 3);
  32. ty = 13 * Math.cos(t)
  33. - 5 * Math.cos(2 * t)
  34. - 2 * Math.cos(3 * t)
  35. - Math.cos(4 * t ),
  36. tz = 0;
  37. return new THREE.Vector3(tx, ty, tz).multiplyScalar(this.scale);
  38. }
  39. );
  40. // Viviani's Curve
  41. // http://en.wikipedia.org/wiki/Viviani%27s_curve
  42. THREE.VivianiCurve = THREE.Curve.create(
  43. function(radius) {
  44. this.radius = radius;
  45. },
  46. function(t) {
  47. t = t * 4 * Math.PI; // Normalized to 0..1
  48. var a = this.radius / 2;
  49. var tx = a * (1 + Math.cos(t)),
  50. ty = a * Math.sin(t),
  51. tz = 2 * a * Math.sin(t / 2);
  52. return new THREE.Vector3(tx, ty, tz);
  53. }
  54. );
  55. THREE.KnotCurve = THREE.Curve.create(
  56. function() {
  57. },
  58. function(t) {
  59. t *= 2 * Math.PI;
  60. var R = 10;
  61. var s = 50;
  62. var tx = s * Math.sin(t),
  63. ty = Math.cos(t) * (R + s * Math.cos(t)),
  64. tz = Math.sin(t) * (R + s * Math.cos(t));
  65. return new THREE.Vector3(tx, ty, tz);
  66. }
  67. );
  68. THREE.HelixCurve = THREE.Curve.create(
  69. function() {
  70. },
  71. function(t) {
  72. var a = 30; // radius
  73. var b = 150; //height
  74. var t2 = 2 * Math.PI * t * b / 30;
  75. var tx = Math.cos(t2) * a,
  76. ty = Math.sin(t2) * a,
  77. tz = b * t;
  78. return new THREE.Vector3(tx, ty, tz);
  79. }
  80. );
  81. var container, stats;
  82. var camera, scene, renderer;
  83. var text, plane;
  84. var targetRotation = 0;
  85. var targetRotationOnMouseDown = 0;
  86. var mouseX = 0;
  87. var mouseXOnMouseDown = 0;
  88. var windowHalfX = window.innerWidth / 2;
  89. var windowHalfY = window.innerHeight / 2;
  90. init();
  91. animate();
  92. function init() {
  93. container = document.createElement('div');
  94. document.body.appendChild(container);
  95. var info = document.createElement('div');
  96. info.style.position = 'absolute';
  97. info.style.top = '10px';
  98. info.style.width = '100%';
  99. info.style.textAlign = 'center';
  100. info.innerHTML = 'Simple procedurally generated 3D shapes example by <a href="http://www.lab4games.net/zz85/blog">zz85</a><br/>Drag to spin';
  101. container.appendChild(info);
  102. scene = new THREE.Scene();
  103. camera = new THREE.PerspectiveCamera(50, window.innerWidth / window.innerHeight, 1, 1000);
  104. camera.position.set(0, 50, 500);
  105. scene.add(camera);
  106. var light = new THREE.DirectionalLight(0xffffff);
  107. light.position.set(0, 0, 1);
  108. scene.add(light);
  109. parent = new THREE.Object3D();
  110. parent.position.y = 50;
  111. scene.add(parent);
  112. function addGeometry(geometry, color, x, y, z, rx, ry, rz, s) {
  113. // 3d shape
  114. var mesh = THREE.SceneUtils.createMultiMaterialObject(geometry, [new THREE.MeshLambertMaterial({
  115. color: color
  116. }), new THREE.MeshBasicMaterial({
  117. color: 0x000000,
  118. wireframe: true,
  119. transparent: true
  120. })]);
  121. if (geometry.debug) mesh.add(geometry.debug);
  122. mesh.position.set(x, y, z - 75);
  123. mesh.rotation.set(rx, ry, rz);
  124. mesh.scale.set(s, s, s);
  125. parent.add(mesh);
  126. }
  127. // var extrudePath = new THREE.SplineCurve3([
  128. // new THREE.Vector3(0, 10, -10), new THREE.Vector3(10, 0, -10), new THREE.Vector3(20, 0, 0), new THREE.Vector3(30, 0, 10), new THREE.Vector3(30, 0, 20), new THREE.Vector3(20, 0, 30), new THREE.Vector3(10, 0, 30), new THREE.Vector3(0, 0, 30), new THREE.Vector3(-10, 10, 30), new THREE.Vector3(-10, 20, 30), new THREE.Vector3(0, 30, 30), new THREE.Vector3(10, 30, 30), new THREE.Vector3(20, 30, 15), new THREE.Vector3(10, 30, 10),
  129. // new THREE.Vector3(0, 30, 10), new THREE.Vector3(-10, 20, 10), new THREE.Vector3(-10, 10, 10), new THREE.Vector3(0, 0, 10), new THREE.Vector3(10, -10, 10), new THREE.Vector3(20, -15, 10), new THREE.Vector3(30, -15, 10), new THREE.Vector3(40, -15, 10), new THREE.Vector3(50, -15, 10), new THREE.Vector3(60, 0, 10), new THREE.Vector3(70, 0, 0), new THREE.Vector3(80, 0, 0), new THREE.Vector3(90, 0, 0), new THREE.Vector3(100, 0, 0)]);
  130. var extrudePath = new THREE.HeartCurve(3.5);
  131. // var extrudePath = new THREE.VivianiCurve(70);
  132. // var extrudePath = new THREE.KnotCurve();
  133. // extrudePath = new THREE.HelixCurve();
  134. // var extrudePath = new THREE.ClosedSplineCurve3([
  135. // new THREE.Vector3(0, -40, -40),
  136. // new THREE.Vector3(0, 40, -40),
  137. // new THREE.Vector3(0, 140, -40),
  138. // new THREE.Vector3(0, 40, 40),
  139. // new THREE.Vector3(0, -40, 40),
  140. // ]);
  141. var tube = new THREE.TubeGeometry(5, 100, 10, extrudePath, true);
  142. addGeometry(tube, 0xff00ff, 0, -80, 0, 0, 0, 0, 3);
  143. //
  144. renderer = new THREE.WebGLRenderer({
  145. antialias: true
  146. });
  147. renderer.setSize(window.innerWidth, window.innerHeight);
  148. container.appendChild(renderer.domElement);
  149. stats = new Stats();
  150. stats.domElement.style.position = 'absolute';
  151. stats.domElement.style.top = '0px';
  152. container.appendChild(stats.domElement);
  153. document.addEventListener('mousedown', onDocumentMouseDown, false);
  154. document.addEventListener('touchstart', onDocumentTouchStart, false);
  155. document.addEventListener('touchmove', onDocumentTouchMove, false);
  156. }
  157. //
  158. function onDocumentMouseDown(event) {
  159. event.preventDefault();
  160. document.addEventListener('mousemove', onDocumentMouseMove, false);
  161. document.addEventListener('mouseup', onDocumentMouseUp, false);
  162. document.addEventListener('mouseout', onDocumentMouseOut, false);
  163. mouseXOnMouseDown = event.clientX - windowHalfX;
  164. targetRotationOnMouseDown = targetRotation;
  165. }
  166. function onDocumentMouseMove(event) {
  167. mouseX = event.clientX - windowHalfX;
  168. targetRotation = targetRotationOnMouseDown + (mouseX - mouseXOnMouseDown) * 0.02;
  169. }
  170. function onDocumentMouseUp(event) {
  171. document.removeEventListener('mousemove', onDocumentMouseMove, false);
  172. document.removeEventListener('mouseup', onDocumentMouseUp, false);
  173. document.removeEventListener('mouseout', onDocumentMouseOut, false);
  174. }
  175. function onDocumentMouseOut(event) {
  176. document.removeEventListener('mousemove', onDocumentMouseMove, false);
  177. document.removeEventListener('mouseup', onDocumentMouseUp, false);
  178. document.removeEventListener('mouseout', onDocumentMouseOut, false);
  179. }
  180. function onDocumentTouchStart(event) {
  181. if (event.touches.length == 1) {
  182. event.preventDefault();
  183. mouseXOnMouseDown = event.touches[0].pageX - windowHalfX;
  184. targetRotationOnMouseDown = targetRotation;
  185. }
  186. }
  187. function onDocumentTouchMove(event) {
  188. if (event.touches.length == 1) {
  189. event.preventDefault();
  190. mouseX = event.touches[0].pageX - windowHalfX;
  191. targetRotation = targetRotationOnMouseDown + (mouseX - mouseXOnMouseDown) * 0.05;
  192. }
  193. }
  194. //
  195. function animate() {
  196. requestAnimationFrame(animate);
  197. render();
  198. stats.update();
  199. }
  200. function render() {
  201. parent.rotation.y += (targetRotation - parent.rotation.y) * 0.05;
  202. renderer.render(scene, camera);
  203. }</script>
  204. </body>
  205. </html>
粤ICP备19079148号