# Is it possible to instance a complete GLTF that is imported to three.js

**URL:** <https://discourse.threejs.org/t/is-it-possible-to-instance-a-complete-gltf-that-is-imported-to-three-js/8894>\
**Category:** Questions\
**Tags:** instancing\
**Created:** [August 7, 2019, 5:35am UTC](https://discourse.threejs.org/t/is-it-possible-to-instance-a-complete-gltf-that-is-imported-to-three-js/8894 "2019-08-07T05:35:12Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![yogZ-codeR](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/yogz-coder/32/6632_2.png) [@yogZ-codeR](https://discourse.threejs.org/u/yogZ-codeR)\
**Post date:** [August 7, 2019, 5:35am UTC](https://discourse.threejs.org/t/is-it-possible-to-instance-a-complete-gltf-that-is-imported-to-three-js/8894/1 "2019-08-07T05:35:12Z")

</div>

Hey Folks,

I am working on a project where I import GLTF and clone them multiple times to display in the scene. I need to access each of this later for a use case.

However, this is causing a lot of draw calls per frame (5000-6500) depending on the viewport and hence consumes a lot of memory and reduces the FPS.

I had gone through multiple posts here and on Stackoverflow but every post talks about either a mesh created from scratch inside three js or OBJs using a single material.

Our GLTF consists of multiple sub meshes and object3Ds and I was looking around if there is a way to instance this complete GLTF instead of cloning them to reduce the draw calls.

Any suggestion or direction is greatly appreciated.

P.S: I do not have a lot of experience writing custom shaders and if need be where should I look to start writing custom shaders for such use cases.

Thanks in advance, cheers!

---

<div class="post-metadata">

**Author:** ![Fyrestar](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/fyrestar/32/52793_2.png) [@Fyrestar](https://discourse.threejs.org/u/Fyrestar)\
**Post date:** [August 7, 2019, 7:01am UTC](https://discourse.threejs.org/t/is-it-possible-to-instance-a-complete-gltf-that-is-imported-to-three-js/8894/2 "2019-08-07T07:01:31Z")

</div>

You need to create a InstancedBufferGeometry using the original buffer attributes of your model and add a offset and orientation InstancedBufferAttribute in order to use instancing, in addition you need to patch your shader, [here](https://codepen.io/Fyrestar/pen/OevJRx) is a example pen that also makes shadows work with instances.

> [@yogZ-codeR](#):
>
> Our GLTF consists of multiple sub meshes and object3Ds and I was looking around if there is a way to instance this complete GLTF instead of cloning them to reduce the draw calls.

You can only make instances per geometry, so you’ll either have to make a instancing batch for each part of your model or merge it into one. If you use different materials and textures that will be an issue, if you’re interested, i’ve made a [merging solution](https://discourse.threejs.org/t/open-world-planetary-engine/1473/9) for this that allows to merge a model with multiple geometries, materials and textures into one, specifically made for instancing.

---

<div class="post-metadata">

**Author:** ![yogZ-codeR](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/yogz-coder/32/6632_2.png) [@yogZ-codeR](https://discourse.threejs.org/u/yogZ-codeR)\
**Post date:** [August 7, 2019, 7:08am UTC](https://discourse.threejs.org/t/is-it-possible-to-instance-a-complete-gltf-that-is-imported-to-three-js/8894/3 "2019-08-07T07:08:30Z")

</div>

Thanks @Fyrestar, and yes our models have different material and texture so i guess this is going to be complicated than it should be. I will check your merging solution and get back for any doubts that may arise.

Thanks again!

---

<div class="post-metadata">

**Author:** ![looeee](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/looeee/32/59_2.png) [@looeee](https://discourse.threejs.org/u/looeee)\
**Post date:** [August 7, 2019, 11:22am UTC](https://discourse.threejs.org/t/is-it-possible-to-instance-a-complete-gltf-that-is-imported-to-three-js/8894/4 "2019-08-07T11:22:39Z")

</div>

This has been discussed previously, see here for a possible solution.

> [@GLTFExporter GLTFLoader and InstancedBufferGeometries](https://discourse.threejs.org/t/gltfexporter-gltfloader-and-instancedbuffergeometries/8348/5):
>
> Here’s a snippet I’ve used before to convert a model loaded from GLTFLoader into an InstancedBufferGeometry – But to export you’d need to undo that. If you use multiple copies of the same BufferGeometry, GLTFExporter should be smart enough to export only one copy and reuse it, but the file format doesn’t have a GPU instancing feature as such, and InstancedBufferGeometry can’t be exported directly.

---

<div class="post-metadata">

**Author:** ![yogZ-codeR](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/yogz-coder/32/6632_2.png) [@yogZ-codeR](https://discourse.threejs.org/u/yogZ-codeR)\
**Post date:** [August 7, 2019, 2:20pm UTC](https://discourse.threejs.org/t/is-it-possible-to-instance-a-complete-gltf-that-is-imported-to-three-js/8894/5 "2019-08-07T14:20:07Z")

</div>

Thanks @looeee, checking it out!

> [@looeee](#):
>
> This has been discussed previously, see here for a possible solution.

---

<div class="post-metadata">

**Author:** ![UI\_UNICORN](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/ui_unicorn/32/9842_2.png) [@UI\_UNICORN](https://discourse.threejs.org/u/UI_UNICORN)\
**Post date:** [November 8, 2022, 2:31am UTC](https://discourse.threejs.org/t/is-it-possible-to-instance-a-complete-gltf-that-is-imported-to-three-js/8894/6 "2022-11-08T02:31:26Z")

</div>

> [@Fyrestar](#):
>
> merge it into one

Whats the case with multiple materials and textures on 1 geometry ?  
is this a problem? should it be 1 geo, 1 material, 1 texture or can you have more materials and textures? so long as its only applied to 1 geo?

---

<div class="post-metadata">

**Author:** ![Fyrestar](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/fyrestar/32/52793_2.png) [@Fyrestar](https://discourse.threejs.org/u/Fyrestar)\
**Post date:** [November 8, 2022, 6:48am UTC](https://discourse.threejs.org/t/is-it-possible-to-instance-a-complete-gltf-that-is-imported-to-three-js/8894/7 "2022-11-08T06:48:31Z")

</div>

Instancing means 1 material, 1 geometry. Textures those passed as uniform.

---

<div class="post-metadata">

**Author:** ![drcmda](https://avatars.discourse-cdn.com/v4/letter/d/91b2a8/32.png) [@drcmda](https://discourse.threejs.org/u/drcmda)\
**Post date:** [November 8, 2022, 8:14am UTC](https://discourse.threejs.org/t/is-it-possible-to-instance-a-complete-gltf-that-is-imported-to-three-js/8894/8 "2022-11-08T08:14:06Z")

</div>

i made [a tool](https://github.com/pmndrs/gltfjsx) that does this, it takes a gltf and finds similar geometry/material pairs, then creates instances from them

> <https://twitter.com/0xca0a/status/1552372534397501446>

and here’s a real world usecase with a complex model from sketchfab, every screw, everything that’s the same is being re-used.

> **[Merged Instance - CodeSandbox](https://codesandbox.io/s/basic-demo-forked-kud9p?file=%2Fsrc%2FApp.js)**
>
> Loading massive assemblies and re-using their parts while keeping the natural scene graph alive.

there’s an option where it creates an instance out of everything, even non-duplicate geometries, with this you can now mount the same model multiple times and it will re-use everything:

 ![Screenshot 2022-11-08 at 09.21.26](https://canada1.discourse-cdn.com/flex035/uploads/threejs/original/3X/a/a/aa8c95201d9bea79a85e1ac22643157207eacc30.jpeg)

it’s react, but if you want to have this in vanilla, it’s based on @donmccurdy’s gltf-transform dedupe. after that it’s really just traversing and finding the pairs, and then forming as many THREE.InstancedMesh as pairs you found.
