Animating textures

Many great effects found in games can be achieved using very simple measures. The effect you will learn about in this recipe falls into this category. Animated textures are used very often to create the illusion of a flowing lava stream, waves on a lake, or details like a transparent plasma container mounted onto an alien gun.

This recipe will teach you how to put a texture onto an object and animate its position, scale, and rotation.

Getting ready

Please make sure you completed the recipe Setting up the game structure found in Chapter 1 before proceeding with the following steps. Also be sure to add a folder called textures to the project's folder structure and add it to Panda3D's content search paths, and have a texture file in PNG format at hand that can be used for this sample.

How to do it...

Follow these steps to complete this recipe:

  1. Copy your texture file to the textures directory and rename it to texture.png.
  2. Open Application.py and add the following code:
    from direct.showbase.ShowBase import ShowBase
    from panda3d.core import *
    from math import *
    class Application(ShowBase):
    def __init__(self):
    ShowBase.__init__(self)
    cm = CardMaker("plane")
    cm.setFrame(-3, 3, -3, 3)
    self.plane = render.attachNewNode(cm.generate())
    tex = loader.loadTexture("texture.png")
    self.plane.setTexture(tex)
    self.cam.setPos(0, -12, 0)
    texturesanimatingtaskMgr.add(self.animate, "texanim")
    def animate(self, task):
    texStage = TextureStage.getDefault()
    offset = self.plane.getTexOffset(texStage)
    offset.setY(offset.getY() - 0.005)
    self.plane.setTexOffset(texStage, offset)
    scale = sin(offset.getY()) * 2
    self.plane.setTexScale(texStage, scale, scale)
    rotate = sin(offset.getY()) * 80
    self.plane.setTexRotate(texStage, rotate)
    return task.cont
    
  3. Press F6 to run the program. The texture will be zoomed in and out while it is being rotated:
How to do it...

How it works...

We begin this sample by adding a textured quad to the scene using the CardMaker class and kicking off the task that will call the animate() method on every frame at runtime.

Texture stages are used to handle the properties of textures that are mapped onto an object. In our sample, there is only one texture map applied to the geometry, so we use the getDefault() method to get a reference to the default TextureStage that holds all necessary data for the next operations.

We then use the methods setTexOffset(), setTexScale(), and setTexRotate() to apply transformation, scaling, and rotation to the texture coordinates. This means that we need to think "in reverse" to get the proper results. To move the texture up, we need to decrease the offset along the y-axis as shown in the code. The same applies to scaling and rotation—bigger values mean that the texture will appear smaller, and turning the coordinate system to the right means that the texture will rotate to the left.

We are using a sinus function for animating the rotation and scale of the texture. This makes these two properties go back and forth, which means we are zooming in and out of the texture while it is rotated to the left and then to the right. We also apply a transformation to the texture that makes it go upwards.

Used together, all these animated properties twist and turn the texture a lot. Experiment and try adding and removing the modifications to the texture offset, scale, and rotation to find new and interesting effects!

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset