Api

Api

引擎接口的高级封装。使用时,不应该去实例化“Api类”,而是通过“Engine实例”的“getApi()”方法, 获取Api实例,然后即可通过获取到的api对象去调用引擎内的各种方法。具体使用方式,如下所示:

Example:
// Step01:实例化一个引擎
const engine = new Qk3d.engine({
     containerId: 'engine-container',
     accessToken: 'xxx...',
     ionToken: 'xxx...'
});
// Step02:调用引擎实例的方法,获取Api对象
const api = engine.getApi();
// Step03:通过调用该api对象的各种方法,可实现大多数场景下的需求
api.addSource(...);

Members

Animation :Animation

Description:
  • 动画实例对象

动画实例对象

Type:
  • Animation

Effect :Effect

Description:
  • 特效实例对象

特效实例对象

Type:
  • Effect

Methods

(async) add3DTiles(options)

Description:
  • 添加新的Cesium 3DTiles模型到场景中

Parameters:

Name Type Description
options Cesium3DTiles.ConstructorOptions

一个Tileset类构造函数的参数对象

addAmbientLight(options) → {string}

Description:
  • 向场景中添加一个环境光

Parameters:

Name Type Description
options AmbientLight.ConstructorOptions
Returns:
string -

返回所添加的环境光实例对象的ID

addDirectionalLight(options) → {string}

Description:
  • 向场景中添加一个平行光

Parameters:

Name Type Description
options DirectionalLight.ConstructorOptions
Returns:
string -

返回所添加的平行光实例对象的ID

addEventListener(e, callback)

Description:
  • 事件监听接口

Parameters:

Name Type Description
e EventType

事件类型

callback EventListenerCallback

事件监听回调函数

addFeatureCollection(options)

Description:
  • 添加新的矢量数据到场景中(目前仅支持GeoJSON和TopoJSON两种格式)

Parameters:

Name Type Description
options FeatureCollection.ConstructorOptions

一个FeatureCollection类构造函数的参数对象

addGeoJson(url, strokeColor, fillColor, callback) → {string}

Description:
  • 添加新的geojson数据到场景中

Parameters:

Name Type Description
url string

geojson文件路径

strokeColor array

边缘颜色

fillColor array

填充颜色

callback callback

成功回调函数

Returns:
string -

返回数据的ID

(async) addGltf(options) → {string}

Description:
  • 添加新的glTF、glb模型到场景中

Parameters:

Name Type Description
options Gltf.ConstructorOptions

一个Gltf类构造函数的参数对象

Returns:
string -

glTF模型添加成功后,返回改模型的ID

addGridLineHelper()

Description:
  • 添加一个XZ平面的辅助格网线

addGroup(models) → {Group}

Description:
  • 向当前活动的场景中,添加一模型组对象实例

Parameters:

Name Type Description
models Array.<Object3D>

要添加到群组的模型对象实例

Returns:
Group -

模型组添加成功后,返回该模型组的ID;添加失败时,返回空字符串

addHemisphereLight(options) → {string}

Description:
  • 向场景中添加一个半球光源

Parameters:

Name Type Description
options HemisphereLight.ConstructorOptions
Returns:
string -

返回所添加的半球光实例对象的ID

addImageCollection(options)

Description:
  • 添加新的影像瓦片数据到场景中

Parameters:

Name Type Description
options ImageCollection.ConstructorOptions

一个ImageCollection类构造函数的参数对象 }

addOrthographicCamera(options) → {string}

Description:
  • 向场景中添加一个正交投影相机

Parameters:

Name Type Description
options OrthographicCamera.ConstructorOptions
Returns:
string -

返回所添加的透视投影相机的ID

addPerspectiveCamera(options) → {string}

Description:
  • 向场景中添加一个透视投影相机

Parameters:

Name Type Description
options PerspectiveCamera.ConstructorOptions
Returns:
string -

返回所添加的透视投影相机的ID

addShadowPlane(options) → {string}

Description:
  • 添加一个平面,用于辅助获取模型阴影

Parameters:

Name Type Description
options Object

描述数据源信息的对象,包括以下字段

Properties
Name Type Description
visible boolean

默认是否显示阴影平面

width number

阴影平面的高度

height number

阴影平面的高度

position Array

阴影平面的位置

rotation Array

阴影平面的旋转参数

scale Array

阴影平面的缩放参数

Returns:
string -

返回阴影平面的ID

addSource(options)

Experimental

目前,还不建议使用该API!因为这是一个实验性的API,尚不稳定,且未经测试。在未来的版本更新中,可能会改变,甚至被移除。

