Vizard 7 » Tutorials & Examples » Example scripts » Shaders » Multi texture
7.6

Multi texture

\examples\shader\tutorial_multiTexture.py

This script uses a multi-texturing shader. Multi-texturing allows you to apply multiple textures to an object.

"""
This script uses a multi-texturing shader effect.
Multitexturing allows you to apply multiple textures to an object.
Drag the slider to change the texture blending mix.
"""
import viz
import vizfx
import vizact

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

import vizinfo
vizinfo.InfoPanel()

viz.clearcolor( viz.SLATE )

# Setup the models
logo1 = vizfx.addChild('logo.osgb', pos=[1, 1, 3])
logo2 = vizfx.addChild('logo.osgb', pos=[-1, 1, 3])

# Add all the textures that the tutorial will use
tex1 = viz.addTexture('images/tile_slate.jpg', wrap=viz.REPEAT)
tex2 = viz.addTexture('images/tile_stone.jpg', wrap=viz.REPEAT)
tex3 = viz.addTexture('images/tile_wood.jpg', wrap=viz.REPEAT)
tex4 = viz.addTexture('images/tile_grass.jpg', wrap=viz.REPEAT)

# Apply textures to the models
logo1.texture(tex1)
logo1.texture(tex2, unit=1)

logo2.texture(tex3)
logo2.texture(tex4, unit=1)

# Create shader effect that blends textures and applies to diffuse color
code = """
Effect "Texture Blend" {

    Float BlendAmount { value 0 }
    Texture2D Texture1 { unit 0 }
    Texture2D Texture2 { unit 1 }

    Shader {

        BEGIN Material
        m.diffuse = mix( texture2D( Texture1, uvTexture1).rgb, texture2D( Texture2, uvTexture2).rgb, BlendAmount);
        END

    }

}
"""
texBlendEffect = viz.addEffect(code)

# Apply the effect to the models
logo1.apply(texBlendEffect)
logo2.apply(texBlendEffect)

def SetBlendAmount(pos, model):
    #Set the value of the blend amount property
    model.setUniformFloat('BlendAmount', pos)

# Create sliders to control the blend amount for each object
slider1 = viz.addSlider(pos=[0.75,0.1,0])
slider2 = viz.addSlider(pos=[0.2,0.1,0])
vizact.onslider(slider1, SetBlendAmount, logo1)
vizact.onslider(slider2, SetBlendAmount, logo2)