diff --git a/README.md b/README.md index 1410c30..cb2a5dc 100644 --- a/README.md +++ b/README.md @@ -1,82 +1,33 @@ # [Project2: Toolbox Functions](https://github.com/CIS700-Procedural-Graphics/Project2-Toolbox-Functions) -## Overview +## Wing Structure +Consists of three joints (shoulder, elbow, wrist). The shoulder joint is stationary within the body but the wrist and elbow are located at a fixed distance (in the x and z direction) from the body. Feathers are placed on a spline with control points at each joint or simply linearly interpolated between pairs of joints (use toggleSpline). Each region of feathers is created by offsetting from this interpolated line. -The objective of this assignment is to procedurally model and animate a bird wing. Let's get creative! +## Wing Animation +My goal was to emulate a motion like this: -Start by forking and then cloning [this repository](https://github.com/CIS700-Procedural-Graphics/Project2-Toolbox-Functions) +![Flapping Motion](https://i.imgur.com/F0Ms1qz.png) -## Modeling +Given that the shoulder joint (green) is stationary, the relative y position of the elbow (red) and wrist (blue) looks something like this: -##### Reference images +![Joint Displacements](https://i.imgur.com/vb2zfz0.png) -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. +This pair of functions has a couple of key characteristics: -##### Make wing curve +The falling edge on each oscillation should take longer than the rising edge. This makes the wing motion more realistic as the upstroke is about twice as fast as the downstroke[\[1\]](http://www.brendanbody.co.uk/flight_tutorial/). This asymmetric sine wave is achieved using some variation of this function: sin(x + a * sin(x))[\[2\]](https://www.quora.com/How-can-I-draw-this-irregular-Sine-function-in-MATLAB-Should-I-add-multiple-the-Sine-function-to-another-term). -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. +The elbow displacement function must reach its high and low peaks slightly before the wrist does. It's important that this is a translation of the function or else the periods will go out of sync. -##### Distribute feathers +Lastly, the amplitude of the elbow displacement is less than that of the wrist. -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. +## Demo +Demo: https://iambrian.github.io/Project2-Toolbox-Functions/ -Feel free to diversify your wings by using multiple base feather models. +![Demo](https://i.imgur.com/VuYbEaP.gif) -## Animation +## References +Bird wing motion: http://www.brendanbody.co.uk/flight_tutorial/ -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. +Baseline sine function: https://www.quora.com/How-can-I-draw-this-irregular-Sine-function-in-MATLAB-Should-I-add-multiple-the-Sine-function-to-another-term -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 +Graphing: http://www.iquilezles.org/apps/graphtoy/ diff --git a/package.json b/package.json index c80e8a3..cde25c8 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": "gh-pages-deploy" }, "gh-pages-deploy": { "prep": [ diff --git a/src/main.js b/src/main.js index fd8fbd4..94e0d87 100755 --- a/src/main.js +++ b/src/main.js @@ -1,9 +1,142 @@ - // Skybox texture from: https://github.com/mrdoob/three.js/tree/master/examples/textures/cube/skybox - const THREE = require('three'); // older modules are imported like this. You shouldn't have to worry about this much import Framework from './framework' + +// 14 step motion: http://www.brendanbody.co.uk/flight_tutorial/ +var state = 0; +var timeDelta = 100; // ms +var startTime; + +var settings = { + speed: 0.003, + primaryColor: [91, 78, 60], + secondaryColor: [76, 64, 47], + tertiaryColor: [45, 37, 26], + elbowX: 3, + elbowZ: 5, + wristX: 7, + wristZ: 5, + numLayers: 1, + maxDensity: 40, + density: 40, + windSpeed: 0.3, + windDirection: 0.0, + showCtrlPts: false, + toggleSpline: true +} + +var featherConfig = { + getName: function(segment, region, side, i, l) { + return "feather(" + segment + "," + region + "," + side + "," + i + "," + l + ")"; + }, + marginalCoverts: { + segment: "se", + region: "marginalCoverts", + yaw: function(i) { + return Math.PI / 2; + }, + scale: function(i) { + return [0.5 + (Math.random() - 0.5) / 3, 1, 1]; + }, + color: 1, + yindex: 0, + zindex: 0 + }, + secondaryCoverts: { + segment: "se", + region: "secondaryCoverts", + yaw: function(i) { + return Math.PI / 2; + }, + scale: function(i) { + return [1 + (Math.random() - 0.5) / 2, 1, 1]; + }, + color: 2, + yindex: -0.05, + zindex: -0.5 + }, + secondaries: { + segment: "se", + region: "secondaries", + yaw: function(i) { + return Math.PI / 2; + }, + scale: function(i) { + return [1.5 + (Math.random() - 0.5) / 2, 1, 1]; + }, + color: 3, + yindex: -0.1, + zindex: -1 + }, + alula: { + segment: "ew", + region: "alula", + yaw: function(i) { + return Math.PI / 2; + }, + scale: function(i) { + return [(10 - i) / 20 + (Math.random() - 0.5) / 3, 1, 1]; + }, + color: 1, + yindex: 0, + zindex: 0 + }, + primaryCoverts: { + segment: "ew", + region: "primaryCoverts", + yaw: function(i, side) { + if (side == "left") { + var coeff = Math.PI / 2; + var offset = 0; + } else { + var coeff = -Math.PI / 2; + var offset = Math.PI; + } + return coeff / (1 + Math.exp(i - 7)) + offset; + }, + scale: function(i) { + return [1 + (Math.random() - 0.5) / 2, 1, 1]; + }, + color: 2, + yindex: -0.05, + zindex: -0.5 + }, + primaries: { + segment: "ew", + region: "primaries", + yaw: function(i, side) { + if (side == "left") { + var coeff = Math.PI / 2; + var offset = 0; + } else { + var coeff = -Math.PI / 2; + var offset = Math.PI; + } + return coeff / (1 + Math.exp(i - 7)) + offset; + }, + scale: function(i) { + return [1.5 + (Math.random() - 0.5) / 2, 1, 1]; + }, + color: 3, + yindex: -0.1, + zindex: -1 + } +}; + +var sections = [featherConfig.marginalCoverts, + featherConfig.secondaryCoverts, + featherConfig.secondaries, + featherConfig.alula, + featherConfig.primaryCoverts, + featherConfig.primaries +]; + +function clamp(num, min, max) { + return num <= min ? min : num >= max ? max : num; +} + + // called after the scene loads function onLoad(framework) { var scene = framework.scene; @@ -12,42 +145,147 @@ function onLoad(framework) { var gui = framework.gui; var stats = framework.stats; - // Basic Lambert white - var lambertWhite = new THREE.MeshLambertMaterial({ color: 0xaaaaaa, side: THREE.DoubleSide }); + // Basic Lambert + var lambertWhite = new THREE.MeshLambertMaterial({ + color: 0xffffff, + side: THREE.DoubleSide + }); // Set light - var directionalLight = new THREE.DirectionalLight( 0xffffff, 1 ); + 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 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 + objLoader.load('geo/feather.obj', function(obj) { var featherGeo = obj.children[0].geometry; - var featherMesh = new THREE.Mesh(featherGeo, lambertWhite); - featherMesh.name = "feather"; - scene.add(featherMesh); + ["left", "right"].forEach(function(value, index, array) { + var side = value; + for (var s = 0; s < sections.length; s++) { + var config = sections[s]; + for (var l = 0; l < settings.numLayers; l++) { + for (var i = 0; i < settings.density; i++) { + var fi = 10 * i / settings.density; + var featherMesh = new THREE.Mesh(featherGeo, lambertWhite); + featherMesh.name = featherConfig.getName(config.segment, config.region, side, i, l); + featherMesh.rotation.y = config.yaw(fi, side); + var scale = config.scale(fi); + featherMesh.scale.set(scale[0], scale[1], scale[2]); + scene.add(featherMesh); + } + } + } + }); + + // tail + for (var i = 5; i < 15; i++) { + var featherMesh = new THREE.Mesh(featherGeo, lambertWhite); + var fi = 10 * i / 20; + featherMesh.name = featherConfig.getName("tail", "tail", "center", i, 0); + featherMesh.rotation.y = 3 * fi / 10; + featherMesh.position.set(0, -0.25, 0); + featherMesh.scale.set(1, 1, 1); + scene.add(featherMesh); + } + }); + + var geometry = new THREE.BoxGeometry(0.5, 0.5, 0.5); + var material = new THREE.MeshBasicMaterial({ + color: 0xCB2400 + }); + var shoulder = new THREE.Mesh(geometry, material); + shoulder.position.set(0, 0, 5); + shoulder.name = "shoulder"; + shoulder.visible = settings.showCtrlPts; + scene.add(shoulder); + + var geometry = new THREE.BoxGeometry(0.5, 0.5, 0.5); + var material = new THREE.MeshBasicMaterial({ + color: 0xC67563 + }); + var elbow = new THREE.Mesh(geometry, material); + elbow.position.set(3, -3, 5); + elbow.name = "elbow"; + elbow.visible = settings.showCtrlPts; + scene.add(elbow); + + + var geometry = new THREE.BoxGeometry(0.5, 0.5, 0.5); + var material = new THREE.MeshBasicMaterial({ + color: 0xCAA8A1 + }); + var wrist = new THREE.Mesh(geometry, material); + wrist.position.set(7, -7, 5); + wrist.name = "wrist"; + wrist.visible = settings.showCtrlPts; + scene.add(wrist); + + // spline + var curve = new THREE.SplineCurve3( [ + shoulder.position, + elbow.position, + wrist.position + ] ); + var path = new THREE.Path( curve.getPoints( 50 ) ); + var geometry = path.createPointsGeometry( 50 ); + var material = new THREE.LineBasicMaterial( { color : 0xff0000 } ); + + // Create the final object to add to the scene + var splineObject = new THREE.Line( geometry, material ); + splineObject.name = "spline"; + splineObject.visible = settings.showCtrlPts; + scene.add(splineObject); + + // beak + var geometry = new THREE.CylinderGeometry(0.01, 0.5, 2, 5); + var material = new THREE.MeshBasicMaterial({ + color: 0xffff00 }); + var cylinder = new THREE.Mesh(geometry, material); + cylinder.position.set(0, 0, 6); + cylinder.rotation.x = 2; + scene.add(cylinder); + + // head + var geometry = new THREE.CylinderGeometry(0.7, 0.8, 1, 5); + var material = new THREE.MeshBasicMaterial({ + color: 0xf7f4ef + }); + var cylinder = new THREE.Mesh(geometry, material); + cylinder.position.set(0, 0.5, 5.5); + cylinder.rotation.x = 1.8; + scene.add(cylinder); + + // body + var geometry = new THREE.CylinderGeometry(1.2, 0.8, 6, 5); + var material = new THREE.MeshBasicMaterial({ + color: 0x2d251a + }); + var cylinder = new THREE.Mesh(geometry, material); + cylinder.position.set(0, -0.5, 2); + cylinder.rotation.x = 1.4; + cylinder.name = "body"; + scene.add(cylinder); + // set camera position - camera.position.set(0, 1, 5); - camera.lookAt(new THREE.Vector3(0,0,0)); + camera.position.set(0, 10, 10); + camera.lookAt(new THREE.Vector3(0, 0, 0)); // scene.add(lambertCube); scene.add(directionalLight); @@ -57,17 +295,220 @@ function onLoad(framework) { gui.add(camera, 'fov', 0, 180).onChange(function(newVal) { camera.updateProjectionMatrix(); }); + + gui.add(settings, 'speed', 0.0, 0.01); + + gui.addColor(settings, 'primaryColor'); + gui.addColor(settings, 'secondaryColor'); + gui.addColor(settings, 'tertiaryColor'); + + gui.add(settings, 'elbowX', 0, 10).onChange(function(newVal) { + var elbow = framework.scene.getObjectByName("elbow"); + if (elbow !== undefined) { + elbow.position.set(newVal, elbow.position.y, elbow.position.z); + } + }); + + gui.add(settings, 'elbowZ', 0, 10).onChange(function(newVal) { + var elbow = framework.scene.getObjectByName("elbow"); + if (elbow !== undefined) { + elbow.position.set(elbow.position.x, elbow.position.y, newVal); + } + }); + + gui.add(settings, 'wristX', 0, 10).onChange(function(newVal) { + var wrist = framework.scene.getObjectByName("wrist"); + if (wrist !== undefined) { + wrist.position.set(newVal, wrist.position.y, wrist.position.z); + } + }); + + gui.add(settings, 'wristZ', 0, 10).onChange(function(newVal) { + var wrist = framework.scene.getObjectByName("wrist"); + if (wrist !== undefined) { + elbow.position.set(wrist.position.x, wrist.position.y, newVal); + } + }); + + gui.add(settings, 'density', 0, settings.maxDensity); + + gui.add(settings, 'showCtrlPts').onChange(function(newVal) { + var shoulder = framework.scene.getObjectByName("shoulder"); + if (shoulder !== undefined) { + shoulder.visible = newVal; + } + + var elbow = framework.scene.getObjectByName("elbow"); + if (elbow !== undefined) { + elbow.visible = newVal; + } + + var wrist = framework.scene.getObjectByName("wrist"); + if (wrist !== undefined) { + wrist.visible = newVal; + } + + var spline = framework.scene.getObjectByName("spline"); + if (spline !== undefined) { + spline.visible = newVal; + } + + }); + + gui.add(settings, 'windSpeed', 0.0, 1.0); + gui.add(settings, 'windDirection', -1.0, 1.0); + + gui.add(settings, 'toggleSpline'); + + startTime = (new Date).getTime(); } // 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 primaryColor = new THREE.Color(settings.primaryColor[0] / 255, settings.primaryColor[1] / 255, settings.primaryColor[2] / 255); + var secondaryColor = new THREE.Color(settings.secondaryColor[0] / 255, settings.secondaryColor[1] / 255, settings.secondaryColor[2] / 255); + var tertiaryColor = new THREE.Color(settings.tertiaryColor[0] / 255, settings.tertiaryColor[1] / 255, settings.tertiaryColor[2] / 255); + var lambertPrimary = new THREE.MeshLambertMaterial({ + color: primaryColor, + side: THREE.DoubleSide + }); + var lambertSecondary = new THREE.MeshLambertMaterial({ + color: secondaryColor, + side: THREE.DoubleSide + }); + var lambertTertiary = new THREE.MeshLambertMaterial({ + color: tertiaryColor, + side: THREE.DoubleSide + }); + + var v = new THREE.Vector3(0, 0, 0); + + // increment the state every timeDelta milliseconds + // and set the lerp alpha to equal the percentage of time elapsed between state changes + var elapsed = ((new Date).getTime() - startTime); + var alpha = elapsed / timeDelta; // amount to lerp + if (elapsed > timeDelta) { + alpha = 0; + startTime = (new Date).getTime(); + state = (state + 1) % 14; + } else { + return; + } + + var curTime = (new Date).getTime() * settings.speed; + var freq = settings.speed; + + // joints + var shoulder = framework.scene.getObjectByName("shoulder"); + var elbow = framework.scene.getObjectByName("elbow"); + if (elbow !== undefined) { + var newY = 3 * Math.sin(curTime + 1 + 0.5 * Math.sin(curTime + 1)); + var delta = 0.05 * (newY - elbow.position.y); + elbow.position.set(elbow.position.x, newY, elbow.position.z); + var body = framework.scene.getObjectByName("body"); + if (body !== undefined) { + body.position.set(body.position.x, body.position.y - delta, body.position.z); + } + + // tail + for (var i = 5; i < 15; i++) { + var fi = 10 * i / 20; + var name = featherConfig.getName("tail", "tail", "center", i, 0); + var feather = framework.scene.getObjectByName(name); + if (feather !== undefined) { + feather.rotation.x = settings.windSpeed * 0.1 * Math.random(); + feather.position.set(feather.position.x, feather.position.y - delta, feather.position.z); + } + } + } + + var wrist = framework.scene.getObjectByName("wrist"); + if (wrist !== undefined) { + wrist.position.set(wrist.position.x, 6 * Math.sin(curTime + 0.5 * Math.sin(curTime)), wrist.position.z); } + + // spline + + // Create the final object to add to the scene + var splineObject = framework.scene.getObjectByName("spline"); + if (splineObject !== undefined) { + var curve = new THREE.SplineCurve3( [ + shoulder.position, + elbow.position, + wrist.position + ] ); + var path = new THREE.Path( curve.getPoints( 50 ) ); + var geometry = path.createPointsGeometry( 50 ); + splineObject.geometry = geometry; + splineObject.position.set(shoulder.position.x, shoulder.position.y, shoulder.position.z); + } + + + // feathers + ["left", "right"].forEach(function(value, index, array) { + var side = value; + for (var s = 0; s < sections.length; s++) { + var config = sections[s]; + for (var l = 0; l < settings.numLayers; l++) { + for (var i = 0; i < settings.maxDensity; i++) { + var fi = 10 * i / settings.density; + var name = featherConfig.getName(config.segment, config.region, side, i, l); + var feather = framework.scene.getObjectByName(name); + if (feather !== undefined) { + + if (i < settings.density) { + + if (config.segment == "se") { + var start = shoulder; + var end = elbow; + var t = fi / 10 * 0.5; + } else if (config.segment == "ew") { + var start = elbow; + var end = wrist; + var t = fi / 10 * 0.5 + 0.5; + } + + if (side == "right") { + var coeff = -1; + } else { + var coeff = 1; + } + + if (settings.toggleSpline) { + var newPos = curve.getPoint(t); + } else { + var newPos = v.lerpVectors(start.position, end.position, (fi + 1) / 10); + } + var direction = newPos.y - feather.position.y; + feather.position.set(coeff * newPos.x, newPos.y + config.yindex, newPos.z + config.zindex); + feather.rotation.z = clamp(-direction, -1, 1); + feather.rotation.x = settings.windSpeed * Math.random(); + feather.rotation.y = config.yaw(fi, side) - settings.windDirection; + feather.visible = true; + + switch (config.color) { + case 1: + feather.material = lambertPrimary; + break; + case 2: + feather.material = lambertSecondary; + break; + case 3: + feather.material = lambertTertiary; + break; + } + } else { + feather.visible = false; + } + } else { + console.log("undefined feather: " + name); + } + } + } + } + }); } // 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);