Render to texture for background scene?

Render to texture only seems to work for the current scene. Is there anyway for it to include the background scene?

Thanks :smiley:

It works for background scenes, but the trick is it doesn’t work for any camera marked as ‘active camera’ so the background scene has to have two cameras, the one you want to see through (not active) and another ‘dummy’ camera (that is active but you’ll never see)/

The script I use:


'''
This module handles the display of one scene on a texture. It also
handles changing aspect ratios.

Author: Geoffrey Irons
'''

import bge
import common
import mathutils


def init(cont):
    '''Replaces the object/controllers texture with that from a camera
    
    Can accept either a controller or an object.
    Assumes the object is dictionary-like and has the attributes:
     - sceneName: the name of a game engine scene (optional, current
                    scene by default)
     - camera: the name of the camera within the scene
     - material: name of the material to work with
    '''
    
    if type(cont) == bge.types.KX_GameObject:
        obj = cont
    else:
        obj = cont.owner
    # get camera object used for render to texture
    
    if 'sceneName' in obj:
        scene = common.get_scene(obj['sceneName'])
        if scene is None:
            print("Scene not available: " + obj['sceneName'])
            return
            
        if 'camera' in obj:
            cam = scene.objects[obj['camera']]
        else:
            cam = scene.active_camera
            
    else:  # use the cameras scene
        cam_name = obj['camera']
        scene = obj.scene
        cam = scene.objects[cam_name]
    
    if cam == scene.active_camera:
        print("Active camera is not supported in ImageRender currently")
    
    #Adjust for non-square view-screen (
    #note:assumes plane scale matches up with initial aspect ratio of camera
    update_aspect(obj)
        
    mat_id = bge.texture.materialID(obj, "MA" + obj['material'])
    tex = bge.texture.Texture(obj, mat_id)
    tex.source = bge.texture.ImageRender(scene, cam)
    
    obj["RenderToTexture"] = tex
    

def update_aspect(obj):
    '''Scales a UV based on the objects scale.
    
    Used to ensure the aspect ratio of a rendered texture matches that
    of the camera it's showing.
    
    Currently assumes wider than high'''
    aspect = obj.localScale.x / obj.localScale.y
    mesh = obj.meshes[0]
    transform = mathutils.Matrix()
    transform = transform.Scale(1 / aspect, 4, [0, 1, 0])
    transform *= transform.Translation([0, (1 - 1 / aspect) / 2, 0])
    mesh.transformUV(-1, transform, -1)


def remove_aspect(obj):
    '''Does the opposite of updateAspect: scales by the inverse
    
    Used to reset the UV in preparation for another updateAspect
    
    Currently assumes wider than high'''
    aspect = obj.localScale.x / obj.localScale.y
    mesh = obj.meshes[0]
    transform = mathutils.Matrix()
    transform = transform.Scale(1 / aspect, 4, [0, 1, 0])
    transform *= transform.Translation([0, (1 - 1 / aspect) / 2, 0])
    transform.invert()
    mesh.transformUV(-1, transform, -1)


def update(cont):
    '''Updates the texture of an object initialized using the
    init method above. Should be called every frame'''
    if type(cont) == bge.types.KX_GameObject:
        obj = cont
    else:
        obj = cont.owner
    
    if 'RenderToTexture' in obj:
        obj["RenderToTexture"].refresh(True)