diff --git a/README.md b/README.md index 1410c30..be46d83 100644 --- a/README.md +++ b/README.md @@ -1,82 +1,5 @@ # [Project2: Toolbox Functions](https://github.com/CIS700-Procedural-Graphics/Project2-Toolbox-Functions) -## Overview +Project 1 - Noise -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. - -##### 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 +Creating an animated bird's wing using toolbox functions. GUI controls are adding to manipulate various aspects of the wing, such as wing curvature, color, feather size, orientation, flapping configuration and feather distribution. \ 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/references/38746066-Spread-out-of-bird-wing-isolated-on-white-background-Stock-Photo.jpg b/references/38746066-Spread-out-of-bird-wing-isolated-on-white-background-Stock-Photo.jpg new file mode 100644 index 0000000..e12ca78 Binary files /dev/null and b/references/38746066-Spread-out-of-bird-wing-isolated-on-white-background-Stock-Photo.jpg differ diff --git a/references/63b9b84361d1e75f666c302a3ac9a7a8 copy.jpg b/references/63b9b84361d1e75f666c302a3ac9a7a8 copy.jpg new file mode 100644 index 0000000..5f9ff42 Binary files /dev/null and b/references/63b9b84361d1e75f666c302a3ac9a7a8 copy.jpg differ diff --git a/references/pair-natural-bird-wings-inside-view-colored-sandpiper-shorebird-36928226.jpg b/references/pair-natural-bird-wings-inside-view-colored-sandpiper-shorebird-36928226.jpg new file mode 100644 index 0000000..0f8b850 Binary files /dev/null and b/references/pair-natural-bird-wings-inside-view-colored-sandpiper-shorebird-36928226.jpg differ diff --git a/src/main.js b/src/main.js index fd8fbd4..6aa5231 100755 --- a/src/main.js +++ b/src/main.js @@ -4,16 +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' +Number.prototype.clamp = function(min, max) { + return Math.min(Math.max(this, min), max); +}; + +var all_feathers = new THREE.Object3D(); +var main_feathers = new THREE.Object3D(); +var middle_feathers = new THREE.Object3D(); +var small_feathers = new THREE.Object3D(); +var curveObject; +var mainFeatherCurve = new THREE.CubicBezierCurve3( + new THREE.Vector3( 0, 0, 0 ), + new THREE.Vector3( 1, 0, 0 ), + new THREE.Vector3( 1.5, 0, -1 ), + new THREE.Vector3( 2.5, 0, -0.9 ) + ); +var mainFeatherGeometry = new THREE.Geometry(); + +var config = { + color : "#ffae23", + scale_x : 1, + scale_y : 1, + scale_z : 1, + rotate_x : 0, + rotate_y : 0, + rotate_z : 0, + flapping_speed : 1, + flapping_motion : 1, + distribution : 1, + curvature : 0, + wind_x : 0, + wind_y : 0, + wind_z : 0 +} + +var scene; +var featherGeo; + +var windForce = new THREE.Vector3( 5, 0, 0 ); + // called after the scene loads function onLoad(framework) { - var scene = framework.scene; + scene = framework.scene; var camera = framework.camera; var renderer = framework.renderer; var gui = framework.gui; var stats = framework.stats; + + var light = new THREE.AmbientLight( 0x404040 ); // soft white light + scene.add( light ); // Basic Lambert white - var lambertWhite = new THREE.MeshLambertMaterial({ color: 0xaaaaaa, side: THREE.DoubleSide }); + var lambertWhite = new THREE.MeshLambertMaterial({ color: 0x8e8e8e, side: THREE.DoubleSide, emissive: 0x0 }); + + var lambertGray = new THREE.MeshLambertMaterial({ color: 0x555555, side: THREE.DoubleSide }); + + var lambertBlack = new THREE.MeshLambertMaterial({ color: 0x393939, side: THREE.DoubleSide }); + //lambertBlack.emissive = 0x181818; // Set light var directionalLight = new THREE.DirectionalLight( 0xffffff, 1 ); @@ -23,7 +70,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', @@ -32,24 +79,92 @@ function onLoad(framework) { ] ); scene.background = skymap; + + + + //Main Feather + mainFeatherCurve = new THREE.CubicBezierCurve3( + new THREE.Vector3( 0, 0, 0 ), + new THREE.Vector3( 1, 0, 0 ), + new THREE.Vector3( 1.5, 0, -1 ), + new THREE.Vector3( 2.5, 0, -0.9 ) + ); + + mainFeatherGeometry = new THREE.Geometry(); + mainFeatherGeometry.vertices = mainFeatherCurve.getPoints(24); + + var material = new THREE.LineBasicMaterial( { color : 0xff0000 } ); + + curveObject = new THREE.Line( mainFeatherGeometry, material ); // 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; + + for (var i = 0; i < mainFeatherGeometry.vertices.length; i++) { - var featherMesh = new THREE.Mesh(featherGeo, lambertWhite); - featherMesh.name = "feather"; - scene.add(featherMesh); - }); + var featherMesh = new THREE.Mesh(featherGeo, lambertBlack); + featherMesh.name = "mainFeather"; + main_feathers.add(featherMesh); + var j = mainFeatherGeometry.vertices.length-i-1; + var p = mainFeatherGeometry.vertices[i]; + featherMesh.position.set(p.x,p.y,p.z); + featherMesh.rotation.z = p.y; + featherMesh.rotation.y = -(Math.PI/2) + lerp(0, Math.PI/2, i/mainFeatherGeometry.vertices.length); + var scale = 0.5; + featherMesh.scale.set(scale,scale,scale); + } + + var numMiddleFeathers = mainFeatherGeometry.vertices.length/1.2; + + for (var i = 0; i < numMiddleFeathers; i++) { + + var featherMesh = new THREE.Mesh(featherGeo, lambertGray); + featherMesh.name = "middleFeather"; + middle_feathers.add(featherMesh); + var j = mainFeatherGeometry.vertices.length-i-1; + var p = mainFeatherGeometry.vertices[i]; + + featherMesh.position.set(p.x,p.y+0.02,p.z); + featherMesh.rotation.z = p.y;// * 1.3; + featherMesh.rotation.y = -(Math.PI/2) + lerp(0, Math.PI/1.9, i/numMiddleFeathers); + var scale = 0.3; + featherMesh.scale.set(scale,scale,scale*2); + + } + + var numSmallFeathers = mainFeatherGeometry.vertices.length/2; + + + for (var i = 0; i < numSmallFeathers; i++) { + + var featherMesh = new THREE.Mesh(featherGeo, lambertWhite); + featherMesh.name = "smallFeather"; + small_feathers.add(featherMesh); + var j = mainFeatherGeometry.vertices.length-i-1; + var p = mainFeatherGeometry.vertices[i]; + + featherMesh.position.set(p.x,p.y+0.04,p.z+0.05); + featherMesh.rotation.z = p.y;// * 3; + featherMesh.rotation.y = -(Math.PI/2) + lerp(0, Math.PI/1.8, i/numSmallFeathers); + var scale = 0.2; + featherMesh.scale.set(scale,scale,scale*3); + + } + }); + + + all_feathers.add(main_feathers); + all_feathers.add(middle_feathers); + all_feathers.add(small_feathers); + scene.add(all_feathers); // 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,16 +172,312 @@ function onLoad(framework) { gui.add(camera, 'fov', 0, 180).onChange(function(newVal) { camera.updateProjectionMatrix(); }); + + var f7 = gui.addFolder('Wind Speed'); + + f7.add(config, 'wind_x').min(-10).max(10); + f7.add(config, 'wind_y').min(-10).max(10); + f7.add(config, 'wind_z').min(-10).max(10); + + + var f6 = gui.addFolder('Wing Curve'); + + f6.add(config, 'curvature').min(0).max(0.5); + + + var f1 = gui.addFolder('Wing Color'); + + + f1.addColor(config, 'color').onChange( function(colorValue) + { + var colorObject = new THREE.Color(colorValue); + + var feathers = all_feathers.children[0]; + + for(var j in feathers.children) { + var feather = feathers.children[j]; + //feather.material.color = colorObject; + var lambertBlack = new THREE.MeshLambertMaterial({ color: colorValue, side: THREE.DoubleSide }); + feather.material = lambertBlack; + + } + }); + + f1.addColor(config, 'color').onChange( function(colorValue) + { + var colorObject = new THREE.Color(colorValue); + + var feathers = all_feathers.children[1]; + + for(var j in feathers.children) { + var feather = feathers.children[j]; + var lambertBlack = new THREE.MeshLambertMaterial({ color: colorValue, side: THREE.DoubleSide }); + feather.material = lambertBlack; + + } + }); + + f1.addColor(config, 'color').onChange( function(colorValue) + { + var colorObject = new THREE.Color(colorValue); + + var feathers = all_feathers.children[2]; + + for(var j in feathers.children) { + var feather = feathers.children[j]; + var lambertBlack = new THREE.MeshLambertMaterial({ color: colorValue, side: THREE.DoubleSide }); + feather.material = lambertBlack; + + } + }); + + + var f2 = gui.addFolder('Feather Size'); + + + f2.add(config, 'scale_x', 0.1, 10).onChange( function(scaleX) + { + for (var i in all_feathers.children) { + + var feathers = all_feathers.children[i]; + + for(var j in feathers.children) { + var feather = feathers.children[j]; + feather.scale.x = scaleX; + } + + } + }); + + f2.add(config, 'scale_y', 0.1, 10).onChange( function(scaleY) + { + + for (var i in all_feathers.children) { + + var feathers = all_feathers.children[i]; + + for(var j in feathers.children) { + var feather = feathers.children[j]; + feather.scale.y = scaleY; + } + + } + + }); + + f2.add(config, 'scale_z', 0.1, 10).onChange( function(scaleZ) + { + for (var i in all_feathers.children) { + + var feathers = all_feathers.children[i]; + + for(var j in feathers.children) { + var feather = feathers.children[j]; + feather.scale.z = scaleZ; + } + + } + }); + + + var f3 = gui.addFolder('Feather Orientation'); + + f3.add(config, 'rotate_x', 0, 360).onChange( function(rotateX){}); + + f3.add(config, 'rotate_y', 0, 360).onChange( function(rotateY){}); + + f3.add(config, 'rotate_z', 0, 360).onChange( function(rotateZ){}); + + + var f4 = gui.addFolder('Flapping Configuration'); + + f4.add(config, 'flapping_speed', 1, 5).onChange( function(speed){}); + f4.add(config, 'flapping_motion', 1, 2).onChange( function(motion){}); + + var f5 = gui.addFolder('Feather Distribution'); + + f5.add(config, 'distribution').min(1).max(3).step(1); + +} + +function getRandom() { + return (Math.random()) / 50; } +function updateFeatherPostions() { + + + if (!mainFeatherCurve || !mainFeatherGeometry) + return; + + var start = 1; + var middle = Math.sin(frame/50 * config.flapping_speed)/2 * config.flapping_motion; + middle = middle; + var end = Math.sin(frame/50 * config.flapping_speed); + end = end ; + var s = smoothstep(middle, end); + //middle *= s; + var s2 = smootherstep(start, middle); + start *= s2; + + mainFeatherCurve = new THREE.CubicBezierCurve3( + new THREE.Vector3( 0 , 0, 0 ), + new THREE.Vector3( 1 , start, 0 + config.curvature ), + new THREE.Vector3( 1.5 , middle, -1 + config.curvature), + new THREE.Vector3( 2.5 , end, -0.9 - config.curvature) + ); + + var tempNum = mainFeatherGeometry.vertices.length; + mainFeatherGeometry.vertices = mainFeatherCurve.getPoints(24 * config.distribution); + var numMiddleFeathers = mainFeatherGeometry.vertices.length/1.2; + var numSmallFeathers = mainFeatherGeometry.vertices.length/2; + + + if (mainFeatherGeometry.vertices.length > tempNum) { + + if (scene) + { + for( var i = scene.children.length - 1; i >= 0; i--) { + var obj = scene.children[i]; + scene.remove(obj); + } + + all_feathers = new THREE.Object3D(); + main_feathers = new THREE.Object3D(); + middle_feathers = new THREE.Object3D(); + small_feathers = new THREE.Object3D(); + + var lambertWhite = new THREE.MeshLambertMaterial({ color: 0x8e8e8e, side: THREE.DoubleSide, emissive: 0x0 }); + + var lambertGray = new THREE.MeshLambertMaterial({ color: 0x555555, side: THREE.DoubleSide }); + + var lambertBlack = new THREE.MeshLambertMaterial({ color: 0x393939, side: THREE.DoubleSide }); + + for (var i = 0; i < mainFeatherGeometry.vertices.length; i++) { + + var featherMesh = new THREE.Mesh(featherGeo, lambertBlack); + featherMesh.name = "mainFeather"; + main_feathers.add(featherMesh); + var j = mainFeatherGeometry.vertices.length-i-1; + var p = mainFeatherGeometry.vertices[i]; + featherMesh.position.set(p.x,p.y,p.z); + featherMesh.rotation.z = p.y; + featherMesh.rotation.y = -(Math.PI/2) + lerp(0, Math.PI/2, i/mainFeatherGeometry.vertices.length); + var scale = 0.5; + featherMesh.scale.set(scale,scale,scale); + } + + var numMiddleFeathers = mainFeatherGeometry.vertices.length/1.2; + + for (var i = 0; i < numMiddleFeathers; i++) { + + var featherMesh = new THREE.Mesh(featherGeo, lambertGray); + featherMesh.name = "middleFeather"; + middle_feathers.add(featherMesh); + var j = mainFeatherGeometry.vertices.length-i-1; + var p = mainFeatherGeometry.vertices[i]; + + featherMesh.position.set(p.x,p.y+0.02,p.z); + featherMesh.rotation.z = p.y;// * 1.3; + featherMesh.rotation.y = -(Math.PI/2) + lerp(0, Math.PI/1.9, i/numMiddleFeathers); + var scale = 0.3; + featherMesh.scale.set(scale,scale,scale*2); + + } + + var numSmallFeathers = mainFeatherGeometry.vertices.length/2; + + + for (var i = 0; i < numSmallFeathers; i++) { + + var featherMesh = new THREE.Mesh(featherGeo, lambertWhite); + featherMesh.name = "smallFeather"; + small_feathers.add(featherMesh); + var j = mainFeatherGeometry.vertices.length-i-1; + var p = mainFeatherGeometry.vertices[i]; + + featherMesh.position.set(p.x,p.y+0.04,p.z+0.05); + featherMesh.rotation.z = p.y;// * 3; + featherMesh.rotation.y = -(Math.PI/2) + lerp(0, Math.PI/1.8, i/numSmallFeathers); + var scale = 0.2; + featherMesh.scale.set(scale,scale,scale*3); + + } + + all_feathers.add(main_feathers); + all_feathers.add(middle_feathers); + all_feathers.add(small_feathers); + scene.add(all_feathers); + } + + } else { + + for (var i in all_feathers.children) { + + var feathers = all_feathers.children[i]; + + for(var j in feathers.children) { + var feather = feathers.children[j]; + + var p = mainFeatherGeometry.vertices[j]; + + if (!p) + continue; + + feather.position.set(p.x,p.y,p.z); + + if (i == 1) { + p.y += 0.02; + } + else if (i == 2) { + p.y += 0.04; + } + + feather.rotation.x = config.rotate_x * (Math.PI/180.0) + getRandom() * config.wind_x; + + feather.rotation.y = -(Math.PI/2) + lerp(0, Math.PI/2, j/mainFeatherGeometry.vertices.length) + config.rotate_y * (Math.PI/180.0) + getRandom() * config.wind_y; + + feather.rotation.z = p.y + config.rotate_z * (Math.PI/180.0) + + getRandom() * config.wind_z; + + } + + } + + } + +} + +function lerp(a, b, t) { + return a * (1.0 - t) + b * t; +} + +function smoothstep(edge0, edge1, x) +{ + x = Number.prototype.clamp((x - edge0)/(edge1 - edge0), 0.0, 1.0); + return x*x*(3 - 2*x); +} + +function smootherstep(edge0, edge1, x) +{ + x = Number.prototype.clamp((x - edge0)/(edge1 - edge0), 0.0, 1.0); + return x*x*x*(x*(x*6 - 15)+10); +} + +function Noise(x, y) +{ + var n = x + y * 57 + var n = (n<<13) ^ n; + return ( 1.0 - ( (n * (n * n * 15731 + 789221) + 1376312589) & 0x7fffffff) / 1073741824.0); +} + +var frame = 0; // 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); - } + updateFeatherPostions(); + frame++; } // when the scene is done initializing, it will call onLoad, then on frame updates, call onUpdate