# Not all properties cloned when cloning ShaderMaterial

**URL:** <https://forum.babylonjs.com/t/not-all-properties-cloned-when-cloning-shadermaterial/6442>\
**Category:** Bugs\
**Created:** [October 18, 2019, 9:04pm UTC](https://forum.babylonjs.com/t/not-all-properties-cloned-when-cloning-shadermaterial/6442 "2019-10-18T21:04:51Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Evgeni\_Popov](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/evgeni_popov/32/4432_2.png) [@Evgeni\_Popov](https://forum.babylonjs.com/u/Evgeni_Popov)\
**Post date:** [October 18, 2019, 9:04pm UTC](https://forum.babylonjs.com/t/not-all-properties-cloned-when-cloning-shadermaterial/6442/1 "2019-10-18T21:04:51Z")

</div>

None of the base properties of `Material` are cloned when cloning a `ShaderMaterial`: is it intended?

I had a bug because I cloned a `ShaderMaterial` with `alphaMode=12` but got `alphaMode=2` in the cloned material (2=default value).

By the way, `clone()` is not implemented in `Material` (it returns `null`) contrary to what the doc is saying.

---

<div class="post-metadata">

**Author:** ![Deltakosh](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/deltakosh/32/26635_2.png) [@Deltakosh](https://forum.babylonjs.com/u/Deltakosh)\
**Post date:** [October 18, 2019, 9:19pm UTC](https://forum.babylonjs.com/t/not-all-properties-cloned-when-cloning-shadermaterial/6442/2 "2019-10-18T21:19:15Z")

</div>

Yes shaderMaterial cloning is not good at all 🙂

Please provide a pg that highlights the issue and I will fix the clone function

---

<div class="post-metadata">

**Author:** ![Evgeni\_Popov](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/evgeni_popov/32/4432_2.png) [@Evgeni\_Popov](https://forum.babylonjs.com/u/Evgeni_Popov)\
**Post date:** [October 18, 2019, 9:35pm UTC](https://forum.babylonjs.com/t/not-all-properties-cloned-when-cloning-shadermaterial/6442/3 "2019-10-18T21:35:33Z")

</div>

Here it is:

[https://www.babylonjs-playground.com/#1OH09K#132](https://www.babylonjs-playground.com/#1OH09K#132)

`alphaMode` / `backFaceCulling` values are not cloned, so the front-most cube is not rendered like the other one. You have to uncomment the last two lines to achieve the same rendering.

---

<div class="post-metadata">

**Author:** ![Deltakosh](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/deltakosh/32/26635_2.png) [@Deltakosh](https://forum.babylonjs.com/u/Deltakosh)\
**Post date:** [October 18, 2019, 9:37pm UTC](https://forum.babylonjs.com/t/not-all-properties-cloned-when-cloning-shadermaterial/6442/4 "2019-10-18T21:37:01Z")

</div>

ok will be fixed by next nightly 🙂

---

<div class="post-metadata">

**Author:** ![Evgeni\_Popov](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/evgeni_popov/32/4432_2.png) [@Evgeni\_Popov](https://forum.babylonjs.com/u/Evgeni_Popov)\
**Post date:** [October 20, 2019, 11:36am UTC](https://forum.babylonjs.com/t/not-all-properties-cloned-when-cloning-shadermaterial/6442/5 "2019-10-20T11:36:09Z")

</div>

I saw you did the changes, thanks!

However, I think you should also deep-copy `_options` and `_shaderPath`, else if we modify a property in these objects in one shader, all cloned instances will get the same change.

---

<div class="post-metadata">

**Author:** ![Deltakosh](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/deltakosh/32/26635_2.png) [@Deltakosh](https://forum.babylonjs.com/u/Deltakosh)\
**Post date:** [October 21, 2019, 3:43pm UTC](https://forum.babylonjs.com/t/not-all-properties-cloned-when-cloning-shadermaterial/6442/6 "2019-10-21T15:43:09Z")

</div>

Fancy doing a PR? I can do it else

---

<div class="post-metadata">

**Author:** ![Evgeni\_Popov](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/evgeni_popov/32/4432_2.png) [@Evgeni\_Popov](https://forum.babylonjs.com/u/Evgeni_Popov)\
**Post date:** [October 21, 2019, 9:16pm UTC](https://forum.babylonjs.com/t/not-all-properties-cloned-when-cloning-shadermaterial/6442/7 "2019-10-21T21:16:16Z")

</div>

Never done it before, I will try!

---

<div class="post-metadata">

**Author:** ![Evgeni\_Popov](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/evgeni_popov/32/4432_2.png) [@Evgeni\_Popov](https://forum.babylonjs.com/u/Evgeni_Popov)\
**Post date:** [October 22, 2019, 4:52pm UTC](https://forum.babylonjs.com/t/not-all-properties-cloned-when-cloning-shadermaterial/6442/8 "2019-10-22T16:52:38Z")

</div>

PR created:

> <https://github.com/BabylonJS/Babylon.js/pull/7040>

---

<div class="post-metadata">

**Author:** ![Evgeni\_Popov](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/evgeni_popov/32/4432_2.png) [@Evgeni\_Popov](https://forum.babylonjs.com/u/Evgeni_Popov)\
**Post date:** [October 22, 2019, 5:57pm UTC](https://forum.babylonjs.com/t/not-all-properties-cloned-when-cloning-shadermaterial/6442/9 "2019-10-22T17:57:34Z")

</div>

Just in case, as I saw a comment of you, tried to reply but could not validate: maybe you removed your comment in-between?

I had answered:

> This create a shallow copy of `_options` object ([https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-1.html#object-spread-and-rest](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-1.html#object-spread-and-rest)).
> 
> Then, in the next block of code, I clone the properties that are arrays (the other types of property - `string` and `boolean` - are already handled by the shallow copy).

---

<div class="post-metadata">

**Author:** ![Deltakosh](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/deltakosh/32/26635_2.png) [@Deltakosh](https://forum.babylonjs.com/u/Deltakosh)\
**Post date:** [October 22, 2019, 8:52pm UTC](https://forum.babylonjs.com/t/not-all-properties-cloned-when-cloning-shadermaterial/6442/10 "2019-10-22T20:52:38Z")

</div>

lol yeah I found the answer by myself (it as a stupid question so I removed it)
