From 8ee728815f2bba0a2900a46f8daa8eefbdce0974 Mon Sep 17 00:00:00 2001 From: Nick Newberg Date: Mon, 30 Jan 2017 15:13:56 -0500 Subject: [PATCH 1/5] basic wing created --- src/main.js | 62 ++++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 57 insertions(+), 5 deletions(-) diff --git a/src/main.js b/src/main.js index fd8fbd4..3057f1c 100755 --- a/src/main.js +++ b/src/main.js @@ -4,6 +4,42 @@ const THREE = require('three'); // older modules are imported like this. You shouldn't have to worry about this much import Framework from './framework' + +function easeLinear(t, start, end, duration){ + return end * (t/duration) + start; +} + +function ease_in_quadratic(t, start, end, duration){ + t = t/duration; + t = t * t; + return t * (end - start) + start +} + +function create_top_layer(wing, mesh, featherDist, numFeathers, steepness){ + for (var i = 0; i < featherDist*numFeathers; i+=featherDist){ + var featherMeshClone = mesh.clone(); + var duration = featherDist*numFeathers; + var z_pos = ease_in_quadratic(i, 0, 1, duration); + featherMeshClone.position.set(i, 0, -z_pos); + var rot = easeLinear(i, 0.0, -90.0, duration); + featherMeshClone.rotateY((Math.PI/180.0) *rot) + wing.add(featherMeshClone); + } +} + +function create_bottom_layer(wing, mesh, featherDist, offset, numFeathers, steepness){ + for (var i = 0; i < featherDist*numFeathers; i+=featherDist){ + var featherMeshClone = mesh.clone(); + var duration = featherDist*numFeathers; + var z_pos = ease_in_quadratic(i, 0, 1, duration); + featherMeshClone.position.set(i, -0.1, -z_pos + offset); + featherMeshClone.scale.set(1.5,1,2); + var rot = easeLinear(i, 0.0, -90.0, duration); + featherMeshClone.rotateY((Math.PI/180.0) *rot) + wing.add(featherMeshClone); + } +} + // called after the scene loads function onLoad(framework) { var scene = framework.scene; @@ -33,17 +69,33 @@ function onLoad(framework) { scene.background = skymap; + //parent wing object + var wing = new THREE.Object3D(); + wing.name = "wing"; // 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 featherGeo = obj.children[0].geometry; var featherMesh = new THREE.Mesh(featherGeo, lambertWhite); + featherMesh.rotation.y = Math.PI / 2; + featherMesh.rotation.z = Math.PI; + + wing.add(featherMesh); featherMesh.name = "feather"; - scene.add(featherMesh); + var featherDist = 0.25; + var numFeathers = 20.0; + var steepness = 15.0; //lower is steeper + create_top_layer(wing, featherMesh, featherDist, numFeathers, steepness); + featherDist = 0.35; + numFeathers = 15.0; + steepness = 12.0; //lower is steeper + create_bottom_layer(wing, featherMesh, featherDist, 0, numFeathers, steepness); + }); + scene.add(wing); // set camera position camera.position.set(0, 1, 5); @@ -61,11 +113,11 @@ function onLoad(framework) { // called on frame updates function onUpdate(framework) { - var feather = framework.scene.getObjectByName("feather"); - if (feather !== undefined) { + var wing = framework.scene.getObjectByName("wing"); + if (wing !== undefined) { // Simply flap wing var date = new Date(); - feather.rotateZ(Math.sin(date.getTime() / 100) * 2 * Math.PI / 180); + wing.rotateX(Math.sin(date.getTime() / 100) * 2 * Math.PI / 180); } } From 268a05ae0f88347445ae6c89909059cee1321c55 Mon Sep 17 00:00:00 2001 From: Nick Newberg Date: Mon, 30 Jan 2017 21:02:17 -0500 Subject: [PATCH 2/5] addedgui input --- src/main.js | 108 ++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 87 insertions(+), 21 deletions(-) diff --git a/src/main.js b/src/main.js index 3057f1c..aa58bc0 100755 --- a/src/main.js +++ b/src/main.js @@ -4,6 +4,32 @@ const THREE = require('three'); // older modules are imported like this. You shouldn't have to worry about this much import Framework from './framework' +var wing; +var feathers = []; +var params = {numFeathers: 20.0, + featherDist: 0.25, + featherCurvature: 0.5, + featherSize: 1.0, + wind: new THREE.Vector3(0,0,1.0), + windSpeed: 1.0}; +var time = 0.0; +var featherMesh; + +function createFeathers(){ + create_top_layer(featherMesh, params.featherDist, params.numFeathers, 0.0, + params.featherCurvature, params.featherSize); + var featherDist = params.featherDist/2.0 + params.featherDist; + var numFeathers = params.numFeathers - params.numFeathers/4.0; + create_bottom_layer(featherMesh, featherDist, 0, numFeathers, 0.0, params.featherCurvature, + params.featherSize); +} + +function removeFeathers(){ +for( var i = wing.children.length - 1; i >= 0; i--) { + wing.remove(wing.children[i]); +} + +} function easeLinear(t, start, end, duration){ return end * (t/duration) + start; @@ -15,28 +41,41 @@ function ease_in_quadratic(t, start, end, duration){ return t * (end - start) + start } -function create_top_layer(wing, mesh, featherDist, numFeathers, steepness){ +function impulse(k,x){ + var h = k*x; + return h * Math.exp(1.0 - h); +} + +function wingProfile(t, decay){ + return impulse(decay,t); +} + +function create_top_layer(mesh, featherDist, numFeathers, steepness, featherCurvature, featherSize){ for (var i = 0; i < featherDist*numFeathers; i+=featherDist){ var featherMeshClone = mesh.clone(); var duration = featherDist*numFeathers; var z_pos = ease_in_quadratic(i, 0, 1, duration); - featherMeshClone.position.set(i, 0, -z_pos); + var y_pos = wingProfile(i, featherCurvature); + featherMeshClone.position.set(i, y_pos, -z_pos); + featherMeshClone.scale.set(featherSize,1,featherSize); var rot = easeLinear(i, 0.0, -90.0, duration); - featherMeshClone.rotateY((Math.PI/180.0) *rot) - wing.add(featherMeshClone); + featherMeshClone.rotateY((Math.PI/180.0) *rot); + wing.add(featherMeshClone); + feathers.push(featherMeshClone); } } -function create_bottom_layer(wing, mesh, featherDist, offset, numFeathers, steepness){ +function create_bottom_layer(mesh, featherDist, offset, numFeathers, steepness, featherCurvature, featherSize){ for (var i = 0; i < featherDist*numFeathers; i+=featherDist){ var featherMeshClone = mesh.clone(); var duration = featherDist*numFeathers; var z_pos = ease_in_quadratic(i, 0, 1, duration); - featherMeshClone.position.set(i, -0.1, -z_pos + offset); - featherMeshClone.scale.set(1.5,1,2); + var y_pos = wingProfile(i, featherCurvature); + featherMeshClone.position.set(i, y_pos -0.1, -z_pos + offset); + featherMeshClone.scale.set(1.5 * featherSize,1,2 * featherSize); var rot = easeLinear(i, 0.0, -90.0, duration); - featherMeshClone.rotateY((Math.PI/180.0) *rot) - wing.add(featherMeshClone); + featherMeshClone.rotateY((Math.PI/180.0) *rot); + wing.add(featherMeshClone); } } @@ -50,7 +89,11 @@ function onLoad(framework) { // Basic Lambert white var lambertWhite = new THREE.MeshLambertMaterial({ color: 0xaaaaaa, side: THREE.DoubleSide }); - + var wingMat = new THREE.MeshPhongMaterial( { + color: 0x2956B2, + specular: 0xaaaaaa, + shininess: 6 + } ); // Set light var directionalLight = new THREE.DirectionalLight( 0xffffff, 1 ); directionalLight.color.setHSL(0.1, 1, 0.95); @@ -70,7 +113,7 @@ function onLoad(framework) { scene.background = skymap; //parent wing object - var wing = new THREE.Object3D(); + wing = new THREE.Object3D(); wing.name = "wing"; // load a simple obj mesh var objLoader = new THREE.OBJLoader(); @@ -79,20 +122,13 @@ function onLoad(framework) { // LOOK: This function runs after the obj has finished loading var featherGeo = obj.children[0].geometry; - var featherMesh = new THREE.Mesh(featherGeo, lambertWhite); + featherMesh = new THREE.Mesh(featherGeo, wingMat); featherMesh.rotation.y = Math.PI / 2; featherMesh.rotation.z = Math.PI; wing.add(featherMesh); featherMesh.name = "feather"; - var featherDist = 0.25; - var numFeathers = 20.0; - var steepness = 15.0; //lower is steeper - create_top_layer(wing, featherMesh, featherDist, numFeathers, steepness); - featherDist = 0.35; - numFeathers = 15.0; - steepness = 12.0; //lower is steeper - create_bottom_layer(wing, featherMesh, featherDist, 0, numFeathers, steepness); + createFeathers(); }); scene.add(wing); @@ -109,6 +145,35 @@ function onLoad(framework) { gui.add(camera, 'fov', 0, 180).onChange(function(newVal) { camera.updateProjectionMatrix(); }); + + gui.add(params, 'numFeathers', 15, 35).onChange(function(newVal) { + removeFeathers(); + createFeathers(); + }); + + gui.add(params, 'featherDist', 0.1, 0.5).onChange(function(newVal) { + removeFeathers(); + createFeathers(); + }); + + gui.add(params, 'featherCurvature', 0, 1.0).onChange(function(newVal) { + removeFeathers(); + createFeathers(); + }); + + gui.add(params, 'featherSize', 0.5, 3.0).onChange(function(newVal) { + removeFeathers(); + createFeathers(); + }); + + gui.add(params, 'windSpeed', 0, 10.0).onChange(function(newVal) { + removeFeathers(); + createFeathers(); + }); +} + +function animateWind(){ + } // called on frame updates @@ -117,7 +182,8 @@ function onUpdate(framework) { if (wing !== undefined) { // Simply flap wing var date = new Date(); - wing.rotateX(Math.sin(date.getTime() / 100) * 2 * Math.PI / 180); + time = date.getTime(); + //wing.rotateZ(Math.sin(date.getTime() / 100) * 2 * Math.PI / 180); } } From 2ad71bd09740b759e877f666ebb1dcf134cebb75 Mon Sep 17 00:00:00 2001 From: Nick Newberg Date: Mon, 30 Jan 2017 22:30:14 -0500 Subject: [PATCH 3/5] lookin good --- src/main.js | 52 +++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 49 insertions(+), 3 deletions(-) diff --git a/src/main.js b/src/main.js index aa58bc0..48a97a1 100755 --- a/src/main.js +++ b/src/main.js @@ -11,7 +11,8 @@ var params = {numFeathers: 20.0, featherCurvature: 0.5, featherSize: 1.0, wind: new THREE.Vector3(0,0,1.0), - windSpeed: 1.0}; + windSpeed: 1.0, + color: 0x2956B2}; var time = 0.0; var featherMesh; @@ -29,6 +30,26 @@ for( var i = wing.children.length - 1; i >= 0; i--) { wing.remove(wing.children[i]); } +function ColorLuminance(hex, lum) { + + // validate hex string + hex = String(hex).replace(/[^0-9a-f]/gi, ''); + if (hex.length < 6) { + hex = hex[0]+hex[0]+hex[1]+hex[1]+hex[2]+hex[2]; + } + lum = lum || 0; + + // convert to decimal and change luminosity + var rgb = "#", c, i; + for (i = 0; i < 3; i++) { + c = parseInt(hex.substr(i*2,2), 16); + c = Math.round(Math.min(Math.max(0, c + (c * lum)), 255)).toString(16); + rgb += ("00"+c).substr(c.length); + } + + return rgb; +} + } function easeLinear(t, start, end, duration){ @@ -58,6 +79,14 @@ function create_top_layer(mesh, featherDist, numFeathers, steepness, featherCurv var y_pos = wingProfile(i, featherCurvature); featherMeshClone.position.set(i, y_pos, -z_pos); featherMeshClone.scale.set(featherSize,1,featherSize); + var lum = easeLinear(i, 0, 0.6, duration); + var baseColor = new THREE.Color(params.color); + var featherMat = new THREE.MeshPhongMaterial( { + color: baseColor.addScalar(-lum), + specular: 0xaaaaaa, + shininess: 6 + } ); + featherMeshClone.material = featherMat; var rot = easeLinear(i, 0.0, -90.0, duration); featherMeshClone.rotateY((Math.PI/180.0) *rot); wing.add(featherMeshClone); @@ -73,9 +102,17 @@ function create_bottom_layer(mesh, featherDist, offset, numFeathers, steepness, var y_pos = wingProfile(i, featherCurvature); featherMeshClone.position.set(i, y_pos -0.1, -z_pos + offset); featherMeshClone.scale.set(1.5 * featherSize,1,2 * featherSize); + var lum = ease_in_quadratic(i, 0, 0.5, duration/1.5); + var featherMat = new THREE.MeshPhongMaterial( { + color: new THREE.Color(params.color).addScalar(-lum), + specular: 0xaaaaaa, + shininess: 6 + } ); + featherMeshClone.material = featherMat; var rot = easeLinear(i, 0.0, -90.0, duration); featherMeshClone.rotateY((Math.PI/180.0) *rot); wing.add(featherMeshClone); + //feathers.push(featherMeshClone); } } @@ -170,10 +207,18 @@ function onLoad(framework) { removeFeathers(); createFeathers(); }); + + gui.addColor(params, 'color').onChange(function(newVal) { + removeFeathers(); + createFeathers(); + }); } function animateWind(){ - + feathers.forEach(function(feather) { + feather.rotation.z = (Math.PI/180.0) * -params.windSpeed + * Math.sin((time/100)); +}); } // called on frame updates @@ -183,7 +228,8 @@ function onUpdate(framework) { // Simply flap wing var date = new Date(); time = date.getTime(); - //wing.rotateZ(Math.sin(date.getTime() / 100) * 2 * Math.PI / 180); + wing.rotateZ(Math.sin(date.getTime() / 200) * 1.2*Math.PI / 180); + animateWind(); } } From 642ce80104969b6c2e9a37643ea6df511c5a12f1 Mon Sep 17 00:00:00 2001 From: Nick Newberg Date: Mon, 30 Jan 2017 22:38:36 -0500 Subject: [PATCH 4/5] updated readme --- README.md | 79 ++-------------------------------------------------- package.json | 2 +- 2 files changed, 3 insertions(+), 78 deletions(-) diff --git a/README.md b/README.md index 1410c30..31f4236 100644 --- a/README.md +++ b/README.md @@ -2,81 +2,6 @@ ## Overview -The objective of this assignment is to procedurally model and animate a bird wing. Let's get creative! +My wing is pretty cool. -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 +You can change its color, the number of feathers, distance between feathers, wing curvature, feather size and wind speed. :-) \ 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": [ From 4700c31c7c436ffd7810ba11b341e5859c63c2ab Mon Sep 17 00:00:00 2001 From: Nick Newberg Date: Mon, 30 Jan 2017 22:41:18 -0500 Subject: [PATCH 5/5] deploy fix --- src/main.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main.js b/src/main.js index 48a97a1..69b5e8f 100755 --- a/src/main.js +++ b/src/main.js @@ -139,7 +139,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', @@ -154,7 +154,7 @@ function onLoad(framework) { wing.name = "wing"; // 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