diff --git a/README.md b/README.md index 1410c30..9259dd0 100644 --- a/README.md +++ b/README.md @@ -1,82 +1,34 @@ # [Project2: Toolbox Functions](https://github.com/CIS700-Procedural-Graphics/Project2-Toolbox-Functions) ## Overview - -The objective of this assignment is to procedurally model and animate a bird wing. Let's get creative! - -Start by forking and then cloning [this repository](https://github.com/CIS700-Procedural-Graphics/Project2-Toolbox-Functions) - -## Modeling - -##### Reference images - -Search for three or more images of a bird wing (or any flying creature, really) in order to provide yourself reference material, as you're going to base your modeling and animation from these images. For the more artistic minds, feel free to sketch your own concept. - -##### Make wing curve - -Begin with a 3D curve for your basic wing shape. Three.js provides classes to create many different types of curves, so you may use whatever type of curve you prefer. - -##### Distribute feathers - -We have provided a simple feather model from which to begin. You are not required to use this model if you have others that you prefer. From this base, you must duplicate the feather to model a complete wing, and your wing should consist of at least thirty feathers. Distribute points along the curve you created previously; you will append the feather primitives to the curve at these points. Make sure that you modify the size, orientation, and color of your feathers depending on their location on the wing. - -Feel free to diversify your wings by using multiple base feather models. - -## Animation - -Add a wind force to your scene, and parameterize its direction and speed. You will use this wind force to animate the feathers of your wing by vibrating them slightly. Using Dat.GUI, allow the user to modify these wind parameters. Please note that we don't care about your feather motion being physically accurate, as long as it looks nice. - -Additionally, animate the control points of your wing curve to make the wing flap, and allow the user to control the speed of the wing flapping. - -## Interactivity - -Using Dat.GUI and the examples provided in the reference code, allow the user to adjust the following controls: - -1. The curvature of the wing's basic shape -2. Feather distribution -3. Feather size -4. Feather color -5. Feather orientation -6. Flapping speed -7. Flapping motion - -## For the Overachievers - -Suggestions: -- Make a pretty iridescent or otherwise feather appropriate shader. -- Otherwise, going the extra mile for this assignment is really in the polish! - -## Submission - -- Create a folder called `references` to include your reference images. - -- Update `README.md` to contain a solid description of your project - -- Publish your project to gh-pages. `npm run deploy`. It should now be visible at http://username.github.io/repo-name - -- Create a [pull request](https://help.github.com/articles/creating-a-pull-request/) to this repository, and in the comment, include a link to your published project. - -- Submit the link to your pull request on Canvas. - -## Getting Started - -1. [Install Node.js](https://nodejs.org/en/download/). Node.js is a JavaScript runtime. It basically allows you to run JavaScript when not in a browser. For our purposes, this is not necessary. The important part is that with it comes `npm`, the Node Package Manager. This allows us to easily declare and install external dependencies such as [three.js](https://threejs.org/), [dat.GUI](https://workshop.chromeexperiments.com/examples/gui/#1--Basic-Usage), and [glMatrix](http://glmatrix.net/). Some other packages we'll be using make it significantly easier to develop your code and create modules for better code reuse and clarity. These tools make it _signficantly_ easier to write code in multiple `.js` files without globally defining everything. - -2. Fork and clone your repository. - -3. In the root directory of your project, run `npm install`. This will download all of those dependencies. - -4. Do either of the following (but I highly recommend the first one for reasons I will explain later). - - a. Run `npm start` and then go to `localhost:7000` in your web browser - - b. Run `npm run build` and then go open `index.html` in your web browser - - You should hopefully see the framework code with a 3D cube at the center of the screen! - - -## Developing Your Code -All of the JavaScript code is living inside the `src` directory. The main file that gets executed when you load the page as you may have guessed is `main.js`. Here, you can make any changes you want, import functions from other files, etc. The reason that I highly suggest you build your project with `npm start` is that doing so will start a process that watches for any changes you make to your code. If it detects anything, it'll automagically rebuild your project and then refresh your browser window for you. Wow. That's cool. If you do it the other way, you'll need to run `npm build` and then refresh your page every time you want to test something. - -## Publishing Your Code -We highly suggest that you put your code on GitHub. One of the reasons we chose to make this course using JavaScript is that the Web is highly accessible and making your awesome work public and visible can be a huge benefit when you're looking to score a job or internship. To aid you in this process, running `npm run deploy` will automatically build your project and push it to `gh-pages` where it will be visible at `username.github.io/repo-name`. \ No newline at end of file +Animated bird wing using Three.js and dat.GUI. + +## Features +- Wing controls + - Bezier control points +- Feather controls + - length + - width + - distribution (density) + - layers + - color +- Wind controls + - Wind speed + - Wind directions (wind always blows in the +z axis, but you can control x and y directions to modulate feather orientation) +- Wing Controls + - Flapping speed + - Flapping motion + - 5 Keyframes + +## The Bird Wing +The wing itself is segmented into 3 sections and 3 layers (modifiable). These sections spawn off of a "bone" cubic Bezier curve. These sections represent the primary, secondary, and tertiary feathers that the bird uses to guide itself, give itself thrust, and stay warm respectively. The added layers gives the wing some density and thickness to make it realistic. The primary feathers is splayed using a power curve as well as a LERP. The rotation along the y-axis as the feathers extend outward uses a LERP as well. The secondary feathers, also use LERPs as well as a parabolic curve for added realism (referencing a wing photo). Finally, the scapular or tertiary wings use a cubic pulse as we can best control the falloff of the curve. The feathers are sampled regularly depending on the layer. The higher layers are more sparse but compensate by being wider (z-axis scaling). + +## Cool Things +Try turning the wind on and raise flapping speed to ~6-8 to see a flapping wing! Note the Bezier controls are reactive to the position of the wing. Two functions help us achieve this: a `diff` function that calculates the keyframe delta with an added alpha parameter to modulate step-sizes (for keyframe interpolation), and a `moveWingTo`/`moveWing` function that moves the wing using Bezier control points in an absolute/relative fashion. + + +## Future work +- Rotational and Scale Keyframes +- Feather shaders +- More anatomically correct wing +- Smoother keyframe interpolations diff --git a/Thumbs.db b/Thumbs.db new file mode 100644 index 0000000..3c4bf81 Binary files /dev/null and b/Thumbs.db differ diff --git a/bird1.jpg b/bird1.jpg new file mode 100644 index 0000000..8647e72 Binary files /dev/null and b/bird1.jpg differ diff --git a/bird2.jpg b/bird2.jpg new file mode 100644 index 0000000..547574d Binary files /dev/null and b/bird2.jpg differ diff --git a/bird3.png b/bird3.png new file mode 100644 index 0000000..952bd00 Binary files /dev/null and b/bird3.png differ diff --git a/deploy.js b/deploy.js new file mode 100644 index 0000000..b6273f6 --- /dev/null +++ b/deploy.js @@ -0,0 +1,38 @@ +let colors = require('colors'); +let path = require('path'); +let git = require('simple-git')(__dirname); +let deploy = require('gh-pages-deploy'); +let packageJSON = require('require-module')('./package.json'); + +let success = 1; +git.fetch('origin', 'master', function(err) { + if (err) throw err; + git.status(function(err, status) { + if (err) throw err; + if (!status.isClean()) { + success = 0; + console.error('Error: You have uncommitted changes! Please commit them first'.red); + } + + if (status.current !== 'master') { + success = 0; + console.warn('Warning: Please deploy from the master branch!'.yellow) + } + + git.diffSummary(['origin/master'], function(err, diff) { + if (err) throw err; + + if (diff.files.length || diff.insertions || diff.deletions) { + success = 0; + console.error('Error: Current branch is different from origin/master! Please push all changes first'.red) + } + + if (success) { + let cfg = packageJSON['gh-pages-deploy'] || {}; + let buildCmd = deploy.getFullCmd(cfg); + deploy.displayCmds(deploy.getFullCmd(cfg)); + deploy.execBuild(buildCmd, cfg); + } + }) + }) +}) \ No newline at end of file diff --git a/feather.obj b/feather.obj new file mode 100644 index 0000000..c504681 --- /dev/null +++ b/feather.obj @@ -0,0 +1,317 @@ +# This file uses centimeters as units for non-parametric coordinates. + +v 0.021840 0.007498 0.031870 +v 1.627541 -0.000403 0.014030 +v 0.020817 0.028495 -0.004365 +v 1.627544 0.024164 0.003509 +v 0.008561 0.008395 -0.025352 +v 1.627555 0.005486 -0.017680 +v 0.005514 -0.013343 -0.002028 +v 1.627554 -0.022364 -0.002045 +v 0.620937 -0.010772 -0.005879 +v 0.622123 0.005747 0.020990 +v 0.622031 0.021952 -0.006975 +v 0.621140 0.006113 -0.023702 +v 2.215028 -0.012329 -0.001152 +v 2.215755 -0.001528 0.005663 +v 2.214896 0.000799 -0.007448 +v 2.216814 0.009689 0.001213 +vt 0.375000 0.000000 +vt 0.625000 0.000000 +vt 0.375000 0.250000 +vt 0.625000 0.250000 +vt 0.375000 0.500000 +vt 0.625000 0.500000 +vt 0.375000 0.750000 +vt 0.625000 0.750000 +vt 0.375000 1.000000 +vt 0.625000 1.000000 +vt 0.875000 0.000000 +vt 0.875000 0.250000 +vt 0.125000 0.000000 +vt 0.125000 0.250000 +vt 0.440704 0.750000 +vt 0.440704 0.000000 +vt 0.440704 1.000000 +vt 0.440704 0.250000 +vt 0.440704 0.500000 +vt 0.625000 0.000000 +vt 0.875000 0.000000 +vt 0.875000 0.250000 +vt 0.625000 0.250000 +vn 0.011600 0.865261 0.501187 +vn 0.005248 0.773886 0.633303 +vn 0.005231 0.773619 0.633629 +vn 0.011600 0.865261 0.501187 +vn 0.004711 0.723113 -0.690714 +vn 0.004856 0.733407 -0.679773 +vn 0.004852 0.733106 -0.680097 +vn 0.004711 0.723113 -0.690714 +vn -0.001055 -0.729071 -0.684437 +vn -0.000225 -0.650684 -0.759349 +vn -0.000230 -0.651224 -0.758886 +vn -0.001055 -0.729071 -0.684437 +vn 0.006904 -0.852738 0.522293 +vn -0.000101 -0.795068 0.606521 +vn -0.000269 -0.793583 0.608461 +vn 0.006904 -0.852738 0.522293 +vn 0.994333 -0.072295 -0.077940 +vn 0.994333 -0.072295 -0.077940 +vn 0.994333 -0.072295 -0.077940 +vn 0.994333 -0.072295 -0.077940 +vn -0.912742 0.345987 0.217246 +vn -0.912741 0.345987 0.217246 +vn -0.912742 0.345987 0.217246 +vn -0.912741 0.345987 0.217246 +vn -0.001167 -0.698697 0.715417 +vn -0.001263 -0.699741 0.714396 +vn 0.003991 0.607615 0.794221 +vn 0.003936 0.608773 0.793334 +vn 0.008567 0.737733 -0.675038 +vn 0.008626 0.737706 -0.675067 +vn 0.003601 -0.559158 -0.829053 +vn 0.003633 -0.558773 -0.829312 +vn 0.009551 -0.572855 0.819601 +vn 0.009551 -0.572855 0.819601 +vn 0.010497 -0.471800 -0.881643 +vn 0.010497 -0.471800 -0.881643 +vn 0.016532 0.733988 -0.678962 +vn 0.016532 0.733988 -0.678961 +vn 0.013462 0.385565 0.922583 +vn 0.013462 0.385565 0.922583 +f 1/1/1 10/16/2 11/18/3 3/3/4 +f 3/3/5 11/18/6 12/19/7 5/5/8 +f 5/5/9 12/19/10 9/15/11 7/7/12 +f 7/7/13 9/15/14 10/17/15 1/9/16 +f 14/20/17 13/21/18 15/22/19 16/23/20 +f 7/13/21 1/1/22 3/3/23 5/14/24 +f 9/15/14 8/8/25 2/10/26 10/17/15 +f 11/18/3 10/16/2 2/2/27 4/4/28 +f 12/19/7 11/18/6 4/4/29 6/6/30 +f 9/15/11 12/19/10 6/6/31 8/8/32 +f 2/2/26 8/11/25 13/21/33 14/20/34 +f 8/11/32 6/12/31 15/22/35 13/21/36 +f 6/12/30 4/4/29 16/23/37 15/22/38 +f 4/4/28 2/2/27 14/20/39 16/23/40 +v 0.388143 -0.000000 0.081000 +v 0.667221 -0.000000 0.197440 +v 0.946608 -0.000000 0.192079 +v 1.818651 -0.000000 0.164215 +v 1.987145 -0.000000 0.113365 +v 2.339494 -0.000000 0.052713 +v 0.374465 -0.014336 0.006780 +v 0.681996 -0.014336 0.006780 +v 0.945831 -0.014336 0.006961 +v 1.856006 -0.014336 0.006221 +v 2.026244 -0.014336 0.006071 +v 2.419290 -0.004730 0.006529 +v 0.375323 0.000000 -0.047900 +v 0.675429 0.000000 -0.141401 +v 0.946002 0.000000 -0.163412 +v 1.883190 0.000000 -0.133212 +v 2.053084 0.000000 -0.132158 +v 2.407803 0.000000 -0.041616 +v 1.109235 -0.000000 0.157419 +v 1.127423 -0.014336 0.006531 +v 1.148529 0.000000 -0.153582 +v 0.388143 0.003354 0.081000 +v 0.667221 0.003354 0.197440 +v 0.681996 0.017690 0.006780 +v 0.374465 0.017690 0.006780 +v 0.946608 0.003354 0.192079 +v 0.945831 0.017690 0.006961 +v 1.109235 0.003354 0.157419 +v 1.127423 0.017690 0.006531 +v 1.818651 0.003354 0.164215 +v 1.987145 0.003354 0.113365 +v 2.026244 0.017690 0.006071 +v 1.856006 0.017690 0.006221 +v 2.339494 0.003354 0.052713 +v 2.419290 0.008084 0.006529 +v 0.675429 0.003354 -0.141401 +v 0.375323 0.003354 -0.047900 +v 0.946002 0.003354 -0.163412 +v 1.148529 0.003354 -0.153582 +v 2.053084 0.003354 -0.132158 +v 1.883190 0.003354 -0.133212 +v 2.407803 0.003354 -0.041616 +vt 0.000000 0.000000 +vt 0.200000 0.000000 +vt 0.400000 0.000000 +vt 0.600000 0.000000 +vt 0.800000 0.000000 +vt 1.000000 0.000000 +vt 0.000000 0.250000 +vt 0.200000 0.250000 +vt 0.400000 0.250000 +vt 0.600000 0.250000 +vt 0.800000 0.250000 +vt 1.000000 0.250000 +vt 0.000000 0.500000 +vt 0.200000 0.500000 +vt 0.400000 0.500000 +vt 0.600000 0.500000 +vt 0.800000 0.500000 +vt 1.000000 0.500000 +vt 0.433224 0.000000 +vt 0.433224 0.250000 +vt 0.433224 0.500000 +vt 0.000000 0.000000 +vt 0.200000 0.000000 +vt 0.200000 0.250000 +vt 0.000000 0.250000 +vt 0.400000 0.000000 +vt 0.400000 0.250000 +vt 0.433224 0.000000 +vt 0.433224 0.250000 +vt 0.600000 0.000000 +vt 0.800000 0.000000 +vt 0.800000 0.250000 +vt 0.600000 0.250000 +vt 1.000000 0.000000 +vt 1.000000 0.250000 +vt 0.200000 0.500000 +vt 0.000000 0.500000 +vt 0.400000 0.500000 +vt 0.433224 0.500000 +vt 0.800000 0.500000 +vt 0.600000 0.500000 +vt 1.000000 0.500000 +vt 0.000000 0.000000 +vt 0.200000 0.000000 +vt 0.000000 0.250000 +vt 0.400000 0.000000 +vt 0.433224 0.000000 +vt 0.600000 0.000000 +vt 0.800000 0.000000 +vt 1.000000 0.000000 +vt 1.000000 0.250000 +vt 0.200000 0.500000 +vt 0.000000 0.500000 +vt 0.400000 0.500000 +vt 0.433224 0.500000 +vt 0.800000 0.500000 +vt 0.600000 0.500000 +vt 1.000000 0.500000 +vn -0.021340 0.993975 0.107507 +vn -0.007968 0.996045 0.088493 +vn -0.009534 0.999954 -0.000556 +vn -0.021551 0.999727 -0.009028 +vn 0.003338 0.996855 0.079171 +vn 0.001292 0.999998 -0.001352 +vn 0.001881 0.995883 0.090624 +vn 0.001375 0.999995 0.002737 +vn 0.001346 0.995535 0.094383 +vn 0.019635 0.992177 0.123289 +vn 0.017452 0.999824 0.006871 +vn 0.002077 0.999959 -0.008868 +vn 0.024195 0.989616 0.141683 +vn 0.024875 0.998735 -0.043697 +vn -0.010314 0.994073 -0.108221 +vn -0.021439 0.990014 -0.139326 +vn -0.001161 0.996106 -0.088156 +vn 0.001469 0.995639 -0.093282 +vn 0.015531 0.994781 -0.100842 +vn 0.001085 0.995315 -0.096685 +vn 0.024913 0.994705 -0.099708 +vn -0.021340 -0.993975 0.107507 +vn -0.021551 -0.999727 -0.009028 +vn -0.009534 -0.999954 -0.000556 +vn -0.007968 -0.996045 0.088493 +vn 0.001292 -0.999998 -0.001352 +vn 0.003338 -0.996855 0.079171 +vn 0.001375 -0.999995 0.002737 +vn 0.001881 -0.995883 0.090624 +vn 0.001346 -0.995535 0.094383 +vn 0.002077 -0.999959 -0.008868 +vn 0.017452 -0.999824 0.006871 +vn 0.019635 -0.992177 0.123289 +vn 0.024875 -0.998735 -0.043697 +vn 0.024195 -0.989616 0.141683 +vn -0.021439 -0.990014 -0.139326 +vn -0.010314 -0.994073 -0.108221 +vn -0.001161 -0.996106 -0.088156 +vn 0.001469 -0.995639 -0.093282 +vn 0.001085 -0.995315 -0.096685 +vn 0.015531 -0.994781 -0.100842 +vn 0.024913 -0.994705 -0.099708 +vn -0.385059 0.000000 0.922892 +vn -0.195078 0.000000 0.980788 +vn -0.195078 0.000000 0.980788 +vn -0.385059 0.000000 0.922892 +vn -0.994845 0.000000 0.101409 +vn -0.983440 0.000000 0.181234 +vn -0.983440 0.000000 0.181234 +vn -0.994845 0.000000 0.101409 +vn 0.090174 0.000000 0.995926 +vn 0.090174 0.000000 0.995926 +vn 0.031935 0.000000 0.999490 +vn 0.031935 0.000000 0.999490 +vn 0.050118 0.000000 0.998743 +vn 0.209337 0.000000 0.977844 +vn 0.209337 0.000000 0.977844 +vn 0.050118 0.000000 0.998743 +vn 0.303675 0.000000 0.952776 +vn 0.303675 0.000000 0.952776 +vn 0.500926 0.000000 0.865490 +vn 0.500926 0.000000 0.865490 +vn -0.198387 0.000000 -0.980124 +vn -0.297457 0.000000 -0.954735 +vn -0.297457 0.000000 -0.954735 +vn -0.198387 0.000000 -0.980124 +vn -0.999877 0.000000 -0.015681 +vn -0.999877 0.000000 -0.015681 +vn -0.025737 0.000000 -0.999669 +vn -0.025737 0.000000 -0.999669 +vn 0.032207 0.000000 -0.999481 +vn 0.032207 0.000000 -0.999481 +vn 0.171995 0.000000 -0.985098 +vn 0.023678 0.000000 -0.999720 +vn 0.023678 0.000000 -0.999720 +vn 0.171995 0.000000 -0.985098 +vn 0.972698 0.000000 -0.232074 +vn 0.972698 0.000000 -0.232074 +vn 0.972698 0.000000 -0.232074 +vn 0.972698 0.000000 -0.232074 +vn 0.247319 0.000000 -0.968934 +vn 0.247319 0.000000 -0.968934 +f 38/66/41 39/67/42 40/31/43 41/68/44 +f 39/67/42 42/69/45 43/32/46 40/31/43 +f 42/69/45 44/70/47 45/43/48 43/32/46 +f 46/71/49 47/72/50 48/34/51 49/33/52 +f 47/72/50 50/73/53 51/74/54 48/34/51 +f 41/68/44 40/31/43 52/75/55 53/76/56 +f 40/31/43 43/32/46 54/77/57 52/75/55 +f 43/32/46 45/43/48 55/78/58 54/77/57 +f 49/33/52 48/34/51 56/79/59 57/80/60 +f 51/74/54 58/81/61 56/79/59 48/34/51 +f 44/70/47 46/71/49 49/33/52 45/43/48 +f 55/78/58 45/43/48 49/33/52 57/80/60 +f 17/45/62 23/48/63 24/47/64 18/46/65 +f 18/46/65 24/47/64 25/50/66 19/49/67 +f 19/49/67 25/50/66 36/52/68 35/51/69 +f 20/53/70 26/56/71 27/55/72 21/54/73 +f 21/54/73 27/55/72 28/58/74 22/57/75 +f 23/48/63 29/60/76 30/59/77 24/47/64 +f 24/47/64 30/59/77 31/61/78 25/50/66 +f 25/50/66 31/61/78 37/62/79 36/52/68 +f 26/56/71 32/64/80 33/63/81 27/55/72 +f 28/58/74 27/55/72 33/63/81 34/65/82 +f 35/51/69 36/52/68 26/56/71 20/53/70 +f 37/62/79 32/64/80 26/56/71 36/52/68 +f 17/24/83 18/25/84 39/67/85 38/66/86 +f 23/30/87 17/24/88 38/66/89 41/68/90 +f 18/25/84 19/26/91 42/69/92 39/67/85 +f 19/26/91 35/42/93 44/70/94 42/69/92 +f 20/27/95 21/28/96 47/72/97 46/71/98 +f 21/28/96 22/29/99 50/73/100 47/72/97 +f 22/29/99 28/35/101 51/74/102 50/73/100 +f 30/37/103 29/36/104 53/76/105 52/75/106 +f 29/36/107 23/30/87 41/68/90 53/76/108 +f 31/38/109 30/37/103 52/75/106 54/77/110 +f 37/44/111 31/38/109 54/77/110 55/78/112 +f 33/40/113 32/39/114 57/80/115 56/79/116 +f 28/35/117 34/41/118 58/81/119 51/74/120 +f 34/41/121 33/40/113 56/79/116 58/81/122 +f 35/42/93 20/27/95 46/71/98 44/70/94 +f 32/39/114 37/44/111 55/78/112 57/80/115 diff --git a/images/Thumbs.db b/images/Thumbs.db new file mode 100644 index 0000000..3c4bf81 Binary files /dev/null and b/images/Thumbs.db differ diff --git a/images/bird1.jpg b/images/bird1.jpg new file mode 100644 index 0000000..8647e72 Binary files /dev/null and b/images/bird1.jpg differ diff --git a/images/bird2.jpg b/images/bird2.jpg new file mode 100644 index 0000000..547574d Binary files /dev/null and b/images/bird2.jpg differ diff --git a/images/bird3.png b/images/bird3.png new file mode 100644 index 0000000..952bd00 Binary files /dev/null and b/images/bird3.png differ diff --git a/nx.jpg b/nx.jpg new file mode 100644 index 0000000..1cae307 Binary files /dev/null and b/nx.jpg differ diff --git a/ny.jpg b/ny.jpg new file mode 100644 index 0000000..fa7a975 Binary files /dev/null and b/ny.jpg differ diff --git a/nz.jpg b/nz.jpg new file mode 100644 index 0000000..1e9c54e Binary files /dev/null and b/nz.jpg differ diff --git a/package.json b/package.json index c80e8a3..c2f95a6 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "scripts": { "start": "webpack-dev-server --hot --inline", "build": "webpack", - "deploy": "rm -rf npm-debug.log && git checkout master && git commit -am 'update' && gh-pages-deploy" + "deploy": "node deploy.js" }, "gh-pages-deploy": { "prep": [ @@ -23,6 +23,7 @@ "babel-loader": "^6.2.8", "babel-preset-es2015": "^6.18.0", "gh-pages-deploy": "^0.4.2", + "simple-git": "^1.65.0", "webpack": "^1.13.3", "webpack-dev-server": "^1.16.2", "webpack-glsl-loader": "^1.0.1" diff --git a/px.jpg b/px.jpg new file mode 100644 index 0000000..0ad2eea Binary files /dev/null and b/px.jpg differ diff --git a/py.jpg b/py.jpg new file mode 100644 index 0000000..de7e5cc Binary files /dev/null and b/py.jpg differ diff --git a/pz.jpg b/pz.jpg new file mode 100644 index 0000000..836cdb4 Binary files /dev/null and b/pz.jpg differ diff --git a/src/framework.js b/src/framework.js index 9ed49e0..dfd1895 100755 --- a/src/framework.js +++ b/src/framework.js @@ -53,6 +53,35 @@ function init(callback, update) { framework.scene = scene; framework.camera = camera; framework.renderer = renderer; + framework.guiVars = {}; + framework.feathers = []; + framework.controlPoints = [ + new THREE.Vector3(0, 0, 0), + new THREE.Vector3(1, -1, 1), + new THREE.Vector3(2, 1, -1), + new THREE.Vector3(5, -1, 1) + ]; + framework.keyframes = [[new THREE.Vector3(0, 0, 0), + new THREE.Vector3(1, -1, 1), + new THREE.Vector3(2, 1, -1), + new THREE.Vector3(5, -1, 1)], + [new THREE.Vector3(2, 0, 0), + new THREE.Vector3(1, 2, 1), + new THREE.Vector3(2, 5, -1), + new THREE.Vector3(0, 10, 1)], + [new THREE.Vector3(0, 1, 0), + new THREE.Vector3(1, -1, 1), + new THREE.Vector3(2, 1, -1), + new THREE.Vector3(5, 11, 1)], + [new THREE.Vector3(0, 0, 0), + new THREE.Vector3(3, -3, 0), + new THREE.Vector3(6, -7, -1), + new THREE.Vector3(9, -10, 0)], + [new THREE.Vector3(0, -2, 0), + new THREE.Vector3(1, -2, 1), + new THREE.Vector3(-2, -7, -1), + new THREE.Vector3(-2, -11, 1)]]; + framework.bone = {}; // begin the animation loop (function tick() { @@ -70,4 +99,4 @@ function init(callback, update) { export default { init: init -} \ No newline at end of file +} diff --git a/src/main.js b/src/main.js index fd8fbd4..b729717 100755 --- a/src/main.js +++ b/src/main.js @@ -4,70 +4,406 @@ const THREE = require('three'); // older modules are imported like this. You shouldn't have to worry about this much import Framework from './framework' +const PI = Math.PI; +const EPSILON = 1e-5; // called after the scene loads function onLoad(framework) { - var scene = framework.scene; - var camera = framework.camera; - var renderer = framework.renderer; - var gui = framework.gui; - var stats = framework.stats; - - // Basic Lambert white - var lambertWhite = new THREE.MeshLambertMaterial({ color: 0xaaaaaa, side: THREE.DoubleSide }); - - // Set light - var directionalLight = new THREE.DirectionalLight( 0xffffff, 1 ); - directionalLight.color.setHSL(0.1, 1, 0.95); - directionalLight.position.set(1, 3, 2); - directionalLight.position.multiplyScalar(10); - - // set skybox - var loader = new THREE.CubeTextureLoader(); - var urlPrefix = '/images/skymap/'; - - var skymap = new THREE.CubeTextureLoader().load([ - urlPrefix + 'px.jpg', urlPrefix + 'nx.jpg', - urlPrefix + 'py.jpg', urlPrefix + 'ny.jpg', - urlPrefix + 'pz.jpg', urlPrefix + 'nz.jpg' - ] ); - - scene.background = skymap; - - // load a simple obj mesh - var objLoader = new THREE.OBJLoader(); - objLoader.load('/geo/feather.obj', function(obj) { - - // LOOK: This function runs after the obj has finished loading - var featherGeo = obj.children[0].geometry; - - var featherMesh = new THREE.Mesh(featherGeo, lambertWhite); - featherMesh.name = "feather"; - scene.add(featherMesh); + let {scene, camera, renderer, gui, stats, guiVars} = framework; + + // Set light + let directionalLight = new THREE.DirectionalLight( 0xffffff, 1 ); + directionalLight.color.setHSL(0.1, 1, 0.95); + directionalLight.position.set(1, 3, 2); + directionalLight.position.multiplyScalar(10); + + // Set ambient light + let ambientLight = new THREE.AmbientLight(0xffffff, 0.2); + + // set skybox + let loader = new THREE.CubeTextureLoader(); + let urlPrefix = ''; + + let skymap = new THREE.CubeTextureLoader().load([ + urlPrefix + 'px.jpg', urlPrefix + 'nx.jpg', + urlPrefix + 'py.jpg', urlPrefix + 'ny.jpg', + urlPrefix + 'pz.jpg', urlPrefix + 'nz.jpg' + ] ); + + scene.background = skymap; + + + // set camera position + camera.position.set(5, 3, 15); + camera.lookAt(new THREE.Vector3(5,0,0)); + + scene.add(directionalLight); + scene.add(ambientLight); + + gui.add(camera, 'fov', 0, 180).onChange((v) => { + camera.updateProjectionMatrix(); + }); + + // control all wing related attributes + let wingControl = gui.addFolder('Wing Controls'); + range(4).forEach((c) => { + let folder = wingControl.addFolder(`Bezier Offset Control ${c}`); + ['x', 'y', 'z'].forEach((axis) => { + let name = `${axis}`; + folder.add(framework.controlPoints[c], name, -50, 50).listen().onChange((v) => { + moveWingTo(framework, framework.controlPoints); + }); + folder.open(); }); + }); - // set camera position - camera.position.set(0, 1, 5); - camera.lookAt(new THREE.Vector3(0,0,0)); + // control all feather related attributes + guiVars['length'] = 0.8; + guiVars['width'] = 1.1; + guiVars['color'] = 0xffffff; + guiVars['distribution'] = 6; + guiVars['layers'] = 2; + let featherControl = gui.addFolder('Feather Controls'); + [ + featherControl.add(guiVars, 'length', 0, 3), + featherControl.add(guiVars, 'width', 0, 3), + featherControl.add(guiVars, 'distribution'), + featherControl.add(guiVars, 'layers', 0, 10).step(1), + featherControl.addColor(guiVars, 'color') + ].forEach((c) => { + c.onChange((v) => { + render(framework); + }); + }); - // scene.add(lambertCube); - scene.add(directionalLight); + // control all wind related attributes + let windControl = gui.addFolder('Wind Controls'); + let args = [ + ['windSpeed', 0.0, 10.0], + ['windDirX', -1.0, 1.0], + ['windDirY', -1.0, 1.0], + ]; + args.forEach((arg) => { + guiVars[arg[0]] = 0.1; + windControl.add(guiVars, ...arg).listen(); + guiVars[arg[0]] = 0; + }); - // edit params and listen to changes like this - // more information here: https://workshop.chromeexperiments.com/examples/gui/#1--Basic-Usage - gui.add(camera, 'fov', 0, 180).onChange(function(newVal) { - camera.updateProjectionMatrix(); + // control all wing motion related attributes + guiVars['flappingSpeed'] = 0; + let motionControl = gui.addFolder('Motion Controls'); + motionControl.add(guiVars, 'flappingSpeed', 0, 10); + let motionKeyFrameControl = motionControl.addFolder('Motion Keyframe Controls'); + range(5).forEach((kf) => { + let kfolder = motionKeyFrameControl.addFolder(`Keyframe Controls ${kf}`); + range(4).forEach((c) => { + let folder = kfolder.addFolder(`Bezier Offset Control ${c}`); + ['x', 'y', 'z'].forEach((axis) => { + let name = `${axis}`; + folder.add(framework.keyframes[kf][c], name, -50, 50).listen().onChange((v) => {}); + folder.open(); + }); }); + kfolder.open(); + }); + + + + wingControl.open(); + featherControl.open(); + windControl.open(); + motionControl.open(); + + + + render(framework); } // called on frame updates function onUpdate(framework) { - var feather = framework.scene.getObjectByName("feather"); - if (feather !== undefined) { - // Simply flap wing - var date = new Date(); - feather.rotateZ(Math.sin(date.getTime() / 100) * 2 * Math.PI / 180); + let {feathers, guiVars, keyframes} = framework; + let date = new Date(); + if (guiVars.windSpeed > EPSILON) { + feathers.forEach((f, i) => { + restoreTruth(f); + let randomOffset = Math.random() * PI / 4 * guiVars.windSpeed; + f.rotateZ((Math.sin(date.getTime() / 100 + randomOffset) - guiVars.windSpeed) * 2 * Math.PI / 180); + f.rotateX(guiVars.windDirX * PI / 36); + f.rotateY(guiVars.windDirY * PI / 36); + }) + } + if (guiVars.flappingSpeed > EPSILON) { + const STEPS = Math.floor(100 / guiVars.flappingSpeed); + if (!framework.t) { + framework.t = 0; } + // for each controlpoint, lerp wing to location + + moveWing(framework, diff(framework.controlPoints, framework.keyframes[0], STEPS)); + if (framework.t % STEPS === 0) { + framework.keyframes.push(framework.keyframes.shift(1)); + } + framework.t++; + } else { + framework.t = undefined; + } +} + +function newCTP_deprecated(framework, ctps) { + let {guiVars} = framework; + return [ + new THREE.Vector3(ctps[0][0] + guiVars.bc0x, ctps[0][1] + guiVars.bc0y, ctps[0][2] + guiVars.bc0z), + new THREE.Vector3(ctps[1][0] + guiVars.bc1x, ctps[1][1] + guiVars.bc1y, ctps[1][2] + guiVars.bc1z), + new THREE.Vector3(ctps[2][0] + guiVars.bc2x, ctps[2][1] + guiVars.bc2y, ctps[2][2] + guiVars.bc2z), + new THREE.Vector3(ctps[3][0] + guiVars.bc3x, ctps[3][1] + guiVars.bc3y, ctps[3][2] + guiVars.bc3z) + ]; +} + +function newCTP(ctps) { + return [ + new THREE.Vector3(ctps[0][0], ctps[0][1], ctps[0][2]), + new THREE.Vector3(ctps[1][0], ctps[1][1], ctps[1][2]), + new THREE.Vector3(ctps[2][0], ctps[2][1], ctps[2][2]), + new THREE.Vector3(ctps[3][0], ctps[3][1], ctps[3][2]) + ]; +} + +function moveWingTo(framework, controlPoints) { + let {scene, guiVars, feathers, bone} = framework + scene.remove(bone); + let curveObj = generateBone(framework, controlPoints); + framework.controlPoints = controlPoints; + feathers.forEach((f, i) => { + let v = curveObj.geometry.vertices[f.geoIdx]; + f.position.set(v.x, v.y, v.z); + }); +} + +function moveWing(framework, offsetPoints) { + let {scene, guiVars, feathers, controlPoints, bone} = framework + scene.remove(bone); + controlPoints.forEach((cp, i) => { + return cp.add(offsetPoints[i]); + }); + let curveObj = generateBone(framework, controlPoints); + feathers.forEach((f, i) => { + let v = curveObj.geometry.vertices[f.geoIdx]; + f.position.set(v.x, v.y, v.z); + }); +} + +function diff(ctp1, ctp2, steps) { + return ctp1.map((ctp, i) => { + let c = ctp2[i].clone(); + c.sub(ctp).divideScalar(steps) + return c; + }); +} + +/* +framework.controlPoints = [ + new THREE.Vector3(0 + guiVars.bc0X, 0 + guiVars.bc0Y, 0 + guiVars.bc0Z), + new THREE.Vector3(1 + guiVars.bc1X, -1 + guiVars.bc1Y, 1 + guiVars.bc1Z), + new THREE.Vector3(2 + guiVars.bc2X, 1 + guiVars.bc2Y, -1 + guiVars.bc2Z), + new THREE.Vector3(5 + guiVars.bc3X, -1 + guiVars.bc3Y, 1 + guiVars.bc3Z) +]; +*/ +function generateBone(framework, controlPoints) { + let {guiVars, bone} = framework + let SUB = guiVars.distribution * 10; + let curve = new THREE.CubicBezierCurve3(...controlPoints); + let geometry = new THREE.Geometry(); + geometry.vertices = curve.getPoints(SUB); + let material = new THREE.LineBasicMaterial({color: 0xffffff, linewidth: 2}); + let curveObj = new THREE.Line(geometry, material); + return curveObj; +} + +function render(framework) { + let {scene, guiVars, feathers, controlPoints, bone} = framework + let SUB = guiVars.distribution * 10; + let LAYERS = guiVars.layers; + let lower, upper, range; + let featherLength = guiVars.length; + feathers.forEach((f) => { + scene.remove(f); + }); + scene.remove(bone); + + // create a simple wing framework + let curveObj = generateBone(framework, controlPoints); + framework.bone = curveObj; + scene.add(curveObj); + // draw feathers + let objLoader = new THREE.OBJLoader(); + objLoader.load('feather.obj', function(obj) { + let lambertWhite = new THREE.MeshLambertMaterial({color: guiVars.color, side: THREE.DoubleSide}); + let featherGeo = obj.children[0].geometry; + let featherMesh = new THREE.Mesh(featherGeo, lambertWhite); + featherMesh.rotation.set(0, PI / 2, -3 * PI / 4); + featherMesh.scale.z = guiVars.width; + + // Primaries + lower = Math.floor(3 * SUB / 4); + upper = Math.floor(SUB); + range = upper - lower; + curveObj.geometry.vertices.slice(lower, upper).forEach((v, i) => { + for (let j = LAYERS; j >= 0; j--) { + if (i % (LAYERS - j + 1) !== 0) { + continue; + } + let feather = featherMesh.clone(true); + feather.name = `f_primaries${i}_${j}`; + feather.geoIdx = lower + i; + feather.layer = j; + feather.position.set(v.x, v.y, v.z); + feather.rotateY(lerp(0, -PI / 4, i / range)); + feather.scale.z += 0.2 * (LAYERS - j); + feather.position.z += (LAYERS - j) / (LAYERS * 10); + feather.scale.x = lerp(featherLength / 3, featherLength, j / LAYERS); + feather.scale.x += powerCurve(i / range, 3, 1) / 2 + 0.3; + updateTruth(feather); + scene.add(feather); + feathers.push(feather); + } + }); + + // Secondaries + lower = Math.floor(1 * SUB / 4); + upper = Math.floor(3 * SUB / 4); + range = upper - lower; + curveObj.geometry.vertices.slice(lower, upper).forEach((v, i) => { + for (let j = LAYERS; j >= 0; j--) { + if (i % (LAYERS - j + 1) !== 0) { + continue; + } + let feather = featherMesh.clone(true); + feather.name = `f_primaries${i}_${j}`; + feather.geoIdx = lower + i; + feather.layer = j; + feather.position.set(v.x, v.y, v.z); + feather.rotateY(lerp(PI / 16, 0, i / range)); + feather.scale.z += 0.5 * (LAYERS - j); + feather.position.z += (LAYERS - j) / (LAYERS * 10); + feather.scale.x = lerp(featherLength / 3, featherLength, j / LAYERS); + feather.scale.x += lerp(0.1, 0.3, i / range); + feather.scale.x += parabola(i / range, 1.5) / 6; + updateTruth(feather); + scene.add(feather); + feathers.push(feather); + } + }); + + // Tertiary + scapulars + lower = Math.floor(0); + upper = Math.floor(1 * SUB / 4); + range = upper - lower; + curveObj.geometry.vertices.slice(lower, upper).forEach((v, i) => { + for (let j = LAYERS; j >= 0; j--) { + if (i % (LAYERS - j + 1) !== 0) { + continue; + } + let feather = featherMesh.clone(true); + feather.name = `f_primaries${i}_${j}`; + feather.geoIdx = lower + i; + feather.layer = j; + feather.position.set(v.x, v.y, v.z); + feather.rotateY(lerp(PI / 8, PI / 16, i / range)); + feather.scale.z = 2 + feather.position.z += (LAYERS - j) / (LAYERS * 10); + feather.scale.x = lerp(featherLength / 3, featherLength, j / LAYERS); + feather.scale.x += cubicPulse(0, range + 5, i) / 2 + updateTruth(feather); + scene.add(feather); + feathers.push(feather); + } + }); + }); +} + +function resetMesh(mesh) { + mesh.position.set(0, 0, 0); + mesh.rotation.set(0, 0, 0); + mesh.scale.set(1, 1, 1); +} + +function clear(framework) { + let {scene} = framework; + scene.children.forEach((o) => { + scene.remove(o); + }); +} + +function restoreTruth(obj) { + obj.position.set(obj.truth.position.x, obj.truth.position.y, obj.truth.position.z); + obj.scale.set(obj.truth.scale.x, obj.truth.scale.y, obj.truth.scale.z); + obj.rotation.set(obj.truth.rotation.x, obj.truth.rotation.y, obj.truth.rotation.z); +} + +function updateTruth(obj) { + obj.truth = { + position: { + x: obj.position.x, + y: obj.position.y, + z: obj.position.z, + }, + scale: { + x: obj.scale.x, + y: obj.scale.y, + z: obj.scale.z, + }, + rotation: { + x: obj.rotation.x, + y: obj.rotation.y, + z: obj.rotation.z, + }, + } +} + +function lerp(a, b, t) { + return a * (1 - t) + b * t; } +function cubicPulse(c, w, x) { + x = Math.abs(x - c); + if (Math.abs(x) > w) { + return 0; + } + x /= w; + return 1 - x * x * (3 - 2 * x); +} + +function parabola(x, k) { + return Math.pow(4 * x * (1 - x), k); +} + +function powerCurve(x, a, b) { + let k = Math.pow(a + b, a + b) / (Math.pow(a, a) * Math.pow(b, b)); + return k * Math.pow(x, a) * Math.pow(1 - x, b); +} + + +// http://stackoverflow.com/questions/8273047/javascript-function-similar-to-python-range +function range(start, stop, step) { + if (typeof stop == 'undefined') { + stop = start; + start = 0; + } + + if (typeof step == 'undefined') { + step = 1; + } + + if ((step > 0 && start >= stop) || (step < 0 && start <= stop)) { + return []; + } + + var result = []; + for (var i = start; step > 0 ? i < stop : i > stop; i += step) { + result.push(i); + } + + return result; +}; + // when the scene is done initializing, it will call onLoad, then on frame updates, call onUpdate -Framework.init(onLoad, onUpdate); \ No newline at end of file +Framework.init(onLoad, onUpdate);