Description:
  • 添加新的数据、模型到场景中(目前暂不支持地形数据的添加)

Parameters:

Name Type Description
options ImageCollection.ConstructorOptions | FeatureCollection.ConstructorOptions | Gltf.ConstructorOptions | Tileset.ConstructorOptions

options - 数据、模型的初始化、实例化参数

addTween()

Description:
  • 向场景中添加补间动画

appendChild(options)

Description:
  • 将一个模型、数据添作为子模型或数据,追加到一个已存在的群组对象上。 如果不是添加目标不是一个有效的群组组,则会尝试升级为组对象 (当前版本暂不支持)。

Parameters:

Name Type Description
options Object

向模型组中追加子模型的参数选项

Properties
Name Type Description
groupId string

接收子模型的模型组的ID

models Array.<Object3D>

要追加的子模型实例对象数组

configureScene(options)

Description:
  • 设置场景的属性

Parameters:

Name Type Description
options Object

场景属性

Properties
Name Type Description
color string

背景颜色

fxaa boolean

是否开启fxaa抗锯齿

msaaSamples number

设置msaa抗锯齿采样数

depthTest boolean

是否开启深度测试

createScene(options) → {string}

Description:
  • 创建一个场景实例对象

Parameters:

Name Type Description
options SceneU3D.ConstructorOptions | SceneM3D.ConstructorOptions

创建场景所需的初始参数选项

Returns:
string -

场景创建成功后,返回所创建场景的ID

destroyTween()

Description:
  • 销毁场景中的补间动画

flyTo(options)

Description:
  • 以动画的方式,将相机飞行至特定的位置

Parameters:

Name Type Description
options Array.<Scene.FlyToOptions>

一个数组,可包多个飞行目标位置及动画控制参数对象

flyToAnimation(options)

Description:
  • 相机飞入动画

Parameters:

Name Type Description
options array

相机参数

getCameraParams() → {Object}

Description:
  • 获取相机参数

Returns:
Object -

相机参数

getChildFromGroup(options) → {Object3D}

Description:
  • 从指定的群组中获取所有的子模型,或者指定ID的模型

Parameters:

Name Type Description
options Object

从群组中获取子模型的参数选项

Properties
Name Type Attributes Default Description
groupId string

群组的ID

childId string <optional>
null

要获取的子对象的ID,当该ID为空时,该API将返回群组中的所有子对象

Returns:
Object3D -

返回一个或多个模型对象实例

getGroupChildren(groupId) → {Object3D}

Description:
  • 从群组中获取指定ID的子模型

Parameters:

Name Type Description
groupId string

要获取的子模型的ID

Returns:
Object3D -

返回获取到的子模型

getPosition(要设置的模型的位置,一个) → {Array.<number>}

Description:
  • 获取模型的位置

Parameters:

Name Type Description
要设置的模型的位置,一个 Array.<number>

(lng, lat, height) 格式的数组

Returns:
Array.<number> -

要设置的模型的位置,一个 (lng, lat, height) 格式的数组

getScene(sceneId) → {Scene}

Description:
  • 获取当前活动的,即持有焦点(focus on),或者指定ID的场景实例对象

Parameters:

Name Type Description
sceneId string

要获取的场景的ID

Returns:
Scene -

返回匹配的场景实例

getSource(options) → {Object3D}

Description:
  • 从场景中获取模型、数据、文字、标签等数据源

Parameters:

Name Type Attributes Description
options Object
object.id string

资源ID

object.groupId string <optional>

群组ID,当指定群组ID时,该API将会在群组对象中,查找指定ID的对象

Returns:
Object3D -

返回一个数据源对象

hideGlobe()

Description:
  • 隐藏地球

hideScene(sceneIdopt)

Description:
  • 隐藏引擎中的当前活动场景

Parameters:

Name Type Attributes Description
sceneId string <optional>

要隐藏的场景的ID,未指定场景ID时,将默认隐藏当前活动的场景

hideSource(id)

Description:
  • 隐藏活动场景中的数据、模型

Parameters:

Name Type Description
id string

要隐藏的数据、模型的ID

hideSurroundings()

Description:
  • 关闭周围所有环境(隐藏倾斜摄影、隐藏特效、隐藏流光)

highlight(options)

Description:
  • 高亮指定ID的模型或指定ID的模型构件

Parameters:

Name Type Description
options Object

高亮模型的参数,包含以下字段

Properties
Name Type Description
id string

要高亮的模型ID

featureId string

要高亮的模型构件要素ID

material MeshBasicMaterial | MeshPbrMaterial | MeshPhongMaterial | MeshLambertMaterial

