## **ModelMakerPlugin**

## 功能说明

<b>模型标注插件</b>提供了以下功能：

- 提供三维空间中的<b>标注模型</b>，包括<b>区域标注(prism)模型</b>、<b>盒(box)模型</b>和<b>多边形(triangles)模型</b>

## 安装引用

**请按需选择 `yarn` 或 `npm` 安装方式：**

```bash npm2yarn
npm install @realsee/dnalogel
```

## 插件方法

### 初始化
```ts twoslash
import { Five } from '@realsee/five'
import { ModelMakerPlugin } from "@realsee/dnalogel";

const five = new Five({
    plugins: [
        [
            ModelMakerPlugin,
            'modelMakerPlugin', // 自定义插件名称
        ]
    ]
})

const plugin = five.plugins.modelMakerPlugin
```

### 加载数据
[数据结构声明](https://open.realsee.cn/docs/dnalogel/ModelMakerPlugin#load数据)
```ts twoslash

// ---cut---
plugin.load(data)
```

### 启用/禁用/显示/隐藏插件
```ts twoslash

// ---cut---
plugin.enable()

plugin.disable()

plugin.show()

plugin.hide()
```

### 销毁插件
```ts twoslash
// ---cut---
plugin.dispose()
```

### 事件监听

[事件类型](https://open.realsee.cn/docs/dnalogel/ModelMakerPlugin#插件事件类型)
```ts twoslash

// ---cut---
plugin.hooks.on('show', () => {
  console.log('插件展示')
})

// @noErrors
plugin.hooks.on('')
//               ^|


```

## 标注方法
插件中加载的每个标注都是一个item。
### 获取 item
方式1
```ts twoslash

// ---cut---
const item = plugin.getItemById(1)
```
方式2
```ts twoslash

// ---cut---
const item = plugin.items[0]
```

### 启用/禁用/显示/隐藏item
```ts twoslash
// ---cut---
item.enable()

item.disable()

item.show()

item.hide()
```

### 显示/隐藏 item 模型
```ts twoslash
// ---cut---
// 显示
item.model.visible = true 

// 隐藏
item.model.visible = false 
```

### 显示/隐藏 item 标签
```ts twoslash
// ---cut---
// 显示
item.tag?.show()

// 隐藏
item.tag?.hide()
```

### 自定义 item 标签样式
```ts twoslash
// ---cut---
// 自定义 box 标签渲染
plugin.registerTagRenderer({ 'box': (container: HTMLElement, item: ModelMakerBaseItem<ServerItem>) => {
  // 示例
  container.innerHTML = item.rawData.name ?? ''

  // 支持 React
  // ReactDOM.render(<div>{item.rawData.name}</div>, container)

  // 返回一个销毁函数
  return () => {
    container.innerHTML = ''
  }
}})
```

### item 事件监听

[事件类型](https://open.realsee.cn/docs/dnalogel/ModelMakerPlugin#item-事件类型)
```ts twoslash

// ---cut---
item.on('show', () => {
  console.log('item展示')
})

// @noErrors
item.on('')
//       ^|


```

## 数据结构

### 插件事件类型

```ts
export interface EventMap {
  /** 插件展示 */
  show: () => void

  /** 插件隐藏 */
  hide: () => void

  /** 插件启用 */
  enable: () => void

  /** 插件禁用 */
  disable: () => void

  /** 数据加载完成 */
  dataLoaded: () => void

  /** 插件被销毁 */
  dispose: () => void
}
```

### item 事件类型

```ts
export interface EventMap {
  /** item展示 */
  show: () => void

  /** item隐藏 */
  hide: () => void

  /** item启用 */
  enable: () => void

  /** item禁用 */
  disable: () => void

  /** item被点击 */
  click: () => void
}
```

### load数据
```ts
/** load 的数据 */
export interface ServerData {
  list: (ServerBoxItem | ServerTrianglesItem | ServerPrismItem)[]
}

export interface ServerBaseItem {
  /** 唯一标识 */
  id?: number
  /** 标签名 */
  name?: string
}

export interface ServerBoxItem extends ServerBaseItem {
  type: 'box'
  object_data: {
    /** box 的其中一个顶点坐标 */
    start: number[]
    /** start 对角位置的顶点坐标 */
    end: number[]
    /** box 的欧拉角旋转，旋转顺序是 [XYZ] */
    rotation?: number[]
    /** 标注颜色 */
    color?: string
    /** 透明度 */
    opacity?: number
  }
}

export interface ServerTrianglesItem extends ServerBaseItem {
  type: 'triangles'
  object_data: {
    /** 多边形的顶点坐标数组 */
    points: number[][]
    /** 标注颜色 */
    color?: string
    /** 透明度 */
    opacity?: number
  }
}

export interface ServerPrismItem extends ServerBaseItem {
  type: 'prism'
  object_data: {
    /** 绘制平面的三维世界坐标点数组 */
    points: number[][]
    /** 楼层高度 */
    height: number
    /** 所在楼层 */
    floorIndex?: number
    /** 人工设置的y坐标方向偏移 */
    fixedY?: number
    /** 人工设置的高度修正 */
    fixedHeight?: number
    /** 标注颜色 */
    color?: string
    /** 透明度 */
    opacity?: number
  }
}
```

网页版本：https://open.realsee.cn/docs/dnalogel/ModelMakerPlugin
文档索引：https://open.realsee.cn/docs.md
