Commit 42b9227b authored by amandaghassaei's avatar amandaghassaei

gpu with actuators

parent af3d6642
......@@ -241,9 +241,13 @@ define(['underscore', 'backbone', 'threeModel', 'lattice', 'plist', 'emWire', 'G
gpuMath.setUniformForProgram("velocityCalc", "u_compositeDs", 7, "1i");
gpuMath.setUniformForProgram("velocityCalc", "u_originalPosition", 8, "1i");
gpuMath.setUniformForProgram("velocityCalc", "u_lastQuaternion", 9, "1i");
gpuMath.setUniformForProgram("velocityCalc", "u_wires", 10, "1i");
gpuMath.setUniformForProgram("velocityCalc", "u_wiresMeta", 11, "1i");
gpuMath.setUniformForProgram("velocityCalc", "u_textureDim", [textureDim, textureDim], "2f");
gpuMath.setUniformForProgram("velocityCalc", "u_multiplier", 1/(plist.allUnitTypes[lattice.getUnits()].multiplier), "1f");
gpuMath.setUniformForProgram("velocityCalc", "u_latticePitch", [latticePitch.x, latticePitch.y, latticePitch.z], "3f");
gpuMath.setUniformForProgram("velocityCalc", "u_wiresMetaLength", this.wiresMeta.length/4, "1f");
gpuMath.setUniformForProgram("velocityCalc", "u_time", 0, "1f");
gpuMath.createProgram("positionCalc", vertexShader, positionCalcShader);
gpuMath.setUniformForProgram("positionCalc", "u_velocity", 0, "1i");
......@@ -369,6 +373,7 @@ define(['underscore', 'backbone', 'threeModel', 'lattice', 'plist', 'emWire', 'G
index++;
});
this.wiresMeta = wiresMeta;
gpuMath.initTextureFromData("u_wiresMeta", 1, this.wiresMeta.length/4, "FLOAT", this.wiresMeta, true);
},
_calcNumberDCConnectedComponents: function(cells){
......@@ -460,8 +465,8 @@ define(['underscore', 'backbone', 'threeModel', 'lattice', 'plist', 'emWire', 'G
gpuMath.step("quaternionCalc", ["u_lastTranslation", "u_lastQuaternion", "u_fixed", "u_neighborsXMapping",
"u_neighborsYMapping", "u_compositeKs"], "u_quaternion", "u_wires", "u_wiresMeta", time);
gpuMath.step("velocityCalc", ["u_lastVelocity", "u_lastTranslation", "u_mass", "u_fixed", "u_neighborsXMapping",
"u_neighborsYMapping", "u_compositeKs", "u_compositeDs", "u_originalPosition", "u_lastQuaternion"],
"u_velocity");
"u_neighborsYMapping", "u_compositeKs", "u_compositeDs", "u_originalPosition", "u_lastQuaternion", "u_wires",
"u_wiresMeta"], "u_velocity", time);
gpuMath.step("positionCalc", ["u_velocity", "u_lastTranslation", "u_fixed"], "u_translation");
if (shouldRender) {
......
......@@ -145,8 +145,8 @@ vec3 neighborOffset(float i, int neighborAxis){
return offset;
}
int calcNeighborAxis(int i){
return int(floor(float(i)/2.0+0.001));
int calcNeighborAxis(float i){
return int(floor(i/2.0+0.001));
}
int convertToInt(float num){
......@@ -154,9 +154,9 @@ int convertToInt(float num){
}
float getActuatorVoltage(float wireIndex){
vec2 wireCoord = vec2(0.5, (floor(wireIndex*4.0+0.001)+0.5)/u_wiresMetaLength);
vec2 wireCoord = vec2(0.5, (floor(wireIndex+0.001)+0.5)/u_wiresMetaLength);
vec4 wireMeta = texture2D(u_wiresMeta, wireCoord);
int type = convertToInt(1.2);
int type = convertToInt(wireMeta[0]);
if (type == -1) {
//no signal connected
return 0.0;
......@@ -198,6 +198,9 @@ void main(){
vec3 translation = texture2D(u_lastTranslation, scaledFragCoord).xyz;
vec4 quaternion = texture2D(u_lastQuaternion, scaledFragCoord);
vec4 wiring = texture2D(u_wires, scaledFragCoord);
bool isActuator = wiring.x < -0.5;//-1
vec3 rTotal = vec3(0);
float rContrib = 0.0;
......@@ -211,13 +214,10 @@ void main(){
vec3 neighborsYMapping = texture2D(u_neighborsYMapping, mappingIndex).xyz;
vec3 compositeKs = texture2D(u_compositeKs, mappingIndex).xyz;
vec4 wiring = texture2D(u_wires, mappingIndex);
bool isActuator = wiring.x < 0.5;//-1
for (int j=0;j<3;j++){
if (neighborsXMapping[j] < 0.0) continue;//no neighbor
int neighborAxis = calcNeighborAxis(j);
int neighborAxis = calcNeighborAxis(i*3.0+float(j));
vec2 neighborIndex = vec2(neighborsXMapping[j], neighborsYMapping[j]);
neighborIndex.x += 0.5;
......@@ -241,13 +241,13 @@ void main(){
}
}
vec4 neighborWiring = texture2D(u_wires, scaledNeighborIndex);
if (neighborWiring[0] < 0.5){
if (neighborWiring[1]>0.1){
actuation += 0.3*getActuatorVoltage(neighborWiring[1]-1.0);
} else if (neighborWiring[2]>0.1){
actuation += 0.3*getActuatorVoltage(neighborWiring[1]-1.0);
} else if (neighborWiring[3]>0.1){
if (neighborWiring[0] < -0.5){
if (neighborAxis == 0 && neighborWiring[1]>0.1){
actuation += 0.3*getActuatorVoltage(neighborWiring[1]-1.0);
} else if (neighborAxis == 1 && neighborWiring[2]>0.1){
actuation += 0.3*getActuatorVoltage(neighborWiring[2]-1.0);
} else if (neighborAxis == 2 && neighborWiring[3]>0.1){
actuation += 0.3*getActuatorVoltage(neighborWiring[3]-1.0);
}
}
if (neighborAxis == 0) actuatedD[0] *= 1.0+actuation;
......
#define M_PI 3.1415926535897932384626433832795
precision mediump float;
uniform vec2 u_textureDim;
......@@ -5,6 +7,9 @@ uniform vec3 u_gravity;
uniform float u_dt;
uniform float u_multiplier;
uniform vec3 u_latticePitch;
uniform float u_wiresMetaLength;
uniform float u_time;
uniform sampler2D u_lastVelocity;
uniform sampler2D u_lastTranslation;
......@@ -16,6 +21,8 @@ uniform sampler2D u_compositeKs;
uniform sampler2D u_compositeDs;
uniform sampler2D u_originalPosition;
uniform sampler2D u_lastQuaternion;
uniform sampler2D u_wires;
uniform sampler2D u_wiresMeta;
vec3 applyQuaternion(vec3 vector, vec4 quaternion) {
......@@ -54,6 +61,45 @@ vec3 neighborOffset(float i){
return offset;
}
int calcNeighborAxis(float i){
return int(floor(i/2.0+0.001));
}
int convertToInt(float num){
return int(floor(num+0.001));
}
float getActuatorVoltage(float wireIndex){
vec2 wireCoord = vec2(0.5, (floor(wireIndex+0.001)+0.5)/u_wiresMetaLength);
vec4 wireMeta = texture2D(u_wiresMeta, wireCoord);
int type = convertToInt(wireMeta[0]);
if (type == -1) {
//no signal connected
return 0.0;
}
float frequency = wireMeta[1];
float period = 1.0/frequency;
float phase = wireMeta[2];
float currentPhase = mod(u_time+phase*period, period)/period;
if (type == 0){
return 0.5*sin(2.0*M_PI*currentPhase);
}
if (type == 1){
float pwm = wireMeta[3];
if (currentPhase < pwm) return 0.5;
return -0.5;
}
if (type == 2){
if (wireMeta[3]>0.5) return 0.5-currentPhase;
return currentPhase-0.5;
}
if (type == 3){
if (currentPhase < 0.5) return currentPhase*2.0-0.5;
return 0.5-(currentPhase-0.5)*2.0;
}
return 0.0;
}
void main(){
vec2 fragCoord = gl_FragCoord.xy;
......@@ -70,6 +116,9 @@ void main(){
vec3 lastVelocity = texture2D(u_lastVelocity, scaledFragCoord).xyz;
vec4 quaternion = texture2D(u_lastQuaternion, scaledFragCoord);
vec4 wiring = texture2D(u_wires, scaledFragCoord);
bool isActuator = wiring[0] < -0.5;//-1
vec3 force = u_gravity*mass;
//simple collision
......@@ -92,6 +141,8 @@ void main(){
for (int j=0;j<3;j++){
if (neighborsXMapping[j] < 0.0) continue;//no neighbor
int neighborAxis = calcNeighborAxis(i*3.0+float(j));
vec2 neighborIndex = vec2(neighborsXMapping[j], neighborsYMapping[j]);
neighborIndex.x += 0.5;
neighborIndex.y += 0.5;
......@@ -102,10 +153,33 @@ void main(){
vec4 neighborQuaternion = texture2D(u_lastQuaternion, scaledNeighborIndex);
vec3 nominalD = neighborOffset(i*3.0+float(j));
vec3 actuatedD = nominalD;
vec3 rotatedNomD = applyQuaternion(nominalD, quaternion);
vec3 neighbRotatedNomD = applyQuaternion(nominalD, neighborQuaternion);
vec3 actuatedD = vec3(nominalD[0], nominalD[1], nominalD[2]);
float actuation = 0.0;
if (isActuator){
if (neighborAxis == 0 && wiring[1]>0.1){//>0
actuation += 0.3*getActuatorVoltage(wiring[1]-1.0);
} else if (neighborAxis == 1 && wiring[2]>0.1){
actuation += 0.3*getActuatorVoltage(wiring[2]-1.0);
} else if (neighborAxis == 2 && wiring[3]>0.1){
actuation += 0.3*getActuatorVoltage(wiring[3]-1.0);
}
}
vec4 neighborWiring = texture2D(u_wires, scaledNeighborIndex);
if (neighborWiring[0] < -0.5){
if (neighborAxis == 0 && neighborWiring[1]>0.1){
actuation += 0.3*getActuatorVoltage(neighborWiring[1]-1.0);
} else if (neighborAxis == 1 && neighborWiring[2]>0.1){
actuation += 0.3*getActuatorVoltage(neighborWiring[2]-1.0);
} else if (neighborAxis == 2 && neighborWiring[3]>0.1){
actuation += 0.3*getActuatorVoltage(neighborWiring[3]-1.0);
}
}
if (neighborAxis == 0) actuatedD[0] *= 1.0+actuation;
else if (neighborAxis == 1) actuatedD[1] *= 1.0+actuation;
else if (neighborAxis == 2) actuatedD[2] *= 1.0+actuation;
vec3 rotatedNomD = applyQuaternion(actuatedD, quaternion);
vec3 neighbRotatedNomD = applyQuaternion(actuatedD, neighborQuaternion);
vec3 rotatedNominalD = (rotatedNomD + neighbRotatedNomD)*0.5;
......
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