@anderpang/obj2json

1.0.2 • Public • Published

obj2json.js

Installation

$ npm i @anderpang/obj2json.js

Usage

 import obj2json from "anderpang/obj2json.js";

or

<script src="./obj2json.js"></script>
import obj2json from "@anderpang/obj2json";

var json=obj2json(`
    # Blender v2.78 (sub 0) OBJ File: ''
    # www.blender.org
    mtllib box.mtl
    o Cube_Cube.001
    v -1.000000 -1.000000 1.000000
    v -1.000000 1.000000 1.000000
    v -1.000000 -1.000000 -1.000000
    v -1.000000 1.000000 -1.000000
    v 1.000000 -1.000000 1.000000
    v 1.000000 1.000000 1.000000
    v 1.000000 -1.000000 -1.000000
    v 1.000000 1.000000 -1.000000
    vn -1.0000 0.0000 0.0000
    vn 0.0000 0.0000 -1.0000
    vn 1.0000 0.0000 0.0000
    vn 0.0000 0.0000 1.0000
    vn 0.0000 -1.0000 0.0000
    vn 0.0000 1.0000 0.0000
    usemtl None
    s off
    f 1//1 2//1 4//1 3//1
    f 3//2 4//2 8//2 7//2
    f 7//3 8//3 6//3 5//3
    f 5//4 6//4 2//4 1//4
    f 3//5 7//5 5//5 1//5
    f 8//6 4//6 2//6 6//6

`);

console.log(json);
// result
/*
{
  vertices: Array(108) [-1, -1, 1, -1, 1, 1, -1, 1, -1, -1, -1, 1, -1, 1, -1, -1, -1, -1, -1, -1, -1, -1, 1, -1, 1, 1, -1, -1, -1, -1, 1, 1, …]
  normals: Array(108) [-1, 0, 0, -1, 0, 0, -1, 0, 0, -1, 0, 0, -1, 0, 0, -1, 0, 0, 0, 0, -1, 0, 0, -1, 0, 0, -1, 0, 0, -1, 0, 0, -1, …]
  colors: Array(144) [0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, …]
  indices: Array(36) [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35]
}
*/
import obj2json from "@anderpang/obj2json";

var json=obj2json({
    files:{
        "box.obj":`
            # Blender v2.78 (sub 0) OBJ File: ''
            # www.blender.org
            mtllib box.mtl
            o Cube_Cube.001
            v -1.000000 -1.000000 1.000000
            v -1.000000 1.000000 1.000000
            v -1.000000 -1.000000 -1.000000
            v -1.000000 1.000000 -1.000000
            v 1.000000 -1.000000 1.000000
            v 1.000000 1.000000 1.000000
            v 1.000000 -1.000000 -1.000000
            v 1.000000 1.000000 -1.000000
            vn -1.0000 0.0000 0.0000
            vn 0.0000 0.0000 -1.0000
            vn 1.0000 0.0000 0.0000
            vn 0.0000 0.0000 1.0000
            vn 0.0000 -1.0000 0.0000
            vn 0.0000 1.0000 0.0000
            usemtl None
            s off
            f 1//1 2//1 4//1 3//1
            f 3//2 4//2 8//2 7//2
            f 7//3 8//3 6//3 5//3
            f 5//4 6//4 2//4 1//4
            f 3//5 7//5 5//5 1//5
            f 8//6 4//6 2//6 6//6
        `,
        "box.mtl":`
            # Blender MTL File: 'None'
            # Material Count: 1

            newmtl None
            Ns 0
            Ka 0.000000 0.000000 0.000000
            Kd 0.8 0.8 0.8
            Ks 0.8 0.8 0.8
            d 1
            illum 2
        `,
        "other.mtl":`
        `
    },
    scale:0.5,
    reverse:false,
    percision:2

  });

console.log(json);
/*
vertices: Array(108) [-0.5, -0.5, 0.5, -0.5, 0.5, 0.5, -0.5, …]
normals: Array(108) [-1, 0, 0, -1, 0, 0, -1, 0, 0, -1, 0, 0, -1, …]
colors: Array(144) [0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1, …]
indices: Array(36) [0, 1, 2, 3, 4, 5,...]
*/
  • reverse (normals direction)
  • percision (normals and vertices percision) default -1. if 0 then Interger, if 1 then 0.1, if 2 then 0.02...

Package Sidebar

Install

npm i @anderpang/obj2json

Weekly Downloads

0

Version

1.0.2

License

ISC

Unpacked Size

24.8 kB

Total Files

3

Last publish

Collaborators

  • anderpang