Socket
Book a DemoInstallSign in
Socket

oo3d

Package Overview
Dependencies
Maintainers
1
Versions
17
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

oo3d - npm Package Compare versions

Comparing version

to
0.0.18

src/class-program.litcoffee

527

build/oo3d.js
// Generated by CoffeeScript 1.9.2
/*! Oo3d 0.0.12 //// MIT Licence //// http://oo3d.richplastow.com/ */
/*! Oo3d 0.0.18 //// MIT Licence //// http://oo3d.richplastow.com/ */
(function() {
var Buffer, Camera, Main, getFragmentSource, getVertexSource, mat4, ª, ªA, ªB, ªC, ªE, ªF, ªN, ªO, ªR, ªS, ªU, ªV, ªW, ªX, ªex, ªhas, ªisU, ªredefine, ªtype, ªuid;
var Camera, Main, Program, Scene, Shader, Shape, getFragmentSource, getVertexSource, mat4, ª, ªA, ªB, ªC, ªE, ªF, ªN, ªO, ªR, ªS, ªU, ªV, ªW, ªX, ªex, ªhas, ªisU, ªredefine, ªtype, ªuid;
ªC = 'Oo3d';
ªV = '0.0.12';
ªV = '0.0.18';

@@ -86,63 +86,2 @@ ªA = 'array';

Buffer = (function() {
Buffer.prototype.C = 'Buffer';
Buffer.prototype.toString = function() {
return "[object " + this.C + "]";
};
function Buffer(config, gl) {
var i, j, k, ref, v;
if (config == null) {
config = {};
}
for (k in config) {
v = config[k];
this[k] = v;
}
this.gl = gl;
if ('webglrenderingcontext' !== ªtype(this.gl)) {
throw Error("gl must be WebGLRenderingContext not " + (ªtype(this.gl)));
}
if (ªA !== ªtype(config.positions)) {
throw Error("config.positions must be an array not " + (ªtype(config.positions)));
}
if (config.positions.length % 3) {
throw Error("config.positions.length must be divisible by 3");
}
this.positions = this.gl.createBuffer();
this.gl.bindBuffer(this.gl.ARRAY_BUFFER, this.positions);
this.gl.bufferData(this.gl.ARRAY_BUFFER, new Float32Array(config.positions), this.gl.STATIC_DRAW);
if (ªU === ªtype(config.colors)) {
config.colors = [];
for (i = j = 0, ref = config.positions.length / 3 * 4 - 1; 0 <= ref ? j <= ref : j >= ref; i = 0 <= ref ? ++j : --j) {
config.colors.push(1);
}
ª(config.colors);
} else if (ªA !== ªtype(config.colors)) {
throw Error("config.colors must be an array not " + (ªtype(config.colors)));
} else if (config.colors.length % 4) {
throw Error("config.colors.length must be divisible by 4");
} else if (config.positions.length / 3 !== config.colors.length / 4) {
throw Error("config.colors has an incorrect vertex count");
}
this.colors = this.gl.createBuffer();
this.gl.bindBuffer(this.gl.ARRAY_BUFFER, this.colors);
this.gl.bufferData(this.gl.ARRAY_BUFFER, new Float32Array(config.colors), this.gl.STATIC_DRAW);
this.count = config.positions.length / 3;
this.matTransform = new Float32Array([1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1]);
this.rotateX = 0;
this.rotateY = 0;
this.rotateZ = 0;
this.translateX = 0;
this.translateY = 0;
this.translateZ = 0;
}
Buffer.prototype.xx = function() {};
return Buffer;
})();
Camera = (function() {

@@ -173,11 +112,14 @@ Camera.prototype.C = 'Camera';

this.matProjection = mat4.perspective(this.fovy, this.aspect, 1, 100);
this.matTransform = new Float32Array([1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, -5, 1]);
this.matTransform = new Float32Array([1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, -10, 1]);
this.matCamera = null;
this.uMatCameraLoc = this.gl.getUniformLocation(this.program, 'uMatCamera');
this.rotateX = 0;
this.rotateY = 0;
this.rotateZ = 0;
this.translateX = 0;
this.translateY = 0;
this.translateZ = -5;
this.rX = 0;
this.rY = 0;
this.rZ = 0;
this.sX = 1;
this.sY = 1;
this.sZ = 1;
this.tX = 0;
this.tY = 0;
this.tZ = -10;
this.updateCamera();

@@ -218,2 +160,11 @@ }

this.gl = null;
if (ªA === ªtype(config.background)) {
this.bkgndR = config.background[0];
this.bkgndG = config.background[1];
this.bkgndB = config.background[2];
this.bkgndA = config.background[3];
} else {
this.bkgndR = this.bkgndG = this.bkgndB = 0.25;
this.bkgndA = 1;
}
this.vertexShader = null;

@@ -224,3 +175,8 @@ this.fragmentShader = null;

this.aVtxColorLoc = null;
this.buffers = [];
this.scenes = [];
this.shapes = [];
this.positionBuffers = [];
this.colorBuffers = [];
this.programs = [];
this.shaders = [];
if (this.$main) {

@@ -252,7 +208,8 @@ this.initGL();

Main.prototype.initCanvas = function() {
this.gl.clearColor(0.3984375, 0.40625, 0.703125, 1.0);
this.gl.clearColor(this.bkgndR, this.bkgndG, this.bkgndB, this.bkgndA);
this.gl.enable(this.gl.DEPTH_TEST);
this.gl.enable(this.gl.SCISSOR_TEST);
this.gl.depthFunc(this.gl.LEQUAL);
this.gl.clear(this.gl.COLOR_BUFFER_BIT | this.gl.DEPTH_BUFFER_BIT);
return this.gl.viewport(0, 0, this.$main.width, this.$main.height);
this.gl.scissor(0, 0, this.$main.width, this.$main.height);
return this.gl.clear(this.gl.COLOR_BUFFER_BIT | this.gl.DEPTH_BUFFER_BIT);
};

@@ -286,3 +243,2 @@

}
this.gl.useProgram(this.program);
this.aVtxPositionLoc = this.gl.getAttribLocation(this.program, 'aVtxPosition');

@@ -292,3 +248,4 @@ this.aVtxColorLoc = this.gl.getAttribLocation(this.program, 'aVtxColor');

this.gl.enableVertexAttribArray(this.aVtxColorLoc);
return this.uMatTransformLoc = this.gl.getUniformLocation(this.program, 'uMatTransform');
this.uMatTransformLoc = this.gl.getUniformLocation(this.program, 'uMatTransform');
return this.gl.useProgram(this.program);
};

@@ -308,55 +265,221 @@

Main.prototype.addBuffer = function(config) {
Main.prototype.addScene = function(config) {
var index;
index = this.buffers.length;
this.buffers[index] = new Buffer(config, this.gl);
index = this.scenes.length;
this.scenes[index] = new Scene(config, this);
return index;
};
Main.prototype.transform = function(config) {
var matNew, matOld, target, x, y, z;
target = ªisU(config.target) ? this.camera : this.buffers[config.target];
matOld = target.matTransform;
matNew = (function() {
switch (config.type) {
case 'rotateX':
target.rotateX += config.rad;
return mat4.multiply(matOld, mat4.makeXRotation(config.rad));
case 'rotateY':
target.rotateY += config.rad;
return mat4.multiply(matOld, mat4.makeYRotation(config.rad));
case 'rotateZ':
target.rotateZ += config.rad;
return mat4.multiply(matOld, mat4.makeZRotation(config.rad));
case 'translate':
x = config.x || 0;
y = config.y || 0;
z = config.z || 0;
target.translateX += x;
target.translateY += y;
target.translateZ += z;
return mat4.multiply(matOld, mat4.makeTranslation(x, y, z));
}
})();
target.matTransform = matNew;
if (ªU === typeof config.target) {
return this.camera.updateCamera();
Main.prototype.addPositionBuffer = function(positions) {
var index;
index = this.positionBuffers.length;
if (ªA !== ªtype(positions)) {
throw Error("`positions` must be an array not " + (ªtype(positions)));
} else if (positions.length % 3) {
throw Error("`positions.length` must be divisible by 3");
}
this.positionBuffers[index] = this.gl.createBuffer();
this.positionBuffers[index].count = positions.length / 3;
this.gl.bindBuffer(this.gl.ARRAY_BUFFER, this.positionBuffers[index]);
this.gl.bufferData(this.gl.ARRAY_BUFFER, new Float32Array(positions), this.gl.STATIC_DRAW);
return index;
};
Main.prototype.addColorBuffer = function(colors) {
var index;
index = this.colorBuffers.length;
if (ªA !== ªtype(colors)) {
throw Error("`colors` must be an array not " + (ªtype(colors)));
} else if (colors.length % 4) {
throw Error("`colors.length` must be divisible by 4");
}
this.colorBuffers[index] = this.gl.createBuffer();
this.colorBuffers[index].count = colors.length / 4;
this.gl.bindBuffer(this.gl.ARRAY_BUFFER, this.colorBuffers[index]);
this.gl.bufferData(this.gl.ARRAY_BUFFER, new Float32Array(colors), this.gl.STATIC_DRAW);
return index;
};
Main.prototype.addShape = function(config) {
var index;
index = this.shapes.length;
this.shapes[index] = new Shape(config, this);
return index;
};
Main.prototype.rotate = function(x, y, z, targetIndex) {
var c, m00, m01, m02, m03, m10, m11, m12, m13, m20, m21, m22, m23, mat, s, target, x0, y0, z0;
target = this.shapes[targetIndex] || this.camera;
mat = target.matTransform;
x0 = 0 === x;
y0 = 0 === y;
z0 = 0 === z;
if (x0 && y0 && z0) {
return this;
} else if (y0 && z0) {
s = Math.sin(x);
c = Math.cos(x);
m10 = mat[4];
m11 = mat[5];
m12 = mat[6];
m13 = mat[7];
m20 = mat[8];
m21 = mat[9];
m22 = mat[10];
m23 = mat[11];
mat[4] = m10 * c + m20 * s;
mat[5] = m11 * c + m21 * s;
mat[6] = m12 * c + m22 * s;
mat[7] = m13 * c + m23 * s;
mat[8] = m20 * c - m10 * s;
mat[9] = m21 * c - m11 * s;
mat[10] = m22 * c - m12 * s;
mat[11] = m23 * c - m13 * s;
target.rX += x;
} else if (x0 && z0) {
s = Math.sin(y);
c = Math.cos(y);
m00 = mat[0];
m01 = mat[1];
m02 = mat[2];
m03 = mat[3];
m20 = mat[8];
m21 = mat[9];
m22 = mat[10];
m23 = mat[11];
mat[0] = m00 * c - m20 * s;
mat[1] = m01 * c - m21 * s;
mat[2] = m02 * c - m22 * s;
mat[3] = m03 * c - m23 * s;
mat[8] = m00 * s + m20 * c;
mat[9] = m01 * s + m21 * c;
mat[10] = m02 * s + m22 * c;
mat[11] = m03 * s + m23 * c;
target.rY += y;
} else if (x0 && y0) {
s = Math.sin(z);
c = Math.cos(z);
m00 = mat[0];
m01 = mat[1];
m02 = mat[2];
m03 = mat[3];
m10 = mat[4];
m11 = mat[5];
m12 = mat[6];
m13 = mat[7];
mat[0] = m00 * c + m10 * s;
mat[1] = m01 * c + m11 * s;
mat[2] = m02 * c + m12 * s;
mat[3] = m03 * c + m13 * s;
mat[4] = m10 * c - m00 * s;
mat[5] = m11 * c - m01 * s;
mat[6] = m12 * c - m02 * s;
mat[7] = m13 * c - m03 * s;
target.rZ += z;
} else {
this.rotate(x, 0, 0, targetIndex);
this.rotate(0, y, 0, targetIndex);
this.rotate(0, 0, z, targetIndex);
}
if (target === this.camera) {
this.camera.updateCamera();
}
return this;
};
Main.prototype.scale = function(x, y, z, targetIndex) {
var mat, target, x1, y1, z1;
target = this.shapes[targetIndex] || this.camera;
mat = target.matTransform;
x1 = 1 === x;
y1 = 1 === y;
z1 = 1 === z;
if (!x1) {
mat[0] *= x;
mat[1] *= x;
mat[2] *= x;
mat[3] *= x;
target.sX *= x;
}
if (!y1) {
mat[4] *= y;
mat[5] *= y;
mat[6] *= y;
mat[7] *= y;
target.sY *= y;
}
if (!z1) {
mat[8] *= z;
mat[9] *= z;
mat[10] *= z;
mat[11] *= z;
target.sZ *= z;
}
if (target === this.camera) {
this.camera.updateCamera();
}
return this;
};
Main.prototype.translate = function(x, y, z, targetIndex) {
var mat, target, x0, y0, z0;
target = this.shapes[targetIndex] || this.camera;
mat = target.matTransform;
x0 = 0 === x;
y0 = 0 === y;
z0 = 0 === z;
if (x0 && y0 && z0) {
return this;
} else if (y0 && z0) {
mat[12] += mat[0] * x;
mat[13] += mat[1] * x;
mat[14] += mat[2] * x;
mat[15] += mat[3] * x;
target.tX += x;
} else if (x0 && z0) {
mat[12] += mat[4] * y;
mat[13] += mat[5] * y;
mat[14] += mat[6] * y;
mat[15] += mat[7] * y;
target.tY += y;
} else if (x0 && y0) {
mat[12] += mat[8] * z;
mat[13] += mat[9] * z;
mat[14] += mat[10] * z;
mat[15] += mat[11] * z;
target.tZ += z;
} else {
mat[12] += mat[0] * x + mat[4] * y + mat[8] * z;
mat[13] += mat[1] * x + mat[5] * y + mat[9] * z;
mat[14] += mat[2] * x + mat[6] * y + mat[10] * z;
mat[15] += mat[3] * x + mat[7] * y + mat[11] * z;
target.tX += x;
target.tY += y;
target.tZ += z;
}
if (target === this.camera) {
this.camera.updateCamera();
}
return this;
};
Main.prototype.setRenderMode = function(renderMode, targetIndex) {
return this.shapes[targetIndex].renderMode = renderMode;
};
Main.prototype.render = function() {
var index, results;
var index, results, scene;
if (!this.gl) {
throw Error("The WebGL rendering context is " + (ªtype(this.gl)));
}
this.initCanvas();
index = this.buffers.length;
this.gl.clearColor(this.bkgndR, this.bkgndG, this.bkgndB, this.bkgndA);
this.gl.scissor(0, 0, this.$main.width, this.$main.height);
this.gl.clear(this.gl.COLOR_BUFFER_BIT | this.gl.DEPTH_BUFFER_BIT);
index = this.scenes.length;
results = [];
while (index--) {
this.gl.bindBuffer(this.gl.ARRAY_BUFFER, this.buffers[index].positions);
this.gl.vertexAttribPointer(this.aVtxPositionLoc, 3, this.gl.FLOAT, false, 0, 0);
this.gl.bindBuffer(this.gl.ARRAY_BUFFER, this.buffers[index].colors);
this.gl.vertexAttribPointer(this.aVtxColorLoc, 4, this.gl.FLOAT, false, 0, 0);
this.gl.uniformMatrix4fv(this.uMatTransformLoc, false, new Float32Array(this.buffers[index].matTransform));
this.gl.drawArrays(this.gl.TRIANGLES, 0, this.buffers[index].count);
scene = this.scenes[index];
if (scene.isActive) {
scene.render();
}
results.push(this.gl.flush());

@@ -379,2 +502,168 @@ }

Program = (function() {
Program.prototype.C = 'Program';
Program.prototype.toString = function() {
return "[object " + this.C + "]";
};
function Program(config) {
if (config == null) {
config = {};
}
this.vertexShader = null;
this.fragmentShader = null;
}
return Program;
})();
Scene = (function() {
Scene.prototype.C = 'Scene';
Scene.prototype.toString = function() {
return "[object " + this.C + "]";
};
function Scene(config, main) {
var index;
if (config == null) {
config = {};
}
this.main = main;
if ('object' !== ªtype(this.main || 'oo3d' !== '' + this.main)) {
throw Error("`main` must be Oo3d not " + (ªtype(this.main)));
}
this.isActive = config.isActive;
if (ªU === ªtype(this.isActive)) {
this.isActive = true;
} else if (ªB !== ªtype(this.isActive)) {
throw Error("config.isActive must be boolean not " + (ªtype(this.isActive)));
}
this.left = config.left || 0;
this.top = config.top || 0;
this.width = config.width || 1;
this.height = config.height || 1;
if (ªA === ªtype(config.background)) {
this.bkgndR = config.background[0];
this.bkgndG = config.background[1];
this.bkgndB = config.background[2];
this.bkgndA = config.background[3];
} else {
this.bkgndR = this.bkgndG = this.bkgndB = 0.25;
this.bkgndA = 1;
}
this.program = null;
this.shapes = (function() {
var i, len, ref, results;
ref = config.shapeIndices;
results = [];
for (i = 0, len = ref.length; i < len; i++) {
index = ref[i];
results.push(main.shapes[index]);
}
return results;
})();
}
Scene.prototype.render = function() {
var $main, aVtxColorLoc, aVtxPositionLoc, gl, index, main, mode, shape, uMatTransformLoc;
main = this.main;
$main = main.$main;
gl = main.gl;
aVtxPositionLoc = main.aVtxPositionLoc;
aVtxColorLoc = main.aVtxColorLoc;
uMatTransformLoc = main.uMatTransformLoc;
if (!gl) {
throw Error("The WebGL rendering context is " + (ªtype(gl)));
}
gl.clearColor(this.bkgndR, this.bkgndG, this.bkgndB, this.bkgndA);
gl.scissor(this.left * $main.width, this.top * $main.height, this.width * $main.width, this.height * $main.height);
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
index = this.shapes.length;
while (index--) {
shape = this.shapes[index];
gl.bindBuffer(gl.ARRAY_BUFFER, shape.positionBuffer);
gl.vertexAttribPointer(aVtxPositionLoc, 3, gl.FLOAT, false, 0, 0);
gl.bindBuffer(gl.ARRAY_BUFFER, shape.colorBuffer);
gl.vertexAttribPointer(aVtxColorLoc, 4, gl.FLOAT, false, 0, 0);
gl.uniformMatrix4fv(uMatTransformLoc, false, new Float32Array(shape.matTransform));
mode = gl[shape.renderMode];
gl.drawArrays(mode, 0, shape.count);
gl.flush();
}
};
return Scene;
})();
Shader = (function() {
Shader.prototype.C = 'Shader';
Shader.prototype.toString = function() {
return "[object " + this.C + "]";
};
function Shader(config) {
if (config == null) {
config = {};
}
}
return Shader;
})();
Shape = (function() {
Shape.prototype.C = 'Shape';
Shape.prototype.toString = function() {
return "[object " + this.C + "]";
};
function Shape(config, main) {
if (config == null) {
config = {};
}
this.gl = main.gl;
if ('webglrenderingcontext' !== ªtype(this.gl)) {
throw Error("main.gl must be WebGLRenderingContext not " + (ªtype(this.gl)));
}
this.positionBuffer = main.positionBuffers[config.positionIndex];
if (!this.positionBuffer) {
throw Error("config.positionIndex " + config.positionIndex + " does not exist");
}
this.colorBuffer = main.colorBuffers[config.colorIndex];
if (!this.colorBuffer) {
throw Error("config.colorIndex " + config.colorIndex + " does not exist");
}
if (this.positionBuffer.count !== this.colorBuffer.count) {
ª(this.positionBuffer.count, '!=', this.colorBuffer.count);
throw Error("config.positionIndex mismatches config.colorIndex");
}
this.count = this.positionBuffer.count;
this.matTransform = new Float32Array([1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1]);
this.rX = 0;
this.rY = 0;
this.rZ = 0;
this.sX = 1;
this.sY = 1;
this.sZ = 1;
this.tX = 0;
this.tY = 0;
this.tZ = 0;
this.renderMode = config.renderMode || 'TRIANGLES';
if (ªN !== ªtype(this.gl[this.renderMode])) {
throw Error("`renderMode` " + this.renderMode + " is not recognised by WebGL");
}
}
Shape.prototype.xx = function() {};
return Shape;
})();
if (ªF === typeof define && define.amd) {

@@ -594,6 +883,6 @@ define(function() {

s = Math.sin(angleInRadians);
return [c, s, 0, 0, -s, c, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1];
return new Float32Array([c, s, 0, 0, -s, c, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1]);
};
mat4.makeScale = function(sx, sy) {
mat4.makeScale = function(sx, sy, sz) {
return new Float32Array([sx, 0, 0, 0, 0, sy, 0, 0, 0, 0, sz, 0, 0, 0, 0, 1]);

@@ -600,0 +889,0 @@ };

// Generated by CoffeeScript 1.9.2
/*! Oo3d 0.0.12 //// MIT Licence //// http://oo3d.richplastow.com/ */
/*! Oo3d 0.0.18 //// MIT Licence //// http://oo3d.richplastow.com/ */
(function() {
var Buffer, Camera, Main, Tudor, getFragmentSource, getVertexSource, mat4, tudor, ª, ªA, ªB, ªC, ªE, ªF, ªN, ªO, ªR, ªS, ªU, ªV, ªW, ªX, ªex, ªhas, ªisU, ªredefine, ªtype, ªuid,
var Camera, Main, Program, Scene, Shader, Shape, Tudor, getFragmentSource, getVertexSource, mat4, tudor, ª, ªA, ªB, ªC, ªE, ªF, ªN, ªO, ªR, ªS, ªU, ªV, ªW, ªX, ªex, ªhas, ªisU, ªredefine, ªtype, ªuid,
bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };

@@ -11,3 +11,3 @@

ªV = '0.0.12';
ªV = '0.0.18';

@@ -88,63 +88,2 @@ ªA = 'array';

Buffer = (function() {
Buffer.prototype.C = 'Buffer';
Buffer.prototype.toString = function() {
return "[object " + this.C + "]";
};
function Buffer(config, gl) {
var i, j, k, ref, v;
if (config == null) {
config = {};
}
for (k in config) {
v = config[k];
this[k] = v;
}
this.gl = gl;
if ('webglrenderingcontext' !== ªtype(this.gl)) {
throw Error("gl must be WebGLRenderingContext not " + (ªtype(this.gl)));
}
if (ªA !== ªtype(config.positions)) {
throw Error("config.positions must be an array not " + (ªtype(config.positions)));
}
if (config.positions.length % 3) {
throw Error("config.positions.length must be divisible by 3");
}
this.positions = this.gl.createBuffer();
this.gl.bindBuffer(this.gl.ARRAY_BUFFER, this.positions);
this.gl.bufferData(this.gl.ARRAY_BUFFER, new Float32Array(config.positions), this.gl.STATIC_DRAW);
if (ªU === ªtype(config.colors)) {
config.colors = [];
for (i = j = 0, ref = config.positions.length / 3 * 4 - 1; 0 <= ref ? j <= ref : j >= ref; i = 0 <= ref ? ++j : --j) {
config.colors.push(1);
}
ª(config.colors);
} else if (ªA !== ªtype(config.colors)) {
throw Error("config.colors must be an array not " + (ªtype(config.colors)));
} else if (config.colors.length % 4) {
throw Error("config.colors.length must be divisible by 4");
} else if (config.positions.length / 3 !== config.colors.length / 4) {
throw Error("config.colors has an incorrect vertex count");
}
this.colors = this.gl.createBuffer();
this.gl.bindBuffer(this.gl.ARRAY_BUFFER, this.colors);
this.gl.bufferData(this.gl.ARRAY_BUFFER, new Float32Array(config.colors), this.gl.STATIC_DRAW);
this.count = config.positions.length / 3;
this.matTransform = new Float32Array([1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1]);
this.rotateX = 0;
this.rotateY = 0;
this.rotateZ = 0;
this.translateX = 0;
this.translateY = 0;
this.translateZ = 0;
}
Buffer.prototype.xx = function() {};
return Buffer;
})();
Camera = (function() {

@@ -175,11 +114,14 @@ Camera.prototype.C = 'Camera';

this.matProjection = mat4.perspective(this.fovy, this.aspect, 1, 100);
this.matTransform = new Float32Array([1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, -5, 1]);
this.matTransform = new Float32Array([1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, -10, 1]);
this.matCamera = null;
this.uMatCameraLoc = this.gl.getUniformLocation(this.program, 'uMatCamera');
this.rotateX = 0;
this.rotateY = 0;
this.rotateZ = 0;
this.translateX = 0;
this.translateY = 0;
this.translateZ = -5;
this.rX = 0;
this.rY = 0;
this.rZ = 0;
this.sX = 1;
this.sY = 1;
this.sZ = 1;
this.tX = 0;
this.tY = 0;
this.tZ = -10;
this.updateCamera();

@@ -220,2 +162,11 @@ }

this.gl = null;
if (ªA === ªtype(config.background)) {
this.bkgndR = config.background[0];
this.bkgndG = config.background[1];
this.bkgndB = config.background[2];
this.bkgndA = config.background[3];
} else {
this.bkgndR = this.bkgndG = this.bkgndB = 0.25;
this.bkgndA = 1;
}
this.vertexShader = null;

@@ -226,3 +177,8 @@ this.fragmentShader = null;

this.aVtxColorLoc = null;
this.buffers = [];
this.scenes = [];
this.shapes = [];
this.positionBuffers = [];
this.colorBuffers = [];
this.programs = [];
this.shaders = [];
if (this.$main) {

@@ -254,7 +210,8 @@ this.initGL();

Main.prototype.initCanvas = function() {
this.gl.clearColor(0.3984375, 0.40625, 0.703125, 1.0);
this.gl.clearColor(this.bkgndR, this.bkgndG, this.bkgndB, this.bkgndA);
this.gl.enable(this.gl.DEPTH_TEST);
this.gl.enable(this.gl.SCISSOR_TEST);
this.gl.depthFunc(this.gl.LEQUAL);
this.gl.clear(this.gl.COLOR_BUFFER_BIT | this.gl.DEPTH_BUFFER_BIT);
return this.gl.viewport(0, 0, this.$main.width, this.$main.height);
this.gl.scissor(0, 0, this.$main.width, this.$main.height);
return this.gl.clear(this.gl.COLOR_BUFFER_BIT | this.gl.DEPTH_BUFFER_BIT);
};

@@ -288,3 +245,2 @@

}
this.gl.useProgram(this.program);
this.aVtxPositionLoc = this.gl.getAttribLocation(this.program, 'aVtxPosition');

@@ -294,3 +250,4 @@ this.aVtxColorLoc = this.gl.getAttribLocation(this.program, 'aVtxColor');

this.gl.enableVertexAttribArray(this.aVtxColorLoc);
return this.uMatTransformLoc = this.gl.getUniformLocation(this.program, 'uMatTransform');
this.uMatTransformLoc = this.gl.getUniformLocation(this.program, 'uMatTransform');
return this.gl.useProgram(this.program);
};

@@ -310,55 +267,221 @@

Main.prototype.addBuffer = function(config) {
Main.prototype.addScene = function(config) {
var index;
index = this.buffers.length;
this.buffers[index] = new Buffer(config, this.gl);
index = this.scenes.length;
this.scenes[index] = new Scene(config, this);
return index;
};
Main.prototype.transform = function(config) {
var matNew, matOld, target, x, y, z;
target = ªisU(config.target) ? this.camera : this.buffers[config.target];
matOld = target.matTransform;
matNew = (function() {
switch (config.type) {
case 'rotateX':
target.rotateX += config.rad;
return mat4.multiply(matOld, mat4.makeXRotation(config.rad));
case 'rotateY':
target.rotateY += config.rad;
return mat4.multiply(matOld, mat4.makeYRotation(config.rad));
case 'rotateZ':
target.rotateZ += config.rad;
return mat4.multiply(matOld, mat4.makeZRotation(config.rad));
case 'translate':
x = config.x || 0;
y = config.y || 0;
z = config.z || 0;
target.translateX += x;
target.translateY += y;
target.translateZ += z;
return mat4.multiply(matOld, mat4.makeTranslation(x, y, z));
}
})();
target.matTransform = matNew;
if (ªU === typeof config.target) {
return this.camera.updateCamera();
Main.prototype.addPositionBuffer = function(positions) {
var index;
index = this.positionBuffers.length;
if (ªA !== ªtype(positions)) {
throw Error("`positions` must be an array not " + (ªtype(positions)));
} else if (positions.length % 3) {
throw Error("`positions.length` must be divisible by 3");
}
this.positionBuffers[index] = this.gl.createBuffer();
this.positionBuffers[index].count = positions.length / 3;
this.gl.bindBuffer(this.gl.ARRAY_BUFFER, this.positionBuffers[index]);
this.gl.bufferData(this.gl.ARRAY_BUFFER, new Float32Array(positions), this.gl.STATIC_DRAW);
return index;
};
Main.prototype.addColorBuffer = function(colors) {
var index;
index = this.colorBuffers.length;
if (ªA !== ªtype(colors)) {
throw Error("`colors` must be an array not " + (ªtype(colors)));
} else if (colors.length % 4) {
throw Error("`colors.length` must be divisible by 4");
}
this.colorBuffers[index] = this.gl.createBuffer();
this.colorBuffers[index].count = colors.length / 4;
this.gl.bindBuffer(this.gl.ARRAY_BUFFER, this.colorBuffers[index]);
this.gl.bufferData(this.gl.ARRAY_BUFFER, new Float32Array(colors), this.gl.STATIC_DRAW);
return index;
};
Main.prototype.addShape = function(config) {
var index;
index = this.shapes.length;
this.shapes[index] = new Shape(config, this);
return index;
};
Main.prototype.rotate = function(x, y, z, targetIndex) {
var c, m00, m01, m02, m03, m10, m11, m12, m13, m20, m21, m22, m23, mat, s, target, x0, y0, z0;
target = this.shapes[targetIndex] || this.camera;
mat = target.matTransform;
x0 = 0 === x;
y0 = 0 === y;
z0 = 0 === z;
if (x0 && y0 && z0) {
return this;
} else if (y0 && z0) {
s = Math.sin(x);
c = Math.cos(x);
m10 = mat[4];
m11 = mat[5];
m12 = mat[6];
m13 = mat[7];
m20 = mat[8];
m21 = mat[9];
m22 = mat[10];
m23 = mat[11];
mat[4] = m10 * c + m20 * s;
mat[5] = m11 * c + m21 * s;
mat[6] = m12 * c + m22 * s;
mat[7] = m13 * c + m23 * s;
mat[8] = m20 * c - m10 * s;
mat[9] = m21 * c - m11 * s;
mat[10] = m22 * c - m12 * s;
mat[11] = m23 * c - m13 * s;
target.rX += x;
} else if (x0 && z0) {
s = Math.sin(y);
c = Math.cos(y);
m00 = mat[0];
m01 = mat[1];
m02 = mat[2];
m03 = mat[3];
m20 = mat[8];
m21 = mat[9];
m22 = mat[10];
m23 = mat[11];
mat[0] = m00 * c - m20 * s;
mat[1] = m01 * c - m21 * s;
mat[2] = m02 * c - m22 * s;
mat[3] = m03 * c - m23 * s;
mat[8] = m00 * s + m20 * c;
mat[9] = m01 * s + m21 * c;
mat[10] = m02 * s + m22 * c;
mat[11] = m03 * s + m23 * c;
target.rY += y;
} else if (x0 && y0) {
s = Math.sin(z);
c = Math.cos(z);
m00 = mat[0];
m01 = mat[1];
m02 = mat[2];
m03 = mat[3];
m10 = mat[4];
m11 = mat[5];
m12 = mat[6];
m13 = mat[7];
mat[0] = m00 * c + m10 * s;
mat[1] = m01 * c + m11 * s;
mat[2] = m02 * c + m12 * s;
mat[3] = m03 * c + m13 * s;
mat[4] = m10 * c - m00 * s;
mat[5] = m11 * c - m01 * s;
mat[6] = m12 * c - m02 * s;
mat[7] = m13 * c - m03 * s;
target.rZ += z;
} else {
this.rotate(x, 0, 0, targetIndex);
this.rotate(0, y, 0, targetIndex);
this.rotate(0, 0, z, targetIndex);
}
if (target === this.camera) {
this.camera.updateCamera();
}
return this;
};
Main.prototype.scale = function(x, y, z, targetIndex) {
var mat, target, x1, y1, z1;
target = this.shapes[targetIndex] || this.camera;
mat = target.matTransform;
x1 = 1 === x;
y1 = 1 === y;
z1 = 1 === z;
if (!x1) {
mat[0] *= x;
mat[1] *= x;
mat[2] *= x;
mat[3] *= x;
target.sX *= x;
}
if (!y1) {
mat[4] *= y;
mat[5] *= y;
mat[6] *= y;
mat[7] *= y;
target.sY *= y;
}
if (!z1) {
mat[8] *= z;
mat[9] *= z;
mat[10] *= z;
mat[11] *= z;
target.sZ *= z;
}
if (target === this.camera) {
this.camera.updateCamera();
}
return this;
};
Main.prototype.translate = function(x, y, z, targetIndex) {
var mat, target, x0, y0, z0;
target = this.shapes[targetIndex] || this.camera;
mat = target.matTransform;
x0 = 0 === x;
y0 = 0 === y;
z0 = 0 === z;
if (x0 && y0 && z0) {
return this;
} else if (y0 && z0) {
mat[12] += mat[0] * x;
mat[13] += mat[1] * x;
mat[14] += mat[2] * x;
mat[15] += mat[3] * x;
target.tX += x;
} else if (x0 && z0) {
mat[12] += mat[4] * y;
mat[13] += mat[5] * y;
mat[14] += mat[6] * y;
mat[15] += mat[7] * y;
target.tY += y;
} else if (x0 && y0) {
mat[12] += mat[8] * z;
mat[13] += mat[9] * z;
mat[14] += mat[10] * z;
mat[15] += mat[11] * z;
target.tZ += z;
} else {
mat[12] += mat[0] * x + mat[4] * y + mat[8] * z;
mat[13] += mat[1] * x + mat[5] * y + mat[9] * z;
mat[14] += mat[2] * x + mat[6] * y + mat[10] * z;
mat[15] += mat[3] * x + mat[7] * y + mat[11] * z;
target.tX += x;
target.tY += y;
target.tZ += z;
}
if (target === this.camera) {
this.camera.updateCamera();
}
return this;
};
Main.prototype.setRenderMode = function(renderMode, targetIndex) {
return this.shapes[targetIndex].renderMode = renderMode;
};
Main.prototype.render = function() {
var index, results;
var index, results, scene;
if (!this.gl) {
throw Error("The WebGL rendering context is " + (ªtype(this.gl)));
}
this.initCanvas();
index = this.buffers.length;
this.gl.clearColor(this.bkgndR, this.bkgndG, this.bkgndB, this.bkgndA);
this.gl.scissor(0, 0, this.$main.width, this.$main.height);
this.gl.clear(this.gl.COLOR_BUFFER_BIT | this.gl.DEPTH_BUFFER_BIT);
index = this.scenes.length;
results = [];
while (index--) {
this.gl.bindBuffer(this.gl.ARRAY_BUFFER, this.buffers[index].positions);
this.gl.vertexAttribPointer(this.aVtxPositionLoc, 3, this.gl.FLOAT, false, 0, 0);
this.gl.bindBuffer(this.gl.ARRAY_BUFFER, this.buffers[index].colors);
this.gl.vertexAttribPointer(this.aVtxColorLoc, 4, this.gl.FLOAT, false, 0, 0);
this.gl.uniformMatrix4fv(this.uMatTransformLoc, false, new Float32Array(this.buffers[index].matTransform));
this.gl.drawArrays(this.gl.TRIANGLES, 0, this.buffers[index].count);
scene = this.scenes[index];
if (scene.isActive) {
scene.render();
}
results.push(this.gl.flush());

@@ -381,2 +504,168 @@ }

Program = (function() {
Program.prototype.C = 'Program';
Program.prototype.toString = function() {
return "[object " + this.C + "]";
};
function Program(config) {
if (config == null) {
config = {};
}
this.vertexShader = null;
this.fragmentShader = null;
}
return Program;
})();
Scene = (function() {
Scene.prototype.C = 'Scene';
Scene.prototype.toString = function() {
return "[object " + this.C + "]";
};
function Scene(config, main) {
var index;
if (config == null) {
config = {};
}
this.main = main;
if ('object' !== ªtype(this.main || 'oo3d' !== '' + this.main)) {
throw Error("`main` must be Oo3d not " + (ªtype(this.main)));
}
this.isActive = config.isActive;
if (ªU === ªtype(this.isActive)) {
this.isActive = true;
} else if (ªB !== ªtype(this.isActive)) {
throw Error("config.isActive must be boolean not " + (ªtype(this.isActive)));
}
this.left = config.left || 0;
this.top = config.top || 0;
this.width = config.width || 1;
this.height = config.height || 1;
if (ªA === ªtype(config.background)) {
this.bkgndR = config.background[0];
this.bkgndG = config.background[1];
this.bkgndB = config.background[2];
this.bkgndA = config.background[3];
} else {
this.bkgndR = this.bkgndG = this.bkgndB = 0.25;
this.bkgndA = 1;
}
this.program = null;
this.shapes = (function() {
var j, len, ref, results;
ref = config.shapeIndices;
results = [];
for (j = 0, len = ref.length; j < len; j++) {
index = ref[j];
results.push(main.shapes[index]);
}
return results;
})();
}
Scene.prototype.render = function() {
var $main, aVtxColorLoc, aVtxPositionLoc, gl, index, main, mode, shape, uMatTransformLoc;
main = this.main;
$main = main.$main;
gl = main.gl;
aVtxPositionLoc = main.aVtxPositionLoc;
aVtxColorLoc = main.aVtxColorLoc;
uMatTransformLoc = main.uMatTransformLoc;
if (!gl) {
throw Error("The WebGL rendering context is " + (ªtype(gl)));
}
gl.clearColor(this.bkgndR, this.bkgndG, this.bkgndB, this.bkgndA);
gl.scissor(this.left * $main.width, this.top * $main.height, this.width * $main.width, this.height * $main.height);
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
index = this.shapes.length;
while (index--) {
shape = this.shapes[index];
gl.bindBuffer(gl.ARRAY_BUFFER, shape.positionBuffer);
gl.vertexAttribPointer(aVtxPositionLoc, 3, gl.FLOAT, false, 0, 0);
gl.bindBuffer(gl.ARRAY_BUFFER, shape.colorBuffer);
gl.vertexAttribPointer(aVtxColorLoc, 4, gl.FLOAT, false, 0, 0);
gl.uniformMatrix4fv(uMatTransformLoc, false, new Float32Array(shape.matTransform));
mode = gl[shape.renderMode];
gl.drawArrays(mode, 0, shape.count);
gl.flush();
}
};
return Scene;
})();
Shader = (function() {
Shader.prototype.C = 'Shader';
Shader.prototype.toString = function() {
return "[object " + this.C + "]";
};
function Shader(config) {
if (config == null) {
config = {};
}
}
return Shader;
})();
Shape = (function() {
Shape.prototype.C = 'Shape';
Shape.prototype.toString = function() {
return "[object " + this.C + "]";
};
function Shape(config, main) {
if (config == null) {
config = {};
}
this.gl = main.gl;
if ('webglrenderingcontext' !== ªtype(this.gl)) {
throw Error("main.gl must be WebGLRenderingContext not " + (ªtype(this.gl)));
}
this.positionBuffer = main.positionBuffers[config.positionIndex];
if (!this.positionBuffer) {
throw Error("config.positionIndex " + config.positionIndex + " does not exist");
}
this.colorBuffer = main.colorBuffers[config.colorIndex];
if (!this.colorBuffer) {
throw Error("config.colorIndex " + config.colorIndex + " does not exist");
}
if (this.positionBuffer.count !== this.colorBuffer.count) {
ª(this.positionBuffer.count, '!=', this.colorBuffer.count);
throw Error("config.positionIndex mismatches config.colorIndex");
}
this.count = this.positionBuffer.count;
this.matTransform = new Float32Array([1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1]);
this.rX = 0;
this.rY = 0;
this.rZ = 0;
this.sX = 1;
this.sY = 1;
this.sZ = 1;
this.tX = 0;
this.tY = 0;
this.tZ = 0;
this.renderMode = config.renderMode || 'TRIANGLES';
if (ªN !== ªtype(this.gl[this.renderMode])) {
throw Error("`renderMode` " + this.renderMode + " is not recognised by WebGL");
}
}
Shape.prototype.xx = function() {};
return Shape;
})();
if (ªF === typeof define && define.amd) {

@@ -596,6 +885,6 @@ define(function() {

s = Math.sin(angleInRadians);
return [c, s, 0, 0, -s, c, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1];
return new Float32Array([c, s, 0, 0, -s, c, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1]);
};
mat4.makeScale = function(sx, sy) {
mat4.makeScale = function(sx, sy, sz) {
return new Float32Array([sx, 0, 0, 0, 0, sy, 0, 0, 0, 0, sz, 0, 0, 0, 0, 1]);

@@ -602,0 +891,0 @@ };

{
"name": "oo3d",
"version": "0.0.12",
"version": "0.0.18",
"description": "The Oopish WebGL library",

@@ -5,0 +5,0 @@ "main": "build/oo3d.js",

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.