I have a javascript object that I am using. I have a verts array that i want to keep as is(The var called verts). Within the object i have a setScalar()method which will take this verts matrix and apply a scalar(jus a simple for loop multipling each value).When I create a new object I apply my scalar but when i create a new object i dont reference the original verts array it seems to apply the scalar to the scaled verts used in the object previous.
here is the verts array:
var verts = [
// Front face
-1.0, -1.0, 1.0,
1.0, -1.0, 1.0,
1.0, 1.0, 1.0,
-1.0, 1.0, 1.0,
// Back face
-1.0, -1.0, -1.0,
-1.0, 1.0, -1.0,
1.0, 1.0, -1.0,
1.0, -1.0, -1.0,
// Top face
-1.0, 1.0, -1.0,
-1.0, 1.0, 1.0,
1.0, 1.0, 1.0,
1.0, 1.0, -1.0,
// Bottom face
-1.0, -1.0, -1.0,
1.0, -1.0, -1.0,
1.0, -1.0, 1.0,
-1.0, -1.0, 1.0,
// Right face
1.0, -1.0, -1.0,
1.0, 1.0, -1.0,
1.0, 1.0, 1.0,
1.0, -1.0, 1.0,
// Left face
-1.0, -1.0, -1.0,
-1.0, -1.0, 1.0,
-1.0, 1.0, 1.0,
-1.0, 1.0, -1.0
];
and here is how I set it in the object.
//Cube object
function Cube(name, vertices, cubeVertInd, color, scale, positionX, positionY, positionZ)
{
this.name = name;
this.vertices = vertices;
this.cubeVertexIndices = cubeVertInd;
this.positionX = positionX;
this.positionY = positionY;
this.positionZ = positionZ;
this.setColor(color);
this.setScale(vertices, scale);
}
this is the setScale method
Cube.prototype.setScale = function(vertices, scale)
{
var length = vertices.length;
for( var i = 0; i < length; i++)
{
vertices[i] *= scale;
}
}
I know this is what happened because when i use this with webgl I get different sizes depending on what size i make the first object. If i set the object to scale 2 then set the next object to scale 0.1 even though the original verts is all 1's i get 0.2 as the second objects scale. this shows that the scale i set in the first object is being used rather than verts