Commit 4a33f810 authored by amandaghassaei's avatar amandaghassaei
Browse files

rotation in shader

parent 5121a394
......@@ -5,9 +5,9 @@
define(['underscore', 'backbone', 'threeModel', 'lattice', 'plist', 'emWire', 'GPUMath', "text!simulation/shaders/vertexShader.js",
"text!simulation/function/EM/shaders/velocityCalcShader.js", "text!simulation/shaders/packToBytesShader.js", "text!simulation/function/EM/shaders/positionCalcShader.js",
"text!simulation/function/EM/shaders/angVelocityCalcShader.js", 'emSimCell'],
"text!simulation/function/EM/shaders/angVelocityCalcShader.js", "text!simulation/function/EM/shaders/quaternionCalcShader.js", 'emSimCell'],
function(_, Backbone, three, lattice, plist, EMWire, gpuMath, vertexShader, velocityCalcShader, packToBytesShader,
positionCalcShader, angVelocityCalcShader) {
positionCalcShader, angVelocityCalcShader, quaternionCalcShader) {
var EMSimLattice = Backbone.Model.extend({
......@@ -280,22 +280,6 @@ define(['underscore', 'backbone', 'threeModel', 'lattice', 'plist', 'emWire', 'G
//programs
//gpuMath.createProgram("quaternionCalc", vertexShader, quaternionCalcShader);
//gpuMath.createProgram("angVelocityCalc", vertexShader, quaternionCalcShader);
//gpuMath.setUniformForProgram("angVelocityCalc", "u_lastTranslation", 0, "1i");
//gpuMath.setUniformForProgram("angVelocityCalc", "u_lastQuaternion", 1, "1i");
//gpuMath.setUniformForProgram("angVelocityCalc", "u_mass", 2, "1i");
//gpuMath.setUniformForProgram("angVelocityCalc", "u_neighborsXMapping", 3, "1i");
//gpuMath.setUniformForProgram("angVelocityCalc", "u_neighborsYMapping", 4, "1i");
//gpuMath.setUniformForProgram("angVelocityCalc", "u_compositeKs", 5, "1i");
//gpuMath.setUniformForProgram("angVelocityCalc", "u_wires", 6, "1i");
//gpuMath.setUniformForProgram("angVelocityCalc", "u_wiresMeta", 7, "1i");
//gpuMath.setUniformForProgram("angVelocityCalc", "u_textureDim", [textureDim, textureDim], "2f");
//gpuMath.setUniformForProgram("angVelocityCalc", "u_latticePitch", [latticePitch.x, latticePitch.y, latticePitch.z], "3f");
//gpuMath.setUniformForProgram("angVelocityCalc", "u_wiresMetaLength", this.wiresMeta.length/4, "1f");
//gpuMath.setUniformForProgram("angVelocityCalc", "u_time", 0, "1f");
//
gpuMath.createProgram("angVelocityCalc", vertexShader, angVelocityCalcShader);
gpuMath.setUniformForProgram("angVelocityCalc", "u_lastAngVelocity", 0, "1i");
......@@ -315,6 +299,12 @@ define(['underscore', 'backbone', 'threeModel', 'lattice', 'plist', 'emWire', 'G
gpuMath.setUniformForProgram("angVelocityCalc", "u_wiresMetaLength", this.wiresMeta.length/4, "1f");
gpuMath.setUniformForProgram("angVelocityCalc", "u_time", 0, "1f");
gpuMath.createProgram("quaternionCalc", vertexShader, quaternionCalcShader);
gpuMath.setUniformForProgram("quaternionCalc", "u_angVelocity", 0, "1i");
gpuMath.setUniformForProgram("quaternionCalc", "u_lastQuaternion", 1, "1i");
gpuMath.setUniformForProgram("quaternionCalc", "u_mass", 2, "1i");
gpuMath.setUniformForProgram("quaternionCalc", "u_textureDim", [textureDim, textureDim], "2f");
gpuMath.createProgram("velocityCalc", vertexShader, velocityCalcShader);
gpuMath.setUniformForProgram("velocityCalc", "u_lastVelocity", 0, "1i");
gpuMath.setUniformForProgram("velocityCalc", "u_lastTranslation", 1, "1i");
......@@ -561,77 +551,80 @@ define(['underscore', 'backbone', 'threeModel', 'lattice', 'plist', 'emWire', 'G
gpuMath.setUniformForProgram("positionCalc", "u_dt", dt, "1f");
gpuMath.setProgram("angVelocityCalc");
gpuMath.setUniformForProgram("angVelocityCalc", "u_dt", dt, "1f");
//gpuMath.setProgram("quaternionCalc");
//gpuMath.setUniformForProgram("quaternionCalc", "u_dt", dt, "1f");
gpuMath.setProgram("quaternionCalc");
gpuMath.setUniformForProgram("quaternionCalc", "u_dt", dt, "1f");
},
iter: function(time, runConstants, shouldRender){
////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_neighborsXMapping",
// "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_mass"], "u_translation");
//
//if (shouldRender) {
// var textureSize = this.textureSize[0]*this.textureSize[1];
//
// //get position
// var vectorLength = 3;
// gpuMath.setProgram("packToBytes");
// gpuMath.setUniformForProgram("packToBytes", "u_vectorLength", vectorLength, "1f");
// gpuMath.setSize(this.textureSize[0]*vectorLength, this.textureSize[1]);
// gpuMath.step("packToBytes", ["u_translation"], "outputPositionBytes");
// var pixels = new Uint8Array(textureSize * 4*vectorLength);
// if (gpuMath.readyToRead()) {
// gpuMath.readPixels(0, 0, this.textureSize[0] * vectorLength, this.textureSize[1], pixels);
// var parsedPixels = new Float32Array(pixels.buffer);
// var cells = lattice.getCells();
// var multiplier = 1 / (plist.allUnitTypes[lattice.getUnits()].multiplier);
// for (var i = 0; i < textureSize; i++) {
// var rgbaIndex = 4 * i;
// if (this.mass[rgbaIndex+1] < 0) continue;//no more cells
// var index = [this.cellsArrayMapping[rgbaIndex], this.cellsArrayMapping[rgbaIndex + 1], this.cellsArrayMapping[rgbaIndex + 2]];
// var parsePixelsIndex = vectorLength * i;
// var translation = [parsedPixels[parsePixelsIndex], parsedPixels[parsePixelsIndex + 1], parsedPixels[parsePixelsIndex + 2]];
// var position = [this.originalPosition[rgbaIndex], this.originalPosition[rgbaIndex + 1], this.originalPosition[rgbaIndex + 2]];
// position[0] += multiplier * translation[0];
// position[1] += multiplier * translation[1];
// position[2] += multiplier * translation[2];
// cells[index[0]][index[1]][index[2]].object3D.position.set(position[0], position[1], position[2]);
// }
// }
//
// vectorLength = 4;
// gpuMath.setUniformForProgram("packToBytes", "u_vectorLength", vectorLength, "1f");
// gpuMath.setSize(this.textureSize[0]*vectorLength, this.textureSize[1]);
// gpuMath.step("packToBytes", ["u_quaternion"], "outputQuaternionBytes");
// pixels = new Uint8Array(textureSize * 4*vectorLength);
// if (gpuMath.readyToRead()) {
// gpuMath.readPixels(0, 0, this.textureSize[0] * vectorLength, this.textureSize[1], pixels);
// parsedPixels = new Float32Array(pixels.buffer);
// for (var i = 0; i < textureSize; i++) {
// var rgbaIndex = 4 * i;
// if (this.mass[rgbaIndex+1] < 0) break;//no more cells
// var index = [this.cellsArrayMapping[rgbaIndex], this.cellsArrayMapping[rgbaIndex + 1], this.cellsArrayMapping[rgbaIndex + 2]];
// var parsePixelsIndex = vectorLength * i;
//
// var quaternion = this._multiplyQuaternions([parsedPixels[parsePixelsIndex], parsedPixels[parsePixelsIndex + 1], parsedPixels[parsePixelsIndex + 2], parsedPixels[parsePixelsIndex + 3]],
// [this.originalQuaternion[rgbaIndex], this.originalQuaternion[rgbaIndex+1], this.originalQuaternion[rgbaIndex+2], this.originalQuaternion[rgbaIndex+3]]);
// var rotation = this._eulerFromQuaternion(quaternion);
//
// cells[index[0]][index[1]][index[2]].object3D.rotation.set(rotation[0], rotation[1], rotation[2]);
// }
// }
//
// gpuMath.setSize(this.textureSize[0], this.textureSize[1]);
//}
//
//gpuMath.swapTextures("u_velocity", "u_lastVelocity");
//gpuMath.swapTextures("u_translation", "u_lastTranslation");
////gpuMath.swapTextures("u_quaternion", "u_lastQuaternion");
//return;
gpuMath.step("velocityCalc", ["u_lastVelocity", "u_lastTranslation", "u_mass", "u_neighborsXMapping",
"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_mass"], "u_translation");
gpuMath.step("angVelocityCalc", ["u_lastAngVelocity", "u_lastVelocity", "u_lastTranslation", "u_mass", "u_neighborsXMapping",
"u_neighborsYMapping", "u_compositeKs", "u_compositeDs", "u_lastQuaternion", "u_wires",
"u_wiresMeta"], "u_angVelocity", time);
gpuMath.step("quaternionCalc", ["u_angVelocity", "u_lastQuaternion", "u_mass"], "u_quaternion");
if (shouldRender) {
var textureSize = this.textureSize[0]*this.textureSize[1];
//get position
var vectorLength = 3;
gpuMath.setProgram("packToBytes");
gpuMath.setUniformForProgram("packToBytes", "u_vectorLength", vectorLength, "1f");
gpuMath.setSize(this.textureSize[0]*vectorLength, this.textureSize[1]);
gpuMath.step("packToBytes", ["u_translation"], "outputPositionBytes");
var pixels = new Uint8Array(textureSize * 4*vectorLength);
if (gpuMath.readyToRead()) {
gpuMath.readPixels(0, 0, this.textureSize[0] * vectorLength, this.textureSize[1], pixels);
var parsedPixels = new Float32Array(pixels.buffer);
var cells = lattice.getCells();
var multiplier = 1 / (plist.allUnitTypes[lattice.getUnits()].multiplier);
for (var i = 0; i < textureSize; i++) {
var rgbaIndex = 4 * i;
if (this.mass[rgbaIndex+1] < 0) continue;//no more cells
var index = [this.cellsArrayMapping[rgbaIndex], this.cellsArrayMapping[rgbaIndex + 1], this.cellsArrayMapping[rgbaIndex + 2]];
var parsePixelsIndex = vectorLength * i;
var translation = [parsedPixels[parsePixelsIndex], parsedPixels[parsePixelsIndex + 1], parsedPixels[parsePixelsIndex + 2]];
var position = [this.originalPosition[rgbaIndex], this.originalPosition[rgbaIndex + 1], this.originalPosition[rgbaIndex + 2]];
position[0] += multiplier * translation[0];
position[1] += multiplier * translation[1];
position[2] += multiplier * translation[2];
cells[index[0]][index[1]][index[2]].object3D.position.set(position[0], position[1], position[2]);
}
}
vectorLength = 4;
gpuMath.setUniformForProgram("packToBytes", "u_vectorLength", vectorLength, "1f");
gpuMath.setSize(this.textureSize[0]*vectorLength, this.textureSize[1]);
gpuMath.step("packToBytes", ["u_quaternion"], "outputQuaternionBytes");
pixels = new Uint8Array(textureSize * 4*vectorLength);
if (gpuMath.readyToRead()) {
gpuMath.readPixels(0, 0, this.textureSize[0] * vectorLength, this.textureSize[1], pixels);
parsedPixels = new Float32Array(pixels.buffer);
for (var i = 0; i < textureSize; i++) {
var rgbaIndex = 4 * i;
if (this.mass[rgbaIndex+1] < 0) break;//no more cells
var index = [this.cellsArrayMapping[rgbaIndex], this.cellsArrayMapping[rgbaIndex + 1], this.cellsArrayMapping[rgbaIndex + 2]];
var parsePixelsIndex = vectorLength * i;
var quaternion = this._multiplyQuaternions([parsedPixels[parsePixelsIndex], parsedPixels[parsePixelsIndex + 1], parsedPixels[parsePixelsIndex + 2], parsedPixels[parsePixelsIndex + 3]],
[this.originalQuaternion[rgbaIndex], this.originalQuaternion[rgbaIndex+1], this.originalQuaternion[rgbaIndex+2], this.originalQuaternion[rgbaIndex+3]]);
var rotation = this._eulerFromQuaternion(quaternion);
cells[index[0]][index[1]][index[2]].object3D.rotation.set(rotation[0], rotation[1], rotation[2]);
}
}
gpuMath.setSize(this.textureSize[0], this.textureSize[1]);
}
gpuMath.swapTextures("u_velocity", "u_lastVelocity");
gpuMath.swapTextures("u_translation", "u_lastTranslation");
gpuMath.swapTextures("u_angVelocity", "u_lastAngVelocity");
gpuMath.swapTextures("u_quaternion", "u_lastQuaternion");
return;
var gravity = runConstants.gravity;
var groundHeight = runConstants.groundHeight;
......@@ -774,7 +767,7 @@ define(['underscore', 'backbone', 'threeModel', 'lattice', 'plist', 'emWire', 'G
this.lastAngVelocity[rgbaIndex+2] = angVelocity[2];
var nextQuaternion = this._multiplyQuaternions(quaternion, quaternionDelta);
rotation = this._eulerFromQuaternion(nextQuaternion);
//rotation = this._eulerFromQuaternion(nextQuaternion);
//nextQuaternion = this._normalize4D(nextQuaternion);
this.quaternion[rgbaIndex] = nextQuaternion[0];
......
......@@ -11,7 +11,7 @@ uniform float u_wiresMetaLength;
uniform float u_time;
uniform sampler2D u_angularVelocity;
uniform sampler2D u_lastAngVelocity;
uniform sampler2D u_lastVelocity;
uniform sampler2D u_lastTranslation;
uniform sampler2D u_mass;
......@@ -284,7 +284,7 @@ void main(){
vec3 translation = texture2D(u_lastTranslation, scaledFragCoord).xyz;
vec3 velocity = texture2D(u_lastVelocity, scaledFragCoord).xyz;
vec4 quaternion = texture2D(u_lastQuaternion, scaledFragCoord);
vec3 angVelocity = texture2D(u_angularVelocity, scaledFragCoord).xyz;
vec3 angVelocity = texture2D(u_lastAngVelocity, scaledFragCoord).xyz;
vec4 wiring = texture2D(u_wires, scaledFragCoord);
bool isActuator = wiring[0] < -0.5;//-1
......@@ -372,6 +372,6 @@ void main(){
}
}
angVelocity += torque/momOfInertia;
angVelocity += (torque/momOfInertia)*u_dt;
gl_FragColor = vec4(angVelocity, 0);
}
\ No newline at end of file
#define M_PI 3.1415926535897932384626433832795
precision mediump float;
uniform vec2 u_textureDim;
uniform float u_dt;
uniform sampler2D u_angVelocity;
uniform sampler2D u_lastQuaternion;
uniform sampler2D u_mass;
vec4 quaternionFromEuler(vec3 euler) {
float c1 = cos(euler[0] / 2.0);
float c2 = cos(euler[1] / 2.0);
float c3 = cos(euler[2] / 2.0);
float s1 = sin(euler[0] / 2.0);
float s2 = sin(euler[1] / 2.0);
float s3 = sin(euler[2] / 2.0);
return vec4(s1 * c2 * c3 - c1 * s2 * s3, c1 * s2 * c3 + s1 * c2 * s3, c1 * c2 * s3 - s1 * s2 * c3, c1 * c2 * c3 + s1 * s2 * s3);//zyx
}
vec4 multiplyQuaternions(vec4 a, vec4 b){
float qax = a[0];
float qay = a[1];
float qaz = a[2];
float qaw = a[3];
float qbx = b[0];
float qby = b[1];
float qbz = b[2];
float qbw = b[3];
return vec4(qax * qbw + qaw * qbx + qay * qbz - qaz * qby, qay * qbw + qaw * qby + qaz * qbx - qax * qbz,
qaz * qbw + qaw * qbz + qax * qby - qay * qbx, qaw * qbw - qax * qbx - qay * qby - qaz * qbz);
}
void main(){
vec2 fragCoord = gl_FragCoord.xy;
vec2 scaledFragCoord = fragCoord/u_textureDim;
float isFixed = texture2D(u_mass, scaledFragCoord).y;
if (isFixed < 0.0 || isFixed == 1.0){//no cell or is fixed
gl_FragColor = vec4(0, 0, 0, 1);
return;
}
vec4 quaternion = texture2D(u_lastQuaternion, scaledFragCoord);
vec3 angVelocity = texture2D(u_angVelocity, scaledFragCoord).xyz;
vec4 quaternionDelta = quaternionFromEuler(angVelocity*u_dt);
gl_FragColor = multiplyQuaternions(quaternion, quaternionDelta);
}
\ No newline at end of file
......@@ -178,13 +178,15 @@ void main(){
vec2 fragCoord = gl_FragCoord.xy;
vec2 scaledFragCoord = fragCoord/u_textureDim;
float isFixed = texture2D(u_mass, scaledFragCoord).y;
vec3 massData = texture2D(u_mass, scaledFragCoord).xyz;
float isFixed = massData.y;
if (isFixed < 0.0 || isFixed == 1.0){//no cell or is fixed
gl_FragColor = vec4(0, 0, 0, 0);
return;
}
float mass = texture2D(u_mass, scaledFragCoord).x;
float mass = massData.x;
vec3 force = u_gravity*mass;
vec3 translation = texture2D(u_lastTranslation, scaledFragCoord).xyz;
......
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