# Postprocess aliasing when using viewport

**URL:** <https://forum.babylonjs.com/t/postprocess-aliasing-when-using-viewport/42355>\
**Category:** Questions\
**Tags:** rendering\
**Created:** [July 12, 2023, 9:22am UTC](https://forum.babylonjs.com/t/postprocess-aliasing-when-using-viewport/42355 "2023-07-12T09:22:39Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![newbeea](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/newbeea/32/29263_2.png) [@newbeea](https://forum.babylonjs.com/u/newbeea)\
**Post date:** [July 12, 2023, 9:22am UTC](https://forum.babylonjs.com/t/postprocess-aliasing-when-using-viewport/42355/1 "2023-07-12T09:22:39Z")

</div>

Hi guys, I found that  
Postprocess aliasing when using viewport, the line is broken  
[multi scene view | Babylon.js Playground (babylonjs.com)](https://playground.babylonjs.com/#4JXV32#199)  
maybe because rendered on full size and then scaled to half size  
Does anyone know how to solve this? thanks！  
(I can’t use FFXA in my case by the way.)

---

<div class="post-metadata">

**Author:** ![newbeea](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/newbeea/32/29263_2.png) [@newbeea](https://forum.babylonjs.com/u/newbeea)\
**Post date:** [July 12, 2023, 9:55am UTC](https://forum.babylonjs.com/t/postprocess-aliasing-when-using-viewport/42355/2 "2023-07-12T09:55:47Z")

</div>

I pass the size, it looks good  
[multi scene view | Babylon.js Playground (babylonjs.com)](https://playground.babylonjs.com/#4JXV32#200)

but how to handle it when resizing

---

<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:** [July 12, 2023, 10:07am UTC](https://forum.babylonjs.com/t/postprocess-aliasing-when-using-viewport/42355/3 "2023-07-12T10:07:52Z")

</div>

You can enable MSAA by doing `pp.samples = 4` for eg.

---

<div class="post-metadata">

**Author:** ![newbeea](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/newbeea/32/29263_2.png) [@newbeea](https://forum.babylonjs.com/u/newbeea)\
**Post date:** [July 12, 2023, 11:16am UTC](https://forum.babylonjs.com/t/postprocess-aliasing-when-using-viewport/42355/4 "2023-07-12T11:16:50Z")

</div>

[multi scene view | Babylon.js Playground (babylonjs.com)](https://playground.babylonjs.com/#4JXV32#201)

It’s better but the line still broken  
can I resize the postprocess’ rendertarget ?

---

<div class="post-metadata">

**Author:** ![newbeea](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/newbeea/32/29263_2.png) [@newbeea](https://forum.babylonjs.com/u/newbeea)\
**Post date:** [July 12, 2023, 11:48am UTC](https://forum.babylonjs.com/t/postprocess-aliasing-when-using-viewport/42355/5 "2023-07-12T11:48:12Z")

</div>

My workaround is setting the \_options.width and height directly.  
[multi scene view | Babylon.js Playground (babylonjs.com)](https://playground.babylonjs.com/#4JXV32#202)

---

<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:** [July 12, 2023, 12:21pm UTC](https://forum.babylonjs.com/t/postprocess-aliasing-when-using-viewport/42355/7 "2023-07-12T12:21:53Z")

</div>

You can pass 0.5 as the 3rd parameter of the pass post process to use a texture dimension that is half the screen.

That’s essentially what you do when you set a with which is half the screen size, but it also applies to the height.

Also, I removed your render loop, the Playground is already setting its own.

> **[Babylon.js Playground](https://playground.babylonjs.com/#4JXV32%23207)**
>
> Babylon.js playground is a live editor for Babylon.js WebGL 3D scenes

Maybe this reference can help you if you want to draw antialiasing lines that are always at least one pixel large:

> [@BabylonJS MeshLine - a port of THREE.MeshLine](https://forum.babylonjs.com/t/babylonjs-meshline-a-port-of-three-meshline/32924/157):
>
> Related to drawing lines, I have implemented [this paper](https://gitea.yiem.net/QianMo/Real-Time-Rendering-4th-Bibliography-Collection/raw/branch/main/Chapter%201-24/%5B1384%5D%20%5BGPU%20Pro5%202014%5D%20Wire%20Antialiasing.pdf) from Emil Person to draw thin wires: Maybe it will be useful to some people.

---

<div class="post-metadata">

**Author:** ![newbeea](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/newbeea/32/29263_2.png) [@newbeea](https://forum.babylonjs.com/u/newbeea)\
**Post date:** [July 12, 2023, 12:37pm UTC](https://forum.babylonjs.com/t/postprocess-aliasing-when-using-viewport/42355/8 "2023-07-12T12:37:41Z")

</div>

Thank you so much

I set adaptScaleToCurrentViewport = true, it looks good  
[adaptScaleToCurrentViewport | Babylon.js Playground (babylonjs.com)](https://playground.babylonjs.com/#4JXV32#208)

---

<div class="post-metadata">

**Author:** ![newbeea](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/newbeea/32/29263_2.png) [@newbeea](https://forum.babylonjs.com/u/newbeea)\
**Post date:** [July 12, 2023, 12:39pm UTC](https://forum.babylonjs.com/t/postprocess-aliasing-when-using-viewport/42355/9 "2023-07-12T12:39:49Z")

</div>

The PG is good, but in my project the ratio is wrong, i will update later

update:  
I use 5.57.1 set forceFullscreenViewport = false, the ratio is right. but…

 ![image](https://us1.discourse-cdn.com/flex024/uploads/babylonjs/original/3X/b/8/b8e8788fd885424c425b53478d56831f5a89b7bb.jpeg)

---

<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:** [July 12, 2023, 3:59pm UTC](https://forum.babylonjs.com/t/postprocess-aliasing-when-using-viewport/42355/10 "2023-07-12T15:59:29Z")

</div>

What if you test with the latest Babylon version?

---

<div class="post-metadata">

**Author:** ![newbeea](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/newbeea/32/29263_2.png) [@newbeea](https://forum.babylonjs.com/u/newbeea)\
**Post date:** [July 13, 2023, 2:49am UTC](https://forum.babylonjs.com/t/postprocess-aliasing-when-using-viewport/42355/11 "2023-07-13T02:49:19Z")

</div>

I tried latest version but I found 3 problems:  
1 [sky box ratio | Babylon.js Playground (babylonjs.com)](https://playground.babylonjs.com/#4JXV32#209)  
createDefaultSkybox cause the ratio wrong  
2 [sky box ratio force full | Babylon.js Playground (babylonjs.com)](https://playground.babylonjs.com/#4JXV32#210)  
forceFullscreenViewport = false then the width is wrong  
3 [sky box ratio prepass | Babylon.js Playground (babylonjs.com)](https://playground.babylonjs.com/#4JXV32#213)  
the prepass is incompatible

---

<div class="post-metadata">

**Author:** ![newbeea](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/newbeea/32/29263_2.png) [@newbeea](https://forum.babylonjs.com/u/newbeea)\
**Post date:** [July 13, 2023, 3:07am UTC](https://forum.babylonjs.com/t/postprocess-aliasing-when-using-viewport/42355/12 "2023-07-13T03:07:14Z")

</div>

[sky box ratio ignoreCameraMaxZ | Babylon.js Playground (babylonjs.com)](https://playground.babylonjs.com/#4JXV32#212)

ignoreCameraMaxZ = false resolve these problems

But I’m curious why it would affect the aspect ratio.

---

<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:** [July 13, 2023, 12:32pm UTC](https://forum.babylonjs.com/t/postprocess-aliasing-when-using-viewport/42355/13 "2023-07-13T12:32:47Z")

</div>

From my understanding of the code, `PostProcess.adaptScaleToCurrentViewport = true` will only work if the projection matrix is not recalculated during the post-process pass.

As part of the projection matrix calculation, we get the current aspect ratio by calling this method:

```typescript
public getAspectRatio(viewportOwner : IViewportOwnerLike, useScreen = false) : number {
    const viewport = viewportOwner.viewport ;
    return (this.getRenderWidth(useScreen) * viewport.width) / (this.getRenderHeight(useScreen) * viewport.height) ;
}

```

The first time we calculate the projection matrix, we’re not in the post-process pass, so `getRenderWidth()` / `getRenderHeight()` will return the screen size, which is correct and will render correctly =\> it’s because the viewport width and height are already taken into account in the post-process texture sizes. For the sake of completeness, here’s what we do when `adaptScaleToCurrentViewport = true` :

```typescript
if (this.adaptScaleToCurrentViewport) {
    const currentViewport = engine.currentViewport;

    if (currentViewport) {
        desiredWidth *= currentViewport.width;
        desiredHeight *= currentViewport.height;
    }
}

```

However, if `getAspectRatio` is called during the post-process pass, `getRenderWidth()` / `getRenderHeight()` will return the size of the post-process texture, which has been reduced because of `adaptScaleToCurrentViewport = true` (see explanations above), resulting in a distorted rendering.

And this is what happens when `currentSkybox.ignoreCameraMaxZ = true`: in this case, we need to recalculate a specific projection matrix for the `currentSkybox` and then restore the normal camera projection matrix. It’s this last step that will call `getAspectRatio()` in the context of the post-process pass and return an erroneous value.

I have to say I don’t see a solution, other than the workaround you did (`currentSkybox.ignoreCameraMaxZ = false`) which only works because it doesn’t trigger a call to `getAspectRatio()` during the post-process pass.

cc @sebavan for his opinion.

---

<div class="post-metadata">

**Author:** ![sebavan](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/sebavan/32/57_2.png) [@sebavan](https://forum.babylonjs.com/u/sebavan)\
**Post date:** [July 13, 2023, 7:53pm UTC](https://forum.babylonjs.com/t/postprocess-aliasing-when-using-viewport/42355/14 "2023-07-13T19:53:44Z")

</div>

Same as you I wonder how we can improve this for 7.0

---

<div class="post-metadata">

**Author:** ![newbeea](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/newbeea/32/29263_2.png) [@newbeea](https://forum.babylonjs.com/u/newbeea)\
**Post date:** [July 17, 2023, 3:08am UTC](https://forum.babylonjs.com/t/postprocess-aliasing-when-using-viewport/42355/15 "2023-07-17T03:08:22Z")

</div>

I understand, thank you for the explanation.
