PDA

View Full Version : Blending/Fading 5 Textures with a Slider


south_bank
05-14-2008, 07:39 AM
Hi,

I'm trying to create an optional extras car showroom simulation which simulates for example, paint colour options. Rather than having lots of car geometry textured in many different colours and showing/hiding them according to which buttons are selected, I would like to use a slider control to fade/blend between the five different colour options of red, black, white, blue and green.

I have searched the example scripts and help files however the blend script seems only to support two textures. I have also experimented with fading between textures but due to relative inexperience, I'm struggling to find a solution with this method.

Is there another way around this issue that would allow me to control the car's paint colour through the five different colours with a slider , preferably of custom appearance? Any help and suggestions will be greatly appreciated.

Thanks.

farshizzo
05-14-2008, 05:04 PM
Here is some sample code that should do what you want. It creates a class that allows to blend between an arbitrary number of values. The slider on the left will blend between 5 different colors and the slider on the right will blend between 5 different textures. Let me know if anything is unclear:import viz
viz.go()

class MultiBlender(object):

def __init__(self,values):
"""Initialize blender with list of values"""
self.values = values[:]
self.dv = 1.0 / (len(values)-1)

def getBlendValues(self,p):
"""Returns begin,end, and percent value to blend between them, given total multiblend percent"""
begin = int(p // self.dv)
if begin < 0:
return self.values[0],self.values[1],0.0
elif begin >= (len(self.values)-1):
return self.values[-2],self.values[-1],1.0
return self.values[begin],self.values[begin+1],(p % self.dv) / self.dv

def getInterpolatedValue(self,p):
"""Return blended value at percent p, 0-1"""
begin,end,p = self.getBlendValues(p)
return vizmat.Interpolate(begin,end,p)


#Create quad and a blender that blends between 5 colors
colorQuad = viz.addTexQuad(pos=(-1,1.8,4))
colorBlender = MultiBlender([viz.RED,viz.BLACK,viz.WHITE,viz.BLUE,viz.GREEN])

#Add a slider for the blender
colorSlider = viz.addSlider(pos=(0.25,0.1,0))
def blendColor(pos,node,blender):
node.color(blender.getInterpolatedValue(pos))
vizact.onslider(colorSlider,blendColor,colorQuad,c olorBlender)


#Create quad and a blender that blends between 5 textures
textures = [
'ball.jpg'
,'brick.jpg'
,'gb_noise.jpg'
,'image2.jpg'
,'lake3.jpg'
]

texQuad = viz.addTexQuad(pos=(1,1.8,4))
texBlender = MultiBlender([viz.addTexture(file) for file in textures])

#Add a slider for the blender
texSlider = viz.addSlider(pos=(0.75,0.1,0))
def blendTexture(pos,node,blender):
begin,end,p = blender.getBlendValues(pos)
node.texture(begin,unit=0)
node.texture(end,unit=1)
node.texblend(p,unit=1)
vizact.onslider(texSlider,blendTexture,texQuad,tex Blender)

south_bank
05-15-2008, 07:59 AM
Thanks for the rapid response, it's much appreciated :D. It all appears pretty clear on first glance and I think I won't have much trouble with it. Thanks again!