From 0b1a6bb821201ff485d2bf7c4ae1bbc930e396ad Mon Sep 17 00:00:00 2001 From: Joe Klinger Date: Mon, 30 Jan 2017 20:33:22 -0500 Subject: [PATCH 1/4] Added more feathers to the wing --- src/main.js | 77 ++++++++++++++++---- src/toolbox_functions.js | 151 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 215 insertions(+), 13 deletions(-) create mode 100644 src/toolbox_functions.js diff --git a/src/main.js b/src/main.js index fd8fbd4..01dae2d 100755 --- a/src/main.js +++ b/src/main.js @@ -3,6 +3,9 @@ const THREE = require('three'); // older modules are imported like this. You shouldn't have to worry about this much import Framework from './framework' +import {layer1, layer2, layer3, layer4} from './toolbox_functions' + +var numFeathers; //set after loading in the feather obj during the callback function // called after the scene loads function onLoad(framework) { @@ -13,7 +16,10 @@ function onLoad(framework) { var stats = framework.stats; // Basic Lambert white - var lambertWhite = new THREE.MeshLambertMaterial({ color: 0xaaaaaa, side: THREE.DoubleSide }); + var physGold = new THREE.MeshPhysicalMaterial({ color: 0xFFD700, side: THREE.DoubleSide }); + var physGreen = new THREE.MeshPhysicalMaterial({ color: 0xDDFF00, side: THREE.DoubleSide }); + var physBlue = new THREE.MeshPhysicalMaterial({ color: 0x0063Af, side: THREE.DoubleSide }); + var physRed = new THREE.MeshPhysicalMaterial({ color: 0xDD0000, side: THREE.DoubleSide }); // Set light var directionalLight = new THREE.DirectionalLight( 0xffffff, 1 ); @@ -32,19 +38,61 @@ function onLoad(framework) { ] ); scene.background = skymap; - + + //Add multiple copies of the feather mesh into the scene upon loading the obj + //https://github.com/mrdoob/three.js/blob/master/examples/webgl_performance.html#L56 + // 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 featherMesh = new THREE.Mesh(featherGeo, lambertWhite); - featherMesh.name = "feather"; - scene.add(featherMesh); + + //Choose how densely populated the first layer should be + var layer1Num = 52; + + //Create the first layer of feathers(along the arm) + for(var i = 0; i < layer1Num; i++) { + var featherMesh = new THREE.Mesh(featherGeo, physGold); + featherMesh.name = "feather_" + i; + layer1(featherMesh, i, layer1Num); + scene.add(featherMesh); + } + + //Continue for other layers + var layer2Num = 100; + + for(var j = 0; j < layer2Num; j++) { + var featherMesh = new THREE.Mesh(featherGeo, physGreen); + featherMesh.name = "feather_" + (j + i); + layer2(featherMesh, j, layer2Num); + scene.add(featherMesh); + } + + //Continue for other layers + var layer3Num = 150; + + for(var k = 0; k < layer3Num; k++) { + var featherMesh = new THREE.Mesh(featherGeo, physBlue); + featherMesh.name = "feather_" + (k + j + i); + layer3(featherMesh, k, layer3Num); + scene.add(featherMesh); + } + + //Continue for other layers + var layer4Num = 25; + + for(var l = 0; l < layer4Num; l++) { + var featherMesh = new THREE.Mesh(featherGeo, physRed); + featherMesh.name = "feather_" + (l + k + j + i); + layer4(featherMesh, l, layer4Num); + scene.add(featherMesh); + } + + numFeathers = i + j + k + l; }); - + // set camera position camera.position.set(0, 1, 5); camera.lookAt(new THREE.Vector3(0,0,0)); @@ -61,12 +109,15 @@ function onLoad(framework) { // 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); - } + for(var i = 0; i < numFeathers; i++) { + var currentFeather = framework.scene.getObjectByName("feather_" + i); + + //Flap the feather + if (currentFeather !== undefined) { + var date = new Date(); + //currentFeather.rotateZ(Math.sin(date.getTime() / 100) * 2 * Math.PI / 180); + } + } } // when the scene is done initializing, it will call onLoad, then on frame updates, call onUpdate diff --git a/src/toolbox_functions.js b/src/toolbox_functions.js new file mode 100644 index 0000000..e4c8f7b --- /dev/null +++ b/src/toolbox_functions.js @@ -0,0 +1,151 @@ +const THREE = require('three'); + +function lerp(a, b, w) { + return a * (1 - w) + b * (w); +} + +function jitter(numToJitter, magnitudeOfJitter) { + //return a random number on [-0.5, 0.5), of the given magnitude, added to the desired number + return (Math.random() - 0.5) * magnitudeOfJitter + numToJitter; +} + +export function layer1(featherMesh, i, numFeathersInlayer) { + //Positions + var startXPos = 0; + var endXPos = 2; + + var startZPos = 0; + var endZPos = 16; + + //Rotations + var startRotY = Math.PI / 2; + var endRotY = Math.PI; + + //Weight reltive to this layer + var weight = i / numFeathersInlayer; //normalize to [0, 1) + + //lerp the positions and rotations + featherMesh.position.x += lerp(startXPos, endXPos, Math.pow(weight, 2.1)); + featherMesh.position.z += lerp(startZPos, endZPos, Math.pow(weight, 0.5)); + featherMesh.rotateY(lerp(startRotY, endRotY, weight)); + + //scale down immensely for the first layer - need to change the positions + //or it will be more spread out + //for now, lerp the scale quadratically- small to Big + var startScale = 2; + var endScale = 0.25; + var scaleVal = lerp(startScale, endScale, Math.pow(weight, 3)); + featherMesh.scale.set(scaleVal, scaleVal, scaleVal); + + //Jitter the feather position along the y-axis a little + featherMesh.position.y += jitter(0, 0.01); + + //Jitter the red component of the color a little, cloning each matieral per feather + var m = featherMesh.material.clone(); + featherMesh.material = m; + featherMesh.material.color.add(new THREE.Color(jitter(0.75, 0.75), jitter(0.5, 0.5), 0)); +} + +export function layer2(featherMesh, j, numFeathersInlayer) { + var startXPos2 = 0; + var endXPos2 = 2; + + var startZPos2 = 0; + var endZPos2 = 16; + var weight = j / numFeathersInlayer; + + var startRotY = Math.PI / 2 - 0.1; + var endRotY = Math.PI; + + var startScale2 = 1.1; + var endScale2 = 0.05; + var scaleVal = lerp(startScale2, endScale2, Math.pow(weight, 3)); + featherMesh.scale.set(scaleVal, scaleVal, scaleVal); + + //place a second layer of smaller feather into the wing with jittered y-value positions + featherMesh.position.x += lerp(startXPos2, endXPos2, Math.pow(weight, 2.1)); + featherMesh.position.z += lerp(startZPos2, endZPos2, Math.pow(weight, 0.5)); + featherMesh.rotateY(lerp(startRotY, endRotY, weight)); + + //Jitter the feather position along the y-axis a little + featherMesh.position.y += jitter(0.05, 0.05); + + //Jitter the red component of the color a little, cloning each matieral per feather + var m = featherMesh.material.clone(); + featherMesh.material = m; + featherMesh.material.color.add(new THREE.Color(jitter(0, 0.2), jitter(0, 0.2), 0)); +} + +export function layer3(featherMesh, k, numFeathersInlayer) { + var startXPos2 = 0; + var endXPos2 = 2; + + var startZPos2 = 0; + var endZPos2 = 16; + var weight = k / numFeathersInlayer; + + var startRotY = Math.PI / 2 - 0.1; + var endRotY = Math.PI; + + var startScale2 = 0.5; + var endScale2 = 0.02; + var scaleVal = lerp(startScale2, endScale2, Math.pow(weight, 3)); + featherMesh.scale.set(scaleVal, scaleVal, scaleVal); + + //place a second layer of smaller feather into the wing with jittered y-value positions + featherMesh.position.x += lerp(startXPos2, endXPos2, Math.pow(weight, 2.1)); + featherMesh.position.z += lerp(startZPos2, endZPos2, Math.pow(weight, 0.5)); + featherMesh.rotateY(lerp(startRotY, endRotY, weight)); + + //Jitter the feather position along the y-axis a little + featherMesh.position.y += 0.1;//jitter(0.08, 0); + + //Jitter the color a little, cloning each matieral per feather + var m = featherMesh.material.clone(); + featherMesh.material = m; + featherMesh.material.color.add(new THREE.Color(0, 0, jitter(0, 0.2))); +} + +export function layer4(featherMesh, l, numFeathersInlayer) { + var startXPos2 = -0.5; + var endXPos2 = 2; + + var startZPos2 = -2; + var endZPos2 = 16.1; + var weight = l / numFeathersInlayer; + + var startRotY = Math.PI / 2; + var endRotY = Math.PI; + + + var startScale2 = 2.5; + var endScale2 = 1.5; + var scaleVal = lerp(startScale2, endScale2, Math.pow(weight, 3)); + featherMesh.scale.set(scaleVal, scaleVal, scaleVal); + + //place a second layer of smaller feather into the wing with jittered y-value positions + featherMesh.position.x += lerp(startXPos2, endXPos2, Math.pow(weight, 2.1)); + featherMesh.position.z += lerp(startZPos2, endZPos2, Math.pow(weight, 0.5)); + featherMesh.rotateY(lerp(startRotY, endRotY, weight)); + + //Jitter the feather position along the y-axis a little + featherMesh.position.y += -0.08;//jitter(-0.05, 0.00); + + //Jitter the color a little, cloning each matieral per feather + var m = featherMesh.material.clone(); + featherMesh.material = m; + featherMesh.material.color.add(new THREE.Color(0, jitter(0, 0.5), 0)); +} + +/* +// http://stackoverflow.com/questions/12537141/how-to-shear-a-cube-in-three-js +export function shear(mesh) { + var shearMatrix = new THREE.Matrix4(); + + shearMatrix.set ( 1, Syx, Szx, 0, + Sxy, 1, Szx, 0, + 1, Syx, 1, 0, + 1, Syx, Szx, 1 ); + +} +*/ \ No newline at end of file From 14dfaa18af14f0fa43c699bd77553ed67f9c4c10 Mon Sep 17 00:00:00 2001 From: Joe Klinger Date: Tue, 31 Jan 2017 11:32:27 -0500 Subject: [PATCH 2/4] Added some gui functionality --- src/main.js | 78 +++++++++++++++++++++++++++++----------- src/toolbox_functions.js | 48 ++++++++++++------------- 2 files changed, 82 insertions(+), 44 deletions(-) diff --git a/src/main.js b/src/main.js index 01dae2d..1f04f2d 100755 --- a/src/main.js +++ b/src/main.js @@ -3,10 +3,39 @@ const THREE = require('three'); // older modules are imported like this. You shouldn't have to worry about this much import Framework from './framework' -import {layer1, layer2, layer3, layer4} from './toolbox_functions' +import {layer1, layer2, layer3, layer4, updatePosition} from './toolbox_functions' var numFeathers; //set after loading in the feather obj during the callback function +//Choose how densely populated the first layer should be +var layer1Num = 52; +var layer2Num = 100; +var layer3Num = 150; +var layer4Num = 25; + +function updatePositions(framework, exponent) { + for(var i = 0; i < layer1Num; i++) { + var currentFeather = framework.scene.getObjectByName("feather_" + i); + //console.log(currentFeather); + updatePosition(currentFeather, i, layer1Num, exponent); + } + + for(var j = 0; j < layer2Num; j++) { + var currentFeather = framework.scene.getObjectByName("feather_" + (i + j)); + updatePosition(currentFeather, j, layer2Num, exponent); + } + + for(var k = 0; k < layer3Num; k++) { + var currentFeather = framework.scene.getObjectByName("feather_" + (i + j + k)); + updatePosition(currentFeather, k, layer3Num, exponent); + } + + for(var l = 0; l < layer4Num; l++) { + var currentFeather = framework.scene.getObjectByName("feather_" + (i + j + k + l)); + updatePosition(currentFeather, l, layer4Num, exponent); + } +} + // called after the scene loads function onLoad(framework) { var scene = framework.scene; @@ -42,6 +71,23 @@ function onLoad(framework) { //Add multiple copies of the feather mesh into the scene upon loading the obj //https://github.com/mrdoob/three.js/blob/master/examples/webgl_performance.html#L56 + /* GUI related parameters */ + + //create methods for each of these, have to iterate over each feather using + //get object by name and start with the corresponding integer offset + var interpolationExponent = { value: 0.5 }; //This is NOT per layer. changes how the position of the feathers is interpolated, default to 2, as it is now. Have to alter every feather's position. + + //this one is for all feathers at once + var curvature; //have all feathers uniformly scale up their y-axis position, which should be quadratically lerped. first 3 layers default 0 to 0, fourth has a lil offset. + + var x_orientation, y_orientation, z_orientation; //This is per layer. rotate each individual feather by what ever amount + + var color_red_component, color_green_component, color_blue_component; //Per layer - alter the material color of each feather's material by whatever newVal + + var flappingSpeed; //This goes into the onUpdate function and scale up/down the sin function. + + var flappingMotion; //Like above. scales up/down the amplitude of the sin function. + // load a simple obj mesh var objLoader = new THREE.OBJLoader(); objLoader.load('/geo/feather.obj', function(obj) { @@ -49,44 +95,32 @@ function onLoad(framework) { // LOOK: This function runs after the obj has finished loading var featherGeo = obj.children[0].geometry; - //Choose how densely populated the first layer should be - var layer1Num = 52; - - //Create the first layer of feathers(along the arm) + //Create the first layer of feathers- actually the 3rd layer if counting outwards for(var i = 0; i < layer1Num; i++) { var featherMesh = new THREE.Mesh(featherGeo, physGold); featherMesh.name = "feather_" + i; - layer1(featherMesh, i, layer1Num); + layer1(featherMesh, i, layer1Num, 0.5); scene.add(featherMesh); } - //Continue for other layers - var layer2Num = 100; - for(var j = 0; j < layer2Num; j++) { var featherMesh = new THREE.Mesh(featherGeo, physGreen); featherMesh.name = "feather_" + (j + i); - layer2(featherMesh, j, layer2Num); + layer2(featherMesh, j, layer2Num, 0.5); scene.add(featherMesh); } - //Continue for other layers - var layer3Num = 150; - for(var k = 0; k < layer3Num; k++) { var featherMesh = new THREE.Mesh(featherGeo, physBlue); featherMesh.name = "feather_" + (k + j + i); - layer3(featherMesh, k, layer3Num); + layer3(featherMesh, k, layer3Num, 0.5); scene.add(featherMesh); - } - - //Continue for other layers - var layer4Num = 25; + } for(var l = 0; l < layer4Num; l++) { var featherMesh = new THREE.Mesh(featherGeo, physRed); featherMesh.name = "feather_" + (l + k + j + i); - layer4(featherMesh, l, layer4Num); + layer4(featherMesh, l, layer4Num, 0.5); scene.add(featherMesh); } @@ -105,6 +139,10 @@ function onLoad(framework) { gui.add(camera, 'fov', 0, 180).onChange(function(newVal) { camera.updateProjectionMatrix(); }); + + gui.add(interpolationExponent, 'value', 0.1, 5).onChange(function(newVal) { + updatePositions(framework, newVal); + }); } // called on frame updates @@ -115,7 +153,7 @@ function onUpdate(framework) { //Flap the feather if (currentFeather !== undefined) { var date = new Date(); - //currentFeather.rotateZ(Math.sin(date.getTime() / 100) * 2 * Math.PI / 180); + currentFeather.rotateZ(Math.sin(date.getTime() / 100) * 2 * Math.PI / 180); } } } diff --git a/src/toolbox_functions.js b/src/toolbox_functions.js index e4c8f7b..08ddc70 100644 --- a/src/toolbox_functions.js +++ b/src/toolbox_functions.js @@ -9,7 +9,7 @@ function jitter(numToJitter, magnitudeOfJitter) { return (Math.random() - 0.5) * magnitudeOfJitter + numToJitter; } -export function layer1(featherMesh, i, numFeathersInlayer) { +export function layer1(featherMesh, i, numFeathersInlayer, pos_exponent) { //Positions var startXPos = 0; var endXPos = 2; @@ -26,7 +26,7 @@ export function layer1(featherMesh, i, numFeathersInlayer) { //lerp the positions and rotations featherMesh.position.x += lerp(startXPos, endXPos, Math.pow(weight, 2.1)); - featherMesh.position.z += lerp(startZPos, endZPos, Math.pow(weight, 0.5)); + featherMesh.position.z += lerp(startZPos, endZPos, Math.pow(weight, pos_exponent)); featherMesh.rotateY(lerp(startRotY, endRotY, weight)); //scale down immensely for the first layer - need to change the positions @@ -46,7 +46,7 @@ export function layer1(featherMesh, i, numFeathersInlayer) { featherMesh.material.color.add(new THREE.Color(jitter(0.75, 0.75), jitter(0.5, 0.5), 0)); } -export function layer2(featherMesh, j, numFeathersInlayer) { +export function layer2(featherMesh, j, numFeathersInlayer, pos_exponent) { var startXPos2 = 0; var endXPos2 = 2; @@ -64,7 +64,7 @@ export function layer2(featherMesh, j, numFeathersInlayer) { //place a second layer of smaller feather into the wing with jittered y-value positions featherMesh.position.x += lerp(startXPos2, endXPos2, Math.pow(weight, 2.1)); - featherMesh.position.z += lerp(startZPos2, endZPos2, Math.pow(weight, 0.5)); + featherMesh.position.z += lerp(startZPos2, endZPos2, Math.pow(weight, pos_exponent)); featherMesh.rotateY(lerp(startRotY, endRotY, weight)); //Jitter the feather position along the y-axis a little @@ -76,7 +76,7 @@ export function layer2(featherMesh, j, numFeathersInlayer) { featherMesh.material.color.add(new THREE.Color(jitter(0, 0.2), jitter(0, 0.2), 0)); } -export function layer3(featherMesh, k, numFeathersInlayer) { +export function layer3(featherMesh, k, numFeathersInlayer, pos_exponent) { var startXPos2 = 0; var endXPos2 = 2; @@ -94,11 +94,11 @@ export function layer3(featherMesh, k, numFeathersInlayer) { //place a second layer of smaller feather into the wing with jittered y-value positions featherMesh.position.x += lerp(startXPos2, endXPos2, Math.pow(weight, 2.1)); - featherMesh.position.z += lerp(startZPos2, endZPos2, Math.pow(weight, 0.5)); + featherMesh.position.z += lerp(startZPos2, endZPos2, Math.pow(weight, pos_exponent)); featherMesh.rotateY(lerp(startRotY, endRotY, weight)); //Jitter the feather position along the y-axis a little - featherMesh.position.y += 0.1;//jitter(0.08, 0); + featherMesh.position.y += 0.12;//jitter(0.08, 0); //Jitter the color a little, cloning each matieral per feather var m = featherMesh.material.clone(); @@ -106,12 +106,15 @@ export function layer3(featherMesh, k, numFeathersInlayer) { featherMesh.material.color.add(new THREE.Color(0, 0, jitter(0, 0.2))); } -export function layer4(featherMesh, l, numFeathersInlayer) { - var startXPos2 = -0.5; +export function layer4(featherMesh, l, numFeathersInlayer, pos_exponent) { + var startXPos2 = -0.25; var endXPos2 = 2; - var startZPos2 = -2; - var endZPos2 = 16.1; + var startYPos2 = 0; + var endYPos2 = 0; + + var startZPos2 = 0; + var endZPos2 = 16; var weight = l / numFeathersInlayer; var startRotY = Math.PI / 2; @@ -125,7 +128,8 @@ export function layer4(featherMesh, l, numFeathersInlayer) { //place a second layer of smaller feather into the wing with jittered y-value positions featherMesh.position.x += lerp(startXPos2, endXPos2, Math.pow(weight, 2.1)); - featherMesh.position.z += lerp(startZPos2, endZPos2, Math.pow(weight, 0.5)); + featherMesh.position.y+= lerp(startYPos2, endYPos2, Math.pow(weight, pos_exponent)); + featherMesh.position.z += lerp(startZPos2, endZPos2, Math.pow(weight, pos_exponent)); featherMesh.rotateY(lerp(startRotY, endRotY, weight)); //Jitter the feather position along the y-axis a little @@ -137,15 +141,11 @@ export function layer4(featherMesh, l, numFeathersInlayer) { featherMesh.material.color.add(new THREE.Color(0, jitter(0, 0.5), 0)); } -/* -// http://stackoverflow.com/questions/12537141/how-to-shear-a-cube-in-three-js -export function shear(mesh) { - var shearMatrix = new THREE.Matrix4(); - - shearMatrix.set ( 1, Syx, Szx, 0, - Sxy, 1, Szx, 0, - 1, Syx, 1, 0, - 1, Syx, Szx, 1 ); - -} -*/ \ No newline at end of file +export function updatePosition(featherMesh, i, numFeathersInLayer, position_exponent) { + var startZPos = 0; + var endZPos = 16; + var weight = i / numFeathersInLayer; + //featherMesh.position.set(featherMesh.position.x, featherMesh.position.y, lerp(startZPos, endZPos, Math.pow(weight, position_exponent))); + featherMesh.position.z = lerp(startZPos, endZPos, Math.pow(weight, position_exponent)); + //console.log(position_exponent); +} \ No newline at end of file From 9b5c7e0eeb273aecec8dd0323eb12c78c6ced6c0 Mon Sep 17 00:00:00 2001 From: Joe Klinger Date: Tue, 31 Jan 2017 14:05:24 -0500 Subject: [PATCH 3/4] added for gui functionality --- src/main.js | 47 +++++++++++++++++++++++++++++++--------- src/toolbox_functions.js | 9 +++++++- 2 files changed, 45 insertions(+), 11 deletions(-) diff --git a/src/main.js b/src/main.js index 1f04f2d..221688f 100755 --- a/src/main.js +++ b/src/main.js @@ -3,7 +3,7 @@ const THREE = require('three'); // older modules are imported like this. You shouldn't have to worry about this much import Framework from './framework' -import {layer1, layer2, layer3, layer4, updatePosition} from './toolbox_functions' +import {layer1, layer2, layer3, layer4, updateYPosition, updateZPosition} from './toolbox_functions' var numFeathers; //set after loading in the feather obj during the callback function @@ -13,26 +13,49 @@ var layer2Num = 100; var layer3Num = 150; var layer4Num = 25; -function updatePositions(framework, exponent) { +function updateYPositions(framework, curvatureAmount) { for(var i = 0; i < layer1Num; i++) { var currentFeather = framework.scene.getObjectByName("feather_" + i); //console.log(currentFeather); - updatePosition(currentFeather, i, layer1Num, exponent); + updateYPosition(currentFeather, i, layer1Num, curvatureAmount); } for(var j = 0; j < layer2Num; j++) { var currentFeather = framework.scene.getObjectByName("feather_" + (i + j)); - updatePosition(currentFeather, j, layer2Num, exponent); + updateYPosition(currentFeather, j, layer2Num, curvatureAmount); } for(var k = 0; k < layer3Num; k++) { var currentFeather = framework.scene.getObjectByName("feather_" + (i + j + k)); - updatePosition(currentFeather, k, layer3Num, exponent); + updateYPosition(currentFeather, k, layer3Num, curvatureAmount); } for(var l = 0; l < layer4Num; l++) { var currentFeather = framework.scene.getObjectByName("feather_" + (i + j + k + l)); - updatePosition(currentFeather, l, layer4Num, exponent); + updateYPosition(currentFeather, l, layer4Num, curvatureAmount); + } +} + +function updateZPositions(framework, exponent) { + for(var i = 0; i < layer1Num; i++) { + var currentFeather = framework.scene.getObjectByName("feather_" + i); + //console.log(currentFeather); + updateZPosition(currentFeather, i, layer1Num, exponent); + } + + for(var j = 0; j < layer2Num; j++) { + var currentFeather = framework.scene.getObjectByName("feather_" + (i + j)); + updateZPosition(currentFeather, j, layer2Num, exponent); + } + + for(var k = 0; k < layer3Num; k++) { + var currentFeather = framework.scene.getObjectByName("feather_" + (i + j + k)); + updateZPosition(currentFeather, k, layer3Num, exponent); + } + + for(var l = 0; l < layer4Num; l++) { + var currentFeather = framework.scene.getObjectByName("feather_" + (i + j + k + l)); + updateZPosition(currentFeather, l, layer4Num, exponent); } } @@ -75,10 +98,10 @@ function onLoad(framework) { //create methods for each of these, have to iterate over each feather using //get object by name and start with the corresponding integer offset - var interpolationExponent = { value: 0.5 }; //This is NOT per layer. changes how the position of the feathers is interpolated, default to 2, as it is now. Have to alter every feather's position. + var interpolationExponent = { distribution: 0.5 }; //This is NOT per layer. changes how the position of the feathers is interpolated, default to 2, as it is now. Have to alter every feather's position. //this one is for all feathers at once - var curvature; //have all feathers uniformly scale up their y-axis position, which should be quadratically lerped. first 3 layers default 0 to 0, fourth has a lil offset. + var curvature = { curvature : 0 }; //have all feathers uniformly scale up their y-axis position, which should be quadratically lerped. first 3 layers default 0 to 0, fourth has a lil offset. var x_orientation, y_orientation, z_orientation; //This is per layer. rotate each individual feather by what ever amount @@ -140,8 +163,12 @@ function onLoad(framework) { camera.updateProjectionMatrix(); }); - gui.add(interpolationExponent, 'value', 0.1, 5).onChange(function(newVal) { - updatePositions(framework, newVal); + gui.add(interpolationExponent, 'distribution', 0.1, 5).onChange(function(newVal) { + updateZPositions(framework, newVal); + }); + + gui.add(curvature, 'curvature', 0, 50).onChange(function(newVal) { + updateYPositions(framework, newVal); }); } diff --git a/src/toolbox_functions.js b/src/toolbox_functions.js index 08ddc70..6125676 100644 --- a/src/toolbox_functions.js +++ b/src/toolbox_functions.js @@ -141,7 +141,14 @@ export function layer4(featherMesh, l, numFeathersInlayer, pos_exponent) { featherMesh.material.color.add(new THREE.Color(0, jitter(0, 0.5), 0)); } -export function updatePosition(featherMesh, i, numFeathersInLayer, position_exponent) { +export function updateYPosition(featherMesh, i, numFeathersInLayer, curvatureAmount) { + var startYPos = -curvatureAmount; + var endYPos = 0; + var weight = i / numFeathersInLayer; + featherMesh.position.y = lerp(startYPos, endYPos, Math.pow(weight, 0.33)); +} + +export function updateZPosition(featherMesh, i, numFeathersInLayer, position_exponent) { var startZPos = 0; var endZPos = 16; var weight = i / numFeathersInLayer; From c90555f786f319ad8a338a6f74ae12827211a139 Mon Sep 17 00:00:00 2001 From: Joe Klinger Date: Tue, 31 Jan 2017 22:00:32 -0500 Subject: [PATCH 4/4] Finished --- README.md | 83 +-------------------- src/main.js | 154 ++++++++++++++++++++++++++++++++++----- src/toolbox_functions.js | 29 +++++++- 3 files changed, 166 insertions(+), 100 deletions(-) diff --git a/README.md b/README.md index 1410c30..a50635e 100644 --- a/README.md +++ b/README.md @@ -1,82 +1,5 @@ -# [Project2: Toolbox Functions](https://github.com/CIS700-Procedural-Graphics/Project2-Toolbox-Functions) +## Joseph Klinger - CIS 700 Procedural Graphics - Project 2 - Bird Wing -## Overview +This project uses various forms of interpolation (mostly quadratic and cubic) to place and displace bird feathers. -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 +The feathers are organized into 4 layers and can be transformed using the GUI parameters provided. \ No newline at end of file diff --git a/src/main.js b/src/main.js index 221688f..f1b22ba 100755 --- a/src/main.js +++ b/src/main.js @@ -3,7 +3,16 @@ const THREE = require('three'); // older modules are imported like this. You shouldn't have to worry about this much import Framework from './framework' -import {layer1, layer2, layer3, layer4, updateYPosition, updateZPosition} from './toolbox_functions' +import {layer1, layer2, layer3, layer4, updateYPosition, updateZPosition, updateXRotation, updateYRotation, updateZRotation, updateXScale, updateYScale, updateZScale, flapWing, applyWind} from './toolbox_functions' + +var featherGroup = new THREE.Object3D(); +featherGroup.name = 'feather_group'; + +var flappingSpeed = { flappingSpeedScale : 1 }; //This goes into the onUpdate function and scale up/down the sin function. + +var flappingMotion = { flappingMotionScale : 1 }; //Like above. scales up/down the amplitude of the sin function. + +var windIntensity = { wind_intensity : 0.01}; var numFeathers; //set after loading in the feather obj during the callback function @@ -13,10 +22,70 @@ var layer2Num = 100; var layer3Num = 150; var layer4Num = 25; +function ApplyWind(framework) { + applyWind(featherGroup, 0.15); +} + +function flapEachFeather(framework, flapScale, flapValue) { + for(var i = 0; i < layer1Num; i++) { + var currentFeather = framework.scene.getObjectByName("feather_" + i); + flapWing(currentFeather, i, layer1Num, flapScale, flapValue); + } + + for(var j = 0; j < layer2Num; j++) { + var currentFeather = framework.scene.getObjectByName("feather_" + (i + j)); + flapWing(currentFeather, j, layer2Num, flapScale, flapValue); + } + + for(var k = 0; k < layer3Num; k++) { + var currentFeather = framework.scene.getObjectByName("feather_" + (i + j + k)); + flapWing(currentFeather, k, layer3Num, flapScale, flapValue); + } + + for(var l = 0; l < layer4Num; l++) { + var currentFeather = framework.scene.getObjectByName("feather_" + (i + j + k + l)); + flapWing(currentFeather, l, layer4Num, flapScale, flapValue); + } +} + +function UpdateXScales(framework, scaleAmount) { + framework.scene.getObjectByName('feather_group').scale.set(scaleAmount, 1, 1); +} + +function UpdateYScales(framework, scaleAmount) { + framework.scene.getObjectByName('feather_group').scale.set(1, scaleAmount, 1); +} + +function UpdateZScales(framework, scaleAmount) { + framework.scene.getObjectByName('feather_group').scale.set(1, 1, scaleAmount); +} + + + +function updateXRotations(framework, rotAmount) { + for(var i = 0; i < numFeathers; i++) { + var currentFeather = framework.scene.getObjectByName('feather_group').children[i]; + updateXRotation(currentFeather, rotAmount); + } +} + +function updateYRotations(framework, rotAmount) { + for(var i = 0; i < numFeathers; i++) { + var currentFeather = framework.scene.getObjectByName('feather_group').children[i]; + updateYRotation(currentFeather, rotAmount); + } +} + +function updateZRotations(framework, rotAmount) { + for(var i = 0; i < numFeathers; i++) { + var currentFeather = framework.scene.getObjectByName('feather_group').children[i]; + updateZRotation(currentFeather, rotAmount); + } +} + function updateYPositions(framework, curvatureAmount) { for(var i = 0; i < layer1Num; i++) { var currentFeather = framework.scene.getObjectByName("feather_" + i); - //console.log(currentFeather); updateYPosition(currentFeather, i, layer1Num, curvatureAmount); } @@ -39,7 +108,6 @@ function updateYPositions(framework, curvatureAmount) { function updateZPositions(framework, exponent) { for(var i = 0; i < layer1Num; i++) { var currentFeather = framework.scene.getObjectByName("feather_" + i); - //console.log(currentFeather); updateZPosition(currentFeather, i, layer1Num, exponent); } @@ -59,6 +127,13 @@ function updateZPositions(framework, exponent) { } } +function UpdateColor(framework, newColor) { + for(var i = 0; i < numFeathers; i++) { + var currentFeather = framework.scene.getObjectByName('feather_group').children[i]; + currentFeather.material.color.set(new THREE.Color(newColor)); + } +} + // called after the scene loads function onLoad(framework) { var scene = framework.scene; @@ -103,13 +178,15 @@ function onLoad(framework) { //this one is for all feathers at once var curvature = { curvature : 0 }; //have all feathers uniformly scale up their y-axis position, which should be quadratically lerped. first 3 layers default 0 to 0, fourth has a lil offset. - var x_orientation, y_orientation, z_orientation; //This is per layer. rotate each individual feather by what ever amount + var x_orientation = { x_orientation : 0}; + var y_orientation = { y_orientation : 0}; + var z_orientation = { z_orientation : 0}; //This is NOT per layer. rotate each individual feather by what ever amount - var color_red_component, color_green_component, color_blue_component; //Per layer - alter the material color of each feather's material by whatever newVal + var x_scale = { x_scale : 1}; + var y_scale = { y_scale : 1}; + var z_scale = { z_scale : 1}; - var flappingSpeed; //This goes into the onUpdate function and scale up/down the sin function. - - var flappingMotion; //Like above. scales up/down the amplitude of the sin function. + var wingColor = { value : 0x000000 }; //Per layer - alter the material color of each feather's material by whatever newVal // load a simple obj mesh var objLoader = new THREE.OBJLoader(); @@ -123,30 +200,34 @@ function onLoad(framework) { var featherMesh = new THREE.Mesh(featherGeo, physGold); featherMesh.name = "feather_" + i; layer1(featherMesh, i, layer1Num, 0.5); - scene.add(featherMesh); + //scene.add(featherMesh); + featherGroup.add(featherMesh); } for(var j = 0; j < layer2Num; j++) { var featherMesh = new THREE.Mesh(featherGeo, physGreen); featherMesh.name = "feather_" + (j + i); layer2(featherMesh, j, layer2Num, 0.5); - scene.add(featherMesh); + //scene.add(featherMesh); + featherGroup.add(featherMesh); } for(var k = 0; k < layer3Num; k++) { var featherMesh = new THREE.Mesh(featherGeo, physBlue); featherMesh.name = "feather_" + (k + j + i); layer3(featherMesh, k, layer3Num, 0.5); - scene.add(featherMesh); + //scene.add(featherMesh); + featherGroup.add(featherMesh); } for(var l = 0; l < layer4Num; l++) { var featherMesh = new THREE.Mesh(featherGeo, physRed); featherMesh.name = "feather_" + (l + k + j + i); layer4(featherMesh, l, layer4Num, 0.5); - scene.add(featherMesh); + //scene.add(featherMesh); + featherGroup.add(featherMesh); } - + scene.add(featherGroup); numFeathers = i + j + k + l; }); @@ -170,19 +251,56 @@ function onLoad(framework) { gui.add(curvature, 'curvature', 0, 50).onChange(function(newVal) { updateYPositions(framework, newVal); }); + + gui.add(flappingSpeed, 'flappingSpeedScale', 0.1, 10); + gui.add(flappingMotion, 'flappingMotionScale', 0.1, 10); + + //Can't get these to work with non-integer steps + gui.add(x_orientation, 'x_orientation', 0.0, 10.0).step(0.1).listen().onChange(function(newVal) { + updateXRotations(framework, newVal); + }); + + gui.add(y_orientation, 'y_orientation', 0.0, 10.0).step(0.1).listen().onChange(function(newVal) { + updateYRotations(framework, newVal); + }); + + gui.add(z_orientation, 'z_orientation', 0.0, 10.0).step(0.1).listen().onChange(function(newVal) { + updateZRotations(framework, newVal); + }); + + gui.add(windIntensity, 'wind_intensity', 0.01, 20.1); + + gui.add(x_scale, 'x_scale', 0.5, 10.0).step(0.1).listen().onChange(function(newVal) { + UpdateXScales(framework, newVal); + }); + + gui.add(y_scale, 'y_scale', 0.5, 10.0).step(0.1).listen().onChange(function(newVal) { + UpdateYScales(framework, newVal); + }); + + gui.add(z_scale, 'z_scale', 0.5, 10.0).step(0.1).listen().onChange(function(newVal) { + UpdateZScales(framework, newVal); + }); + + gui.addColor(wingColor, 'value').name('Wing Color').onChange(function(newVal) { + //iterate over all the feathers and add this color + UpdateColor(framework, newVal); + }); } // called on frame updates function onUpdate(framework) { - for(var i = 0; i < numFeathers; i++) { - var currentFeather = framework.scene.getObjectByName("feather_" + i); + //for(var i = 0; i < numFeathers; i++) { + var featherGroup = framework.scene.getObjectByName('feather_group');//.children[i]; //Flap the feather - if (currentFeather !== undefined) { + if (featherGroup !== undefined) { var date = new Date(); - currentFeather.rotateZ(Math.sin(date.getTime() / 100) * 2 * Math.PI / 180); + //featherGroup.rotateX(flappingMotion.flappingMotionScale* Math.sin(date.getTime() * flappingSpeed.flappingSpeedScale / 100) * 2 * Math.PI / 180); + flapEachFeather(framework, flappingMotion.flappingMotionScale, Math.sin(date.getTime() * flappingSpeed.flappingSpeedScale / 1000)); + ApplyWind(framework); } - } + //} } // when the scene is done initializing, it will call onLoad, then on frame updates, call onUpdate diff --git a/src/toolbox_functions.js b/src/toolbox_functions.js index 6125676..07d9a7f 100644 --- a/src/toolbox_functions.js +++ b/src/toolbox_functions.js @@ -152,7 +152,32 @@ export function updateZPosition(featherMesh, i, numFeathersInLayer, position_exp var startZPos = 0; var endZPos = 16; var weight = i / numFeathersInLayer; - //featherMesh.position.set(featherMesh.position.x, featherMesh.position.y, lerp(startZPos, endZPos, Math.pow(weight, position_exponent))); featherMesh.position.z = lerp(startZPos, endZPos, Math.pow(weight, position_exponent)); - //console.log(position_exponent); +} + +export function updateXRotation(featherMesh, rotAmount) { + featherMesh.rotateX(rotAmount); +} + +export function updateYRotation(featherMesh, rotAmount) { + featherMesh.rotateY(rotAmount); +} + +export function updateZRotation(featherMesh, rotAmount) { + featherMesh.rotateZ(rotAmount); +} + +export function flapWing(featherMesh, i, numFeathersInLayer, flapScale, flapValue) { + var startYPos = -(flapScale / 4); + var endYPos = flapScale / 4; + var Yweight = (flapValue + 1) / 2; + var yFinalPos = lerp(startYPos, endYPos, Yweight); + var weight = i / numFeathersInLayer; + featherMesh.position.y += lerp(yFinalPos, 0, Math.pow(weight, 0.33)); +} + +export function applyWind(featherGroup, windIntensity) { + featherGroup.position.x += jitter(0, windIntensity); + featherGroup.position.y += jitter(0, windIntensity); + featherGroup.position.z += jitter(0, windIntensity); } \ No newline at end of file