Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Submission #13

Open
wants to merge 17 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 16 additions & 67 deletions README.md
Original file line number Diff line number Diff line change
@@ -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`.
f. Flapping motion
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": [
Expand Down
Binary file added references/1_5015007473_48b3eea88d_o.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added references/images.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added references/wing1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added references/wing2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
245 changes: 230 additions & 15 deletions src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -23,31 +79,123 @@ 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',
urlPrefix + 'py.jpg', urlPrefix + 'ny.jpg',
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);
Expand All @@ -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);
Framework.init(onLoad, onUpdate);
//-----------------------------------------------------------------------------
Loading