高亮材质

openSurroundings()

Description:
  • 打开周围所有环境(显示倾斜摄影、显示特效、显示流光)

playAllAnimations(options)

Description:
  • 以指定的循环渲染模式,播放场景中的动画。须注意的是, 该API会改变场景实例的渲染模式。场景实例的渲染模式, 通常在创建场景实例对象时,通过“LoopMode”参数指定

Parameters:

Name Type Description
options Object

动画播放参数,包括以下字段:

Properties
Name Type Attributes Default Description
loopMode LoopMode <optional>
LoopMode.LoopRepeat

循环渲染模式

repetitions number <optional>
Infinity

循环渲染次数

restart boolean <optional>
false

是否从头重新播放动画

removeAllSources(optionsopt)

Description:
  • 从活动的场景中,移除所有的数据、模型

Parameters:

Name Type Attributes Description
options Object <optional>

removeGeoJson(content)

Description:
  • 移除场景中的geojson数据

Parameters:

Name Type Description
content object | string

geojson文件的对象或路径

removeScene(sceneId)

Description:
  • 移除并销毁指定ID的场景

Parameters:

Name Type Description
sceneId string

场景id

removeSource(id)

Description:
  • 从活动的场景中,移除指定ID的数据、模型

Parameters:

Name Type Description
id string

要移除的数据、模型的ID

removeTranslationController()

Description:
  • 移除平移模型的操作句柄,即退出平移操作状态,并释放相应的资源

resizeScene(width, height)

Description:
  • 重新调整场景的大小

Parameters:

Name Type Description
width number

场景的宽

height number

场景的高

setBackground(options)

Description:
  • 设置场景的背景色或图片,其中背景色,目前只能设置垂直渐变色

Parameters:

Name Type Description
options Object

场景背景色设置参数,包含以下字段

Properties
Name Type Attributes Description
color string <optional>

场景为纯色背景时的颜色

startColor string <optional>

场景为垂直渐变背景时的起始颜色

endColor string <optional>

场景为垂直渐变背景时的截止颜色

setCameraController(options)

Description:
  • 设置相机的控制(或者说是交互)方式。值得注意的是,Controllers是一个数组,这意味着,同一个交互方式, 可以绑定多个输入事件(包括鼠标、键盘、触屏操作)

Example
// 设置相机交互控制:左键平移、右键旋转,滚轮缩放
api.setCameraController({
    // 旋转控制(对于地表物体来说,就是倾斜相机的角度)
    tilt: {
        // disabled: false,       // 禁止旋转地表模型(为True时,下面的“controllers”参数设置无效)
        controllers: [
            Qk3d.CameraEventType.RIGHT_DRAG,
        ]
    },

    // 平移控制(并不是直接旋转地表的模型,而是通过旋转地球,以达到平移地表模型的效果。)
    rotate: {
        // disabled: false,       // 禁止旋平移表模型(为True时,下面的“controllers”参数设置无效)
        controllers: [
            Qk3d.CameraEventType.LEFT_DRAG
        ]
    },

    // 缩放控制
    zoom: {
        // disabled: false,      // 禁止缩放地表模型(为True时,下面的“controllers”参数设置无效)
        controllers: [
            Qk3d.CameraEventType.WHEEL,   // 鼠标中键滚轮
            Qk3d.CameraEventType.PINCH,   // 双指触控屏幕
        ]
    }
});

Parameters:

Name Type Description
options Object

一个对象,包括以下属性字段:

Properties
Name Type Description
tilt Object

相机旋转控制,包括以下属性字段:

Properties
Name Type Attributes Default Description
disabled boolean <optional>
false

是否禁用相机旋转操作(禁用时,controllers设置将会无效)

controllers Array.<CameraEventType>

相机距离模型的最小距离

rotate Object

相机平移控制,包括以下属性字段:

Properties
Name Type Attributes Default Description
disabled boolean <optional>
false

是否禁用相机平移操作(禁用时,controllers设置将会无效)

controllers Array.<CameraEventType>

相机距离模型的最小距离

zoom Object

相机缩放控制,包括以下属性字段:

Properties
Name Type Attributes Default Description
disabled boolean <optional>
false

是否禁用相机缩放操作(禁用时,controllers设置将会无效)

controllers Array.<CameraEventType>

相机距离模型的最小距离

setCameraDistance(options)

Description:
  • 设置相机距离观测模板的距离

Parameters:

Name Type Description
options Object

一个对象,包括以下属性字段:

Properties
Name Type Description
min number

相机距离模型的最小距离

max number

相机距离模型的最大距离

setColor(options)

