gs-json
Version:
gs-JSON is a domain agnostic unifying 3D file format for geometric and semantic modelling (hence the 'gs').
217 lines • 5.27 kB
JavaScript
import { create_UUID } from "../libs/uuid/uuid";
/**
* Generate default materials.
*/
export function genDefaultMaterials() {
const black_line = {
uuid: create_UUID(),
type: "LineBasicMaterial",
color: 0,
vertexColors: 0,
side: 2,
blending: 0,
depthFunc: 3,
depthTest: true,
depthWrite: true,
};
const blue_line = {
uuid: create_UUID(),
type: "LineBasicMaterial",
color: 255,
vertexColors: 0,
side: 2,
blending: 0,
depthFunc: 3,
depthTest: true,
depthWrite: true,
};
const meshes_mat = {
uuid: create_UUID(),
type: "MeshPhongMaterial",
color: 16777215,
emissive: 0,
specular: 16777215,
shininess: 10,
vertexColors: 0,
side: 2,
blending: 0,
depthFunc: 3,
depthTest: true,
depthWrite: true,
transparent: false,
wireframe: false,
flatShading: true,
};
const meshes_glass_mat = {
uuid: create_UUID(),
type: "MeshPhongMaterial",
color: 0,
emissive: 0,
specular: 16777215,
shininess: 40,
vertexColors: 0,
side: 1,
blending: 0,
depthFunc: 3,
depthTest: true,
depthWrite: true,
transparent: true,
opacity: 0.5,
wireframe: false,
flatShading: true,
};
const points_mat = {
uuid: create_UUID(),
type: "PointsMaterial",
color: 16777215,
size: 2,
sizeAttenuation: false,
};
return [black_line, blue_line, meshes_mat, meshes_glass_mat, points_mat];
}
/**
* Generate the scene.
*/
export function genScene() {
return {
metadata: {
version: 4.5,
type: "Object",
generator: "gs-json",
},
geometries: [],
materials: [],
object: {
type: "Scene",
name: "Scene",
children: [],
},
};
}
/**
* Generate geometry entity from data.
*/
export function genGeom(xyzs, indexes, normals) {
const geom = {
uuid: create_UUID(),
type: "BufferGeometry",
data: {
attributes: {
position: {
itemSize: 3,
type: "Float32Array",
array: xyzs,
normalized: false,
},
},
},
};
if (indexes !== undefined) {
geom.data.index = {
type: "Uint16Array",
array: indexes,
};
}
if (normals !== undefined) {
geom.data.attributes.normal = {
itemSize: 3,
type: "Float32Array",
array: normals,
normalized: false,
};
}
return geom;
}
/**
* Generate a group entity. This has nothing to do with gs-json groups.
*/
export function genGroup(name) {
const obj = {
uuid: create_UUID(),
type: "Group",
name: name,
children: [],
};
return obj;
}
/**
* Generate an obj entity.
*/
export function genObj(type, name, geom, mat) {
const obj = {
uuid: create_UUID(),
type: type,
name: name,
geometry: geom.uuid,
material: mat.uuid,
};
if (type === "Mesh") {
obj.castShadow = true;
obj.receiveShadow = true;
}
else {
obj.castShadow = false;
obj.receiveShadow = false;
}
return obj;
}
/**
* Add a obj entity to the group.
*/
export function addObjToGroup(group, obj) {
group.children.push(obj);
}
/**
* Add an obj entity to the scene.
*/
export function addObjToScene(scene, obj) {
scene.object.children.push(obj);
}
/**
* Add a group entity to the scene.
*/
export function addGroupToScene(scene, group) {
scene.object.children.push(group);
}
/**
* Add some materials to the scene.
*/
export function addMatsToScene(scene, mats) {
for (const mat of mats) {
scene.materials.push(mat);
}
}
/**
* Add a geom entity to the the scene.
*/
export function addGeomToScene(scene, geom) {
scene.geometries.push(geom);
}
/**
* Add a sprite to the scene.
*/
export function addSpriteToScene(scene, group, name, labels_xyzs) {
const sprite = genSprites(name, labels_xyzs);
sprite.uuid = create_UUID();
group.children.push(sprite);
return sprite;
}
/**
* Generate a bunch of sprites, from labels and label centroids.
*/
export function genSprites(name, labels_xyzs) {
const group = genGroup(name);
for (const label_xyz of labels_xyzs) {
const label = label_xyz.label;
const xyz = label_xyz.xyz;
const sprite = {
type: "Sprite",
name: label,
matrix: [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, xyz[0], xyz[1], xyz[2], 1],
visible: false,
};
group.children.push(sprite);
}
return group;
}
//# sourceMappingURL=three_scene.js.map