Documentation

There are no "classes", constructors or special data types in K3D.js. The library consists of static functions only. It is very fast.

JSONable object is a Javascript object, that can be converted to and from String using JSON.stringify and JSON.parse.

  • load ( url : String, resp : Function )

    Loads any file, which is handled to "resp" as ArrayBuffer after loading.

  • save ( buff : ArrayBuffer, name : String )

    Saves any file into client's computer.

  • clone ( o : Object )

    Clones any JSONable object.

Functions for generating basic 3D primitives. The output structure is as follows:

{
    verts : Array,  // vertex coordinates
    uvt   : Array,  // index  coordinates
    inds  : Array  // verts and uvt indices
}
    
  • Plane ( sx : int, sy : int) : Object

    Generates a plane going from (-1,-1,0) to (1,1,0). sx - segments horizontally, sy - segments vertically.

  • Cube ( ) : Object

    Returns a 8-vertex cube going from (-1,-1,0) to (1,1,0).

  • Sphere ( sx : int, sy : int ) : Object

    Returns a centered sphere with radius 1. sx - segments horizontally, sy - segments vertically.

  • interpolate ( a : Array, b : Array, d : Array, t : Number)

    Interpolates the values between a and b into d, using coefficient t.

  • transform ( a : Array, m : Array)

    Transforms the values from a into a using 4x4 matrix m.

  • unwrap ( ind : Array, crd : Array, cpi : int ) : Array

    Returns an array "crd", re-arranged by "ind", so it can be indexable with [0, 1, 2, 3, 4, ...]. "cpi" - coordinates per index. E.g.:

    {
        // unwrapping square makes it have 6 vertices (3 triangles)
        var a = K3D.edit.unwrap( [0,1,2,1,2,3], [0,0, 1,0, 0,1, 1,1], 2 );
        //  a = [0,0, 1,0, 0,1, 1,0, 0,1, 1,1]
    }
        

Functions for generating 4x4 matrices.

  • scale ( sx : Number, sy : Number, sz : Number) : Array

    Generates scale matrix.

  • translate ( tx : Number, ty : Number, tz : Number) : Array

    Generates translation matrix.

  • rotate ( rx : Number, ry : Number, rz : Number) : Array

    Generates rotation matrix. Angles in radians.

  • rotateDeg ( rx : Number, ry : Number, rz : Number) : Array

    Generates rotation matrix. Angles in degrees.

  • getAABB ( vts : Array) : Object

    Returns an axis-aligned bounding box for 3D vertices. The output is:

    {
        min : { x : Number, y : Number, z : Number },
        max : { x : Number, y : Number, z : Number }
    }
    

All functions work with JSONable data. There is no universal format for 3D model, so each parser works with different structures.

  • fromJSON ( buff : ArrayBuffer ) : Object

    Parses any file from JSON format.

  • toJSON ( o : Object ) : ArrayBuffer

    Parses any file to JSON format.

  • fromOBJ ( buff : ArrayBuffer ) : Object

    Parses any file from OBJ format. The output structure is as follows:

    {
        groups  : {   // groups in OBJ file
            groupName : { from : int, to : int },    // index limits of group
            ...
        },
        
        i_verts : Array, // vertex  indices
        i_uvt   : Array, // texture indices
        i_norms : Array, // normal  indices
                        
        c_verts : Array, // vertex  coordinates
        c_uvt   : Array, // texture coordinates
        c_norms : Array, // normal  coordinates
    }
                
  • fromMD2 ( buff : ArrayBuffer ) : Object

    Parses any file from MD2 format. The output structure is as follows:

    {
        frames  : [      // frames in MD2 file        
            {
                name : String,     // frame name
                norms : Array,    // frame normals
                verts : Array    // frame vertices
            },
            ...
        ],
        
        skins   : Array,  // array of Strings
        
        i_verts : Array,  // vertex   indices
        i_uvt   : Array,  // texture  indices
        
        c_uvt   : Array, // texture coordinates
    }
                
  • from3DS ( buff : ArrayBuffer ) : Object

    Parses any file from 3DS format. The output structure is as follows:

    {
        edit : {          
            objects : [
                {
                    name : String,     // object name
                    mesh : {
                        vertices : Array,
                        indices : Array,
                        uvt : Array
                    }
                },
                ...
            ]
        },
        
        keyf : {
            desc : [
                {
                    hierarchy : {
                        name : String,
                        hierarchy : int
                    }
                },
                ...
            ]
        }
    }
                
  • fromCollada ( buff : ArrayBuffer ) : Object

    Parses any file from Collada format. Collada is very rich format and is not fully parsed. The output structure is as follows:

    {
        asset  : {
            created  : String,
            modified : String,
            up_axis  : String
        },
        geometries : [
            {
                sources : {
                    name1 : Array,  // array of floats
                    name2 : Array,  // array of floats
                    ...
                },
                triangles : [
                    {
                        i_NORMAL   : Array   // normal  indices
                        i_TEXCOORD : Array   // texture indices
                        i_VERTEX   : Array   // vertex  indices
                        s_NORMAL   : String  // normal  source
                        s_TEXCOORD : String  // texture source
                        s_VERTEX   : String  // vertex  source
                        material   : String  // material name
                    },
                    ...
                ]
            },
            ...
        ],
        images : {
            name1 : String,
            ...
        },
        materials : {
            name1 : String, 
            name2 : String,
            ...
        },
        effects : {
            name1 : {
    			surface : String
    		},
            ...
        }
    }