diff options
Diffstat (limited to 'o3d/samples/o3djs/debug.js')
-rw-r--r-- | o3d/samples/o3djs/debug.js | 338 |
1 files changed, 9 insertions, 329 deletions
diff --git a/o3d/samples/o3djs/debug.js b/o3d/samples/o3djs/debug.js index 429a75f..427c286 100644 --- a/o3d/samples/o3djs/debug.js +++ b/o3d/samples/o3djs/debug.js @@ -43,6 +43,7 @@ o3djs.provide('o3djs.debug'); o3djs.require('o3djs.math'); o3djs.require('o3djs.primitives'); +o3djs.require('o3djs.lineprimitives'); var O3D_DEBUG_PREFIX = 'o3dDebug_'; var O3D_DEBUG_PREFIX_LENGTH = O3D_DEBUG_PREFIX.length; @@ -170,329 +171,6 @@ o3djs.debug.createScaleShaders_ = function(colorParamName, scaleParamName) { o3djs.debug = o3djs.debug || {}; /** - * Creates a line list shape and primitive given a material, vertex array and - * index array. - * - * @param {!o3d.Pack} pack Pack to create objects in. - * @param {!o3d.Material} material to use. - * @param {!Array.<!o3djs.math.Vector3>} vertices array of numbers in the - * format positionX, positionY, positionZ. - * @param {!Array.<number>} indices array of vertex indices, 2 per line. - * @return {!o3d.Shape} The created shape. - */ -o3djs.debug.createLineShape = function(pack, - material, - vertices, - indices) { - // create a shape and primitive for the vertices. - var shape = pack.createObject('Shape'); - var primitive = pack.createObject('Primitive'); - var streamBank = pack.createObject('StreamBank'); - primitive.owner = shape; - primitive.streamBank = streamBank; - - // Apply the material - primitive.material = material; - - primitive.numberPrimitives = indices.length / 2; - primitive.primitiveType = o3djs.base.o3d.Primitive.LINELIST; - primitive.numberVertices = vertices.length / 3; - primitive.createDrawElement(pack, null); - - var vertexBuffer = pack.createObject('VertexBuffer'); - var positionField = vertexBuffer.createField('FloatField', 3); - vertexBuffer.set(vertices); - - // Attach our buffers to our primitive. - streamBank.setVertexStream(o3djs.base.o3d.Stream.POSITION, - 0, - positionField, - 0); - - var indexBuffer = pack.createObject('IndexBuffer'); - indexBuffer.set(indices); - primitive.indexBuffer = indexBuffer; - return shape; -}; - -/** - * VertexInfo. Used to store vertices and indices. - * @constructor - * @param {!Array.<!o3djs.math.Vector3>} opt_vertices array of numbers in the - * format positionX, positionY, positionZ. - * @param {!Array.<number>} opt_indices array of indices in pairs. - */ -o3djs.debug.VertexInfo = function(opt_vertices, opt_indices) { - /** - * The vertices for this VertexInfo. - * @type {!Array.<o3djs.math.Vector3>} - */ - this.vertices = opt_vertices || []; - - /** - * The vertex indices this VertexInfo. - * @type {!Array.<number>} - */ - this.indices = opt_indices || []; -}; - -/** - * Creates a new VertexInfo. - * @param {!Array.<!o3djs.math.Vector3>} opt_vertices array of numbers in the - * format positionX, positionY, positionZ. - * @param {!Array.<number>} opt_indices array of indices, 2 per line. - * @return {!o3djs.debug.VertexInfo} The new VertexInfo. - */ -o3djs.debug.createVertexInfo = function(opt_vertices, opt_indices) { - return new o3djs.debug.VertexInfo(opt_vertices, opt_indices); -}; - -/** - * Enum for vertex component offsets. - * - * To get/set a vertex component you can do something like this. - * - * vertInfo.vertices[vertInfo.vertexIndex(index) + vertInfo.Offset.X] *= 2; - * - * @enum {number} - */ -o3djs.debug.VertexInfo.prototype.Offset = { - X: 0, - Y: 1, - Z: 2 -}; - -/** - * Computes the number of vertices in this vertex info. - * @return {number} The number of vertices. - */ -o3djs.debug.VertexInfo.prototype.numVertices = function() { - return this.vertices.length / 3; -}; - - -/** - * Given the number of a vertex returns the index in the array where - * the coordinates of that vertex vertexIndex. - * @param {number} vertexNumber The vertex number. - * @return {number} The index where that vertex begins. - */ -o3djs.debug.VertexInfo.prototype.vertexIndex = function(vertexNumber) { - return vertexNumber * 3; -} - -/** - * Adds a vertex. - * @param {number} positionX The x position of the vertex. - * @param {number} positionY The y position of the vertex. - * @param {number} positionZ The z position of the vertex. - */ -o3djs.debug.VertexInfo.prototype.addVertex = function( - positionX, positionY, positionZ) { - this.vertices.push(positionX, positionY, positionZ); -}; - -/** - * Adds a line. - * @param {number} index1 The index of the first vertex of the triangle. - * @param {number} index2 The index of the second vertex of the triangle. - */ -o3djs.debug.VertexInfo.prototype.addLine = function( - index1, index2) { - this.indices.push(index1, index2); -}; - -/** - * Creates a shape from a VertexInfo - * @param {!o3d.Pack} pack Pack to create objects in. - * @param {!o3d.Material} material to use. - * @return {!o3d.Shape} The created shape. - */ -o3djs.debug.VertexInfo.prototype.createShape = function( - pack, - material) { - return o3djs.debug.createLineShape(pack, - material, - this.vertices, - this.indices); -}; - - -/** - * Reorients the vertex positions of this vertexInfo by the - * given matrix. In other words it multiplies each vertex by the - * given matrix. - * @param {!o3djs.math.Matrix4} matrix Matrix to multiply by. - */ -o3djs.debug.VertexInfo.prototype.reorient = function(matrix) { - var math = o3djs.math; - var numVerts = this.numVertices(); - for (var v = 0; v < numVerts; ++v) { - var index = this.vertexIndex(v); - var position = [this.vertices[index + this.Offset.X], - this.vertices[index + this.Offset.Y], - this.vertices[index + this.Offset.Z], - 1]; - position = math.mulVectorMatrix(position, matrix); - this.vertices[index + this.Offset.X] = position[0]; - this.vertices[index + this.Offset.Y] = position[1]; - this.vertices[index + this.Offset.Z] = position[2]; - } -}; - -/** - * Creates the vertices and indices for a cube of lines. The - * cube will be created around the origin. (-size / 2, size / 2) - * The created cube has a position stream only and can therefore only be used - * with shaders that support those a position stream. - * - * @param {number} size Width, height and depth of the cube. - * @param {!o3djs.math.Matrix4} opt_matrix A matrix by which to multiply all - * the vertices. - * @return {!o3djs.debug.VertexInfo} The created cube vertices. - */ -o3djs.debug.createLineCubeVertices = function(size, opt_matrix) { - var k = size / 2; - - var vertices = [ - -k, -k, -k, - +k, -k, -k, - -k, +k, -k, - +k, +k, -k, - -k, -k, +k, - +k, -k, +k, - -k, +k, +k, - +k, +k, +k - ]; - - var indices = [ - 0, 1, 1, 3, 3, 2, 2, 0, - 4, 5, 5, 7, 7, 6, 6, 4, - 0, 4, 1, 5, 2, 6, 3, 7 - ]; - - var vertexInfo = o3djs.debug.createVertexInfo(vertices, indices); - if (opt_matrix) { - vertexInfo.reorient(opt_matrix); - } - return vertexInfo; -}; - -/** - * Creates a cube of lines. - * @param {!o3d.Pack} pack Pack to create sphere elements in. - * @param {!o3d.Material} material to use. - * @param {number} size Width, height and depth of the cube. - * @param {!o3djs.math.Matrix4} opt_matrix A matrix by which to multiply all - * the vertices. - * @return {!o3d.Shape} The created cube. - */ -o3djs.debug.createLineCube = function(pack, - material, - size, - opt_matrix) { - var vertexInfo = o3djs.debug.createLineCubeVertices(size, opt_matrix); - return vertexInfo.createShape(pack, material); -}; - -/** - * Creates sphere vertices. - * The created sphere has a position stream only and can therefore only be - * used with shaders that support those a position stream. - * - * @param {number} radius radius of the sphere. - * @param {number} subdivisionsAxis number of steps around the sphere. - * @param {number} subdivisionsHeight number of vertically on the sphere. - * @param {!o3djs.math.Matrix4} opt_matrix A matrix by which to multiply all - * the vertices. - * @return {!o3djs.debug.VertexInfo} The created sphere vertices. - */ -o3djs.debug.createLineSphereVertices = function(radius, - subdivisionsAxis, - subdivisionsHeight, - opt_matrix) { - if (subdivisionsAxis <= 0 || subdivisionsHeight <= 0) { - throw Error('subdivisionAxis and subdivisionHeight must be > 0'); - } - - // We are going to generate our sphere by iterating through its - // spherical coordinates and generating 1 quad for each quad on a - // ring of the sphere. - - var vertexInfo = o3djs.debug.createVertexInfo(); - - // Generate the individual vertices in our vertex buffer. - for (var y = 0; y <= subdivisionsHeight; y++) { - for (var x = 0; x <= subdivisionsAxis; x++) { - // Generate a vertex based on its spherical coordinates - var u = x / subdivisionsAxis - var v = y / subdivisionsHeight; - var theta = 2 * Math.PI * u; - var phi = Math.PI * v; - var sinTheta = Math.sin(theta); - var cosTheta = Math.cos(theta); - var sinPhi = Math.sin(phi); - var cosPhi = Math.cos(phi); - var ux = cosTheta * sinPhi; - var uy = cosPhi; - var uz = sinTheta * sinPhi; - vertexInfo.addVertex(radius * ux, radius * uy, radius * uz); - } - } - var numVertsAround = subdivisionsAxis + 1; - - for (var x = 0; x < subdivisionsAxis; x++) { - for (var y = 0; y < subdivisionsHeight; y++) { - // Make 2 lines per quad. - vertexInfo.addLine( - (y + 0) * numVertsAround + x, - (y + 0) * numVertsAround + x + 1); - vertexInfo.addLine( - (y + 0) * numVertsAround + x, - (y + 1) * numVertsAround + x); - } - } - - if (opt_matrix) { - vertexInfo.reorient(opt_matrix); - } - return vertexInfo; -}; - -/** - * Creates a sphere. - * The created sphere has position, normal, uv and vertex color streams only - * and can therefore only be used with shaders that support those 4 - * streams. - * - * @param {!o3d.Pack} pack Pack to create sphere elements in. - * @param {!o3d.Material} material to use. - * @param {number} radius radius of the sphere. - * @param {number} subdivisionsAxis number of steps around the sphere. - * @param {number} subdivisionsHeight number of vertically on the sphere. - * @param {!o3djs.math.Matrix4} opt_matrix A matrix by which to multiply all - * the vertices. - * @return {!o3d.Shape} The created sphere. - * - * @see o3d.Pack - * @see o3d.Shape - */ -o3djs.debug.createLineSphere = function(pack, - material, - radius, - subdivisionsAxis, - subdivisionsHeight, - opt_matrix) { - var vertexInfo = o3djs.debug.createLineSphereVertices( - radius, - subdivisionsAxis, - subdivisionsHeight, - opt_matrix); - - return vertexInfo.createShape(pack, material); -}; - -/** * An object to manage a single debug line. * @constructor * @param {!o3djs.debug.DebugLineGroup} debugLineGroup DebugLineGroup @@ -895,9 +573,10 @@ o3djs.debug.DebugHelper = function(pack, viewInfo) { } { - this.sphereShape_ = o3djs.debug.createLineSphere(pack, - this.axisMaterial_, - 0.5, 8, 8); + this.sphereShape_ = o3djs.lineprimitives.createLineSphere( + pack, + this.axisMaterial_, + 0.5, 8, 8); this.sphereShape_.name = O3D_DEBUG_SPHERE_SHAPE_NAME; var primitive = this.sphereShape_.elements[0]; this.sphereScaleParam_ = primitive.createParam( @@ -906,9 +585,10 @@ o3djs.debug.DebugHelper = function(pack, viewInfo) { } { - this.cubeShape_ = o3djs.debug.createLineCube(pack, - this.axisMaterial_, - 1); + this.cubeShape_ = o3djs.lineprimitives.createLineCube( + pack, + this.axisMaterial_, + 1); this.cubeShape_.name = O3D_DEBUG_CUBE_SHAPE_NAME; var primitive = this.cubeShape_.elements[0]; this.cubeScaleParam_ = primitive.createParam( |