From f654bca595ce019bdaecfde52babed04e0d00108 Mon Sep 17 00:00:00 2001 From: gracexu94 Date: Tue, 31 Jan 2017 15:55:22 -0500 Subject: [PATCH 01/14] initial commit --- src/main.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main.js b/src/main.js index fd8fbd4..2a25f02 100755 --- a/src/main.js +++ b/src/main.js @@ -65,7 +65,7 @@ function onUpdate(framework) { if (feather !== undefined) { // Simply flap wing var date = new Date(); - feather.rotateZ(Math.sin(date.getTime() / 100) * 2 * Math.PI / 180); + //feather.rotateZ(Math.sin(date.getTime() / 100) * 2 * Math.PI / 180); } } From 63ef103454ab83dd427f7f0f80f04608c3971e18 Mon Sep 17 00:00:00 2001 From: gracexu94 Date: Tue, 31 Jan 2017 16:01:00 -0500 Subject: [PATCH 02/14] multiple feathers --- src/main.js | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/src/main.js b/src/main.js index 2a25f02..1b34f37 100755 --- a/src/main.js +++ b/src/main.js @@ -37,12 +37,16 @@ function onLoad(framework) { 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); + for (var i = 0; i < 20; i++) { + // 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" + i; + scene.add(featherMesh); + var feather = framework.scene.getObjectByName(featherMesh.name); + feather.rotateY(i); + } }); // set camera position From f5dda2c58ce85fb2ad699343ffbf7db1bd63196e Mon Sep 17 00:00:00 2001 From: gracexu94 Date: Tue, 31 Jan 2017 22:36:27 -0500 Subject: [PATCH 03/14] working on feather distribution --- src/main.js | 81 +++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 76 insertions(+), 5 deletions(-) diff --git a/src/main.js b/src/main.js index 1b34f37..bec4fe5 100755 --- a/src/main.js +++ b/src/main.js @@ -4,6 +4,19 @@ const THREE = require('three'); // older modules are imported like this. You shouldn't have to worry about this much import Framework from './framework' +function linearInterpolate(a, b, t) { + return a * (1 - t) + b * t; +} + +function cosineInterpolate(a, b, t) { + var cos_t = (1 - Math.cos(t * Math.PI)) * 0.5; + return linearInterpolate(a, b, cos_t); +} + +function degreesToRads(degrees) { + return Math.PI / 180.0 * degrees; +} + // called after the scene loads function onLoad(framework) { var scene = framework.scene; @@ -32,20 +45,43 @@ function onLoad(framework) { ] ); scene.background = skymap; + var curve = new THREE.CubicBezierCurve3( + new THREE.Vector3( 0, 0, 0 ), + new THREE.Vector3( 1, 0.75, 0 ), + new THREE.Vector3( 2, 3, 0 ), + new THREE.Vector3( 5, 2, 0 ) + ); + + var geometry = new THREE.Geometry(); + geometry.vertices = curve.getPoints( 50 ); + + var material = new THREE.LineBasicMaterial( { color : 0xff0000 } ); + + // Create the final object to add to the scene + var curveObject = new THREE.Line( geometry, material ); + scene.add(curveObject); // load a simple obj mesh var objLoader = new THREE.OBJLoader(); objLoader.load('/geo/feather.obj', function(obj) { - - for (var i = 0; i < 20; i++) { + for (var i = 0; i < geometry.vertices.length; i++) { // 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" + i; + var scaleAmt = linearInterpolate(0.25, 1.0, i/geometry.vertices.length); + featherMesh.scale.x = scaleAmt; + featherMesh.scale.y = scaleAmt; + featherMesh.scale.z = scaleAmt; + scene.add(featherMesh); var feather = framework.scene.getObjectByName(featherMesh.name); - feather.rotateY(i); + feather.position.set(geometry.vertices[i].x,geometry.vertices[i].y,geometry.vertices[i].z); + + var yRotateAmt = linearInterpolate(80, 90, i/geometry.vertices.length); + feather.rotateY(degreesToRads(yRotateAmt)); + var zRotateAmt = linearInterpolate(270, 0, i/geometry.vertices.length); + feather.rotateZ(degreesToRads(zRotateAmt)); } }); @@ -56,6 +92,41 @@ function onLoad(framework) { // scene.add(lambertCube); scene.add(directionalLight); + function buildAxes( length ) { + var axes = new THREE.Object3D(); + + axes.add( buildAxis( new THREE.Vector3( 0, 0, 0 ), new THREE.Vector3( length, 0, 0 ), 0xFF0000, false ) ); // +X + axes.add( buildAxis( new THREE.Vector3( 0, 0, 0 ), new THREE.Vector3( -length, 0, 0 ), 0xFF0000, true) ); // -X + axes.add( buildAxis( new THREE.Vector3( 0, 0, 0 ), new THREE.Vector3( 0, length, 0 ), 0x00FF00, false ) ); // +Y + axes.add( buildAxis( new THREE.Vector3( 0, 0, 0 ), new THREE.Vector3( 0, -length, 0 ), 0x00FF00, true ) ); // -Y + axes.add( buildAxis( new THREE.Vector3( 0, 0, 0 ), new THREE.Vector3( 0, 0, length ), 0x0000FF, false ) ); // +Z + axes.add( buildAxis( new THREE.Vector3( 0, 0, 0 ), new THREE.Vector3( 0, 0, -length ), 0x0000FF, true ) ); // -Z + + return axes; + + } + function buildAxis( src, dst, colorHex, dashed ) { + var geom = new THREE.Geometry(), + mat; + + if(dashed) { + mat = new THREE.LineDashedMaterial({ linewidth: 3, color: colorHex, dashSize: 3, gapSize: 3 }); + } else { + mat = new THREE.LineBasicMaterial({ linewidth: 3, color: colorHex }); + } + + geom.vertices.push( src.clone() ); + geom.vertices.push( dst.clone() ); + geom.computeLineDistances(); // This one is SUPER important, otherwise dashed lines will appear as simple plain lines + + var axis = new THREE.Line( geom, mat, THREE.LinePieces ); + + return axis; + + } + + scene.add(buildAxes(1)); + // edit params and listen to changes like this // more information here: https://workshop.chromeexperiments.com/examples/gui/#1--Basic-Usage gui.add(camera, 'fov', 0, 180).onChange(function(newVal) { @@ -69,7 +140,7 @@ function onUpdate(framework) { if (feather !== undefined) { // Simply flap wing var date = new Date(); - //feather.rotateZ(Math.sin(date.getTime() / 100) * 2 * Math.PI / 180); + feather.rotateZ(Math.sin(date.getTime() / 100) * 2 * Math.PI / 180); } } From 962c99b1bcda1bb0af77e29c0de92f536e39f59e Mon Sep 17 00:00:00 2001 From: gracexu94 Date: Tue, 31 Jan 2017 22:50:35 -0500 Subject: [PATCH 04/14] orientation and distribution of feathers done --- src/main.js | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/src/main.js b/src/main.js index bec4fe5..7a02b0b 100755 --- a/src/main.js +++ b/src/main.js @@ -17,6 +17,10 @@ function degreesToRads(degrees) { return Math.PI / 180.0 * degrees; } +var xAxis = new THREE.Vector3(1,0,0); +var yAxis = new THREE.Vector3(0,1,0); +var zAxis = new THREE.Vector3(0,0,1); + // called after the scene loads function onLoad(framework) { var scene = framework.scene; @@ -53,7 +57,7 @@ function onLoad(framework) { ); var geometry = new THREE.Geometry(); - geometry.vertices = curve.getPoints( 50 ); + geometry.vertices = curve.getPoints(45); var material = new THREE.LineBasicMaterial( { color : 0xff0000 } ); @@ -78,10 +82,10 @@ function onLoad(framework) { var feather = framework.scene.getObjectByName(featherMesh.name); feather.position.set(geometry.vertices[i].x,geometry.vertices[i].y,geometry.vertices[i].z); - var yRotateAmt = linearInterpolate(80, 90, i/geometry.vertices.length); - feather.rotateY(degreesToRads(yRotateAmt)); - var zRotateAmt = linearInterpolate(270, 0, i/geometry.vertices.length); - feather.rotateZ(degreesToRads(zRotateAmt)); + var zRotateAmt = linearInterpolate(270, 360, i/geometry.vertices.length); + feather.rotateOnAxis(zAxis, degreesToRads(zRotateAmt)); + var xRotateAmt = linearInterpolate(80, 90, i/geometry.vertices.length); + feather.rotateOnAxis(xAxis, degreesToRads(xRotateAmt)); } }); From 61d06b81fee0bb61b9a3b399140468b6ac287f2a Mon Sep 17 00:00:00 2001 From: gracexu94 Date: Tue, 31 Jan 2017 23:28:54 -0500 Subject: [PATCH 05/14] feather color also distributed --- src/main.js | 60 ++++++++++++++--------------------------------------- 1 file changed, 15 insertions(+), 45 deletions(-) diff --git a/src/main.js b/src/main.js index 7a02b0b..8a33af9 100755 --- a/src/main.js +++ b/src/main.js @@ -29,8 +29,7 @@ function onLoad(framework) { var gui = framework.gui; var stats = framework.stats; - // Basic Lambert white - var lambertWhite = new THREE.MeshLambertMaterial({ color: 0xaaaaaa, side: THREE.DoubleSide }); + // Set light var directionalLight = new THREE.DirectionalLight( 0xffffff, 1 ); @@ -59,7 +58,7 @@ function onLoad(framework) { var geometry = new THREE.Geometry(); geometry.vertices = curve.getPoints(45); - var material = new THREE.LineBasicMaterial( { color : 0xff0000 } ); + var material = new THREE.LineBasicMaterial(); // Create the final object to add to the scene var curveObject = new THREE.Line( geometry, material ); @@ -71,21 +70,27 @@ function onLoad(framework) { for (var i = 0; i < geometry.vertices.length; i++) { // LOOK: This function runs after the obj has finished loading var featherGeo = obj.children[0].geometry; - var featherMesh = new THREE.Mesh(featherGeo, lambertWhite); + // Wing material + // interpolates between #5e4fa2, #d1836b + var interpolateFactor = linearInterpolate(1, 0, i/geometry.vertices.length); + var featherMaterial = new THREE.MeshPhongMaterial(); + featherMaterial.color.setRGB(interpolateFactor, 1, 1); + console.log(featherMaterial); + + var featherMesh = new THREE.Mesh(featherGeo, featherMaterial); featherMesh.name = "feather" + i; var scaleAmt = linearInterpolate(0.25, 1.0, i/geometry.vertices.length); featherMesh.scale.x = scaleAmt; featherMesh.scale.y = scaleAmt; featherMesh.scale.z = scaleAmt; - scene.add(featherMesh); - var feather = framework.scene.getObjectByName(featherMesh.name); - feather.position.set(geometry.vertices[i].x,geometry.vertices[i].y,geometry.vertices[i].z); - + featherMesh.position.set(geometry.vertices[i].x,geometry.vertices[i].y,geometry.vertices[i].z); var zRotateAmt = linearInterpolate(270, 360, i/geometry.vertices.length); - feather.rotateOnAxis(zAxis, degreesToRads(zRotateAmt)); + featherMesh.rotateOnAxis(zAxis, degreesToRads(zRotateAmt)); var xRotateAmt = linearInterpolate(80, 90, i/geometry.vertices.length); - feather.rotateOnAxis(xAxis, degreesToRads(xRotateAmt)); + featherMesh.rotateOnAxis(xAxis, degreesToRads(xRotateAmt)); + + scene.add(featherMesh); } }); @@ -96,41 +101,6 @@ function onLoad(framework) { // scene.add(lambertCube); scene.add(directionalLight); - function buildAxes( length ) { - var axes = new THREE.Object3D(); - - axes.add( buildAxis( new THREE.Vector3( 0, 0, 0 ), new THREE.Vector3( length, 0, 0 ), 0xFF0000, false ) ); // +X - axes.add( buildAxis( new THREE.Vector3( 0, 0, 0 ), new THREE.Vector3( -length, 0, 0 ), 0xFF0000, true) ); // -X - axes.add( buildAxis( new THREE.Vector3( 0, 0, 0 ), new THREE.Vector3( 0, length, 0 ), 0x00FF00, false ) ); // +Y - axes.add( buildAxis( new THREE.Vector3( 0, 0, 0 ), new THREE.Vector3( 0, -length, 0 ), 0x00FF00, true ) ); // -Y - axes.add( buildAxis( new THREE.Vector3( 0, 0, 0 ), new THREE.Vector3( 0, 0, length ), 0x0000FF, false ) ); // +Z - axes.add( buildAxis( new THREE.Vector3( 0, 0, 0 ), new THREE.Vector3( 0, 0, -length ), 0x0000FF, true ) ); // -Z - - return axes; - - } - function buildAxis( src, dst, colorHex, dashed ) { - var geom = new THREE.Geometry(), - mat; - - if(dashed) { - mat = new THREE.LineDashedMaterial({ linewidth: 3, color: colorHex, dashSize: 3, gapSize: 3 }); - } else { - mat = new THREE.LineBasicMaterial({ linewidth: 3, color: colorHex }); - } - - geom.vertices.push( src.clone() ); - geom.vertices.push( dst.clone() ); - geom.computeLineDistances(); // This one is SUPER important, otherwise dashed lines will appear as simple plain lines - - var axis = new THREE.Line( geom, mat, THREE.LinePieces ); - - return axis; - - } - - scene.add(buildAxes(1)); - // edit params and listen to changes like this // more information here: https://workshop.chromeexperiments.com/examples/gui/#1--Basic-Usage gui.add(camera, 'fov', 0, 180).onChange(function(newVal) { From 2cb5bfd439c122b85e1716e32f4d1c5c8262f063 Mon Sep 17 00:00:00 2001 From: gracexu94 Date: Tue, 31 Jan 2017 23:55:07 -0500 Subject: [PATCH 06/14] right and left wing geometry added --- src/main.js | 84 +++++++++++++++++++++++++++++++++++------------------ 1 file changed, 55 insertions(+), 29 deletions(-) diff --git a/src/main.js b/src/main.js index 8a33af9..16924c7 100755 --- a/src/main.js +++ b/src/main.js @@ -29,8 +29,6 @@ function onLoad(framework) { var gui = framework.gui; var stats = framework.stats; - - // Set light var directionalLight = new THREE.DirectionalLight( 0xffffff, 1 ); directionalLight.color.setHSL(0.1, 1, 0.95); @@ -48,49 +46,77 @@ function onLoad(framework) { ] ); scene.background = skymap; - var curve = new THREE.CubicBezierCurve3( - new THREE.Vector3( 0, 0, 0 ), + var lineMaterial = new THREE.LineBasicMaterial(); + var numFeathers = 45; + //right wing + var rightWing = new THREE.CubicBezierCurve3( + new THREE.Vector3( 0.1, 0, 0 ), new THREE.Vector3( 1, 0.75, 0 ), new THREE.Vector3( 2, 3, 0 ), new THREE.Vector3( 5, 2, 0 ) ); - var geometry = new THREE.Geometry(); - geometry.vertices = curve.getPoints(45); + var rightWingGeom = new THREE.Geometry(); + rightWingGeom.vertices = rightWing.getPoints(numFeathers); + var rightWingObject = new THREE.Line(rightWingGeom, lineMaterial ); + scene.add(rightWingObject); + + //legt wing + var leftWing = new THREE.CubicBezierCurve3( + new THREE.Vector3( -0.1, 0, 0 ), + new THREE.Vector3( -1, 0.75, 0 ), + new THREE.Vector3( -2, 3, 0 ), + new THREE.Vector3( -5, 2, 0 ) + ); - var material = new THREE.LineBasicMaterial(); + var leftWingGeom = new THREE.Geometry(); + leftWingGeom.vertices = leftWing.getPoints(numFeathers); // Create the final object to add to the scene - var curveObject = new THREE.Line( geometry, material ); - scene.add(curveObject); + var leftWingObject = new THREE.Line(leftWingGeom, lineMaterial); + scene.add(leftWingObject); // load a simple obj mesh var objLoader = new THREE.OBJLoader(); objLoader.load('/geo/feather.obj', function(obj) { - for (var i = 0; i < geometry.vertices.length; i++) { + for (var i = 0; i < numFeathers; i++) { // LOOK: This function runs after the obj has finished loading var featherGeo = obj.children[0].geometry; - // Wing material - // interpolates between #5e4fa2, #d1836b - var interpolateFactor = linearInterpolate(1, 0, i/geometry.vertices.length); + + // Wing material: color interpolation + var interpolateFactor = linearInterpolate(1, 0, i/numFeathers); var featherMaterial = new THREE.MeshPhongMaterial(); featherMaterial.color.setRGB(interpolateFactor, 1, 1); - console.log(featherMaterial); - - var featherMesh = new THREE.Mesh(featherGeo, featherMaterial); - featherMesh.name = "feather" + i; - var scaleAmt = linearInterpolate(0.25, 1.0, i/geometry.vertices.length); - featherMesh.scale.x = scaleAmt; - featherMesh.scale.y = scaleAmt; - featherMesh.scale.z = scaleAmt; - - featherMesh.position.set(geometry.vertices[i].x,geometry.vertices[i].y,geometry.vertices[i].z); - var zRotateAmt = linearInterpolate(270, 360, i/geometry.vertices.length); - featherMesh.rotateOnAxis(zAxis, degreesToRads(zRotateAmt)); - var xRotateAmt = linearInterpolate(80, 90, i/geometry.vertices.length); - featherMesh.rotateOnAxis(xAxis, degreesToRads(xRotateAmt)); - - scene.add(featherMesh); + + var rightFeatherMesh = new THREE.Mesh(featherGeo, featherMaterial); + rightFeatherMesh.name = "rightFeather" + i; + var leftFeatherMesh = new THREE.Mesh(featherGeo, featherMaterial); + leftFeatherMesh.name = "leftFeather" + i; + + // scale interpolation + var scaleAmt = linearInterpolate(0.25, 1.0, i/numFeathers); + rightFeatherMesh.scale.x = scaleAmt; + rightFeatherMesh.scale.y = scaleAmt; + rightFeatherMesh.scale.z = scaleAmt; + leftFeatherMesh.scale.x = scaleAmt; + leftFeatherMesh.scale.y = scaleAmt; + leftFeatherMesh.scale.z = scaleAmt; + + // orientation interpolation + rightFeatherMesh.position.set(rightWingGeom.vertices[i].x,rightWingGeom.vertices[i].y,rightWingGeom.vertices[i].z); + var zRotateAmt = linearInterpolate(270, 360, i/numFeathers); + rightFeatherMesh.rotateOnAxis(zAxis, degreesToRads(zRotateAmt)); + var xRotateAmt = linearInterpolate(80, 90, i/numFeathers); + rightFeatherMesh.rotateOnAxis(xAxis, degreesToRads(xRotateAmt)); + //rightFeatherMesh.rotateZ(degreesToRads(90)); + + leftFeatherMesh.position.set(leftWingGeom.vertices[i].x,leftWingGeom.vertices[i].y,leftWingGeom.vertices[i].z); + zRotateAmt = linearInterpolate(270, 180, i/numFeathers); + leftFeatherMesh.rotateOnAxis(zAxis, degreesToRads(zRotateAmt)); + leftFeatherMesh.rotateOnAxis(xAxis, degreesToRads(xRotateAmt)); + + scene.add(rightFeatherMesh); + scene.add(leftFeatherMesh); } }); From add5bc444904004970d2788ad08ac404c404fac6 Mon Sep 17 00:00:00 2001 From: gracexu94 Date: Wed, 1 Feb 2017 00:37:48 -0500 Subject: [PATCH 07/14] some code reorganizing --- src/main.js | 85 ++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 62 insertions(+), 23 deletions(-) diff --git a/src/main.js b/src/main.js index 16924c7..075204f 100755 --- a/src/main.js +++ b/src/main.js @@ -20,6 +20,7 @@ function degreesToRads(degrees) { var xAxis = new THREE.Vector3(1,0,0); var yAxis = new THREE.Vector3(0,1,0); var zAxis = new THREE.Vector3(0,0,1); +var numFeathers = 45; // called after the scene loads function onLoad(framework) { @@ -46,8 +47,8 @@ function onLoad(framework) { ] ); scene.background = skymap; + var lineMaterial = new THREE.LineBasicMaterial(); - var numFeathers = 45; //right wing var rightWing = new THREE.CubicBezierCurve3( new THREE.Vector3( 0.1, 0, 0 ), @@ -58,10 +59,11 @@ function onLoad(framework) { var rightWingGeom = new THREE.Geometry(); rightWingGeom.vertices = rightWing.getPoints(numFeathers); - var rightWingObject = new THREE.Line(rightWingGeom, lineMaterial ); + var rightWingObject = new THREE.Line(rightWingGeom, lineMaterial); + rightWingObject.name = "rightWingCurve"; scene.add(rightWingObject); - //legt wing + //left wing var leftWing = new THREE.CubicBezierCurve3( new THREE.Vector3( -0.1, 0, 0 ), new THREE.Vector3( -1, 0.75, 0 ), @@ -72,11 +74,35 @@ function onLoad(framework) { var leftWingGeom = new THREE.Geometry(); leftWingGeom.vertices = leftWing.getPoints(numFeathers); - // Create the final object to add to the scene var leftWingObject = new THREE.Line(leftWingGeom, lineMaterial); + leftWingObject.name = "leftWingCurve"; scene.add(leftWingObject); - // load a simple obj mesh + createWings(numFeathers, scene, rightWing, leftWing); + + // set camera position + camera.position.set(0, 1, 5); + camera.lookAt(new THREE.Vector3(0,0,0)); + scene.add(directionalLight); + + // edit params and listen to changes like this + // more information here: https://workshop.chromeexperiments.com/examples/gui/#1--Basic-Usage + gui.add(camera, 'fov', 0, 180).onChange(function(newVal) { + camera.updateProjectionMatrix(); + }); +} + +function removeWings(numFeathers, scene) { + for (var i = 0; i < numFeathers; i++) { + var rightFeather = scene.getObjectByName("rightFeather"+i); + scene.remove(rightFeather); + var leftFeather = scene.getObjectByName("leftFeather"+i); + scene.remove(leftFeather); + } +} + +function createWings(numFeathers, scene, rightWingCurve, leftWingCurve) { + // load a simple obj mesh multiple times to create the feathers for the wings var objLoader = new THREE.OBJLoader(); objLoader.load('/geo/feather.obj', function(obj) { for (var i = 0; i < numFeathers; i++) { @@ -103,6 +129,9 @@ function onLoad(framework) { leftFeatherMesh.scale.z = scaleAmt; // orientation interpolation + var rightWing = scene.getObjectByName("rightWingCurve"); + var rightWingGeom = rightWing.geometry; + rightWingGeom.vertices = rightWingCurve.getPoints(numFeathers); rightFeatherMesh.position.set(rightWingGeom.vertices[i].x,rightWingGeom.vertices[i].y,rightWingGeom.vertices[i].z); var zRotateAmt = linearInterpolate(270, 360, i/numFeathers); rightFeatherMesh.rotateOnAxis(zAxis, degreesToRads(zRotateAmt)); @@ -110,6 +139,9 @@ function onLoad(framework) { rightFeatherMesh.rotateOnAxis(xAxis, degreesToRads(xRotateAmt)); //rightFeatherMesh.rotateZ(degreesToRads(90)); + var leftWing = scene.getObjectByName("leftWingCurve"); + var leftWingGeom = leftWing.geometry; + leftWingGeom.vertices = leftWingCurve.getPoints(numFeathers); leftFeatherMesh.position.set(leftWingGeom.vertices[i].x,leftWingGeom.vertices[i].y,leftWingGeom.vertices[i].z); zRotateAmt = linearInterpolate(270, 180, i/numFeathers); leftFeatherMesh.rotateOnAxis(zAxis, degreesToRads(zRotateAmt)); @@ -119,29 +151,36 @@ function onLoad(framework) { scene.add(leftFeatherMesh); } }); +} - // 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 - // more information here: https://workshop.chromeexperiments.com/examples/gui/#1--Basic-Usage - gui.add(camera, 'fov', 0, 180).onChange(function(newVal) { - camera.updateProjectionMatrix(); - }); +function flutterWings(numFeathers, scene) { + for (var i = 0; i < numFeathers; i++) { + var date = new Date(); + var rightFeather = scene.getObjectByName("rightFeather"+i); + rightFeather.rotateZ(Math.sin(date.getTime() / 100) * 2 * Math.PI / 1800); + var leftFeather = scene.getObjectByName("leftFeather"+i); + //leftFeather.rotateY(Math.sin(date.getTime() / 100) * 2 * Math.PI / 1800); + } } // 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); - } + removeWings(numFeathers, framework.scene); + //right wing + var rightWing = new THREE.CubicBezierCurve3( + new THREE.Vector3( 0.1, 0, 0 ), + new THREE.Vector3( 1, 0.75, 0 ), + new THREE.Vector3( 2, 3, 0 ), + new THREE.Vector3( 5, 2, 0 ) + ); + + var leftWing = new THREE.CubicBezierCurve3( + new THREE.Vector3( -0.1, 0, 0 ), + new THREE.Vector3( -1, 0.75, 0 ), + new THREE.Vector3( -2, 3, 0 ), + new THREE.Vector3( -5, 2, 0 ) + ); + createWings(numFeathers, framework.scene, rightWing, leftWing); } // when the scene is done initializing, it will call onLoad, then on frame updates, call onUpdate From a44d12c643466d0b54eddcd918a886f6bfaad28f Mon Sep 17 00:00:00 2001 From: gracexu94 Date: Wed, 1 Feb 2017 00:52:48 -0500 Subject: [PATCH 08/14] basic animation working --- src/main.js | 36 ++++++++++-------------------------- 1 file changed, 10 insertions(+), 26 deletions(-) diff --git a/src/main.js b/src/main.js index 075204f..93bd311 100755 --- a/src/main.js +++ b/src/main.js @@ -21,6 +21,7 @@ var xAxis = new THREE.Vector3(1,0,0); var yAxis = new THREE.Vector3(0,1,0); var zAxis = new THREE.Vector3(0,0,1); var numFeathers = 45; +var windForce = 10; // called after the scene loads function onLoad(framework) { @@ -78,7 +79,7 @@ function onLoad(framework) { leftWingObject.name = "leftWingCurve"; scene.add(leftWingObject); - createWings(numFeathers, scene, rightWing, leftWing); + createWings(numFeathers, scene, rightWingObject, leftWingObject); // set camera position camera.position.set(0, 1, 5); @@ -101,7 +102,7 @@ function removeWings(numFeathers, scene) { } } -function createWings(numFeathers, scene, rightWingCurve, leftWingCurve) { +function createWings(numFeathers, scene, rightWingObject, leftWingObject) { // load a simple obj mesh multiple times to create the feathers for the wings var objLoader = new THREE.OBJLoader(); objLoader.load('/geo/feather.obj', function(obj) { @@ -129,9 +130,7 @@ function createWings(numFeathers, scene, rightWingCurve, leftWingCurve) { leftFeatherMesh.scale.z = scaleAmt; // orientation interpolation - var rightWing = scene.getObjectByName("rightWingCurve"); - var rightWingGeom = rightWing.geometry; - rightWingGeom.vertices = rightWingCurve.getPoints(numFeathers); + var rightWingGeom = rightWingObject.geometry; rightFeatherMesh.position.set(rightWingGeom.vertices[i].x,rightWingGeom.vertices[i].y,rightWingGeom.vertices[i].z); var zRotateAmt = linearInterpolate(270, 360, i/numFeathers); rightFeatherMesh.rotateOnAxis(zAxis, degreesToRads(zRotateAmt)); @@ -139,9 +138,7 @@ function createWings(numFeathers, scene, rightWingCurve, leftWingCurve) { rightFeatherMesh.rotateOnAxis(xAxis, degreesToRads(xRotateAmt)); //rightFeatherMesh.rotateZ(degreesToRads(90)); - var leftWing = scene.getObjectByName("leftWingCurve"); - var leftWingGeom = leftWing.geometry; - leftWingGeom.vertices = leftWingCurve.getPoints(numFeathers); + var leftWingGeom = leftWingObject.geometry; leftFeatherMesh.position.set(leftWingGeom.vertices[i].x,leftWingGeom.vertices[i].y,leftWingGeom.vertices[i].z); zRotateAmt = linearInterpolate(270, 180, i/numFeathers); leftFeatherMesh.rotateOnAxis(zAxis, degreesToRads(zRotateAmt)); @@ -157,30 +154,17 @@ function flutterWings(numFeathers, scene) { for (var i = 0; i < numFeathers; i++) { var date = new Date(); var rightFeather = scene.getObjectByName("rightFeather"+i); - rightFeather.rotateZ(Math.sin(date.getTime() / 100) * 2 * Math.PI / 1800); var leftFeather = scene.getObjectByName("leftFeather"+i); - //leftFeather.rotateY(Math.sin(date.getTime() / 100) * 2 * Math.PI / 1800); + if (leftFeather !== undefined && rightFeather !== undefined) { + rightFeather.rotateY(Math.sin(date.getTime() / 100) * 2 * Math.PI / 1800); + leftFeather.rotateY(Math.sin(date.getTime() / 100) * 2 * Math.PI / 1800); + } } } // called on frame updates function onUpdate(framework) { - removeWings(numFeathers, framework.scene); - //right wing - var rightWing = new THREE.CubicBezierCurve3( - new THREE.Vector3( 0.1, 0, 0 ), - new THREE.Vector3( 1, 0.75, 0 ), - new THREE.Vector3( 2, 3, 0 ), - new THREE.Vector3( 5, 2, 0 ) - ); - - var leftWing = new THREE.CubicBezierCurve3( - new THREE.Vector3( -0.1, 0, 0 ), - new THREE.Vector3( -1, 0.75, 0 ), - new THREE.Vector3( -2, 3, 0 ), - new THREE.Vector3( -5, 2, 0 ) - ); - createWings(numFeathers, framework.scene, rightWing, leftWing); + flutterWings(numFeathers, framework.scene); } // when the scene is done initializing, it will call onLoad, then on frame updates, call onUpdate From 1b5afb44fbbc992770f1519d0c25315b458df180 Mon Sep 17 00:00:00 2001 From: gracexu94 Date: Wed, 1 Feb 2017 01:08:19 -0500 Subject: [PATCH 09/14] wind added --- src/main.js | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/src/main.js b/src/main.js index 93bd311..c74da2e 100755 --- a/src/main.js +++ b/src/main.js @@ -4,13 +4,14 @@ const THREE = require('three'); // older modules are imported like this. You shouldn't have to worry about this much import Framework from './framework' -function linearInterpolate(a, b, t) { - return a * (1 - t) + b * t; +function getRandomInt(min, max) { + min = Math.ceil(min); + max = Math.floor(max); + return Math.floor(Math.random() * (max - min)) + min; } -function cosineInterpolate(a, b, t) { - var cos_t = (1 - Math.cos(t * Math.PI)) * 0.5; - return linearInterpolate(a, b, cos_t); +function linearInterpolate(a, b, t) { + return a * (1 - t) + b * t; } function degreesToRads(degrees) { @@ -21,7 +22,7 @@ var xAxis = new THREE.Vector3(1,0,0); var yAxis = new THREE.Vector3(0,1,0); var zAxis = new THREE.Vector3(0,0,1); var numFeathers = 45; -var windForce = 10; +var windForce = {windStrength: 3.0}; // called after the scene loads function onLoad(framework) { @@ -91,6 +92,9 @@ function onLoad(framework) { gui.add(camera, 'fov', 0, 180).onChange(function(newVal) { camera.updateProjectionMatrix(); }); + gui.add(windForce, 'windStrength', 0, 20).onChange(function(newVal) { + + }); } function removeWings(numFeathers, scene) { @@ -152,12 +156,15 @@ function createWings(numFeathers, scene, rightWingObject, leftWingObject) { function flutterWings(numFeathers, scene) { for (var i = 0; i < numFeathers; i++) { + var random = getRandomInt(0,2); var date = new Date(); var rightFeather = scene.getObjectByName("rightFeather"+i); var leftFeather = scene.getObjectByName("leftFeather"+i); if (leftFeather !== undefined && rightFeather !== undefined) { - rightFeather.rotateY(Math.sin(date.getTime() / 100) * 2 * Math.PI / 1800); - leftFeather.rotateY(Math.sin(date.getTime() / 100) * 2 * Math.PI / 1800); + if (random === 0) { + rightFeather.rotateX(Math.sin(date.getTime() / 100) * windForce.windStrength * Math.PI / 1800); + leftFeather.rotateX(Math.sin(date.getTime() / 100) * windForce.windStrength * Math.PI / 1800); + } } } } From b0973160370c227a129fe76510818020c26f8abb Mon Sep 17 00:00:00 2001 From: gracexu94 Date: Wed, 1 Feb 2017 01:29:43 -0500 Subject: [PATCH 10/14] gui controls added for color and number of feathers --- src/main.js | 78 ++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 60 insertions(+), 18 deletions(-) diff --git a/src/main.js b/src/main.js index c74da2e..0b56550 100755 --- a/src/main.js +++ b/src/main.js @@ -21,8 +21,15 @@ function degreesToRads(degrees) { var xAxis = new THREE.Vector3(1,0,0); var yAxis = new THREE.Vector3(0,1,0); var zAxis = new THREE.Vector3(0,0,1); -var numFeathers = 45; -var windForce = {windStrength: 3.0}; +var guiVars = {numFeathers: 45, + windStrength: 3.0, + featherSize: 1.0, + featherColorR_start: 0.0, + featherColorG_start: 1.0, + featherColorB_start: 1.0, + featherColorR_end: 1.0, + featherColorG_end: 1.0, + featherColorB_end: 1.0 }; // called after the scene loads function onLoad(framework) { @@ -60,7 +67,7 @@ function onLoad(framework) { ); var rightWingGeom = new THREE.Geometry(); - rightWingGeom.vertices = rightWing.getPoints(numFeathers); + rightWingGeom.vertices = rightWing.getPoints(guiVars.numFeathers); var rightWingObject = new THREE.Line(rightWingGeom, lineMaterial); rightWingObject.name = "rightWingCurve"; scene.add(rightWingObject); @@ -74,13 +81,13 @@ function onLoad(framework) { ); var leftWingGeom = new THREE.Geometry(); - leftWingGeom.vertices = leftWing.getPoints(numFeathers); + leftWingGeom.vertices = leftWing.getPoints(guiVars.numFeathers); var leftWingObject = new THREE.Line(leftWingGeom, lineMaterial); leftWingObject.name = "leftWingCurve"; scene.add(leftWingObject); - createWings(numFeathers, scene, rightWingObject, leftWingObject); + createWings(scene, rightWingObject, leftWingObject); // set camera position camera.position.set(0, 1, 5); @@ -92,13 +99,45 @@ function onLoad(framework) { gui.add(camera, 'fov', 0, 180).onChange(function(newVal) { camera.updateProjectionMatrix(); }); - gui.add(windForce, 'windStrength', 0, 20).onChange(function(newVal) { - + gui.add(guiVars, 'windStrength', 0, 20).onChange(function(newVal) {}); + gui.add(guiVars, 'numFeathers', 20, 100).onChange(function(newVal) { + removeWings(framework.scene); + rightWingObject.geometry.vertices = rightWing.getPoints(newVal); + leftWingObject.geometry.vertices = leftWing.getPoints(newVal); + createWings(framework.scene, rightWingObject, leftWingObject); + }); + gui.add(guiVars, 'featherSize', 0.5, 3.0).onChange(function(newVal) { + removeWings(framework.scene); + createWings(framework.scene, rightWingObject, leftWingObject); + }); + gui.add(guiVars, 'featherColorR_start', 0.0, 1.0).onChange(function(newVal) { + removeWings(framework.scene); + createWings(framework.scene, rightWingObject, leftWingObject); + }); + gui.add(guiVars, 'featherColorR_end', 0.0, 1.0).onChange(function(newVal) { + removeWings(framework.scene); + createWings(framework.scene, rightWingObject, leftWingObject); + }); + gui.add(guiVars, 'featherColorG_start', 0.0, 1.0).onChange(function(newVal) { + removeWings(framework.scene); + createWings(framework.scene, rightWingObject, leftWingObject); + }); + gui.add(guiVars, 'featherColorG_end', 0.0, 1.0).onChange(function(newVal) { + removeWings(framework.scene); + createWings(framework.scene, rightWingObject, leftWingObject); + }); + gui.add(guiVars, 'featherColorB_start', 0.0, 1.0).onChange(function(newVal) { + removeWings(framework.scene); + createWings(framework.scene, rightWingObject, leftWingObject); + }); + gui.add(guiVars, 'featherColorB_end', 0.0, 1.0).onChange(function(newVal) { + removeWings(framework.scene); + createWings(framework.scene, rightWingObject, leftWingObject); }); } -function removeWings(numFeathers, scene) { - for (var i = 0; i < numFeathers; i++) { +function removeWings(scene) { + for (var i = 0; i < guiVars.numFeathers; i++) { var rightFeather = scene.getObjectByName("rightFeather"+i); scene.remove(rightFeather); var leftFeather = scene.getObjectByName("leftFeather"+i); @@ -106,18 +145,21 @@ function removeWings(numFeathers, scene) { } } -function createWings(numFeathers, scene, rightWingObject, leftWingObject) { +function createWings(scene, rightWingObject, leftWingObject) { // load a simple obj mesh multiple times to create the feathers for the wings var objLoader = new THREE.OBJLoader(); + var numFeathers = guiVars.numFeathers; objLoader.load('/geo/feather.obj', function(obj) { for (var i = 0; i < numFeathers; i++) { // LOOK: This function runs after the obj has finished loading var featherGeo = obj.children[0].geometry; // Wing material: color interpolation - var interpolateFactor = linearInterpolate(1, 0, i/numFeathers); + var interpolateColorR = linearInterpolate(guiVars.featherColorR_start, guiVars.featherColorR_end, i/numFeathers); + var interpolateColorG = linearInterpolate(guiVars.featherColorG_start, guiVars.featherColorG_end, i/numFeathers); + var interpolateColorB = linearInterpolate(guiVars.featherColorB_start, guiVars.featherColorB_end, i/numFeathers); var featherMaterial = new THREE.MeshPhongMaterial(); - featherMaterial.color.setRGB(interpolateFactor, 1, 1); + featherMaterial.color.setRGB(interpolateColorR, interpolateColorG, interpolateColorB); var rightFeatherMesh = new THREE.Mesh(featherGeo, featherMaterial); rightFeatherMesh.name = "rightFeather" + i; @@ -125,7 +167,7 @@ function createWings(numFeathers, scene, rightWingObject, leftWingObject) { leftFeatherMesh.name = "leftFeather" + i; // scale interpolation - var scaleAmt = linearInterpolate(0.25, 1.0, i/numFeathers); + var scaleAmt = linearInterpolate(0.25 * guiVars.featherSize, guiVars.featherSize, i/numFeathers); rightFeatherMesh.scale.x = scaleAmt; rightFeatherMesh.scale.y = scaleAmt; rightFeatherMesh.scale.z = scaleAmt; @@ -154,16 +196,16 @@ function createWings(numFeathers, scene, rightWingObject, leftWingObject) { }); } -function flutterWings(numFeathers, scene) { - for (var i = 0; i < numFeathers; i++) { +function flutterWings(scene) { + for (var i = 0; i < guiVars.numFeathers; i++) { var random = getRandomInt(0,2); var date = new Date(); var rightFeather = scene.getObjectByName("rightFeather"+i); var leftFeather = scene.getObjectByName("leftFeather"+i); if (leftFeather !== undefined && rightFeather !== undefined) { if (random === 0) { - rightFeather.rotateX(Math.sin(date.getTime() / 100) * windForce.windStrength * Math.PI / 1800); - leftFeather.rotateX(Math.sin(date.getTime() / 100) * windForce.windStrength * Math.PI / 1800); + rightFeather.rotateX(Math.sin(date.getTime() / 100) * guiVars.windStrength * Math.PI / 1800); + leftFeather.rotateX(Math.sin(date.getTime() / 100) * guiVars.windStrength * Math.PI / 1800); } } } @@ -171,7 +213,7 @@ function flutterWings(numFeathers, scene) { // called on frame updates function onUpdate(framework) { - flutterWings(numFeathers, framework.scene); + flutterWings(framework.scene); } // when the scene is done initializing, it will call onLoad, then on frame updates, call onUpdate From 3172af3d751446491c9304e32c8045694fad4c6e Mon Sep 17 00:00:00 2001 From: gracexu94 Date: Wed, 1 Feb 2017 02:06:56 -0500 Subject: [PATCH 11/14] curve controls added --- src/main.js | 261 ++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 253 insertions(+), 8 deletions(-) diff --git a/src/main.js b/src/main.js index 0b56550..bd54c21 100755 --- a/src/main.js +++ b/src/main.js @@ -21,7 +21,32 @@ function degreesToRads(degrees) { var xAxis = new THREE.Vector3(1,0,0); var yAxis = new THREE.Vector3(0,1,0); var zAxis = new THREE.Vector3(0,0,1); -var guiVars = {numFeathers: 45, +var guiVars = { + leftWingCurve_startPtX: -0.1, + leftWingCurve_startPtY: 0.0, + leftWingCurve_startPtZ: 0.0, + leftWingCurve_Pt1X: -1.0, + leftWingCurve_Pt1Y: 0.75, + leftWingCurve_Pt1Z: 0.0, + leftWingCurve_Pt2X: -2.0, + leftWingCurve_Pt2Y: 3.0, + leftWingCurve_Pt2Z: 0.0, + leftWingCurve_endPtX: -5.0, + leftWingCurve_endPtY: 2.0, + leftWingCurve_endPtZ: 0.0, + rightWingCurve_startPtX: 0.1, + rightWingCurve_startPtY: 0.0, + rightWingCurve_startPtZ: 0.0, + rightWingCurve_Pt1X: 1.0, + rightWingCurve_Pt1Y: 0.75, + rightWingCurve_Pt1Z: 0.0, + rightWingCurve_Pt2X: 2.0, + rightWingCurve_Pt2Y: 3.0, + rightWingCurve_Pt2Z: 0.0, + rightWingCurve_endPtX: 5.0, + rightWingCurve_endPtY: 2.0, + rightWingCurve_endPtZ: 0.0, + numFeathers: 45, windStrength: 3.0, featherSize: 1.0, featherColorR_start: 0.0, @@ -29,7 +54,13 @@ var guiVars = {numFeathers: 45, featherColorB_start: 1.0, featherColorR_end: 1.0, featherColorG_end: 1.0, - featherColorB_end: 1.0 }; + featherColorB_end: 1.0, + xRotationLocal_degrees: 0.0, + yRotationLocal_degrees: 0.0, + zRotationLocal_degrees: 0.0, + xRotationGlobal_degrees: 0.0, + yRotationGlobal_degrees: 0.0, + zRotationGlobal_degrees: 0.0}; // called after the scene loads function onLoad(framework) { @@ -110,30 +141,244 @@ function onLoad(framework) { removeWings(framework.scene); createWings(framework.scene, rightWingObject, leftWingObject); }); - gui.add(guiVars, 'featherColorR_start', 0.0, 1.0).onChange(function(newVal) { + + var rightCurveFolder = gui.addFolder('Right wing curve controls'); + rightCurveFolder.add(guiVars, 'rightWingCurve_startPtX', -10.0, 10.0).onChange(function(newVal) { + removeWings(framework.scene); + var updatedRightWing = updateRightWingCurve(framework.scene); + createWings(framework.scene, updatedRightWing, leftWingObject); + }); + rightCurveFolder.add(guiVars, 'rightWingCurve_startPtY', -10.0, 10.0).onChange(function(newVal) { + removeWings(framework.scene); + var updatedRightWing = updateRightWingCurve(framework.scene); + createWings(framework.scene, updatedRightWing, leftWingObject); + }); + rightCurveFolder.add(guiVars, 'rightWingCurve_startPtZ', -10.0, 10.0).onChange(function(newVal) { + removeWings(framework.scene); + var updatedRightWing = updateRightWingCurve(framework.scene); + createWings(framework.scene, updatedRightWing, leftWingObject); + }); + rightCurveFolder.add(guiVars, 'rightWingCurve_Pt1X', -10.0, 10.0).onChange(function(newVal) { + removeWings(framework.scene); + var updatedRightWing = updateRightWingCurve(framework.scene); + createWings(framework.scene, updatedRightWing, leftWingObject); + }); + rightCurveFolder.add(guiVars, 'rightWingCurve_Pt1Y', -10.0, 10.0).onChange(function(newVal) { + removeWings(framework.scene); + var updatedRightWing = updateRightWingCurve(framework.scene); + createWings(framework.scene, updatedRightWing, leftWingObject); + }); + rightCurveFolder.add(guiVars, 'rightWingCurve_Pt1Z', -10.0, 10.0).onChange(function(newVal) { + removeWings(framework.scene); + var updatedRightWing = updateRightWingCurve(framework.scene); + createWings(framework.scene, updatedRightWing, leftWingObject); + }); + rightCurveFolder.add(guiVars, 'rightWingCurve_Pt2X', -10.0, 10.0).onChange(function(newVal) { + removeWings(framework.scene); + var updatedRightWing = updateRightWingCurve(framework.scene); + createWings(framework.scene, updatedRightWing, leftWingObject); + }); + rightCurveFolder.add(guiVars, 'rightWingCurve_Pt2Y', -10.0, 10.0).onChange(function(newVal) { + removeWings(framework.scene); + var updatedRightWing = updateRightWingCurve(framework.scene); + createWings(framework.scene, updatedRightWing, leftWingObject); + }); + rightCurveFolder.add(guiVars, 'rightWingCurve_Pt2Z', -10.0, 10.0).onChange(function(newVal) { + removeWings(framework.scene); + var updatedRightWing = updateRightWingCurve(framework.scene); + createWings(framework.scene, updatedRightWing, leftWingObject); + }); + rightCurveFolder.add(guiVars, 'rightWingCurve_endPtX', -10.0, 10.0).onChange(function(newVal) { + removeWings(framework.scene); + var updatedRightWing = updateRightWingCurve(framework.scene); + createWings(framework.scene, updatedRightWing, leftWingObject); + }); + rightCurveFolder.add(guiVars, 'rightWingCurve_endPtY', -10.0, 10.0).onChange(function(newVal) { + removeWings(framework.scene); + var updatedRightWing = updateRightWingCurve(framework.scene); + createWings(framework.scene, updatedRightWing, leftWingObject); + }); + rightCurveFolder.add(guiVars, 'rightWingCurve_endPtZ', -10.0, 10.0).onChange(function(newVal) { + removeWings(framework.scene); + var updatedRightWing = updateRightWingCurve(framework.scene); + createWings(framework.scene, updatedRightWing, leftWingObject); + }); + + var leftCurveFolder = gui.addFolder('Left wing curve controls'); + leftCurveFolder.add(guiVars, 'leftWingCurve_startPtX', -10.0, 10.0).onChange(function(newVal) { + removeWings(framework.scene); + var updatedLeftWing = updateLeftWingCurve(framework.scene); + createWings(framework.scene, rightWingObject, updatedLeftWing); + }); + leftCurveFolder.add(guiVars, 'leftWingCurve_startPtY', -10.0, 10.0).onChange(function(newVal) { + removeWings(framework.scene); + var updatedLeftWing = updateLeftWingCurve(framework.scene); + createWings(framework.scene, rightWingObject, updatedLeftWing); + }); + leftCurveFolder.add(guiVars, 'leftWingCurve_startPtZ', -10.0, 10.0).onChange(function(newVal) { + removeWings(framework.scene); + var updatedLeftWing = updateLeftWingCurve(framework.scene); + createWings(framework.scene, rightWingObject, updatedLeftWing); + }); + leftCurveFolder.add(guiVars, 'leftWingCurve_Pt1X', -10.0, 10.0).onChange(function(newVal) { + removeWings(framework.scene); + var updatedLeftWing = updateLeftWingCurve(framework.scene); + createWings(framework.scene, rightWingObject, updatedLeftWing); + }); + leftCurveFolder.add(guiVars, 'leftWingCurve_Pt1Y', -10.0, 10.0).onChange(function(newVal) { + removeWings(framework.scene); + var updatedLeftWing = updateLeftWingCurve(framework.scene); + createWings(framework.scene, rightWingObject, updatedLeftWing); + }); + leftCurveFolder.add(guiVars, 'leftWingCurve_Pt1Z', -10.0, 10.0).onChange(function(newVal) { + removeWings(framework.scene); + var updatedLeftWing = updateLeftWingCurve(framework.scene); + createWings(framework.scene, rightWingObject, updatedLeftWing); + }); + leftCurveFolder.add(guiVars, 'leftWingCurve_Pt2X', -10.0, 10.0).onChange(function(newVal) { + removeWings(framework.scene); + var updatedLeftWing = updateLeftWingCurve(framework.scene); + createWings(framework.scene, rightWingObject, updatedLeftWing); + }); + leftCurveFolder.add(guiVars, 'leftWingCurve_Pt2Y', -10.0, 10.0).onChange(function(newVal) { + removeWings(framework.scene); + var updatedLeftWing = updateLeftWingCurve(framework.scene); + createWings(framework.scene, rightWingObject, updatedLeftWing); + }); + leftCurveFolder.add(guiVars, 'leftWingCurve_Pt2Z', -10.0, 10.0).onChange(function(newVal) { + removeWings(framework.scene); + var updatedLeftWing = updateLeftWingCurve(framework.scene); + createWings(framework.scene, rightWingObject, updatedLeftWing); + }); + leftCurveFolder.add(guiVars, 'leftWingCurve_endPtX', -10.0, 10.0).onChange(function(newVal) { + removeWings(framework.scene); + var updatedLeftWing = updateLeftWingCurve(framework.scene); + createWings(framework.scene, rightWingObject, updatedLeftWing); + }); + leftCurveFolder.add(guiVars, 'leftWingCurve_endPtY', -10.0, 10.0).onChange(function(newVal) { + removeWings(framework.scene); + var updatedLeftWing = updateLeftWingCurve(framework.scene); + createWings(framework.scene, rightWingObject, updatedLeftWing); + }); + leftCurveFolder.add(guiVars, 'leftWingCurve_endPtZ', -10.0, 10.0).onChange(function(newVal) { + removeWings(framework.scene); + var updatedLeftWing = updateLeftWingCurve(framework.scene); + createWings(framework.scene, rightWingObject, updatedLeftWing); + }); + + var colorFolder = gui.addFolder('Color controls'); + colorFolder.add(guiVars, 'featherColorR_start', 0.0, 1.0).onChange(function(newVal) { removeWings(framework.scene); createWings(framework.scene, rightWingObject, leftWingObject); }); - gui.add(guiVars, 'featherColorR_end', 0.0, 1.0).onChange(function(newVal) { + colorFolder.add(guiVars, 'featherColorR_end', 0.0, 1.0).onChange(function(newVal) { removeWings(framework.scene); createWings(framework.scene, rightWingObject, leftWingObject); }); - gui.add(guiVars, 'featherColorG_start', 0.0, 1.0).onChange(function(newVal) { + colorFolder.add(guiVars, 'featherColorG_start', 0.0, 1.0).onChange(function(newVal) { removeWings(framework.scene); createWings(framework.scene, rightWingObject, leftWingObject); }); - gui.add(guiVars, 'featherColorG_end', 0.0, 1.0).onChange(function(newVal) { + colorFolder.add(guiVars, 'featherColorG_end', 0.0, 1.0).onChange(function(newVal) { removeWings(framework.scene); createWings(framework.scene, rightWingObject, leftWingObject); }); - gui.add(guiVars, 'featherColorB_start', 0.0, 1.0).onChange(function(newVal) { + colorFolder.add(guiVars, 'featherColorB_start', 0.0, 1.0).onChange(function(newVal) { removeWings(framework.scene); createWings(framework.scene, rightWingObject, leftWingObject); }); - gui.add(guiVars, 'featherColorB_end', 0.0, 1.0).onChange(function(newVal) { + colorFolder.add(guiVars, 'featherColorB_end', 0.0, 1.0).onChange(function(newVal) { removeWings(framework.scene); createWings(framework.scene, rightWingObject, leftWingObject); }); + + var orientationFolder = gui.addFolder('Feather orientation controls'); + orientationFolder.add(guiVars, 'xRotationLocal_degrees', 0.0, 360.0).onChange(function(newVal) { + rotateWingsLocal(scene); + }); + orientationFolder.add(guiVars, 'yRotationLocal_degrees', 0.0, 360.0).onChange(function(newVal) { + rotateWingsLocal(scene); + }); + orientationFolder.add(guiVars, 'zRotationLocal_degrees', 0.0, 360.0).onChange(function(newVal) { + rotateWingsLocal(scene); + }); + orientationFolder.add(guiVars, 'xRotationGlobal_degrees', 0.0, 360.0).onChange(function(newVal) { + rotateWingsGlobal(scene); + }); + orientationFolder.add(guiVars, 'yRotationGlobal_degrees', 0.0, 360.0).onChange(function(newVal) { + rotateWingsGlobal(scene); + }); + orientationFolder.add(guiVars, 'zRotationGlobal_degrees', 0.0, 360.0).onChange(function(newVal) { + rotateWingsGlobal(scene); + }); +} + +function updateRightWingCurve(scene) { + var rightWingCurve = scene.getObjectByName("rightWingCurve"); + scene.remove(rightWingCurve); + var newRightWing = new THREE.CubicBezierCurve3( + new THREE.Vector3(guiVars.rightWingCurve_startPtX, guiVars.rightWingCurve_startPtY, guiVars.rightWingCurve_startPtZ), + new THREE.Vector3(guiVars.rightWingCurve_Pt1X, guiVars.rightWingCurve_Pt1Y, guiVars.rightWingCurve_Pt1Z), + new THREE.Vector3(guiVars.rightWingCurve_Pt2X, guiVars.rightWingCurve_Pt2Y, guiVars.rightWingCurve_Pt2Z), + new THREE.Vector3(guiVars.rightWingCurve_endPtX, guiVars.rightWingCurve_endPtY, guiVars.rightWingCurve_endPtZ) + ); + + var rightWingGeom = new THREE.Geometry(); + rightWingGeom.vertices = newRightWing.getPoints(guiVars.numFeathers); + var lineMaterial = new THREE.LineBasicMaterial(); + var rightWingObject = new THREE.Line(rightWingGeom, lineMaterial); + rightWingObject.name = "rightWingCurve"; + scene.add(rightWingObject); + return rightWingCurve; +} + +function updateLeftWingCurve(scene) { + var leftWingCurve = scene.getObjectByName("leftWingCurve"); + scene.remove(leftWingCurve); + var newLeftWing = new THREE.CubicBezierCurve3( + new THREE.Vector3(guiVars.leftWingCurve_startPtX, guiVars.leftWingCurve_startPtY, guiVars.leftWingCurve_startPtZ), + new THREE.Vector3(guiVars.leftWingCurve_Pt1X, guiVars.leftWingCurve_Pt1Y, guiVars.leftWingCurve_Pt1Z), + new THREE.Vector3(guiVars.leftWingCurve_Pt2X, guiVars.leftWingCurve_Pt2Y, guiVars.leftWingCurve_Pt2Z), + new THREE.Vector3(guiVars.leftWingCurve_endPtX, guiVars.leftWingCurve_endPtY, guiVars.leftWingCurve_endPtZ) + ); + + var leftWingGeom = new THREE.Geometry(); + leftWingGeom.vertices = newLeftWing.getPoints(guiVars.numFeathers); + var lineMaterial = new THREE.LineBasicMaterial(); + var leftWingObject = new THREE.Line(leftWingGeom, lineMaterial); + leftWingObject.name = "leftWingCurve"; + scene.add(leftWingObject); + return leftWingCurve; +} + +function rotateWingsLocal(scene) { + for (var i = 0; i < guiVars.numFeathers; i++) { + var rightFeather = scene.getObjectByName("rightFeather"+i); + var leftFeather = scene.getObjectByName("leftFeather"+i); + if (leftFeather !== undefined && rightFeather !== undefined) { + rightFeather.rotateX(degreesToRads(guiVars.xRotationLocal_degrees)); + leftFeather.rotateX(degreesToRads(guiVars.xRotationLocal_degrees)); + rightFeather.rotateY(degreesToRads(guiVars.yRotationLocal_degrees)); + leftFeather.rotateY(degreesToRads(guiVars.yRotationLocal_degrees)); + rightFeather.rotateZ(degreesToRads(guiVars.zRotationLocal_degrees)); + leftFeather.rotateZ(degreesToRads(guiVars.zRotationLocal_degrees)); + } + } +} + +function rotateWingsGlobal(scene) { + for (var i = 0; i < guiVars.numFeathers; i++) { + var rightFeather = scene.getObjectByName("rightFeather"+i); + var leftFeather = scene.getObjectByName("leftFeather"+i); + if (leftFeather !== undefined && rightFeather !== undefined) { + rightFeather.rotateOnAxis(xAxis, degreesToRads(guiVars.xRotationGlobal_degrees)); + leftFeather.rotateOnAxis(xAxis, degreesToRads(guiVars.xRotationGlobal_degrees)); + rightFeather.rotateOnAxis(yAxis, degreesToRads(guiVars.yRotationGlobal_degrees)); + leftFeather.rotateOnAxis(yAxis, degreesToRads(guiVars.yRotationGlobal_degrees)); + rightFeather.rotateOnAxis(zAxis, degreesToRads(guiVars.zRotationGlobal_degrees)); + leftFeather.rotateOnAxis(zAxis, degreesToRads(guiVars.zRotationGlobal_degrees)); + } + } } function removeWings(scene) { From a0cbc0fe29960a7ab4c6bb678fa5a5d6fec37db0 Mon Sep 17 00:00:00 2001 From: gracexu94 Date: Wed, 1 Feb 2017 03:07:26 -0500 Subject: [PATCH 12/14] added flap controls, done --- src/main.js | 33 ++++++++++++++++++++++++++++++--- 1 file changed, 30 insertions(+), 3 deletions(-) diff --git a/src/main.js b/src/main.js index bd54c21..5cd7690 100755 --- a/src/main.js +++ b/src/main.js @@ -22,6 +22,10 @@ var xAxis = new THREE.Vector3(1,0,0); var yAxis = new THREE.Vector3(0,1,0); var zAxis = new THREE.Vector3(0,0,1); var guiVars = { + flapWing: true, + flapStrength: 1.0, + flapSpeed: 100.0, + flapMotion: 'sin', leftWingCurve_startPtX: -0.1, leftWingCurve_startPtY: 0.0, leftWingCurve_startPtZ: 0.0, @@ -50,9 +54,9 @@ var guiVars = { windStrength: 3.0, featherSize: 1.0, featherColorR_start: 0.0, - featherColorG_start: 1.0, + featherColorG_start: 0.5, featherColorB_start: 1.0, - featherColorR_end: 1.0, + featherColorR_end: 0.5, featherColorG_end: 1.0, featherColorB_end: 1.0, xRotationLocal_degrees: 0.0, @@ -130,7 +134,11 @@ function onLoad(framework) { gui.add(camera, 'fov', 0, 180).onChange(function(newVal) { camera.updateProjectionMatrix(); }); - gui.add(guiVars, 'windStrength', 0, 20).onChange(function(newVal) {}); + gui.add(guiVars, 'flapWing'); + gui.add(guiVars, 'flapStrength', 0.0, 50.0); + gui.add(guiVars, 'flapSpeed', 50, 500.0); + gui.add(guiVars, 'flapMotion', ['cosine', 'sin']); + gui.add(guiVars, 'windStrength', 0, 20); gui.add(guiVars, 'numFeathers', 20, 100).onChange(function(newVal) { removeWings(framework.scene); rightWingObject.geometry.vertices = rightWing.getPoints(newVal); @@ -458,6 +466,25 @@ function flutterWings(scene) { // called on frame updates function onUpdate(framework) { + if (guiVars.flapWing) { + removeWings(framework.scene); + var date = new Date(); + var flapAmt = 0; + if (guiVars.flapMotion === 'sin') { + flapAmt = Math.sin(date.getTime() / guiVars.flapSpeed) * Math.PI; + } else if (guiVars.flapMotion === 'cosine') { + flapAmt = Math.cos(date.getTime() / guiVars.flapSpeed) * Math.PI; + } + + guiVars.leftWingCurve_Pt2Z = (flapAmt < 0) ? 0.0 : flapAmt; + guiVars.rightWingCurve_Pt2Z = (flapAmt < 0) ? 0.0 : flapAmt; + guiVars.leftWingCurve_endPtZ = flapAmt * guiVars.flapStrength; + guiVars.rightWingCurve_endPtZ = flapAmt * guiVars.flapStrength; + + var updatedLeftWing = updateLeftWingCurve(framework.scene); + var updatedRightWing = updateRightWingCurve(framework.scene); + createWings(framework.scene, updatedRightWing, updatedLeftWing); + } flutterWings(framework.scene); } From f5af0c2fce8f872562ffef8271f99a9ead6cb5b0 Mon Sep 17 00:00:00 2001 From: gracexu94 Date: Wed, 1 Feb 2017 03:09:10 -0500 Subject: [PATCH 13/14] added folder for flap controls. done done --- src/main.js | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/main.js b/src/main.js index 5cd7690..dc8b578 100755 --- a/src/main.js +++ b/src/main.js @@ -24,7 +24,7 @@ var zAxis = new THREE.Vector3(0,0,1); var guiVars = { flapWing: true, flapStrength: 1.0, - flapSpeed: 100.0, + flapSpeed: 200.0, flapMotion: 'sin', leftWingCurve_startPtX: -0.1, leftWingCurve_startPtY: 0.0, @@ -134,10 +134,6 @@ function onLoad(framework) { gui.add(camera, 'fov', 0, 180).onChange(function(newVal) { camera.updateProjectionMatrix(); }); - gui.add(guiVars, 'flapWing'); - gui.add(guiVars, 'flapStrength', 0.0, 50.0); - gui.add(guiVars, 'flapSpeed', 50, 500.0); - gui.add(guiVars, 'flapMotion', ['cosine', 'sin']); gui.add(guiVars, 'windStrength', 0, 20); gui.add(guiVars, 'numFeathers', 20, 100).onChange(function(newVal) { removeWings(framework.scene); @@ -150,6 +146,12 @@ function onLoad(framework) { createWings(framework.scene, rightWingObject, leftWingObject); }); + var flapControls = gui.addFolder('Flapping controls'); + flapControls.add(guiVars, 'flapWing'); + flapControls.add(guiVars, 'flapStrength', 0.0, 50.0); + flapControls.add(guiVars, 'flapSpeed', 50, 500.0); + flapControls.add(guiVars, 'flapMotion', ['cosine', 'sin']); + var rightCurveFolder = gui.addFolder('Right wing curve controls'); rightCurveFolder.add(guiVars, 'rightWingCurve_startPtX', -10.0, 10.0).onChange(function(newVal) { removeWings(framework.scene); From 87b3fc17c36a657643f873403e31fc110e9b9736 Mon Sep 17 00:00:00 2001 From: gracexu94 Date: Wed, 1 Feb 2017 03:11:11 -0500 Subject: [PATCH 14/14] updated README --- README.md | 81 +++---------------------------------------------------- 1 file changed, 3 insertions(+), 78 deletions(-) diff --git a/README.md b/README.md index 1410c30..619c170 100644 --- a/README.md +++ b/README.md @@ -2,81 +2,6 @@ ## Overview -The objective of this assignment is to procedurally model and animate a bird wing. Let's get creative! - -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 +Grace Xu +2/1/17 +Procedurally modeled and animate a set of bird wing. Provided controls for flapping motions, various characteristics of the feathers (color, size, orientation distribution), as well as the basic curve the feathers are drawn along.