diff --git a/README.md b/README.md index 1410c30..9467568 100644 --- a/README.md +++ b/README.md @@ -2,81 +2,13 @@ ## Overview -The objective of this assignment is to procedurally model and animate a bird wing. Let's get creative! +This is an animation of a bird wing. In a bird wing, feathers closer to the bird's body tend to be more clustered whereas feathers further away tend to be more spread out. Additionally, feathers at the top of its arm are typically smaller than the feathers lower down on its wing. -Start by forking and then cloning [this repository](https://github.com/CIS700-Procedural-Graphics/Project2-Toolbox-Functions) +The wing is customizable. You should change the feather's colors and size. You can also change its flapping speeds and how big it's wing motion is. -## 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 +Other features: +Wind +Flapping +Gradient Color +Scaling of feathers along the wing +Beginning curves made with Bezier Curves \ No newline at end of file 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..c74ccee 100755 --- a/src/main.js +++ b/src/main.js @@ -4,6 +4,62 @@ const THREE = require('three'); // older modules are imported like this. You shouldn't have to worry about this much import Framework from './framework' +// Array of feathers on their corresponding curves; +var featherGeo; +var loaded = false; + +var curve = new THREE.CubicBezierCurve3( + new THREE.Vector3( 0, -5, 0.4 ), + new THREE.Vector3( 25, 5, 10.4 ), + new THREE.Vector3( 40, -5, 5.4 ), + new THREE.Vector3( 60, -5, 15 ) + ); +var curve2 = new THREE.CubicBezierCurve3( + new THREE.Vector3( 0, -5, 0.2 ), + new THREE.Vector3( 25, 5, 10.2 ), + new THREE.Vector3( 50, -20, 0.2 ), + new THREE.Vector3( 90, 10, 27 ) + ); +var curve3 = new THREE.CubicBezierCurve3( + new THREE.Vector3( 0, -5, 0 ), + new THREE.Vector3( 30, 10, 10 ), + new THREE.Vector3( 50, -30, 0 ), + new THREE.Vector3( 90, 10, 27 ) + ); +var points; +var points2; +var points3; +var lambertWhite; + +// Stores the feather meshes and the rotation data +var f1 = []; +var f2 = []; +var f3 = []; +var f1_rad = []; +var f2_rad = []; +var f3_rad = []; + +// Threshold of the Wind +var threshold = Math.PI * 0.05; + +// Settings, Parameters +var curveParams = { + +} + +var featherParams = { + distribution: 1.0, + size: 5.0, + color: [ 0.0, 128.0, 255.0 ], + orientation: 50, + points: 1500 +}; + +var flappingParams = { + speed: 1.0, + motion: 1.0 +} + // called after the scene loads function onLoad(framework) { var scene = framework.scene; @@ -13,7 +69,7 @@ function onLoad(framework) { var stats = framework.stats; // Basic Lambert white - var lambertWhite = new THREE.MeshLambertMaterial({ color: 0xaaaaaa, side: THREE.DoubleSide }); + lambertWhite = new THREE.MeshLambertMaterial({ color: 0xaaaaaa, side: THREE.DoubleSide }); // Set light var directionalLight = new THREE.DirectionalLight( 0xffffff, 1 ); @@ -23,7 +79,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', @@ -33,20 +89,32 @@ function onLoad(framework) { scene.background = skymap; + // First Curve + var geometry = new THREE.Geometry(); + points = curve.getPoints (featherParams.points); + geometry.vertices = points; + + // Second Curve + var geometry2 = new THREE.Geometry(); + points2 = curve2.getPoints (featherParams.points); + geometry2.vertices = points2; + + // Third CURVE + var geometry3 = new THREE.Geometry(); + points3 = curve3.getPoints (featherParams.points); + geometry3.vertices = points3; + + // TODO: CLEAN UP THIS MESSY REPEATING CODE, WHO ARE YOU ELLEN. // 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); + featherGeo = obj.children[0].geometry; + createWing(featherGeo, scene); }); // set camera position - camera.position.set(0, 1, 5); + camera.position.set(0, 1, 50); camera.lookAt(new THREE.Vector3(0,0,0)); // scene.add(lambertCube); @@ -57,15 +125,227 @@ function onLoad(framework) { gui.add(camera, 'fov', 0, 180).onChange(function(newVal) { camera.updateProjectionMatrix(); }); + + // Curve Controls + // Feather Controls + var f1 = gui.addFolder('Feather'); + // f1.add(featherParams, 'distribution', 0, 10).onChange(function(newVal) { + // updateWing(); + // }); + f1.add(featherParams, 'size', 0, 10).onChange(function(newVal) { + updateWing(); + }); + f1.addColor(featherParams, 'color').onChange(function(newVal) { + updateWing(); + }); + + // Flapping Controls + var f2 = gui.addFolder('Flapping'); + f2.add(flappingParams, 'speed', 0.0, 5.0).onChange(function(newVal) { + updateWing(); + }); + f2.add(flappingParams, 'motion', 0.0, 5.0).onChange(function(newVal) { + updateWing(); + }); +} + +function createWing(featherGeo, scene) { + var featherMesh = new THREE.Mesh(featherGeo, lambertWhite); + + var axis = new THREE.Vector3(); + var up = new THREE.Vector3( 0, 1, 0 ); + + var threshold = 4.0; + var add = 0.0; + for (var i = 0.0; i < featherParams.points; i++) { + if (i > threshold) { + threshold *= 1.5; + add++; + } + + // FIRST + var featherInstance = new THREE.Mesh(featherGeo, lambertWhite.clone()); + featherInstance.position.set(points[i].x, points[i].y, points[i].z); + var s = featherParams.size * i / featherParams.points / 2 + 2.0; + featherInstance.scale.set(s, s, s); + + var tangent = curve.getTangent(i / featherParams.points).normalize(); + axis.crossVectors(up, tangent).normalize(); + var rad = Math.acos(up.dot(tangent)); + + featherInstance.quaternion.setFromAxisAngle( axis, rad ); + featherInstance.rotateX(Math.PI / 2.0); + f1.push(featherInstance); + f1_rad.push(featherInstance.rotation); + scene.add(featherInstance); + + + // SECOND + var featherInstance2 = new THREE.Mesh(featherGeo, lambertWhite.clone()); + featherInstance2.position.set(points2[i].x, points2[i].y, points2[i].z); + var s2 = featherParams.size * 2 * i / featherParams.points + 1.0; + featherInstance2.scale.set(s2, s2, s2); + + var tangent2 = curve2.getTangent(i / featherParams.points).normalize(); + axis.crossVectors(up, tangent2).normalize(); + var rad2 = Math.acos(up.dot(tangent2)); + + featherInstance2.quaternion.setFromAxisAngle( axis, rad2 ); + featherInstance2.rotateX(Math.PI / 2.0); + f2.push(featherInstance2); + f2_rad.push(featherInstance2.rotation); + scene.add(featherInstance2); + + + // THIRD + var featherInstance3 = new THREE.Mesh(featherGeo, lambertWhite.clone()); + featherInstance3.position.set(points3[i].x, points3[i].y, points3[i].z); + var s3 = featherParams.size * 2 * i / featherParams.points + 7.0; + featherInstance3.scale.set(s3, s3, s3); + + var tangent3 = curve3.getTangent(i / featherParams.points).normalize(); + axis.crossVectors(up, tangent3).normalize(); + var rad3 = Math.acos(up.dot(tangent3)); + + featherInstance3.quaternion.setFromAxisAngle( axis, rad3 ); + featherInstance3.rotateX(Math.PI / 2.0); + f3.push(featherInstance3); + f3_rad.push(featherInstance3.rotation); + scene.add(featherInstance3); + + i += add; + } + + updateWing(); +} + +function updateWing() { + // Color + var r = featherParams.color[0] / 255.0; + var g = featherParams.color[1] / 255.0; + var b = featherParams.color[2] / 255.0; + + for (var i = 0; i < f1.length; i++) { + // Color + f1[i].material.color = new THREE.Color(r * 1.5, g * 1.5, b + i / f1.length); + + // Scale + var s = featherParams.size * i / f1.length / 2 + 2.0; + f1[i].scale.set(s, s, s); + } + + for (var j = 0; j < f2.length; j++) { + // Color + f2[j].material.color = new THREE.Color(r + j / f2.length, g + j / f2.length, b + j / f2.length); + + // Scale + var s2 = featherParams.size * 2 * j / f2.length + 3.0; + f2[j].scale.set(s2, s2, s2); + } + + for (var k = 0; k < f3.length; k++) { + // Color + f3[k].material.color = new THREE.Color(r, g + k / f3.length, b); + + // + var s3 = featherParams.size * 2 * k / f2.length + 7.0; + f3[k].scale.set(s3, s3, s3); + } + + loaded = true; +} + + + +function moveWing() { + var date = new Date(); + var motion = flappingParams.motion; + var speed = flappingParams.speed; + + + for (var i = 0; i < f1.length; i++) { + var x = f1[i].position.x; + var y = f1[i].position.y; + var z = f1[i].position.z; + f1[i].position.set(x, + y, + x * x * Math.sin(date * speed * 0.001) * (motion * 0.01)); + + var rotz = Math.sin((Math.random() - 0.5) / 3.0) * 2 * Math.PI / 180; + var roty = Math.sin((Math.random() - 0.5) / 2.0) * 2 * Math.PI / 180; + + if (f1[i].rotation.z + rotz <= f1_rad[i].z + threshold + && f1[i].rotation.z + rotz >= f1_rad[i].z - threshold) { + f1[i].rotateZ(rotz); + } else { + f1[i].rotateZ(-rotz); + } + + if (f1[i].rotation.y + roty <= f1_rad[i].y + threshold + && f1[i].rotation.y + roty >= f1_rad[i].y - threshold) { + f1[i].rotateY(roty); + } else { + f1[i].rotateY(-roty); + } + } + + for (var j = 0; j < f2.length; j++) { + var x = f2[j].position.x; + var y = f2[j].position.y; + var z = f2[j].position.z; + f2[j].position.set(x, + y, + x * x * Math.sin(date * speed * 0.001) * (motion * 0.01)); + + var rotz = Math.sin((Math.random() - 0.5) / 3.0) * 2 * Math.PI / 180; + var roty = Math.sin((Math.random() - 0.5) / 2.0) * 2 * Math.PI / 180; + + if (f2[j].rotation.z + rotz <= f2_rad[j].z + threshold + && f1[j].rotation.z + rotz >= f2_rad[j].z - threshold) { + f2[j].rotateZ(rotz); + } else { + f2[j].rotateZ(-rotz); + } + + if (f2[j].rotation.y + roty <= f2_rad[j].y + threshold + && f2[j].rotation.y + roty >= f2_rad[j].y - threshold) { + f2[j].rotateY(roty); + } else { + f2[j].rotateY(-roty); + } + } + + for (var k = 0; k < f3.length; k++) { + var x = f3[k].position.x; + var y = f3[k].position.y; + var z = f3[k].position.z; + f3[k].position.set(x, + y, + x * x * Math.sin(date * speed * 0.001) * (motion * 0.01)); + + var rotz = Math.sin((Math.random() - 0.5) / 3.0) * 2 * Math.PI / 180; + var roty = Math.sin((Math.random() - 0.5) / 2.0) * 2 * Math.PI / 180; + + if (f3[k].rotation.z + rotz <= f3_rad[k].z + threshold + && f3[k].rotation.z + rotz >= f3_rad[k].z - threshold) { + f3[k].rotateZ(rotz); + } else { + f3[k].rotateZ(-rotz); + } + + if (f3[k].rotation.y + roty <= f3_rad[k].y + threshold + && f3[k].rotation.y + roty >= f3_rad[k].y - threshold) { + f3[k].rotateY(roty); + } else { + f3[k].rotateY(-roty); + } + } } // 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); + if (loaded) { + moveWing(); } }