Babylon.js 使用JSON文件格式来描述场景.
你可以在这儿找到完整的加载器代码:
https://github.com/BabylonJS/Babylon.js/blob/master/src/Loading/Plugins/babylon.babylonFileLoader.js
基础类型
.babylon 文件使用下面的约定来定义基础类型:
- Vector3: 一个含有3个浮点数([x, y, z])的数组. 例子: [1.1, 2.2, 3.3]
- Vector4: 一个含有4个浮点数([x, y, z, w])的数组. 例子: [1.1, 2.2, 3.3, 4.4]
- Color3: 一个含有3个浮点数 ([r, g, b])且每个数的值大小在0和1间的数组. 例子: [0.2, 0.3, 0.5]
- Matrix: 行优先(先按行存储)的含有16个浮点数的数组
- Boolean: 真或假
全局结构体
.babylon文件里的全局结构体是下面这样的:
{
"autoClear": boolean,
"clearColor": color3,
"ambientColor": color3,
"gravity": vector3 (通常是[0,-9,0]),
"cameras": 相机数组 (见下面),
"activeCamera_": string,
"lights": 光源数组(见下面),
"materials": 材质数组 (见下面),
"geometries": {...} (见下面),
"meshes": 网格数组 (见下面),
"multiMaterials": 多材质数组(见下面),
"shadowGenerators": 阴影生成器数组 (见下面),
"skeletons": 骨架数组 (见下面),
"particleSystems": 例子系统弄个数组 (见下面),
"lensFlareSystems": 镜头光晕数组 (见下面),
"actions": 动作数组 (见下面),
"sounds": 音效数组 (见下面),
"workerCollisions": boolean,
"collisionsEnabled": boolean,
"physicsEnabled": boolean,
"physicsGravity": vector3 (默认是[0,-9.81,0]),
"physicsEngine": string ("oimo" 或 "cannon", 引擎默认为(oimo),
"animations": 动画数组 (见下面,可以省略),
"autoAnimate": boolean,
"autoAnimateFrom": int,
"autoAnimateTo": int,
"autoAnimateLoop": boolean (可以省略),
"autoAnimateSpeed": number (可以省略)
}
相机
一个相机通过如下JSON格式定义:
{
"name": string,
"id": string,
"type": string,
"tags": string,
"parentId": string,
"lockedTargetId": string,
"position": vector3,
"target": vector3,
"alpha": float, // 仅仅支持ArcRotateCamera 和 AnaglyphArcRotateCamera
"beta": float, // 仅仅支持 ArcRotateCamera 和 AnaglyphArcRotateCamera
"radius": float, // 仅仅支持 ArcRotateCamera, FollowCamera 和 AnaglyphArcRotateCamera
"eye_space": float, // 仅仅支持 AnaglyphFreeCamera 和 AnaglyphArcRotateCamera
"heightOffset": float, // 仅仅支持 FollowCamera
"rotationOffset": float, // 仅仅支持 FollowCamera
"cameraRigMode": int (可以忽略),
"fov": float (弧度单位),
"minZ": float,
"maxZ": float,
"speed": float,
"inertia": float (在0和1间),
"checkCollisions": boolean,
"applyGravity": boolean,
"ellipsoid": vector3,
"animations": Animations数组 (见下面,可以忽略),
"autoAnimate": boolean,
"autoAnimateFrom": int,
"autoAnimateTo": int,
"autoAnimateLoop": boolean (可以忽略),
"autoAnimateSpeed": number (可以忽略),
"inputmgr" : 相机的输入映射(可以忽略, 见下面)
}
相机的输入映射
这是个对象,它字面上用输入类型作为键,同时用输入设定作为存储值对象。每个输入类型都有其自身的属性.
光源
光源通过如下的JSON格式定义:
{
"name": string,
"id": string,
"tags": string,
"type": int (0表示点光,1表示方向光, 2表示聚光,3表示球面光),
"position": vector3,
"direction": vector3,
"angle": float (用于点光源),
"exponent": float (用于点光源),
"groundColor": color3 (用于球面光光源),
"intensity": float,
"range": float,
"diffuse": color3,
"specular": color3,
"excludedMeshesIds": array of string,
"includedOnlyMeshesIds": array of string,
"animations": array of Animations (见下面,可以忽略),
"autoAnimate": boolean,
"autoAnimateFrom": int,
"autoAnimateTo": int,
"autoAnimateLoop": boolean (可以忽略),
"autoAnimateSpeed": number (可以忽略)
}
材质
一个材质通过如下JSON格式定义:
{
"name": string,
"id": string,
"tags": string,
"disableDepthWrite": boolean (可以忽略),
"ambient": color3,
"diffuse": color3,
"specular": color3,
"specularPower": float,
"emissive": color3,
"alpha": float,
"backFaceCulling": boolean,
"wireframe": boolean,
"diffuseTexture": Texture (见下面),
"ambientTexture": Texture (见下面),
"opacityTexture": Texture (见下面,
"reflectionTexture": Texture (见下面),
"refractionTexture": Texture (见下面),
"indexOfRefraction": float,
"emissiveTexture": Texture (见下面),
"specularTexture": Texture (见下面),
"bumpTexture": Texture (见下面),
"lightmapTexture": Texture (见下面),
"useLightmapAsShadowmap": boolean,
"checkReadyOnlyOnce": boolean
"useReflectionFresnelFromSpecular": boolean (可以忽略),
"useEmissiveAsIllumination": boolean (可以忽略),
"diffuseFresnelParameters": 菲涅耳参数 (见下面),
"opacityFresnelParameters": 菲涅耳参数 (见下面),
"reflectionFresnelParameters": 菲涅耳参数 (见下面),
"refractionFresnelParameters": 菲涅耳参数 (见下面),
"emissiveFresnelParameters": 菲涅耳参数 (见下面)
}
菲涅耳参数
菲涅耳参数通过如下的JSON格式定义:
{
"isEnabled": boolean,
"leftColor": color3,
"rightColor": color3,
"bias": float,
"power": float
}
纹理
纹理通过如下的JSON个格式定义:
{
"name": string (文件名),
"level": float (在0和1间),
"hasAlpha": boolean,
"getAlphaFromRGB": boolean,
"coordinatesMode": int (0 = 显式, 1 = 球, 2 = 平面, 3 = 立方, 4 = 投影, 5 = 天空盒),
"uOffset": float,
"vOffset": float,
"uScale": float,
"vScale": float,
"uAng": float,
"vAng": float,
"wAng": float,
"wrapU": boolean,
"wrapV": boolean,
"coordinatesIndex": int,
"animations": array of Animations (见下面,可以忽略),
"base64String": string (可以忽略)
}
多纹理
多纹理通过如下JSON格式定义:
{
"name": string,
"id": string,
"tags": string,
"materials": string数组 (都是子纹理的ID)
}
几何体
几何体的结构如下:
{
"boxes": array of Boxes (见下面),
"spheres": array of Spheres (见下面),
"cylinders": array of Cylinders (见下面),
"toruses": array of Toruses (见下面),
"grounds": array of Grounds (见下面),
"planes": array of Planes (见下面),
"torusKnots": array of TorusKnots (见下面),
"vertexData": array of VertexData (见下面)
}
盒子
盒子通过如下的JSON格式定义:
{
"id": string,
"size": float,
"canBeRegenerated": bool,
"tags": string
}
球体
球体通过如下的JSON格式定义:
{
"id": string,
"segments": float,
"diameter": float,
"canBeRegenerated": bool,
"tags": string
}
圆柱体
圆柱体通过如下的JSON格式定义:
{
"id": string,
"height": float,
"diameterTop": float,