diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000..23f8f78 Binary files /dev/null and b/.DS_Store differ diff --git a/README.md b/README.md index 1410c30..107856f 100644 --- a/README.md +++ b/README.md @@ -1,37 +1,27 @@ # [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. +See the project [here](https://sknop8.github.io/Project2-Toolbox-Functions/)! -##### 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 +## Overview -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. +The objective of this project is to procedurally model and animate a bird wing. -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. +### Built using interpolation functions, bias, and gain +* Curve of the wing (the base structure for the feathers) +* Feather spread (how feathers are spread out horizontally along the wing) +* Feather rotation (feathers change orientation depending on their position along the wing) +* Feather size (feathers change size depending on their position along the wing) +* Feather color (feathers get lighter nearer to the tip) -## Interactivity +### Built using noise +* Wind jitter (feathers jitter slightly based on pseudo-random noise) -Using Dat.GUI and the examples provided in the reference code, allow the user to adjust the following controls: +### Libraries used +* THREE.js +* Dat.GUI +### Interactivity +I've made the following attributes interactive for the user! 1. The curvature of the wing's basic shape 2. Feather distribution 3. Feather size @@ -39,44 +29,3 @@ Using Dat.GUI and the examples provided in the reference code, allow the user to 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 diff --git a/references/b8be2ee1b7717a2d8b87eea63b89f088.jpg b/references/b8be2ee1b7717a2d8b87eea63b89f088.jpg new file mode 100644 index 0000000..a61d2c1 Binary files /dev/null and b/references/b8be2ee1b7717a2d8b87eea63b89f088.jpg differ diff --git a/references/high_aspect_ratio_wing.jpg b/references/high_aspect_ratio_wing.jpg new file mode 100644 index 0000000..547574d Binary files /dev/null and b/references/high_aspect_ratio_wing.jpg differ diff --git a/src/main.js b/src/main.js index fd8fbd4..1a9e5f3 100755 --- a/src/main.js +++ b/src/main.js @@ -3,6 +3,18 @@ const THREE = require('three'); // older modules are imported like this. You shouldn't have to worry about this much import Framework from './framework' +import Wing from './wing' + +var feathers = []; +var wingParams = { + 'Curvature': 0.2, + 'Feather distr': 1, + 'Feather size': 0.8, + 'Feather orient': 0.8, + 'Feather color': 0, + 'Flapping speed': 1, + 'Flapping motion': 2, +}; // called after the scene loads function onLoad(framework) { @@ -23,7 +35,7 @@ function onLoad(framework) { // set skybox var loader = new THREE.CubeTextureLoader(); - var urlPrefix = '/images/skymap/'; + var urlPrefix = 'images/skymap/'; var skymap = new THREE.CubeTextureLoader().load([ urlPrefix + 'px.jpg', urlPrefix + 'nx.jpg', @@ -35,21 +47,43 @@ function onLoad(framework) { // load a simple obj mesh var objLoader = new THREE.OBJLoader(); - objLoader.load('/geo/feather.obj', function(obj) { + 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); + // var featherMesh = new THREE.Mesh(featherGeo, lambertWhite); + // featherMesh.name = "feather"; + // scene.add(featherMesh); + + for(var i = 1; i < 50; i++) { + var material = lambertWhite.clone(); + var fMesh = new THREE.Mesh(featherGeo, material); + + var posZ = Wing.wingCurve(i / 50, wingParams['Curvature']); + var posX = Wing.featherSpread(i / 50); + fMesh.position.setX(i / 10);//posX); + fMesh.position.setZ((posZ - 0.5) * 10); + feathers.push(fMesh); + + var rot = Wing.featherRotation(i / 50, wingParams['Feather orient']); + fMesh.rotation.y = Math.PI / 3; + fMesh.rotation.y -= rot * 1.8; + + var scale = 2 - Wing.featherSize(i / 50, wingParams['Feather size']); + fMesh.scale.set(scale, scale, scale); + + var color = Wing.featherColor(i / 50, wingParams['Feather color']); + fMesh.material.color = new THREE.Color(color[0], color[1], color[2]); + + scene.add(fMesh); + } }); // set camera position - camera.position.set(0, 1, 5); - camera.lookAt(new THREE.Vector3(0,0,0)); + camera.position.set(5, 10, -5); + camera.lookAt(new THREE.Vector3(0,-10,0)); - // scene.add(lambertCube); scene.add(directionalLight); // edit params and listen to changes like this @@ -57,15 +91,62 @@ function onLoad(framework) { gui.add(camera, 'fov', 0, 180).onChange(function(newVal) { camera.updateProjectionMatrix(); }); + + gui.add(wingParams, 'Curvature', 0, 1).onChange(function (value) { + for (var i = 0; i < feathers.length; i++) { + var posZ = Wing.wingCurve(i / 50, value); + feathers[i].position.setZ((posZ - 0.5) * 10); + } + }); + + gui.add(wingParams, 'Feather distr', 0, 1).onChange(function(value) { + for (var i = 0; i < feathers.length; i++) { + feathers[i].position.setX(i / (10 * value)); + } + }); + + gui.add(wingParams, 'Feather size', 0, 1).onChange(function(value) { + for (var i = 0; i < feathers.length; i++) { + var scale = 2 - Wing.featherSize(i / 50, 1-value); + feathers[i].scale.set(scale, scale, scale); + } + }); + + gui.add(wingParams, 'Feather orient', 0, 1).onChange(function(value) { + for (var i = 0; i < feathers.length; i++) { + var rot = Wing.featherRotation(i / 50, value); + feathers[i].rotation.y = Math.PI / 3;//(30 * rot) ; + feathers[i].rotation.y -= rot * 1.8; + } + }); + gui.add(wingParams, 'Feather color', -50, 50).onChange(function(value) { + for (var i = 0; i < feathers.length; i++) { + var color = Wing.featherColor(i / 50, value); + feathers[i].material.color = new THREE.Color(color[0], color[1], color[2]); + } + }); + + gui.add(wingParams,'Flapping speed', 0, 2); + gui.add(wingParams, 'Flapping motion', 0, 10); } + + // 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); + var feather = framework.scene.getObjectByName("feather"); + for (var i = 0; i < feathers.length; i++) { + feather = feathers[i]; + + if (feather !== undefined) { + // Simply flap wing + var date = new Date(); + + var jitter = Wing.windJitter(feather.position.x + date.getMilliseconds(), feather.position.z + date.getMilliseconds()); + feather.rotateZ(Math.sin(date.getTime() /100 * wingParams['Flapping speed']+ jitter/10 ) * wingParams['Flapping motion'] * Math.PI / 180); + + + } } } diff --git a/src/wing.js b/src/wing.js new file mode 100644 index 0000000..b9b4eb4 --- /dev/null +++ b/src/wing.js @@ -0,0 +1,96 @@ +function clamp(num, min, max) { + return Math.min(Math.max(num, min), max); +} + +function smoothstep(a, b, t) { + t = clamp((t - a) / (b - a), 0, 1); + return t * t * (3 - 2 * t); +} + +/** + * Bias b [0,1] + * Higher b => more time spent at beginning of transition + * Lower b => more time spent at end of transition + */ +function bias(b, t) { + return Math.pow(t, Math.log(b) / Math.log(0.5)); +} + +/** + * Gain g [0, 1] + * Higher g => more time on the edges + * Lower g => more time in the middle + */ +function gain(g, t) { + if (t < 0.5) { + return bias(1 - g, 2 * t) / 2; + } else { + return 1 - bias(1 - g, 2 - 2 * t) / 2; + } +} + + +function wingCurve(pos, curve) { + var result = smoothstep(0, 1 + curve, pos); + result = gain(curve, result); + return result; +} + +function featherSpread(pos) { + var result = smoothstep(0, 1, pos); + result = bias(0.2, result); + return result; +} + +function featherRotation(pos, orient) { + var result = smoothstep(0, 1, pos); + result = bias(0.2, result); + result = gain(orient, result); + + return result; +} + +function featherSize(pos, size) { + var result = smoothstep(0, 1, pos); + result = bias(size, result); + result = gain(0.2, result); + + return result; +} + +function featherColor(pos, color) { + var offset = smoothstep(0, 1, pos) + 0.0001; + offset = bias(0.8, offset); + offset = gain(0.1, offset); + offset *= 150; + + var color = [120 + color + offset, 110 + 0.2 * Math.abs(color) + offset, 140 - color + offset]; + color[0] /= 255; + color[1] /= 255; + color[2] /= 255; + + return color; +} + +/** + * Noise generator modified from the one in HW1 + */ +function noise_gen1(pos) { + return (Math.sin(pos + 12.9898) * 43758.5453) % 1; +} + +function windJitter(pos1, pos2) { + var include = noise_gen1(pos1) > 0.7; + var jitter = include * (noise_gen1(pos2) - 1); + + return jitter; +} + +export default { + wingCurve: wingCurve, + featherSpread: featherSpread, + featherRotation: featherRotation, + featherSize: featherSize, + featherColor: featherColor, + windJitter: windJitter, +} \ No newline at end of file