Description:
  • 设置模型的颜色

Parameters:

Name Type Description
options Array.<number>

要设置的RGBA格式的颜色

setMaterial(options)

Description:
  • 设置指定模型的材质

Parameters:

Name Type Description
options Object

一个对象,用于描述要添加的模型数组,包括以下属性字段:

Properties
Name Type Description
id string

要设置材质的模型的ID

materials Array

一个使用材质过滤表达式构件的数组,根据指定的ID和材质,设置匹配到的模型构件的材质

setPosition(position)

Description:
  • 设置模型的位置

Parameters:

Name Type Description
position Array.<number>

要设置的模型的位置,一个 (lng, lat, height) 格式的数组

setScene(sceneId)

Description:
  • 场景切换,即设置当前活动的场景为指定ID的场景 这将使得将指定ID的场景获取焦点

Parameters:

Name Type Description
sceneId string

setTerrain(options)

Description:
  • 设置地形数据

Parameters:

Name Type Description
options Object

showGlobe()

Description:
  • 显示地球

showMonomerBuilding(options)

Description:
  • 显示需要展示的单体化建筑

Parameters:

Name Type Description
options Object

单体化建筑配置信息

Properties
Name Type Description
modelType string

单体化建筑模型类型

modelIds arrar

需要展示的单体化建筑id

enableOpen boolean

是否允许模型展开呈现

duration number

显示模型需要的过渡时间

complete callbcak

加载模型完成后的回调函数

showScene(sceneId)

Description:
  • 显示引擎中的当前活动场景

Parameters:

Name Type Description
sceneId string

要显示的场景的ID

showSource(id)

Description:
  • 显示活动场景中的数据、模型

Parameters:

Name Type Description
id string

要显示的数据、模型的ID

stopAllAnimations()

Description:
  • 停止场景中所有的动画播放

syncViews(sceneId1, sceneId2)

Description:
  • 同步两个场景的视图,即视角同步

Parameters:

Name Type Description
sceneId1 string

第一个场景的ID

sceneId2 string

第二个场景的ID

translate(options, callback)

Description:
  • 平移模型

Parameters:

Name Type Description
options Object

平移模型的辅助坐标轴、平移参数设置

Properties
Name Type Attributes Default Description
id string

要平移的模型ID

heightCoeff Array.<Number> <optional>
0.9

options.heightCoeff 高度变化系数

axisOffset Array.<Number> <optional>
[0.0, 0.0, 0.0]

options.axisOffset 辅助坐标轴的偏移量

axisLength Number <optional>
BoundingSphere.radius

options.axisLength 辅助坐标轴的长度

callback TranslationCallback

回调函数

translateMulti(options, callback)

Description:
  • 批量平移模型,可以一次性进行多个模型的平移操作

Parameters:

Name Type Description
options Object

平移模型的辅助坐标轴、平移参数设置

Properties
Name Type Attributes Default Description
ids Array.<string>

一个存储要平移的模型ID的数组

heightCoeff Array.<Number> <optional>
0.9

options.heightCoeff 高度变化系数

axisOffset Array.<Number> <optional>
[0,0,0]

options.axisOffset 辅助坐标轴的偏移量

axisLength Number <optional>
BoundingSphere.radius

options.axisLength 辅助坐标轴的长度

callback TranslationCallback

回调函数

unhighlight(options)

Description:
  • 取消高亮指定ID的模型或指定ID的模型构件

Parameters:

Name Type Description
options Object

取消高亮模型的参数,包含以下字段

Properties
Name Type Description
id string

要高亮的模型ID

featureId string

要高亮的模型构件要素ID

material MeshBasicMaterial | MeshPbrMaterial | MeshPhongMaterial | MeshLambertMaterial

取消高亮后的材质,一般为该模型或构件原来的材质

unsyncViews(sceneId1, sceneId2)

Description:
  • 取消两个场景的视图同步操作

Parameters:

Name Type Description
sceneId1 string

第一个场景的ID

sceneId2 string

第二个场景的ID

zoomTo(options)

Description:
  • 将当前场景的视角,缩放至指定模型的视野范围

Parameters:

Name Type Description
options Object

一个对象,包括以下属性字段:

Properties
Name Type Attributes Description
id string

模型的ID

preferences Object <optional>

一个对象,包括以下属性字段:

Properties
Name Type Attributes Default Description
heading number <optional>
0.0

相机沿着Z轴旋转,设置当前的角度(单位:度)

pitch number <optional>
-0.5

相机沿着Y轴旋转,设置当前的角度(单位:度)

range number <optional>
0.0

相机距离模型的高度(单位:米)