The bge.texture module allows you to manipulate textures during the game.
Several sources for texture are possible: video files, image files, video capture, memory buffer, camera render or a mix of that.
The video and image files can be loaded from the internet using an URL instead of a file name.
In addition, you can apply filters on the images before sending them to the GPU, allowing video effect: blue screen, color band, gray, normal map.
bge.texture uses FFmpeg to load images and videos. All the formats and codecs that FFmpeg supports are supported by this module, including but not limited to:
* AVI
* Ogg
* Xvid
* Theora
* dv1394 camera
* video4linux capture card (this includes many webcams)
* videoForWindows capture card (this includes many webcams)
* JPG
The principle is simple: first you identify a texture on an existing object using the :materialID: function, then you create a new texture with dynamic content and swap the two textures in the GPU.
The GE is not aware of the substitution and continues to display the object as always, except that you are now in control of the texture.
When the texture object is deleted, the new texture is deleted and the old texture restored.
Basic Video Playback
++++++++++++++++++++
Example of how to replace a texture in game with a video. It needs to run everyframe
import bge
from bge import texture
from bge import logic
cont = logic.getCurrentController()
obj = cont.owner
# the creation of the texture must be done once: save the
# texture object in an attribute of bge.logic module makes it persistent
if not hasattr(logic, 'video'):
# identify a static texture by name
matID = texture.materialID(obj, 'IMvideo.png')
# create a dynamic texture that will replace the static texture
logic.video = texture.Texture(obj, matID)
# define a source of image for the texture, here a movie
movie = logic.expandPath('//trailer_400p.ogg')
logic.video.source = texture.VideoFFmpeg(movie)
logic.video.source.scale = True
# quick off the movie, but it wont play in the background
logic.video.source.play()
# you need to call this function every frame to ensure update of the texture.
logic.video.refresh(True)
Texture Replacement
+++++++++++++++++++
Example of how to replace a texture in game with an external image.
createTexture() and removeTexture() are to be called from a module Python
Controller.
from bge import logic
from bge import texture
def createTexture(cont):
"""Create a new Dynamic Texture"""
obj = cont.owner
# get the reference pointer (ID) of the internal texture
ID = texture.materialID(obj, 'IMoriginal.png')
# create a texture object
object_texture = texture.Texture(obj, ID)
# create a new source with an external image
url = logic.expandPath("//newtexture.jpg")
new_source = texture.ImageFFmpeg(url)
# the texture has to be stored in a permanent Python object
logic.texture = object_texture
# update/replace the texture
logic.texture.source = new_source
logic.texture.refresh(False)
def removeTexture(cont):
"""Delete the Dynamic Texture, reversing back the final to its original state."""
try:
del logic.texture
except:
pass
FFmpeg video source
video status
replay range
repeat count, -1 for infinite repeat
Type: | int |
---|
frame rate
Type: | float |
---|
Tells if an image is available
Type: | bool |
---|
image data
image size
fast scale of image (near neighbour)
flip image vertically
pixel filter
number of frames of preseek
Type: | int |
---|
deinterlace image
Type: | bool |
---|
Play (restart) video
pause video
stop video (play will replay it from start)
Refresh video - get its status
FFmpeg image source
video status
Tells if an image is available
Type: | bool |
---|
image data
image size
fast scale of image (near neighbour)
flip image vertically
pixel filter
Refresh image, i.e. load it
Reload image, i.e. reopen it
Image source from image buffer
pixel filter
flip image vertically
image data
Load image from buffer
update image buffer
fast scale of image (near neighbour)
image size
bool to tell if an image is available
Image source from mirror
use alpha in texture
background color
size of render area
clipping distance
pixel filter
flip image vertically
image data
Refresh image - invalidate its current content
fast scale of image (near neighbour)
image size
bool to tell if an image is available
use whole viewport to render
Image mixer
pixel filter
flip image vertically
get image source
get image source weight
image data
Refresh image - invalidate its current content
fast scale of image (near neighbour)
set image source
set image source weight
bool to tell if an image is available
Image source from render
use alpha in texture
background color
size of render area
pixel filter
flip image vertically
image data
Refresh image - invalidate its current content
fast scale of image (near neighbour)
image size
bool to tell if an image is available
use whole viewport to render
use depth component of render as array of float - not suitable for texture source, should only be used with bge.texture.imageToArray(mode=’F’)
use depth component of render as grey scale color - suitable for texture source
Image source from viewport
use alpha in texture
size of viewport area being captured
pixel filter
flip image vertically
image data
upper left corner of captured area
Refresh image - invalidate its current content
fast scale of image (near neighbour)
image size
bool to tell if an image is available
use whole viewport to capture
use depth component of viewport as array of float - not suitable for texture source, should only be used with bge.texture.imageToArray(mode=’F’)
use depth component of viewport as grey scale color - suitable for texture source
Texture objects
OpenGL Bind Name
Close dynamic texture and restore original
mipmap texture
Refresh texture from source
source of texture
Source filter BGR24 objects
Filter for Blue Screen objects
blue screen color
blue screen color limits
previous pixel filter
Filter for color calculations
matrix [4][5] for color calculation
previous pixel filter
Filter for levels calculations
levels matrix [4] (min, max)
previous pixel filter
Filter for Blue Screen objects
index of color used to calculate normal (0 - red, 1 - green, 2 - blue)
depth of relief
previous pixel filter
Returns a new input filter object to be used with ImageBuff object when the image passed to the ImageBuff.load() function has the 3-bytes pixel format BGR.
Source filter RGBA32 objects
Last error that occurred in a bge.texture function.
Returns: | the description of the last error occurred in a bge.texture function. |
---|---|
Return type: | string |
Returns a buffer corresponding to the current image stored in a texture source object.
Parameters: |
|
---|---|
Return type: | buffer |
Returns: | A object representing the image as one dimensional array of bytes of size (pixel_size*width*height), line by line starting from the bottom of the image. The pixel size and format is determined by the mode parameter. For mode ‘F’, the array is a one dimensional array of float of size (width*height). |
Returns a numeric value that can be used in Texture to create a dynamic texture.
The value corresponds to an internal material number that uses the texture identified by name. name is a string representing a texture name with IM prefix if you want to identify the texture directly. This method works for basic tex face and for material, provided the material has a texture channel using that particular texture in first position of the texture stack. name can also have MA prefix if you want to identify the texture by material. In that case the material must have a texture channel in first position.
If the object has no material that matches name, it generates a runtime error. Use try/except to catch the exception.
Ex: bge.texture.materialID(obj, ‘IMvideo.png’)
Parameters: |
|
---|---|
Return type: | integer |
Sets the name of a text file in which runtime error messages will be written, in addition to the printing of the messages on the Python console. Only the runtime errors specific to the VideoTexture module are written in that file, ordinary runtime time errors are not written.
Parameters: | filename (string) – name of error log file |
---|---|
Return type: | integer |