Commit d237a10b authored by amandaghassaei's avatar amandaghassaei

starting sim

parent e686ee26
...@@ -9,6 +9,145 @@ ...@@ -9,6 +9,145 @@
<link rel="stylesheet" type="text/css" href="dependencies/jquery-ui.min.css"/> <link rel="stylesheet" type="text/css" href="dependencies/jquery-ui.min.css"/>
<link rel="stylesheet" type="text/css" href="main.css"/> <link rel="stylesheet" type="text/css" href="main.css"/>
<script id="vertexShader" type="x-shader/x-vertex">
attribute vec2 a_position;
void main() {
gl_Position = vec4(a_position, 0, 1);
}
</script>
<script id="packToBytesShader" type="x-shader/x-fragment">
precision mediump float;
uniform vec2 u_floatTextureDim;
uniform sampler2D u_floatTexture;
uniform float u_vectorLength;
float shift_right (float v, float amt) {
v = floor(v) + 0.5;
return floor(v / exp2(amt));
}
float shift_left (float v, float amt) {
return floor(v * exp2(amt) + 0.5);
}
float mask_last (float v, float bits) {
return mod(v, shift_left(1.0, bits));
}
float extract_bits (float num, float from, float to) {
from = floor(from + 0.5); to = floor(to + 0.5);
return mask_last(shift_right(num, from), to - from);
}
vec4 encode_float (float val) {
if (val == 0.0) return vec4(0, 0, 0, 0);
float sign = val > 0.0 ? 0.0 : 1.0;
val = abs(val);
float exponent = floor(log2(val));
float biased_exponent = exponent + 127.0;
float fraction = ((val / exp2(exponent)) - 1.0) * 8388608.0;
float t = biased_exponent / 2.0;
float last_bit_of_biased_exponent = fract(t) * 2.0;
float remaining_bits_of_biased_exponent = floor(t);
float byte4 = extract_bits(fraction, 0.0, 8.0) / 255.0;
float byte3 = extract_bits(fraction, 8.0, 16.0) / 255.0;
float byte2 = (last_bit_of_biased_exponent * 128.0 + extract_bits(fraction, 16.0, 23.0)) / 255.0;
float byte1 = (sign * 128.0 + remaining_bits_of_biased_exponent) / 255.0;
return vec4(byte4, byte3, byte2, byte1);
}
void main(){
vec2 fragCoord = gl_FragCoord.xy;
float textureXcoord = floor((fragCoord.x - 0.5)/u_vectorLength+0.0001) + 0.5;
vec4 data = texture2D(u_floatTexture, vec2(textureXcoord, fragCoord.y)/u_floatTextureDim);
int textureIndex = int(floor(mod(fragCoord.x-0.5+0.0001, u_vectorLength)));
if (textureIndex == 0) gl_FragColor = encode_float(data[0]);
else if (textureIndex == 1) gl_FragColor = encode_float(data[1]);
else if (textureIndex == 2) gl_FragColor = encode_float(data[2]);
else if (textureIndex == 3) gl_FragColor = encode_float(data[3]);
}
</script>
<script id="zeroTexture" type="x-shader/x-fragment">
void main(){
gl_FragColor = vec4(0.0, 0.0, 0.0, 0.0);
}
</script>
<script id="positionCalcShader" type="x-shader/x-fragment">
precision mediump float;
uniform vec2 u_textureDim;
uniform float u_dt;
uniform sampler2D u_lastPosition;
uniform sampler2D u_velocity;
uniform sampler2D u_mass;
void main(){
vec2 fragCoord = gl_FragCoord.xy;
vec2 scaledFragCoord = fragCoord/u_textureDim;
float isFixed = texture2D(u_mass, scaledFragCoord).y;
if (isFixed == 1.0){
gl_FragColor = vec4(0.0, 0.0, 0.0, 0.0);
return;
}
vec3 lastPosition = texture2D(u_lastPosition, scaledFragCoord).xyz;
vec3 velocity = texture2D(u_velocity, scaledFragCoord).xyz;
vec3 position = velocity*u_dt + lastPosition;
gl_FragColor = vec4(position,0.0);
}
</script>
<script id="velocityCalcShader" type="x-shader/x-fragment">
precision mediump float;
uniform vec2 u_textureDim;
uniform float u_dt;
uniform sampler2D u_lastPosition;
uniform sampler2D u_lastVelocity;
uniform sampler2D u_originalPosition;
uniform sampler2D u_externalForces;
uniform sampler2D u_mass;
uniform sampler2D u_meta;
uniform sampler2D u_beamK;
uniform sampler2D u_beamD;
void main(){
vec2 fragCoord = gl_FragCoord.xy;
vec2 scaledFragCoord = fragCoord/u_textureDim;
vec2 mass = texture2D(u_mass, scaledFragCoord).xy;
if (mass.y == 1.0){
gl_FragColor = vec4(0.0, 0.0, 0.0, 0.0);
return;
}
vec3 force = texture2D(u_externalForces, scaledFragCoord).xyz;
force.y = 1.0;
vec3 lastPosition = texture2D(u_lastPosition, scaledFragCoord).xyz;
vec3 lastVelocity = texture2D(u_lastVelocity, scaledFragCoord).xyz;
vec3 originalPosition = texture2D(u_originalPosition, scaledFragCoord).xyz;
vec4 neighborIndices = texture2D(u_meta, scaledFragCoord);
vec4 beamKs = texture2D(u_beamK, scaledFragCoord);
vec4 beamDs = texture2D(u_beamD, scaledFragCoord);
for (int j=0;j<4;j++){
float neighborIndex1D = neighborIndices[j];
if (neighborIndex1D<0.0) continue;//no beam
vec2 neighborIndex = vec2(mod(neighborIndex1D, u_textureDim.x)+0.5, floor(neighborIndex1D/u_textureDim.x)+0.5);
vec2 scaledNeighborIndex = neighborIndex/u_textureDim;
vec3 neighborLastPosition = texture2D(u_lastPosition, scaledNeighborIndex).xyz;
vec3 neighborLastVelocity = texture2D(u_lastVelocity, scaledNeighborIndex).xyz;
vec3 neighborOriginalPosition = texture2D(u_originalPosition, scaledNeighborIndex).xyz;
vec3 nominalDist = neighborOriginalPosition-originalPosition;
vec3 deltaP = neighborLastPosition-lastPosition+nominalDist;
// deltaP -= normalize(deltaP)*length(nominalDist);
vec3 deltaV = neighborLastVelocity-lastVelocity;
vec3 _force = deltaP*beamKs[j] + deltaV*beamDs[j];
force += _force;
}
vec3 velocity = force*u_dt/mass.x + lastVelocity;
gl_FragColor = vec4(velocity,0.0);
}
</script>
<script type="text/javascript" src="dependencies/jquery-3.1.1.min.js"></script> <script type="text/javascript" src="dependencies/jquery-3.1.1.min.js"></script>
<script type="text/javascript" src="dependencies/jquery-ui.min.js"></script> <script type="text/javascript" src="dependencies/jquery-ui.min.js"></script>
<script type="text/javascript" src="dependencies/flat-ui.min.js"></script> <script type="text/javascript" src="dependencies/flat-ui.min.js"></script>
...@@ -23,11 +162,16 @@ ...@@ -23,11 +162,16 @@
<script type="text/javascript" src="js/controls.js"></script> <script type="text/javascript" src="js/controls.js"></script>
<script type="text/javascript" src="js/threeView.js"></script> <script type="text/javascript" src="js/threeView.js"></script>
<script type="text/javascript" src="js/globals.js"></script> <script type="text/javascript" src="js/globals.js"></script>
<script type="text/javascript" src="js/node.js"></script>
<script type="text/javascript" src="js/beam.js"></script>
<script type="text/javascript" src="js/model.js"></script>
<script type="text/javascript" src="js/dynamicModel.js"></script>
<script type="text/javascript" src="js/main.js"></script> <script type="text/javascript" src="js/main.js"></script>
</head> </head>
<body> <body>
<div id="threeContainer"></div> <div id="threeContainer"></div>
<canvas id="gpuMathCanvas"></canvas>
<div id="controlsLeft" class="flipped"> <div id="controlsLeft" class="flipped">
<div> <div>
<a id="logo" target="_blank" href="http://cba.mit.edu/"> <a id="logo" target="_blank" href="http://cba.mit.edu/">
......
/**
* Created by ghassaei on 9/16/16.
*/
var beamMaterialHighlight = new THREE.LineBasicMaterial({color: 0xffffff, linewidth: 4});
var beamMaterial = new THREE.LineBasicMaterial({color: 0x000000, linewidth: 4});
function Beam(nodes){
this.type = "beam";
nodes[0].addBeam(this);
nodes[1].addBeam(this);
this.vertices = [nodes[0].getPosition(), nodes[1].getPosition()];
this.nodes = nodes;
var lineGeometry = new THREE.Geometry();
lineGeometry.dynamic = true;
lineGeometry.vertices = this.vertices;
this.object3D = new THREE.Line(lineGeometry, beamMaterial);
this.object3D._myBeam = this;
}
Beam.prototype.highlight = function(){
this.object3D.material = beamMaterialHighlight;
};
Beam.prototype.unhighlight = function(){
this.object3D.material = beamMaterial;
};
Beam.prototype.getLength = function(){
return this.getVector().length();
};
Beam.prototype.isFixed = function(){
return this.nodes[0].fixed && this.nodes[1].fixed;
};
Beam.prototype.getVector = function(){
return this.vertices[0].clone().sub(this.vertices[1]);
};
//dynamic solve
Beam.prototype.getK = function(){
return globals.axialStiffness/this.getLength();
};
Beam.prototype.getD = function(){
return globals.percentDamping*2*Math.sqrt(this.getK()*this.getMinMass());
};
Beam.prototype.getNaturalFrequency = function(){
return Math.sqrt(this.getK()/this.getMinMass());
};
Beam.prototype.getMinMass = function(){
var minMass = this.nodes[0].getSimMass();
if (this.nodes[1].getSimMass()<minMass) minMass = this.nodes[1].getSimMass();
return minMass;
};
Beam.prototype.getOtherNode = function(node){
if (this.nodes[0] == node) return this.nodes[1];
return this.nodes[0];
};
//render
Beam.prototype.getObject3D = function(){
return this.object3D;
};
Beam.prototype.render = function(shouldComputeLineDistance){
this.object3D.geometry.verticesNeedUpdate = true;
this.object3D.geometry.computeBoundingSphere();
if (shouldComputeLineDistance) this.object3D.geometry.computeLineDistances();//for dashed lines
};
//deallocate
Beam.prototype.destroy = function(){
var self = this;
_.each(this.nodes, function(node){
node.removeBeam(self);
});
this.vertices = null;
this.object3D._myBeam = null;
this.object3D = null;
this.nodes = null;
};
\ No newline at end of file
This diff is collapsed.
...@@ -18,7 +18,8 @@ function initGlobals(){ ...@@ -18,7 +18,8 @@ function initGlobals(){
panelStiffness: 1, panelStiffness: 1,
//dynamic sim settings //dynamic sim settings
percentDamping: 1 percentDamping: 1,
density: 1
}; };
var isMobile = { var isMobile = {
...@@ -44,7 +45,7 @@ function initGlobals(){ ...@@ -44,7 +45,7 @@ function initGlobals(){
if(isMobile.any()) _globals.dynamicSimVisible = false; if(isMobile.any()) _globals.dynamicSimVisible = false;
_globals.threeView = initThreeView(_globals); _globals.threeView = initThreeView(_globals);
// _globals.gpuMath = initGPUMath(); _globals.gpuMath = initGPUMath();
_globals.controls = initControls(_globals); _globals.controls = initControls(_globals);
return _globals; return _globals;
......
...@@ -16,6 +16,7 @@ $(function() { ...@@ -16,6 +16,7 @@ $(function() {
var isDragging = false; var isDragging = false;
var mouseDown = false; var mouseDown = false;
$(document).dblclick(function() { $(document).dblclick(function() {
}); });
...@@ -34,13 +35,13 @@ $(function() { ...@@ -34,13 +35,13 @@ $(function() {
} }
e.preventDefault(); e.preventDefault();
// globals.controls.hideMoreInfo();
mouse.x = (e.clientX/window.innerWidth)*2-1; mouse.x = (e.clientX/window.innerWidth)*2-1;
mouse.y = - (e.clientY/window.innerHeight)*2+1; mouse.y = - (e.clientY/window.innerHeight)*2+1;
raycaster.setFromCamera(mouse, globals.threeView.camera); raycaster.setFromCamera(mouse, globals.threeView.camera);
} }
globals = initGlobals(); globals = initGlobals();
globals.model = initModel(globals);
globals.dynamicModel = initDynamicModel(globals);
globals.threeView.render(); globals.threeView.render();
}); });
\ No newline at end of file
/**
* Created by amandaghassaei on 2/24/17.
*/
//wireframe model and folding structure
function initModel(globals){
var nodes = [];
nodes.push(new Node(new THREE.Vector3(0,0,0), nodes.length));
nodes.push(new Node(new THREE.Vector3(0,0,10), nodes.length));
nodes.push(new Node(new THREE.Vector3(10,0,0), nodes.length));
nodes.push(new Node(new THREE.Vector3(0,0,-10), nodes.length));
nodes[0].setFixed(true);
nodes[1].setFixed(true);
nodes[2].setFixed(true);
var edges = [];
edges.push(new Beam([nodes[0], nodes[1]]));
edges.push(new Beam([nodes[1], nodes[2]]));
edges.push(new Beam([nodes[0], nodes[2]]));
edges.push(new Beam([nodes[3], nodes[0]]));
edges.push(new Beam([nodes[3], nodes[2]]));
_.each(nodes, function(node){
globals.threeView.sceneAddModel(node.getObject3D());
});
_.each(edges, function(edge){
globals.threeView.sceneAddModel(edge.getObject3D());
});
function getNodes(){
return nodes;
}
function getEdges(){
return edges;
}
return {
getNodes: getNodes,
getEdges: getEdges
}
}
\ No newline at end of file
/**
* Created by ghassaei on 9/16/16.
*/
var nodeMaterial = new THREE.MeshBasicMaterial({color: 0x000000, side:THREE.DoubleSide});
var nodeMaterialFixed = new THREE.MeshBasicMaterial({color: 0x000000, side:THREE.DoubleSide});
var nodeMaterialDelete = new THREE.MeshBasicMaterial({color: 0xff0000, side:THREE.DoubleSide});
var nodeMaterialHighlight = new THREE.MeshBasicMaterial({color: 0xff00ff, side:THREE.DoubleSide});
var nodeGeo = new THREE.CircleGeometry(0.2,20);
nodeGeo.rotateX(Math.PI/2);
var nodeFixedGeo = new THREE.CubeGeometry(1, 0.5, 1);
nodeFixedGeo.applyMatrix( new THREE.Matrix4().makeTranslation(0, 0.25, 0) );
function Node(position, index){
this.type = "node";
this.index = index;
this._originalPosition = position.clone();
this.object3D = new THREE.Mesh(nodeGeo, nodeMaterial);
this.object3D._myNode = this;
this.beams = [];
this.externalForce = null;
this.fixed = false;
this.render(new THREE.Vector3(0,0,0));
}
Node.prototype.setFixed = function(fixed){
this.fixed = fixed;
if (fixed) {
this.object3D.material = nodeMaterialFixed;
this.object3D.geometry = nodeFixedGeo;
if (this.externalForce) this.externalForce.hide();
}
else {
this.object3D.material = nodeMaterial;
this.object3D.geometry = nodeGeo;
if (this.externalForce) this.externalForce.show();
}
};
Node.prototype.isFixed = function(){
return this.fixed;
};
//forces
Node.prototype.addExternalForce = function(force){
// this.externalForce = force;
// var position = this.getOriginalPosition();
// this.externalForce.setOrigin(position);
// if (this.fixed) this.externalForce.hide();
};
Node.prototype.getExternalForce = function(){
if (!this.externalForce) return new THREE.Vector3(0,0,0);
return this.externalForce.getForce();
};
Node.prototype.getLocalLength = function(){
var length = 0;
_.each(this.beams, function(beam){
length += beam.getLength(true)/2;
});
return length;
};
Node.prototype.getMass = function(){
return globals.density*this.getLocalLength();
};
Node.prototype.addBeam = function(beam){
this.beams.push(beam);
};
Node.prototype.removeBeam = function(beam){
if (this.beams === null) return;
var index = this.beams.indexOf(beam);
if (index>=0) this.beams.splice(index, 1);
};
Node.prototype.getBeams = function(){
return this.beams;
};
Node.prototype.getIndex = function(){//in nodes array
return this.index;
};
Node.prototype.getObject3D = function(){
return this.object3D;
};
Node.prototype.setDeleteMode = function(){
this.object3D.material = nodeMaterialDelete;
};
Node.prototype.highlight = function(){
this.object3D.material = nodeMaterialHighlight;
};
Node.prototype.unhighlight = function(){
if (this.fixed) {
this.object3D.material = nodeMaterialFixed;
}
else {
this.object3D.material = nodeMaterial;
}
};
Node.prototype.hide = function(){
this.object3D.visible = false;
};
Node.prototype.render = function(position){
position.add(this.getOriginalPosition());
this.object3D.position.set(position.x, position.y, position.z);
};
//dynamic solve
Node.prototype.getOriginalPosition = function(){
return this._originalPosition.clone();
};
Node.prototype.getPosition = function(){
return this.object3D.position;
};
Node.prototype.getSimMass = function(){
return 1;
};
//deallocate
Node.prototype.destroy = function(){
//object3D is removed in outer scope
this.object3D._myNode = null;
this.object3D = null;
this.beams = null;
this.externalForce = null;
};
\ No newline at end of file
...@@ -50,20 +50,21 @@ function initThreeView(globals) { ...@@ -50,20 +50,21 @@ function initThreeView(globals) {
} }
function render() { function render() {
renderer.render(scene, camera); // renderer.render(scene, camera);
} }
function startAnimation(callback){ function startAnimation(callback){
console.log("starting animation"); console.log("starting animation");
// _loop(function(){ _loop(function(){
// if (!globals.stlEditing) callback();//only run dynamic sim if not editing stl // if (!globals.stlEditing) //only run dynamic sim if not editing stl
// _render(); callback();
// }); _render();
});
} }
function _render(){ function _render(){
// renderer.render(scene, camera); renderer.render(scene, camera);
} }
function _loop(callback){ function _loop(callback){
...@@ -85,6 +86,18 @@ function initThreeView(globals) { ...@@ -85,6 +86,18 @@ function initThreeView(globals) {
patternWrapper.children = []; patternWrapper.children = [];
} }
function sceneAddModel(object){
modelWrapper.add(object);
}
function sceneRemoveModel(object){
modelWrapper.remove(object);
}
function sceneClearModel(object){
modelWrapper.children =[];
}
function sceneMakeModelFromPattern(){ function sceneMakeModelFromPattern(){
modelWrapper.children = []; modelWrapper.children = [];
//todo copy pattern to model //todo copy pattern to model
...@@ -110,9 +123,12 @@ function initThreeView(globals) { ...@@ -110,9 +123,12 @@ function initThreeView(globals) {
return { return {
sceneRemoveCrease: sceneRemoveCrease,
sceneAddCrease: sceneAddCrease, sceneAddCrease: sceneAddCrease,
sceneRemoveCrease: sceneRemoveCrease,
sceneClearPattern: sceneClearPattern, sceneClearPattern: sceneClearPattern,
sceneAddModel: sceneAddModel,
sceneRemoveModel: sceneRemoveModel,
sceneClearModel: sceneClearModel,
render: render, render: render,
onWindowResize: onWindowResize, onWindowResize: onWindowResize,
startAnimation: startAnimation, startAnimation: startAnimation,
......
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