Commit 1b89046d authored by amandaghassaei's avatar amandaghassaei

panel stiffness

parent 83e5d9a7
...@@ -98,14 +98,21 @@ ...@@ -98,14 +98,21 @@
precision mediump float; precision mediump float;
uniform vec2 u_textureDim; uniform vec2 u_textureDim;
uniform vec2 u_textureDimEdges; uniform vec2 u_textureDimEdges;
uniform vec2 u_textureDimFaces;
uniform vec2 u_textureDimCreases;
uniform vec2 u_textureDimNodeCreases;
uniform float u_dt; uniform float u_dt;
uniform sampler2D u_lastPosition; uniform sampler2D u_lastPosition;
uniform sampler2D u_lastVelocity; uniform sampler2D u_lastVelocity;
uniform sampler2D u_originalPosition; uniform sampler2D u_originalPosition;
uniform sampler2D u_externalForces; uniform sampler2D u_externalForces;
uniform sampler2D u_mass; uniform sampler2D u_mass;
uniform sampler2D u_meta;//[beamsIndex, numBeams] uniform sampler2D u_meta;//[beamsIndex, numBeam, creaseMeta2Index, numCreases]
uniform sampler2D u_beamMeta;//[k, d, length, otherNodeIndex] uniform sampler2D u_beamMeta;//[k, d, length, otherNodeIndex]
uniform sampler2D u_creaseMeta;//[k, d, targetTheta]
uniform sampler2D u_creaseMeta2;//[creaseIndex, momentArmLength, nodeIndex]
uniform sampler2D u_normals;
uniform sampler2D u_theta;//[theta, z, normal1Index, normal2Index]
void main(){ void main(){
vec2 fragCoord = gl_FragCoord.xy; vec2 fragCoord = gl_FragCoord.xy;
...@@ -123,7 +130,7 @@ ...@@ -123,7 +130,7 @@
vec3 originalPosition = texture2D(u_originalPosition, scaledFragCoord).xyz; vec3 originalPosition = texture2D(u_originalPosition, scaledFragCoord).xyz;
vec4 neighborIndices = texture2D(u_meta, scaledFragCoord); vec4 neighborIndices = texture2D(u_meta, scaledFragCoord);
vec2 meta = texture2D(u_meta, scaledFragCoord).xy; vec4 meta = texture2D(u_meta, scaledFragCoord);
for (int j=0;j<100;j++){//for all beams (up to 100, had to put a const int in here) for (int j=0;j<100;j++){//for all beams (up to 100, had to put a const int in here)
if (j >= int(meta[1])) break; if (j >= int(meta[1])) break;
...@@ -148,6 +155,36 @@ ...@@ -148,6 +155,36 @@
vec3 _force = deltaP*beamMeta[0] + deltaV*beamMeta[1]; vec3 _force = deltaP*beamMeta[0] + deltaV*beamMeta[1];
force += _force; force += _force;
} }
for (int j=0;j<100;j++){//for all creases (up to 100, had to put a const int in here)
if (j >= int(meta[3])) break;
float nodeCreaseIndex1D = meta[2]+float(j);
vec2 nodeCreaseIndex = vec2(mod(nodeCreaseIndex1D, u_textureDimNodeCreases.x)+0.5, floor(nodeCreaseIndex1D/u_textureDimNodeCreases.x)+0.5);
vec2 scaledNodeCreaseIndex = nodeCreaseIndex/u_textureDimNodeCreases;
vec4 creaseMeta2 = texture2D(u_creaseMeta2, scaledNodeCreaseIndex);
float creaseIndex1D = creaseMeta2[0];
vec2 creaseIndex = vec2(mod(creaseIndex1D, u_textureDimCreases.x)+0.5, floor(creaseIndex1D/u_textureDimCreases.x)+0.5);
vec2 scaledCreaseIndex = creaseIndex/u_textureDimCreases;
vec4 thetas = texture2D(u_theta, scaledCreaseIndex);
vec3 creaseMeta = texture2D(u_creaseMeta, scaledCreaseIndex).xyz;//[k, d, targetTheta]
float nodeNum = creaseMeta2[2];
float normalIndex1D = thetas[2];
if (nodeNum > 1.1) normalIndex1D = thetas[3];
vec2 normalsIndex = vec2(mod(normalIndex1D, u_textureDimFaces.x)+0.5, floor(normalIndex1D/u_textureDimFaces.x)+0.5);
vec2 scaledNormalsIndex = normalsIndex/u_textureDimFaces;
vec3 normal = texture2D(u_normals, scaledNormalsIndex).xyz;
float angForce = creaseMeta[0]*(creaseMeta[2]-thetas[0]);
float momentArm = creaseMeta2[1];
vec3 _force = angForce/momentArm*normal;
force += _force;
}
vec3 velocity = force*u_dt/mass.x + lastVelocity; vec3 velocity = force*u_dt/mass.x + lastVelocity;
gl_FragColor = vec4(velocity,0.0); gl_FragColor = vec4(velocity,0.0);
} }
......
...@@ -53,10 +53,12 @@ function initControls(globals){ ...@@ -53,10 +53,12 @@ function initControls(globals){
setSliderInput("#creaseStiffness", globals.creaseStiffness, 1, 1000, 1, function(val){ setSliderInput("#creaseStiffness", globals.creaseStiffness, 1, 1000, 1, function(val){
globals.creaseStiffness = val; globals.creaseStiffness = val;
globals.dynamicModel.updateCreasesMeta();
}); });
setSliderInput("#panelStiffness", globals.panelStiffness, 1, 1000, 1, function(val){ setSliderInput("#panelStiffness", globals.panelStiffness, 1, 1000, 1, function(val){
globals.panelStiffness = val; globals.panelStiffness = val;
globals.dynamicModel.updateCreasesMeta();
}); });
setSlider("#damping", globals.percentDamping, 0, 1, 0.01, function(val){ setSlider("#damping", globals.percentDamping, 0, 1, 0.01, function(val){
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
* Created by amandaghassaei on 2/25/17. * Created by amandaghassaei on 2/25/17.
*/ */
function Crease(edge, face1Index, face2Index, targetTheta, type, node1, node2){//type = 0 panel, 1 crease function Crease(edge, face1Index, face2Index, targetTheta, type, node1, node2, index){//type = 0 panel, 1 crease
//face1 corresponds to node1, face2 to node2 //face1 corresponds to node1, face2 to node2
this.edge = edge; this.edge = edge;
...@@ -12,6 +12,7 @@ function Crease(edge, face1Index, face2Index, targetTheta, type, node1, node2){/ ...@@ -12,6 +12,7 @@ function Crease(edge, face1Index, face2Index, targetTheta, type, node1, node2){/
this.type = type; this.type = type;
this.node1 = node1; this.node1 = node1;
this.node2 = node2; this.node2 = node2;
this.index = index;
node1.addCrease(this); node1.addCrease(this);
node2.addCrease(this); node2.addCrease(this);
} }
...@@ -46,6 +47,25 @@ Crease.prototype.getD = function(){ ...@@ -46,6 +47,25 @@ Crease.prototype.getD = function(){
return globals.percentDamping*2*Math.sqrt(this.getK()); return globals.percentDamping*2*Math.sqrt(this.getK());
}; };
Crease.prototype.getIndex = function(){
return this.index;
};
Crease.prototype.getLengthTo = function(node){
var vector1 = this.getVector().normalize();
var nodePosition = node.getOriginalPosition();
var vector2 = nodePosition.sub(this.edge.nodes[1].getOriginalPosition());
var projLength = vector1.dot(vector2);
return Math.sqrt(vector2.lengthSq()-projLength*projLength);
};
Crease.prototype.getNodeIndex = function(node){
if (node == this.node1) return 1;
else if (node == this.node2) return 2;
console.warn("no node found");
return 0;
};
Crease.prototype.destroy = function(){ Crease.prototype.destroy = function(){
this.node1.removeCrease(this); this.node1.removeCrease(this);
this.node2.removeCrease(this); this.node2.removeCrease(this);
...@@ -56,4 +76,5 @@ Crease.prototype.destroy = function(){ ...@@ -56,4 +76,5 @@ Crease.prototype.destroy = function(){
this.type = null; this.type = null;
this.node1 = null; this.node1 = null;
this.node2 = null; this.node2 = null;
this.index = null;
}; };
\ No newline at end of file
...@@ -25,7 +25,8 @@ function initDynamicModel(globals){ ...@@ -25,7 +25,8 @@ function initDynamicModel(globals){
var beamMeta;//[K, D, length, otherNodeIndex] var beamMeta;//[K, D, length, otherNodeIndex]
var normals; var normals;
var creaseMeta;//[k, d, targetTheta, length (to node)] var creaseMeta;//[k, d, targetTheta]
var creaseMeta2;//[creaseIndex, length to node, nodeIndex (1/2)]
var creaseVectors;//vectors of oriented edges in crease var creaseVectors;//vectors of oriented edges in crease
var theta;//[theta, w, normalIndex1, normalIndex2] var theta;//[theta, w, normalIndex1, normalIndex2]
var lastTheta;//[theta, w, normalIndex1, normalIndex2] var lastTheta;//[theta, w, normalIndex1, normalIndex2]
...@@ -56,6 +57,7 @@ function initDynamicModel(globals){ ...@@ -56,6 +57,7 @@ function initDynamicModel(globals){
var textureDimEdges = 0; var textureDimEdges = 0;
var textureDimFaces = 0; var textureDimFaces = 0;
var textureDimCreases = 0; var textureDimCreases = 0;
var textureDimNodeCreases = 0;
syncNodesAndEdges(); syncNodesAndEdges();
initTexturesAndPrograms(globals.gpuMath); initTexturesAndPrograms(globals.gpuMath);
steps = parseInt(setSolveParams()); steps = parseInt(setSolveParams());
...@@ -119,7 +121,7 @@ function initDynamicModel(globals){ ...@@ -119,7 +121,7 @@ function initDynamicModel(globals){
gpuMath.step("thetaCalc", ["u_normals", "u_lastTheta", "u_creaseVectors"], "u_theta"); gpuMath.step("thetaCalc", ["u_normals", "u_lastTheta", "u_creaseVectors"], "u_theta");
gpuMath.step("velocityCalc", ["u_lastPosition", "u_lastVelocity", "u_originalPosition", "u_externalForces", gpuMath.step("velocityCalc", ["u_lastPosition", "u_lastVelocity", "u_originalPosition", "u_externalForces",
"u_mass", "u_meta", "u_beamMeta"], "u_velocity"); "u_mass", "u_meta", "u_beamMeta", "u_creaseMeta", "u_creaseMeta2", "u_normals", "u_theta"], "u_velocity");
gpuMath.step("positionCalc", ["u_velocity", "u_lastPosition", "u_mass"], "u_position"); gpuMath.step("positionCalc", ["u_velocity", "u_lastPosition", "u_mass"], "u_position");
gpuMath.swapTextures("u_theta", "u_lastTheta"); gpuMath.swapTextures("u_theta", "u_lastTheta");
...@@ -216,6 +218,7 @@ function initDynamicModel(globals){ ...@@ -216,6 +218,7 @@ function initDynamicModel(globals){
gpuMath.initFrameBufferForTexture("u_lastTheta"); gpuMath.initFrameBufferForTexture("u_lastTheta");
gpuMath.initTextureFromData("u_meta", textureDim, textureDim, "FLOAT", meta); gpuMath.initTextureFromData("u_meta", textureDim, textureDim, "FLOAT", meta);
gpuMath.initTextureFromData("u_creaseMeta2", textureDimNodeCreases, textureDimNodeCreases, "FLOAT", creaseMeta2);
gpuMath.createProgram("positionCalc", vertexShader, document.getElementById("positionCalcShader").text); gpuMath.createProgram("positionCalc", vertexShader, document.getElementById("positionCalcShader").text);
gpuMath.setUniformForProgram("positionCalc", "u_velocity", 0, "1i"); gpuMath.setUniformForProgram("positionCalc", "u_velocity", 0, "1i");
...@@ -231,8 +234,15 @@ function initDynamicModel(globals){ ...@@ -231,8 +234,15 @@ function initDynamicModel(globals){
gpuMath.setUniformForProgram("velocityCalc", "u_mass", 4, "1i"); gpuMath.setUniformForProgram("velocityCalc", "u_mass", 4, "1i");
gpuMath.setUniformForProgram("velocityCalc", "u_meta", 5, "1i"); gpuMath.setUniformForProgram("velocityCalc", "u_meta", 5, "1i");
gpuMath.setUniformForProgram("velocityCalc", "u_beamMeta", 6, "1i"); gpuMath.setUniformForProgram("velocityCalc", "u_beamMeta", 6, "1i");
gpuMath.setUniformForProgram("velocityCalc", "u_creaseMeta", 7, "1i");
gpuMath.setUniformForProgram("velocityCalc", "u_creaseMeta2", 8, "1i");
gpuMath.setUniformForProgram("velocityCalc", "u_normals", 9, "1i");
gpuMath.setUniformForProgram("velocityCalc", "u_theta", 10, "1i");
gpuMath.setUniformForProgram("velocityCalc", "u_textureDim", [textureDim, textureDim], "2f"); gpuMath.setUniformForProgram("velocityCalc", "u_textureDim", [textureDim, textureDim], "2f");
gpuMath.setUniformForProgram("velocityCalc", "u_textureDimEdges", [textureDimEdges, textureDimEdges], "2f"); gpuMath.setUniformForProgram("velocityCalc", "u_textureDimEdges", [textureDimEdges, textureDimEdges], "2f");
gpuMath.setUniformForProgram("velocityCalc", "u_textureDimFaces", [textureDimFaces, textureDimFaces], "2f");
gpuMath.setUniformForProgram("velocityCalc", "u_textureDimCreases", [textureDimCreases, textureDimCreases], "2f");
gpuMath.setUniformForProgram("velocityCalc", "u_textureDimNodeCreases", [textureDimNodeCreases, textureDimNodeCreases], "2f");
gpuMath.createProgram("thetaCalc", vertexShader, document.getElementById("thetaCalcShader").text); gpuMath.createProgram("thetaCalc", vertexShader, document.getElementById("thetaCalcShader").text);
gpuMath.setUniformForProgram("thetaCalc", "u_normals", 0, "1i"); gpuMath.setUniformForProgram("thetaCalc", "u_normals", 0, "1i");
...@@ -357,6 +367,12 @@ function initDynamicModel(globals){ ...@@ -357,6 +367,12 @@ function initDynamicModel(globals){
} }
textureDimEdges = calcTextureSize(numEdges); textureDimEdges = calcTextureSize(numEdges);
var numNodeCreases = 0;
for (var i=0;i<nodes.length;i++){
numNodeCreases += nodes[i].numCreases();
}
textureDimNodeCreases = calcTextureSize(numNodeCreases);
var numFaces = geometry.faces.length; var numFaces = geometry.faces.length;
textureDimFaces = calcTextureSize(numFaces); textureDimFaces = calcTextureSize(numFaces);
...@@ -375,6 +391,7 @@ function initDynamicModel(globals){ ...@@ -375,6 +391,7 @@ function initDynamicModel(globals){
normals = new Float32Array(textureDimFaces*textureDimFaces*4); normals = new Float32Array(textureDimFaces*textureDimFaces*4);
creaseMeta = new Float32Array(textureDimCreases*textureDimCreases*4); creaseMeta = new Float32Array(textureDimCreases*textureDimCreases*4);
creaseMeta2 = new Float32Array(textureDimNodeCreases*textureDimNodeCreases*4);
creaseVectors = new Float32Array(textureDimCreases*textureDimCreases*4); creaseVectors = new Float32Array(textureDimCreases*textureDimCreases*4);
theta = new Float32Array(textureDimCreases*textureDimCreases*4); theta = new Float32Array(textureDimCreases*textureDimCreases*4);
lastTheta = new Float32Array(textureDimCreases*textureDimCreases*4); lastTheta = new Float32Array(textureDimCreases*textureDimCreases*4);
...@@ -397,6 +414,19 @@ function initDynamicModel(globals){ ...@@ -397,6 +414,19 @@ function initDynamicModel(globals){
lastTheta[i*4+3] = creases[i].getNormal2Index(); lastTheta[i*4+3] = creases[i].getNormal2Index();
} }
var index = 0;
for (var i=0;i<nodes.length;i++){
meta[i*4+2] = index;
var nodeCreases = nodes[i].creases;
meta[i*4+3] = nodeCreases.length;
for (var j=0;j<nodeCreases.length;j++){
creaseMeta2[index*4] = nodeCreases[j].getIndex();
creaseMeta2[index*4+1] = nodeCreases[j].getLengthTo(nodes[i]);
creaseMeta2[index*4+2] = nodeCreases[j].getNodeIndex(nodes[i]);
index++;
}
}
updateOriginalPosition(); updateOriginalPosition();
updateMaterials(true); updateMaterials(true);
updateFixed(); updateFixed();
...@@ -418,6 +448,7 @@ function initDynamicModel(globals){ ...@@ -418,6 +448,7 @@ function initDynamicModel(globals){
syncNodesAndEdges: syncNodesAndEdges, syncNodesAndEdges: syncNodesAndEdges,
updateOriginalPosition: updateOriginalPosition, updateOriginalPosition: updateOriginalPosition,
updateMaterials:updateMaterials, updateMaterials:updateMaterials,
updateCreasesMeta: updateCreasesMeta,
reset: reset, reset: reset,
pause: pause, pause: pause,
resume: resume, resume: resume,
......
...@@ -22,7 +22,7 @@ function initModel(globals){ ...@@ -22,7 +22,7 @@ function initModel(globals){
edges.push(new Beam([nodes[3], nodes[2]])); edges.push(new Beam([nodes[3], nodes[2]]));
var creases = []; var creases = [];
creases.push(new Crease(edges[1], 1, 0, 0, 0, nodes[1], nodes[0])); creases.push(new Crease(edges[2], 1, 0, 0, 0, nodes[3], nodes[1], 0));
_.each(nodes, function(node){ _.each(nodes, function(node){
globals.threeView.sceneAddModel(node.getObject3D()); globals.threeView.sceneAddModel(node.getObject3D());
......
...@@ -108,6 +108,10 @@ Node.prototype.numBeams = function(){ ...@@ -108,6 +108,10 @@ Node.prototype.numBeams = function(){
return this.beams.length; return this.beams.length;
}; };
Node.prototype.numCreases = function(){
return this.creases.length;
};
Node.prototype.getIndex = function(){//in nodes array Node.prototype.getIndex = function(){//in nodes array
return this.index; return this.index;
}; };
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment