Transition through multiple textures

I have an initial texture loaded on my model and then I want to fade / transition gracefully into a second texture and then do another fade / transition into a third texture.

I found this shader code that works to fade from the original model’s texture to a second texture, but I can’t figure out how to fade to the third.

from pandac.PandaModules import *
from direct.interval.IntervalGlobal import *
import direct.directbase.DirectStart
import os

t1 = loader.loadTexture('maps/envir-ground.jpg')
ts = TextureStage('2')

s = loader.loadModel('smiley').getChild(0)
s.reparentTo(render)
s.setTexture(ts,t1)

sha = loader.loadShader('fade.sha')
s.setShader(sha)
s.setShaderInput('fade',0)

def applyFade(fade):
    print fade
    s.setShaderInput('fade',fade)

lerp1 = LerpFunctionInterval(applyFade,duration=3)

Sequence(
  lerp1, Wait(1)
).start()
//Cg
void vshader(
  in float4 vtx_color: COLOR,
  out float4 l_color0 : COLOR0,
  in float4 vtx_position : POSITION,
  out float4 l_position : POSITION,
  in float2 vtx_texcoord0 : TEXCOORD0,
  out float2 l_texcoord0 : TEXCOORD0,
  in uniform float4x4 mat_modelproj)
{
  l_color0 = vtx_color;
  l_position = mul(mat_modelproj, vtx_position);
  l_texcoord0 = vtx_texcoord0;
}

void fshader(
  in float4 l_color0 : COLOR0,
  in float2 l_texcoord0: TEXCOORD0,
  in uniform sampler2D tex_0: TEXUNIT0,
  in uniform sampler2D tex_1: TEXUNIT1,
  in uniform float4 k_fade,
  out float4 o_color : COLOR)
{
  float4 first_color = tex2D(tex_0, l_texcoord0);
  float4 second_color = tex2D(tex_1, l_texcoord0);
  o_color = lerp(second_color, first_color, k_fade.x);
}

You can use a nested lerp:

lerp(col1, lerp(col2, col3, fade1), fade2)

Where fade1 = saturate(k_fade.x * 2.0);
And fade2 = saturate(k_fade.x * 2.0 - 1.0);

How do I setup texture2 and texture3 in Python?

I also need to fire off one transition at a time and then wait an undetermined amount of time before doing the next transition. So doing them all in the nested lerp may not work.

I’m successfully transitioning through a few textures.

Initial model texture > envir-ground > envir-mountain1

When I initiate ground > mountain I get a quick flicker of mountain on top of ground and then it fades correctly from ground > mountain. Can you tell why it is flickering?

On the last fade, I’m trying to go from envir-mountain1 > envir-rock2, but it isn’t working, mountain transitions back to ground. Why can’t I get to this last texture? Thanks!

from pandac.PandaModules import *
from direct.interval.IntervalGlobal import *
import direct.directbase.DirectStart
import os, time

s = loader.loadModel('smiley').getChild(0)
s.reparentTo(render)

sha = loader.loadShader('fade.sha')

def applyFade(fade):
    print fade
    s.setShaderInput('fade',fade)

def fade1():
	t1 = loader.loadTexture('maps/envir-ground.jpg')
	ts = TextureStage('1')		
	s.setTexture(ts,t1)	
	s.setShader(sha)
	s.setShaderInput('fade',0)

	lerp1 = LerpFunctionInterval(applyFade,duration=3)
	lerp2 = LerpFunctionInterval(applyFade,duration=3,fromData=1,toData=0)

	Sequence(
	  lerp1, Wait(1)
	).start()

def fade2():
	t1 = loader.loadTexture('maps/envir-mountain1.png')
	ts = TextureStage('2')
	
	s.setTexture(ts,t1)
	s.setShaderInput('fade',1)
	
	lerp1 = LerpFunctionInterval(applyFade,duration=3)
	lerp2 = LerpFunctionInterval(applyFade,duration=3,fromData=1,toData=0)
	
	Sequence(
	  lerp2, Wait(1)
	).start()

def fade3():
	t1 = loader.loadTexture('maps/envir-rock2.jpg')
	ts = TextureStage('3')
	
	s.setTexture(ts,t1)
	s.setShaderInput('fade',0)
	
	lerp1 = LerpFunctionInterval(applyFade,duration=3)
	
	Sequence(
	  lerp1, Wait(1)
	).start()	
#mechanism to call next fade stage
def next(value):
	print "nextValue: " + str(base.nextValue)
	if base.nextValue == 0:
		fade1()
	elif base.nextValue == 1:
		fade2()
	else:
		fade3()
	base.nextValue += 1

base.nextValue = 0

base.accept('r',next,[0])
base.accept('escape',os._exit,[0])
base.cam.setY(-10)
run()