diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000..d45803c Binary files /dev/null and b/.DS_Store differ diff --git a/.gitignore b/.gitignore index 8d4aaf4..7efe013 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ node_modules -*.map \ No newline at end of file +*.map +*.un~ diff --git a/README.md b/README.md index 1410c30..834029f 100644 --- a/README.md +++ b/README.md @@ -1,82 +1,30 @@ # [Project2: Toolbox Functions](https://github.com/CIS700-Procedural-Graphics/Project2-Toolbox-Functions) -## Overview +* Author: Joseph Gao +* PennKey: gaoj -The objective of this assignment is to procedurally model and animate a bird wing. Let's get creative! +# Features Implemented -Start by forking and then cloning [this repository](https://github.com/CIS700-Procedural-Graphics/Project2-Toolbox-Functions) +Modeling +------- -## Modeling +I based my wing and feather structure from that of an owl. -##### Reference images +To make the bone, I used QuadraticBezierCurve3 to sketch out its rough outline. I attempted to make the bone follow the general structure of an owl wing by finding points that would make the closest parabola possible. Once the Bezier curve was created, I distributed vertices evenly along the bone structure that would serve as the locations of the 100 feathers. -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. +Animation +--------- -##### Make wing curve +My scene contains a dynamic, movable wind (just use the GUI). The wind force causes the feathers to be blown a different direction in 3D space. The wind itself causes the feathers to vibrate in place, and the intensity of this can be changed. Additionally, the wing is always flapping. -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. +Interactivity +------------- -##### Distribute feathers +All the required interactivity features have been implemented, and the user is able to alter the intensity and direction of the wind, manually change the curvature of the bone, alter the number of feathers present on the bone, change the feather size, color, and orientation, as well as alter the flapping speed and intensity of the wing. All this can be done through the GUI in the upper right. -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. +Extra Credit +------------ +No extra credit implemented for this homework. -## 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 diff --git a/images/.DS_Store b/images/.DS_Store new file mode 100644 index 0000000..2f3a33d Binary files /dev/null and b/images/.DS_Store differ 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/references/owl_wing_bone.jpg b/references/owl_wing_bone.jpg new file mode 100644 index 0000000..7bc0c9f Binary files /dev/null and b/references/owl_wing_bone.jpg differ diff --git a/references/wing_with_feathers.jpg b/references/wing_with_feathers.jpg new file mode 100644 index 0000000..ba65cc4 Binary files /dev/null and b/references/wing_with_feathers.jpg differ diff --git a/src/.DS_Store b/src/.DS_Store new file mode 100644 index 0000000..5008ddf Binary files /dev/null and b/src/.DS_Store differ diff --git a/src/main.js b/src/main.js index fd8fbd4..d8f62f0 100755 --- a/src/main.js +++ b/src/main.js @@ -4,6 +4,63 @@ const THREE = require('three'); // older modules are imported like this. You shouldn't have to worry about this much import Framework from './framework' +var tick = 0; +var damping = 0.1; +var feathers = 100; +var featherGeo; +var wind_direction = 0; + +// bone stuff +var bone_geometry = new THREE.Geometry(); +var curve = new THREE.QuadraticBezierCurve3(); +var boneMaterial = new THREE.LineBasicMaterial( { color: 0xe3dac9, linewidth: 1 } ); +var bone = new THREE.Line(bone_geometry, boneMaterial); + + +var base_color = 0xFFDFF00; + +var feather_color = function() { + this.color = 0; +}; + +var visible_feathers = function() { + this.feather_count = 100; +} + +var wind_speed = function() { + this.wind_speed = 10; +}; + +var wing_curve = function() { + this.curve = 0; +}; + +var wind_direction = function() { + this.wind_direction = 0; +} + +var feather_size = function() { + this.feather_size = 1; +} + +var flap_speed = function() { + this.flap_speed = 1; +} + +var flap_range = function() { + this.flap_range = 1; +} + +// defining our scales +var wind_speed_scale = new wind_speed(); +var wing_curve_scale = new wing_curve(); +var feather_color_scale = new feather_color(); +var visible_feathers_scale = new visible_feathers(); +var wind_direction_scale = new wind_direction(); +var feather_size_scale = new feather_size(); +var feather_flap_speed = new flap_speed(); +var feather_flap_range = new flap_range(); + // called after the scene loads function onLoad(framework) { var scene = framework.scene; @@ -23,7 +80,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,23 +90,41 @@ function onLoad(framework) { scene.background = skymap; + curve.v0 = new THREE.Vector3(-3, 0, 0); + curve.v1 = new THREE.Vector3(0, 0, 0); + curve.v2 = new THREE.Vector3(3, 0, 0); + for (var j = 0; j < feathers; j++) { + bone_geometry.vertices.push(curve.getPoint(j / feathers)); + } + scene.add(bone); + // 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; + featherGeo = obj.children[0].geometry; var featherMesh = new THREE.Mesh(featherGeo, lambertWhite); featherMesh.name = "feather"; - scene.add(featherMesh); + featherMesh.position.set(0.1, 0.3, 0); + for (var i = 0; i < bone_geometry.vertices.length; i++) { + var vertex = bone_geometry.vertices[i]; + var feather = featherMesh.clone(true); + feather.name = i; + feather.position.set(vertex.x, vertex.y, vertex.z); + feather.rotateY(90); + feather.rotateZ(180); + feather.scale.set((i/feathers), (i/feathers), (i/feathers)); + feather.traverse( function ( object ) { object.visible = true; } ); + scene.add(feather); + } }); // set camera position camera.position.set(0, 1, 5); camera.lookAt(new THREE.Vector3(0,0,0)); - // scene.add(lambertCube); scene.add(directionalLight); // edit params and listen to changes like this @@ -57,17 +132,131 @@ function onLoad(framework) { gui.add(camera, 'fov', 0, 180).onChange(function(newVal) { camera.updateProjectionMatrix(); }); + + gui.add(wind_speed_scale, 'wind_speed', 0, 100).onChange(function(newVal) { + wind_speed_scale.scale = newVal; + }); + + gui.add(feather_color_scale, 'color', 0, 100).onChange(function(newVal) { + feather_color_scale.color = newVal; + }); + + gui.add(visible_feathers_scale, 'feather_count', 0, 100).onChange(function(newVal) { + visible_feathers_scale.feather_count = newVal; + for (var i = 0; i < newVal; i++) { + var feather = framework.scene.getObjectByName(i); + if (feather) { + feather.traverse( function ( object ) { object.visible = true; }); + } + } + for (var i = Math.round(newVal); i < 100; i++) { + var feather = framework.scene.getObjectByName(i); + if (feather) { + feather.traverse( function ( object ) { object.visible = false; }); + } + } + }); + + gui.add(wing_curve_scale, 'curve', -5, 5).onChange(function(newVal) { + wing_curve_scale.curve = newVal; + curve.v0 = new THREE.Vector3(-3, -1 * newVal, 0); + curve.v1 = new THREE.Vector3(0, newVal, 0); + curve.v2 = new THREE.Vector3(3, -1 * newVal, 0); + scene.remove(bone); + bone_geometry = new THREE.Geometry(); + for (var j = 0; j < feathers; j++) { + bone_geometry.vertices.push(curve.getPoint(j / feathers)); + } + bone = new THREE.Line(bone_geometry, boneMaterial); + scene.add(bone); + + for (var i = 0; i < bone_geometry.vertices.length; i++) { + var vertex = bone_geometry.vertices[i]; + var feather = framework.scene.getObjectByName(i); + feather.position.set(vertex.x, vertex.y, vertex.z); + } + }); + + gui.add(wind_direction_scale, 'wind_direction', 0, 1).onChange(function(newVal) { + if (wind_direction !== 0) { + for (var i = 0; i < feathers; i++) { + var feather = framework.scene.getObjectByName(i); + if (feather && newVal != 1) { + feather.rotateY(0.03); + } + } + if (newVal === 0) { + wind_direction = 0; + } + } + else { + for (var i = 0; i < feathers; i++) { + var feather = framework.scene.getObjectByName(i); + if (feather && newVal != 0) { + feather.rotateY(-0.03); + } + } + if (newVal === 1) { + wind_direction = 1; + } + } + wind_direction_scale.wind_direction = newVal; + }); + + gui.add(feather_size_scale, 'feather_size', 1, 10).onChange(function(newVal) { + feather_size_scale.feather_size = newVal; + }); + + gui.add(feather_flap_speed, 'flap_speed', 1, 10).onChange(function(newVal) { + feather_flap_speed.flap_speed = newVal; + }); + + gui.add(feather_flap_range, 'flap_range', 1, 5).onChange(function(newVal) { + feather_flap_range.flap_range = newVal; + }); } // called on frame updates function onUpdate(framework) { - var feather = framework.scene.getObjectByName("feather"); - if (feather !== undefined) { - // Simply flap wing + tick += feather_flap_speed.flap_speed / 10; + var flap_range = feather_flap_range.flap_range; + var disp = flap_range * Math.sin(tick); + + curve.v0 = new THREE.Vector3(-3, -1 * disp, 0); + curve.v1 = new THREE.Vector3(0, disp, 0); + curve.v2 = new THREE.Vector3(3, -1 * disp, 0); + framework.scene.remove(bone); + bone_geometry = new THREE.Geometry(); + for (var j = 0; j < feathers; j++) { + bone_geometry.vertices.push(curve.getPoint(j / feathers)); + } + bone = new THREE.Line(bone_geometry, boneMaterial); + framework.scene.add(bone); + console.log(bone_geometry.vertices.length); + for (var i = 0; i < bone_geometry.vertices.length; i++) { + var vertex = bone_geometry.vertices[i]; + var feather = framework.scene.getObjectByName(i); + if (feather) { + feather.position.set(vertex.x, vertex.y, vertex.z); + } + } + + for (var i = 0; i < visible_feathers_scale.feather_count; i++) { + var feather = framework.scene.getObjectByName(i); var date = new Date(); - feather.rotateZ(Math.sin(date.getTime() / 100) * 2 * Math.PI / 180); + var size_factor = feather_size_scale.feather_size; + if (feather !== undefined) { + feather.scale.set((size_factor * i/feathers), (size_factor * i/feathers), (size_factor * i/feathers)); + feather.material.color.set(base_color + feather_color_scale.color * 167772); + if (wind_direction_scale.wind_direction == 0) { + feather.rotateZ(wind_speed_scale.wind_speed / 100 * Math.sin(i + date.getTime() / 100) * 2 * Math.PI / 180); + } + else { + feather.rotateY(wind_speed_scale.wind_speed / 100 * Math.cos(i + date.getTime() / 100) * 2 * Math.PI / 180); + } + } } } // 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);