How to make subviewport with visible canvas layers of original viewport?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Calamander

So I want to create sub-viewport over main viewport and I want to be able to see parallax background in my subview or simple background in some canvaslayer or shader overlay - all of it is ignored by subview.
I assume there should be some way to let subviewport take into consideration camera transformation of original viewport so it will know how to merge viewport with its canvas layers before showing part of it.
example screenshot

I tried to use this:

VisualServer.viewport_attach_canvas(SubView.get_viewport_rid(),$'/root'.world_2d.canvas)

And it works, but it started to ignore camera completely, showing only top-left portion of the screen. Maybe there is some way to offset viewport without relying on camera?
I tried to use something like this:

get_viewport().set_canvas_transform(Transform2D(Vector2(1,0),Vector2(0,1),Vector2(500,500)))

from Camera2D description on how to do custom camera, but it is still ignored.

Calamander | 2018-11-07 03:09

:bust_in_silhouette: Reply From: Calamander

Alright, so after some experiments I managed to do it, though I feel that it may be roundabout way to do it. I had to use 3 viewports, one to display original scene with all of it nodes and canvas layers, one viewport to just be its parent so scene will render all layers to it, child fixed camera for parent viewport and finally sub viewport. Viewports should be properly sized, anchored and their containers - stretched.
There isn’t much code, basic char movement and initialization in first node:

extends Node
var SubView
var ParentView
var SubCam
func _ready():
	SubView = find_node('SubView')
	ParentView = find_node('ParentView')
	SubCam = find_node('SubViewCamera')
	SubView.world_2d = ParentView.world_2d
	SubCam.set_custom_viewport(SubView)
	SubCam.zoom = Vector2(2,2)
	_on_ParentView_size_changed()
func _on_ParentView_size_changed():
	SubCam.position = ParentView.size/2-SubView.size#/2

Here’s she screenshot of example:
screenshot

And test project:
test project (3.0.6)

Hope it helps someone.