# Is it possible to render only chosen meshes which are inside an invisible object? Like a color separator, renderOnly?

**URL:** <https://discourse.threejs.org/t/is-it-possible-to-render-only-chosen-meshes-which-are-inside-an-invisible-object-like-a-color-separator-renderonly/46405>\
**Category:** Questions\
**Tags:** animation, materials, geometry, shaders\
**Created:** [January 1, 2023, 10:29am UTC](https://discourse.threejs.org/t/is-it-possible-to-render-only-chosen-meshes-which-are-inside-an-invisible-object-like-a-color-separator-renderonly/46405 "2023-01-01T10:29:13Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![theoristanbul](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/theoristanbul/32/32202_2.png) [@theoristanbul](https://discourse.threejs.org/u/theoristanbul)\
**Post date:** [January 1, 2023, 10:29am UTC](https://discourse.threejs.org/t/is-it-possible-to-render-only-chosen-meshes-which-are-inside-an-invisible-object-like-a-color-separator-renderonly/46405/1 "2023-01-01T10:29:13Z")

</div>

![Screenshot_3](https://canada1.discourse-cdn.com/flex035/uploads/threejs/original/3X/3/9/3933d2f84cabe01ca050bf63def9abbdf9b6c85e.jpeg)

Hello everyone, I’m trying to make invisible tunnels that only show certain colors going into them with flow animation. The screenshot gives a top view of the scene.

Tried ways;

Of these flows objects will be thousounds so i do not want to use loop to check and control objects at everyframe. This will not be high performance at all.

Using renderOrder can be best way to check it but renderorder logic works for only one tunnel changing renderorders for every checkpoints changing the all flows objects renderorder. So it seems impossible to sync without loop function.

clippingPlane, clippingAdvanced or portal examples can be solution for my case but failed to create clippinBox.

So,the best way in my opinion is it like renderOnly function which shows only definated mesh inside the invisible mesh or seperate the object color inside the mesh.

Can you show or discuss the way to do it ?

---

<div class="post-metadata">

**Author:** ![seanwasere](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/seanwasere/32/75911_2.png) [@seanwasere](https://discourse.threejs.org/u/seanwasere)\
**Post date:** [January 1, 2023, 11:35am UTC](https://discourse.threejs.org/t/is-it-possible-to-render-only-chosen-meshes-which-are-inside-an-invisible-object-like-a-color-separator-renderonly/46405/2 "2023-01-01T11:35:26Z")

</div>

Maybe try layers.

Layers Example : [three.js examples](https://threejs.org/examples/?q=layers#webgl_layers)

[![image](https://canada1.discourse-cdn.com/flex035/uploads/threejs/original/3X/4/d/4d8831ab94aa15d4ad0105886987181e35da34d7.png)](https://threejs.org/examples/?q=layers#webgl_layers)

---

<div class="post-metadata">

**Author:** ![theoristanbul](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/theoristanbul/32/32202_2.png) [@theoristanbul](https://discourse.threejs.org/u/theoristanbul)\
**Post date:** [January 1, 2023, 11:44am UTC](https://discourse.threejs.org/t/is-it-possible-to-render-only-chosen-meshes-which-are-inside-an-invisible-object-like-a-color-separator-renderonly/46405/3 "2023-01-01T11:44:29Z")

</div>

i checked also layers. it seems to me that the layers hide the whole flow. Without loop function not toggle layers again i think.

i feel like, the tunnel it should be like xray device. Must shows only definated colors,objects etc.

---

<div class="post-metadata">

**Author:** ![anidivr](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/anidivr/32/32924_2.png) [@anidivr](https://discourse.threejs.org/u/anidivr)\
**Post date:** [January 1, 2023, 12:33pm UTC](https://discourse.threejs.org/t/is-it-possible-to-render-only-chosen-meshes-which-are-inside-an-invisible-object-like-a-color-separator-renderonly/46405/4 "2023-01-01T12:33:49Z")

</div>

If you’re going to have thousands of these, you’re going to need to use an instanced mesh. Instanced meshes work best when you iterate over a data structure to dynamically change the appearance of individual meshes. As a result, you’re going to need to have a loop each frame.

Depending on your visual and data structure, you may not have to have one big loop, but perhaps, multiple smaller loops where only some are called each frame.

If you’re target is mobile or VR, this limits the iterations per frame. However, if its only desktop, you can likely get away with looping over 20K objects per frame and still get 60 fps.

---

<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:** [January 1, 2023, 12:56pm UTC](https://discourse.threejs.org/t/is-it-possible-to-render-only-chosen-meshes-which-are-inside-an-invisible-object-like-a-color-separator-renderonly/46405/5 "2023-01-01T12:56:57Z")

</div>

> Is it possible to render only chosen meshes which are inside an invisible object

that sounds like a stencil to me

> **[Inverted stencil buffer - CodeSandbox](https://codesandbox.io/s/inverted-stencil-buffer-7n2yru)**
>
> How to invert a masked stencil.

the invisible object in that box is a circle geometry for instance

---

<div class="post-metadata">

**Author:** ![theoristanbul](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/theoristanbul/32/32202_2.png) [@theoristanbul](https://discourse.threejs.org/u/theoristanbul)\
**Post date:** [January 1, 2023, 1:44pm UTC](https://discourse.threejs.org/t/is-it-possible-to-render-only-chosen-meshes-which-are-inside-an-invisible-object-like-a-color-separator-renderonly/46405/6 "2023-01-01T13:44:13Z")

</div>

this can be solution if the 2d ring plane can changes to 3d cylinder mesh or smt. let me try

---

<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:** [January 1, 2023, 1:56pm UTC](https://discourse.threejs.org/t/is-it-possible-to-render-only-chosen-meshes-which-are-inside-an-invisible-object-like-a-color-separator-renderonly/46405/7 "2023-01-01T13:56:26Z")

</div>

it can be any geometry. stencils are impossible to understand, if you think that’s your solution you can copy the stencil args into your vanilla project from here [drei/Mask.tsx at 17d6d5dcff431f6dae9d36d88235c1d92bde3f03 · pmndrs/drei · GitHub](https://github.com/pmndrs/drei/blob/17d6d5dcff431f6dae9d36d88235c1d92bde3f03/src/core/Mask.tsx)

---

<div class="post-metadata">

**Author:** ![theoristanbul](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/theoristanbul/32/32202_2.png) [@theoristanbul](https://discourse.threejs.org/u/theoristanbul)\
**Post date:** [January 3, 2023, 12:41am UTC](https://discourse.threejs.org/t/is-it-possible-to-render-only-chosen-meshes-which-are-inside-an-invisible-object-like-a-color-separator-renderonly/46405/8 "2023-01-03T00:41:41Z")

</div>

Stencils are impossible to understand… I see now…

I feel like so close to success… I tried something to do it but i stuck to control chosen color to hide.

Different colored spheres should be only seen in their same color boxes. without loop function…

Example; Red Sphere should be seen in red boxes, Blue should be in blue and green should be in green.

> **[Edit fiddle - JSFiddle - Code Playground](https://jsfiddle.net/fcr354yt/)**
>
> Test your JavaScript, CSS, HTML or CoffeeScript online with JSFiddle code editor.

Can someone help me to improve this code for this case…

---

<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:** [January 3, 2023, 10:37am UTC](https://discourse.threejs.org/t/is-it-possible-to-render-only-chosen-meshes-which-are-inside-an-invisible-object-like-a-color-separator-renderonly/46405/9 "2023-01-03T10:37:32Z")

</div>

you dont need that webgl code imo, the link i sent you is react but it is still just threejs as well. you can just copy the properties.

the following properties:

```plaintext
{
      colorWrite: false, 
      depthWrite: false,
      stencilWrite: true,
      stencilRef: id,
      stencilFunc: THREE.AlwaysStencilFunc,
      stencilFail: THREE.ReplaceStencilOp,
      stencilZFail: THREE.ReplaceStencilOp,
      stencilZPass: THREE.ReplaceStencilOp,
}

```

need to be on the material. on the object that forms the mask, just do `Object.assign(yourMaterial, theseProps)`

and these props:

```plaintext
    stencilWrite: true,
    stencilRef: id,
    stencilFunc: inverse ? THREE.NotEqualStencilFunc : THREE.EqualStencilFunc,
    stencilFail: THREE.KeepStencilOp,
    stencilZFail: THREE.KeepStencilOp,
    stencilZPass: THREE.KeepStencilOp,

```

must be on the objects material that are affected by the mask. the ID must match between them

---

<div class="post-metadata">

**Author:** ![theoristanbul](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/theoristanbul/32/32202_2.png) [@theoristanbul](https://discourse.threejs.org/u/theoristanbul)\
**Post date:** [January 3, 2023, 12:12pm UTC](https://discourse.threejs.org/t/is-it-possible-to-render-only-chosen-meshes-which-are-inside-an-invisible-object-like-a-color-separator-renderonly/46405/10 "2023-01-03T12:12:36Z")

</div>

This effect is what i want … Thank you for enlightening me…

I changed this

```javascript
stencilFunc: inverse ? THREE.NotEqualStencilFunc : THREE.EqualStencilFunc,

```

to

```javascript
stencilFunc: THREE.EqualStencilFunc,

```

and it works.

> **[Edit fiddle - JSFiddle - Code Playground](https://jsfiddle.net/dfsoja24/)**
>
> Test your JavaScript, CSS, HTML or CoffeeScript online with JSFiddle code editor.
