diff --git a/README.md b/README.md index 1410c30..8f66273 100644 --- a/README.md +++ b/README.md @@ -1,82 +1,31 @@ # [Project2: Toolbox Functions](https://github.com/CIS700-Procedural-Graphics/Project2-Toolbox-Functions) -## Overview +Demo [here](http://andrea-lin.com/Project2-Toolbox-Functions/). -The objective of this assignment is to procedurally model and animate a bird wing. Let's get creative! +![Alt text](references/wing1.png?raw=true) +*Default wing shape and color* -Start by forking and then cloning [this repository](https://github.com/CIS700-Procedural-Graphics/Project2-Toolbox-Functions) +![Alt text](references/wing2.png?raw=true) +*Wing with wind displacement turned on* -## Modeling +## Project Description -##### Reference images +I procedurally generated and animated a model of a bird wing. -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. +## Project Features -##### Make wing curve +The basic wing model is generated from three separate spline curves in the XZ direction. Each curve is offset in the Y direction by a cosine function based on its position. Feathers are apppended to each curve, and their size is based on the position on the curve. The wing moves with a function which rotates each feather based on time. -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. +Interactive Features Include: -##### Distribute feathers +a. Wing shape -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. +b. Feather size -Feel free to diversify your wings by using multiple base feather models. +c. Feather color -## Animation +d. Feather orientation -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. +e. Flapping speed -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 +f. Flapping motion 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/1_5015007473_48b3eea88d_o.jpg b/references/1_5015007473_48b3eea88d_o.jpg new file mode 100644 index 0000000..746f5f5 Binary files /dev/null and b/references/1_5015007473_48b3eea88d_o.jpg differ diff --git a/references/images.jpg b/references/images.jpg new file mode 100644 index 0000000..d9e3fe7 Binary files /dev/null and b/references/images.jpg differ diff --git a/references/wing1.png b/references/wing1.png new file mode 100644 index 0000000..0e6e8a8 Binary files /dev/null and b/references/wing1.png differ diff --git a/references/wing2.png b/references/wing2.png new file mode 100644 index 0000000..342c35f Binary files /dev/null and b/references/wing2.png differ diff --git a/src/main.js b/src/main.js index fd8fbd4..2b6b48e 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' +// create new feather material + var featherMaterial = new THREE.ShaderMaterial({ + uniforms: { + time: { value: 0.0 }, + u_persistance: { value : 0.7 }, + u_color: {value : 0.0 }, + }, + vertexShader: require('./shaders/feather-vert.glsl'), + fragmentShader: require('./shaders/feather-frag.glsl') + }); + +// GUI FOR: +// Wing speed, curvature of the wing's basic shape, feather size, +// Feather color, Feather orientation, Flapping speed, Flapping motion +var guiItems = function() { + this.wind = 0.0; + this.shape = 4.0; + this.size = 1.0; + this.distribution = 2; + this.color = 0.5; + this.orientation = 0.0; + this.speed = 0.0; + this.flapping = 1.0; +}; + +// Uniform time to be sent to shader +var totalTime = 0.0; + +// adjust the feather distribution by changing ratio of distance between each +// todo: make these two variable user adjustable +// var finalFeatherCollection = []; +var featherDistrib = 2.0; +var featherSize = 3.0; +var wingFlap = 1.0; +var windStrength = 0.0; +var wingShape = 4.0; +//----------------------------------------------------------------------------- +// toolbox functions +function lerp(a, b, t) { + return a * (1- t) + b * t; +} + +function smoothstep(a, b, x) { + x = clamp((x - a) / (b - a), 0.0, 1.0); + return x * x * (3 - 2 * x); +} + +// c = centering +// w = taper length +function cubicPulse(c, w, x) { + x = Math.abs(x - c); + if (x > w) { return 0.0; } + x /= w; + return 1.0 - x * x * (3.0 - 2.0 * x); +} +//----------------------------------------------------------------------------- // called after the scene loads function onLoad(framework) { var scene = framework.scene; @@ -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', @@ -31,23 +87,115 @@ function onLoad(framework) { urlPrefix + 'pz.jpg', urlPrefix + 'nz.jpg' ] ); + // take out skybox temporarily here: scene.background = skymap; // load a simple obj mesh var objLoader = new THREE.OBJLoader(); - objLoader.load('/geo/feather.obj', function(obj) { + objLoader.load('geo/feather.obj', function(obj) { + // Project2-Toolbox-Functions/blob/master/geo/feather.obj // LOOK: This function runs after the obj has finished loading var featherGeo = obj.children[0].geometry; + // var featherOrientation = 0.0; + + // add feathers based on the spline positions + for (var i = 0; i < 100; i ++) { + // spline 1 + var feath = new THREE.Mesh(featherGeo, featherMaterial); + feath.position.set(splinePoints1[i * featherDistrib].x, + splinePoints1[i * featherDistrib].y, + splinePoints1[i * featherDistrib].z); + var featherOrientation = lerp(-0.5,-3.0, feath.position.z / 50.0); + feath.name = "" + i; + var dSize = (100 - i) / 100.0; + feath.scale.set(featherSize * dSize,featherSize,featherSize); + feath.rotation.set(0, featherOrientation , 0.1); + scene.add(feath); + + // spline 2 + var feath2 = new THREE.Mesh(featherGeo, featherMaterial); + feath2.position.set(splinePoints2[i * featherDistrib].x , + splinePoints2[i * featherDistrib].y - 0.3, + splinePoints2[i * featherDistrib].z); + var featherOrientation = lerp(-0.5, -3.0, feath2.position.z / 50.0); + feath2.name = "" + (100 + i); + feath2.scale.set(featherSize * 1.4,featherSize * 1.4, featherSize * 1.4) ; + feath2.rotation.set(0, featherOrientation, 0.1); + scene.add(feath2); - var featherMesh = new THREE.Mesh(featherGeo, lambertWhite); - featherMesh.name = "feather"; - scene.add(featherMesh); + // spline 3 + var feath3 = new THREE.Mesh(featherGeo, featherMaterial); + feath3.position.set(splinePoints3[i * featherDistrib].x , + splinePoints3[i * featherDistrib].y - 0.7, + splinePoints3[i * featherDistrib].z); + var featherOrientation = lerp(-0.5,-3.0, feath3.position.z / 50.0); + feath3.name = "" + (200 + i); + feath3.scale.set(featherSize * 1.4,featherSize * 1.4, featherSize * 1.4) ; + feath3.rotation.set(0 , featherOrientation , 0.1); + scene.add(feath3); + } }); +// user data +//----------------------------------------------------------------------------- +// CREATE BASIC CURVE +var numPoints = 100; + +// first spline +var spline1 = new THREE.SplineCurve3([ + new THREE.Vector3(0,0,0), + new THREE.Vector3(1,0,-2), + new THREE.Vector3(3,0,-15), + new THREE.Vector3(12,0,-30) +]); + +var geometry = new THREE.Geometry(); +var splinePoints1 = spline1.getPoints(numPoints); + +for(var i = 0; i < splinePoints1.length; i++){ + geometry.vertices.push(splinePoints1[i]); +} +// var line = new THREE.Line(geometry, lambertWhite); +// scene.add(line); + +// second spline +var spline2 = new THREE.SplineCurve3([ + new THREE.Vector3(0,0,1), + new THREE.Vector3(4,0,-15), + new THREE.Vector3(13,0,-30) +]); + +var geometry2 = new THREE.Geometry(); +var splinePoints2 = spline2.getPoints(numPoints); + +for(var i = 0; i < splinePoints2.length; i++){ + geometry2.vertices.push(splinePoints2[i]); +} +// line = new THREE.Line(geometry2, lambertWhite); +// scene.add(line); + +// third spline +var spline3 = new THREE.SplineCurve3([ + new THREE.Vector3(2,0,0), + new THREE.Vector3(8,0,-22) +]); +var geometry3 = new THREE.Geometry(); +var splinePoints3 = spline3.getPoints(numPoints); + +for(var i = 0; i < splinePoints3.length; i++){ + geometry3.vertices.push(splinePoints3[i]); +} +// line = new THREE.Line(geometry3, lambertWhite); +// scene.add(line); + +// include an axis for visualization help +// var axis = new THREE.AxisHelper(75); +// scene.add(axis); +//----------------------------------------------------------------------------- // set camera position - camera.position.set(0, 1, 5); - camera.lookAt(new THREE.Vector3(0,0,0)); + camera.position.set(1, 10, 10); + camera.lookAt(new THREE.Vector3(4,0,-5)); // scene.add(lambertCube); scene.add(directionalLight); @@ -57,17 +205,84 @@ function onLoad(framework) { gui.add(camera, 'fov', 0, 180).onChange(function(newVal) { camera.updateProjectionMatrix(); }); + + // GUI Items + var items = new guiItems(); + gui.add(items, "wind", 0.0, 3.0).onChange(function(newVal) { + windStrength = newVal; + }); + + gui.add(items, "shape", 4.0, 10.0).onChange(function(newVal) { + wingShape = newVal; + }); + + gui.add(items, "color", -1.0, 1.0).onChange(function(newVal) { + featherMaterial.uniforms.u_color.value = newVal; + }); + + gui.add(items, "size", 1.0, 5.0).onChange(function(newVal) { + // featherSize = newVal; + for (var i = 0; i < 300; i++) { + var feather = framework.scene.getObjectByName("" + i); + if (feather !== undefined) { + feather.scale.set(newVal, newVal, newVal); + } + } + }); + + gui.add(items, "orientation", -1.0, 1.0).onChange(function(newVal) { + for (var i = 0; i < 300; i++) { + var feather = framework.scene.getObjectByName("" + i); + if (feather !== undefined) { + feather.rotation.set(0, newVal,0); + } + } + }); + + gui.add(items, "flapping", 0.0, 5.0).onChange(function(newVal) { + wingFlap = newVal; + }); } // 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); - } +function onUpdate(framework) { + // move the entire wing based on simple animation + var date = new Date(); + var x = date.getTime(); + for (var i = 0; i < 300; i++) { + var feather = framework.scene.getObjectByName("" + i); + if (feather !== undefined) { + // set the y positions of the feathers + if (i < 100) { + feather.position.set(feather.position.x, + - Math.cos(feather.position.z / wingShape + 2.0) + 0.1, + feather.position.z); + + } else if (i < 200) { + feather.position.set(feather.position.x, + - Math.cos(feather.position.z / wingShape + 2.0) - 0.2, + feather.position.z); + + } else { + feather.position.set(feather.position.x, + - Math.cos(feather.position.z / wingShape + 2.0) - 0.7, + feather.position.z); + } + + // Define animation of the wing: + // render without any wind + if (windStrength < 1.0) { + feather.rotateZ(Math.sin(x / 200.0) * - (1.0 * wingFlap) * Math.PI / 180 ); + } + // render with wind + else { + var displace = Math.sin(x * windStrength / (i % 100.0) * 200.0) * -(1.0 * 2.0) * Math.PI / 90; + feather.rotateY(displace); + } + } + } } // 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); +//----------------------------------------------------------------------------- diff --git a/src/shaders/feather-frag.glsl b/src/shaders/feather-frag.glsl new file mode 100644 index 0000000..4e5b15b --- /dev/null +++ b/src/shaders/feather-frag.glsl @@ -0,0 +1,34 @@ +varying vec2 vUv; +varying vec3 vNormal; +varying float vNoiseValue; +varying float noise; + +varying vec3 vPos; + +uniform sampler2D image; +uniform float u_color; + +varying float facingRatio; + +float lerp(float a, float b, float t) { + return a * (1.0 - t) + b * t; +} + +void main() { + vec3 turq = vec3(0.407, 0.694, 0.972); + vec3 coral = vec3(0.980, 0.6, 0.6); + vec3 navy = vec3(0.019, 0.196, 0.678); + float x = lerp(navy.x, turq.x, vPos.x); + // float y = lerp(navy.y, turq.y, vPos.y); + // float z = lerp(navy.z, turq.z, vPos.z); + + // Pretty blue : 0.607, 0.909, 0.898 + vec4 baseCol = vec4(turq.x + vNormal.x / 10.0 , turq.y + vNormal.y / 10.0, turq.z + vNormal.z / 10.0 , 1.0); + + vec4 lerpCol = vec4(.5 * (1.0 - u_color), x + .2 ,x + .4,1.0); + + // Color based on surface normals + // vec4 color = vec4(vNormal.x, vNormal.y, vNormal.z, 1.0); + gl_FragColor = lerpCol * .5 + baseCol * .5; + +} \ No newline at end of file diff --git a/src/shaders/feather-vert.glsl b/src/shaders/feather-vert.glsl new file mode 100644 index 0000000..b572d82 --- /dev/null +++ b/src/shaders/feather-vert.glsl @@ -0,0 +1,38 @@ +varying vec2 vUv; +varying vec3 vNormal; +varying vec3 vPos; +varying float vNoiseValue; +uniform float time; +uniform float u_persistance; + +varying float facingRatio; + +void main() { + + // iradescence + // vec3 N = gl_NormalMatrix * gl_Normal; //vertex normal vector + // vec3 V = gl_ModelViewMatrix * gl_Vertex; //eye to vertex vector + // vec3 E = normalize(-V); //normalized vertex to eye vector + // facingRatio = dot(N, E); //facing ratio + + vUv = uv; + vNormal = normal; + vPos = position; + + // alter positions based on noise function + float timeMod = time / 300.0; + // float noiseHeight = fbm( + // float(position.x) + timeMod, + // float(position.y) + timeMod, + // float(position.z) + timeMod); + // vec3 noisyPosition = (vec3( + // position.x + noiseHeight / 100.0 + normal.x * noiseHeight , + // position.y + noiseHeight / 100.0 + normal.y * noiseHeight , + // position.z + noiseHeight / 100.0 + normal.z * noiseHeight)); + // vNoiseValue = noiseHeight; + // gl_Position = projectionMatrix * modelViewMatrix * vec4( noisyPosition, 1.0 ); + + // display the regular position + gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 ); + +} \ No newline at end of file