Vizard 8 » Tutorials & Examples » Example scripts » Render Nodes » Cubemap reflection
8.0

Cubemap reflection

\examples\renderNodes\renderNodeCubeMapReflection.py

This script shows how to reflect the surrounding environment onto an object.

"""
This script demonstrates how to have a render node
render into a specific face of a cubemap.
The sphere in the center of the gallery is reflecting
the surrounding environment.
"""
import viz
import vizact
import vizcam
import vizshape

viz.setMultiSample(4)
viz.fov(60)
viz.go()

import vizinfo
vizinfo.InfoPanel()

# Center of cubemap
CENTER = [0,1.8,3]

# Cubemap resolution
RESOLUTION = [256,256]

# Use pivot camera
vizcam.PivotNavigate(center=CENTER,distance=5)

# Add environment
viz.add('gallery.osgb')

# Add logo spinning around cubemp
logo = viz.add('logo.osgb')
logo.setPosition([2,1,3])
logo.setCenter([-2,0,0])
logo.runAction(vizact.spin(0,1,0,90))

# Add sphere to display dynamic cubemap
sphere = vizshape.addSphere(radius=0.3,pos=CENTER)
sphere.appearance(viz.TEXREPLACE|viz.ENVIRONMENT_MAP)

#Add cubemap to render texture into
cube = viz.addRenderTexture(type=viz.TEX_CUBE)

#Add depth texture that will be used for all render nodes
depth = viz.addRenderTexture(format=viz.TEX_DEPTH)

# Rotation offset for each cubemap face
CubeFacesEuler = {  viz.POSITIVE_X : [90,0,0],
                    viz.NEGATIVE_X : [-90,0,0],
                    viz.POSITIVE_Y : [0,-90,0],
                    viz.NEGATIVE_Y : [0,90,0],
                    viz.POSITIVE_Z : [0,0,0],
                    viz.NEGATIVE_Z : [180,0,0] }

# 90 degree FOV projection for each face
CubeProjectionMatrix = viz.Matrix.perspective(90.0,1.0,0.1,100.0)

# Create render node for each face of cubemap
cams = []
for face,euler in CubeFacesEuler.items():

    # Create render node
    cam = viz.addRenderNode(size=RESOLUTION,inheritView=False)
    cam.setRenderTexture(cube,face=face)
    cam.setRenderTexture(depth)
    cam.setPosition(CENTER)
    cam.setEuler(euler)
    cam.setProjectionMatrix(CubeProjectionMatrix)

    # Render to texture only when sphere is visible
    cam.renderOnlyIfNodeVisible([sphere])
    cam.setRenderLimit(viz.RENDER_LIMIT_FRAME)
    cams.append(cam)

# Do not render sphere to cubemap cameras
sphere.renderToAllRenderNodesExcept(cams)

# Apply cubemap texture to sphere
sphere.texture(cube)