# ModelComponent

Class · extends [`Component`](https://api.playcanvas.com/engine/classes/Component.md) · category: Graphics

Source: https://github.com/playcanvas/engine/blob/970d89f6d3bc6667f4e1f88153abd6e7e82ed52c/src/framework/components/model/component.js#L54

The ModelComponent enables an [Entity](https://api.playcanvas.com/engine/classes/Entity.md) to render 3D models. The [type](https://api.playcanvas.com/engine/classes/ModelComponent.md#type) property can
be set to one of several predefined shapes (such as `box`, `sphere`, `cone` and so on).
Alternatively, the component can be configured to manage an arbitrary [Model](https://api.playcanvas.com/engine/classes/Model.md). This can
either be created programmatically or loaded from an [Asset](https://api.playcanvas.com/engine/classes/Asset.md).

The [Model](https://api.playcanvas.com/engine/classes/Model.md) managed by this component is positioned, rotated, and scaled in world space by
the world transformation matrix of the owner [Entity](https://api.playcanvas.com/engine/classes/Entity.md). This world matrix is derived by
combining the entity's local transformation (position, rotation, and scale) with the world
transformation matrix of its parent entity in the scene hierarchy.

You should never need to use the ModelComponent constructor directly. To add a ModelComponent
to an Entity, use [Entity#addComponent](https://api.playcanvas.com/engine/classes/Entity.md#addcomponent):

```javascript
const entity = new Entity();
entity.addComponent('model', {
    type: 'box'
});
```

Once the ModelComponent is added to the entity, you can access it via the [Entity#model](https://api.playcanvas.com/engine/classes/Entity.md#model)
property:

```javascript
entity.model.type = 'capsule';  // Set the model component's type

console.log(entity.model.type); // Get the model component's type and print it
```

## Properties

### isStatic

```ts
isStatic: boolean = false
```

Mark meshes as non-movable (optimization).

## Accessors

### asset

```ts
get asset(): number | Asset<string> | null
set asset(value: number | Asset<string> | null)
```

Gets the model asset id for the component.

### batchGroupId

```ts
get batchGroupId(): number
set batchGroupId(value: number)
```

Gets the batch group for the mesh instances in this component (see [BatchGroup](https://api.playcanvas.com/engine/classes/BatchGroup.md)).

### castShadows

```ts
get castShadows(): boolean
set castShadows(value: boolean)
```

Gets whether attached meshes will cast shadows for lights that have shadow casting enabled.

### castShadowsLightmap

```ts
get castShadowsLightmap(): boolean
set castShadowsLightmap(value: boolean)
```

Gets whether meshes instances will cast shadows when rendering lightmaps.

### customAabb

```ts
get customAabb(): BoundingBox | null
set customAabb(value: BoundingBox | null)
```

Gets the custom object space bounding box that is used for visibility culling of attached
mesh instances.

### layers

```ts
get layers(): readonly number[]
set layers(value: readonly number[])
```

Gets the array of layer IDs ([Layer#id](https://api.playcanvas.com/engine/classes/Layer.md#id)) to which the mesh instances belong.

### lightmapped

```ts
get lightmapped(): boolean
set lightmapped(value: boolean)
```

Gets whether the component is affected by the runtime lightmapper.

### lightmapSizeMultiplier

```ts
get lightmapSizeMultiplier(): number
set lightmapSizeMultiplier(value: number)
```

Gets the lightmap resolution multiplier.

### mapping

```ts
get mapping(): Readonly<Record<string, number>>
set mapping(value: Readonly<Record<string, number>>)
```

Gets the dictionary that holds material overrides for each mesh instance.

### material

```ts
get material(): Material
set material(value: Material)
```

Gets the [Material](https://api.playcanvas.com/engine/classes/Material.md) that will be used to render the model.

### materialAsset

```ts
get materialAsset(): number | Asset<string> | null
set materialAsset(value: number | Asset<string> | null)
```

Gets the material [Asset](https://api.playcanvas.com/engine/classes/Asset.md) that will be used to render the component.

### meshInstances

```ts
get meshInstances(): readonly MeshInstance[] | null
set meshInstances(value: readonly MeshInstance[] | null)
```

Gets the array of mesh instances contained in the component's model. Use the setter to
replace the array; do not mutate the returned array.

### model

```ts
get model(): Model | null
set model(value: Model | null)
```

Gets the model owned by this component. In this case a model is not set or loaded, this will
return null.

### receiveShadows

```ts
get receiveShadows(): boolean
set receiveShadows(value: boolean)
```

Gets whether shadows will be cast on attached meshes.

### type

```ts
get type(): "plane" | "box" | "capsule" | "cone" | "cylinder" | "sphere" | "asset" | "torus"
set type(value: "plane" | "box" | "capsule" | "cone" | "cylinder" | "sphere" | "asset" | "torus")
```

Gets the type of the component.

## Methods

### hide

```ts
hide(): void
```

Stop rendering model without removing it from the scene hierarchy. This method sets the
[MeshInstance#visible](https://api.playcanvas.com/engine/classes/MeshInstance.md#visible) property of every MeshInstance in the model to false Note, this
does not remove the model or mesh instances from the scene hierarchy or draw call list. So
the model component still incurs some CPU overhead.

**Example**

```ts
this.timer = 0;
this.visible = true;
// ...
// blink model every 0.1 seconds
this.timer += dt;
if (this.timer > 0.1) {
    if (!this.visible) {
        this.entity.model.show();
        this.visible = true;
    } else {
        this.entity.model.hide();
        this.visible = false;
    }
    this.timer = 0;
}
```

### show

```ts
show(): void
```

Enable rendering of the model if hidden using [hide](https://api.playcanvas.com/engine/classes/ModelComponent.md#hide). This method sets all the
[MeshInstance#visible](https://api.playcanvas.com/engine/classes/MeshInstance.md#visible) property on all mesh instances to true.

## Inherited from [Component](https://api.playcanvas.com/engine/classes/Component.md)

- `entity: Entity`
- `system: ComponentSystem`
- `get enabled(): boolean` · `set enabled(value: boolean)`
- `fire(name: string, arg1?: any, arg2?: any, arg3?: any, arg4?: any, arg5?: any, arg6?: any, arg7?: any, arg8?: any): EventHandler`
- `hasEvent(name: string): boolean`
- `off(name?: string, callback?: HandleEventCallback, scope?: any): EventHandler`
- `on(name: string, callback: HandleEventCallback, scope?: any): EventHandle`
- `once(name: string, callback: HandleEventCallback, scope?: any): EventHandle`
