# Get current frame buffer texture for shader material

**URL:** <https://forum.babylonjs.com/t/get-current-frame-buffer-texture-for-shader-material/17443>\
**Category:** Questions\
**Created:** [January 13, 2021, 8:19am UTC](https://forum.babylonjs.com/t/get-current-frame-buffer-texture-for-shader-material/17443 "2021-01-13T08:19:12Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![phaselock](https://avatars.discourse-cdn.com/v4/letter/p/da6949/32.png) [@phaselock](https://forum.babylonjs.com/u/phaselock)\
**Post date:** [January 13, 2021, 8:19am UTC](https://forum.babylonjs.com/t/get-current-frame-buffer-texture-for-shader-material/17443/1 "2021-01-13T08:19:12Z")

</div>

Hi ! I’m trying to create a ShaderMaterial passing the current scene’s rendered result to the textureSampler.

`shaderMaterial.setTexture("textureSampler", <pass current framebuffer texture >);`

I’m not exactly sure where to get the texture from. I seem to recall its meant to be protected from access ?? sorry, memory is foggy. Does anyone have any clues ?

In case the texture is not meant for access, I’d like to ask how I can sample the pixel color of the current scene’s current camera result in my shaderMaterial as that is what the texture was meant for.

---

<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:** [January 13, 2021, 12:13pm UTC](https://forum.babylonjs.com/t/get-current-frame-buffer-texture-for-shader-material/17443/2 "2021-01-13T12:13:49Z")

</div>

You can create a custom render target texture and render the scene (meaning all the meshes) in it, then pass this RTT to your shader. The drawback is that you will effectively render the scene twice.

You can also use a “pass” post process and use `Effect.setTextureFromPostProcess(...)` to pass the texture to your shader.

Here’s an example of using a pass post process and `setTextureFromPostProcess`: [https://playground.babylonjs.com/#18QHZC#1](https://playground.babylonjs.com/#18QHZC#1)

---

<div class="post-metadata">

**Author:** ![phaselock](https://avatars.discourse-cdn.com/v4/letter/p/da6949/32.png) [@phaselock](https://forum.babylonjs.com/u/phaselock)\
**Post date:** [January 14, 2021, 3:59am UTC](https://forum.babylonjs.com/t/get-current-frame-buffer-texture-for-shader-material/17443/3 "2021-01-14T03:59:36Z")

</div>

(foreword: I just finished a minor surgery and am pumped on drugs. Pls forgive me if I am not fully coherent)

What I’m trying to do: port a stylized water shader to bjs (ref: [https://www.reddit.com/r/Unity3D/comments/f6bh50/making\_stylized\_water\_with\_shader\_graph/](https://www.reddit.com/r/Unity3D/comments/f6bh50/making_stylized_water_with_shader_graph/))  
Todo list:

- depth to color control (completed: [Babylon.js Playground](https://playground.babylonjs.com/#UNGKGD))
- Transparency control
- UV distortion control
- foam control

As you can see from the PG, I have a simple bathtub setup and the water color control to depth working. But the water mesh isn’t showing the texture of the objects behind it. To do so, I have to pass the current framebuffer to the shader and mix the result with the water color params. I have trouble passing the framebuffer texture to the shaderMaterial.

The post process route does not accommodate vertex shader which I have. If I uncomment lines 114 & 141, I get the depth map showing up on my water mesh indicating the framebuffer texture not going thru. If I rty to load it thru `shaderMaterial.setTexture`, console throws `TypeError: this._textures[A].isReady is not a function`

How can I check the ready state of the texture before passing into the shader?

---

<div class="post-metadata">

**Author:** ![Pryme8](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/pryme8/32/7212_2.png) [@Pryme8](https://forum.babylonjs.com/u/Pryme8)\
**Post date:** [January 14, 2021, 4:24am UTC](https://forum.babylonjs.com/t/get-current-frame-buffer-texture-for-shader-material/17443/4 "2021-01-14T04:24:41Z")

</div>

Ive been there! Looks like something I could whip up for you pretty quick. Just now getting off for the night but Ill help tomorrow for sure. Get some rest after your surgery.

---

<div class="post-metadata">

**Author:** ![phaselock](https://avatars.discourse-cdn.com/v4/letter/p/da6949/32.png) [@phaselock](https://forum.babylonjs.com/u/phaselock)\
**Post date:** [January 15, 2021, 2:08am UTC](https://forum.babylonjs.com/t/get-current-frame-buffer-texture-for-shader-material/17443/5 "2021-01-15T02:08:55Z")

</div>

> [@Pryme8](#):
>
> Get some rest after your surgery.

rest doesn’t pay the bills… 😭

---

<div class="post-metadata">

**Author:** ![Pryme8](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/pryme8/32/7212_2.png) [@Pryme8](https://forum.babylonjs.com/u/Pryme8)\
**Post date:** [January 15, 2021, 2:13am UTC](https://forum.babylonjs.com/t/get-current-frame-buffer-texture-for-shader-material/17443/6 "2021-01-15T02:13:49Z")

</div>

Well I did not get a chance to look at this yet, but I got you for sure. Was too busy doing as you said above today.

---

<div class="post-metadata">

**Author:** ![Pryme8](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/pryme8/32/7212_2.png) [@Pryme8](https://forum.babylonjs.com/u/Pryme8)\
**Post date:** [January 15, 2021, 8:33pm UTC](https://forum.babylonjs.com/t/get-current-frame-buffer-texture-for-shader-material/17443/7 "2021-01-15T20:33:45Z")

</div>

[https://playground.babylonjs.com/#UNGKGD#4](https://playground.babylonjs.com/#UNGKGD#4)

Its not how he did it, but its kinda the same idea, just a single pass.

there are other ways to do it too

---

<div class="post-metadata">

**Author:** ![Pryme8](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/pryme8/32/7212_2.png) [@Pryme8](https://forum.babylonjs.com/u/Pryme8)\
**Post date:** [January 15, 2021, 11:55pm UTC](https://forum.babylonjs.com/t/get-current-frame-buffer-texture-for-shader-material/17443/8 "2021-01-15T23:55:11Z")

</div>

[https://playground.babylonjs.com/#P5HUDJ#11](https://playground.babylonjs.com/#P5HUDJ#11)

This is actually going to be closer to what you requested.

I dont have time to look at it anymore, have lots of work to do.

---

<div class="post-metadata">

**Author:** ![phaselock](https://avatars.discourse-cdn.com/v4/letter/p/da6949/32.png) [@phaselock](https://forum.babylonjs.com/u/phaselock)\
**Post date:** [January 18, 2021, 1:19am UTC](https://forum.babylonjs.com/t/get-current-frame-buffer-texture-for-shader-material/17443/9 "2021-01-18T01:19:11Z")

</div>

well, you did WAY WAY too much ! Many thanks ! 🙂  
I did mine slightly differently cos keeping the vertex shader is a must. I’ll integrate yours and post in Demos. Cheers!
