Soya/Python animation de personnage, ombres et cell-shading

Un livre de Wikilivres.
# -*- indent-tabs-mode: t -*-

# Soya 3D tutorial
# Copyright (C) 2001-2002 Jean-Baptiste LAMY
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA


# character-animation-shadow-cellshading-1: ombrage et cellshading pour les personnages animés Cal3D

# Dans cette leçon, nous ajoutons des ombres et du cell-shading à Balazar.
#
# Voir les leçons modeling-shadow et modeling-cellshading pour plus d'information sur les ombres et le
# cell-shading.


# Importation et initialisation de Soya.

import sys, os, os.path, soya, soya.widget as widget

soya.init()
soya.path.append(os.path.join(os.path.dirname(sys.argv[0]), "data"))

# Création de la scène.

scene = soya.World()

# Ajout d'une atmosphère, pour que le background soit gris (in order to see the black outline
# of the cell-shading).

scene.atmosphere = soya.Atmosphere()
scene.atmosphere.bg_color = (0.6, 0.6, 0.6, 1.0)

# Chargement du modèle du sorcier.

sorcerer_model = soya.AnimatedModel.get("balazar")

# Activation de l'ombre sur Balazar !

sorcerer_model.shadow = 1

# Activation du cell-shading sur Balazar !
# Arguments (ils sont tous optionnels):
# - shader (par défaut : soya.SHADER_DEFAULT_MATERIAL)
# - outline_color (par défaut : defaults to black)
# - outline_width (par défaut : 4.0)
# - outline_attenuation (par défaut : 0.3)
#
# Voir la leçon modeling-cellshading-1
# pour plus d'informations sur ces arguments

sorcerer_model.set_cellshading()

# Notez que vous pouvez ajouter ces attributs dans le fichier .cfg du Cal3D 
# (par exemple : balazar/balazar.cfg) :
#
#double_sided=1
#shadow=1
#cellshading=1
#cellshading_outline_color=1.0, 1.0, 1.0, 1.0
#cellshading_outline_width=4.0
#cellshading_outline_attenuation=0.0
#
# double_sided dessine un modèle Cal3D avec une double face
# (équivalent à Face.double_sided). Les autres attributs ont une signification évidente

#
# Et souvenez vous : ne pas mettre d'espace avant ou après le "=" !

# Création du Body du sorcier

sorcerer = soya.Body(scene, sorcerer_model)

# Rotation du sorcier Balazar

sorcerer.rotate_y(-120.0)

# Début de l'animation "marche" en cycle.

sorcerer.animate_blend_cycle("marche")


# Création d'un mur pour recevoir l'ombre.
# Notez que le mur peut être n'importe quel objet (tel que un terrain ou autre) : recevoir
# une ombre ne require pas de propriété spéciales.

wall_model = soya.World()
wall_face = soya.Face(wall_model, [
	soya.Vertex(wall_model, -5.0, 0.0, -5.0),
	soya.Vertex(wall_model, -5.0, 0.0, 5.0),
	soya.Vertex(wall_model, 5.0, 0.0, 5.0),
	soya.Vertex(wall_model, 5.0, 0.0, -5.0),
	])
wall_face.double_sided = 1
wall = soya.Body(scene, wall_model.to_model())
wall.set_xyz(-1.0, 0.0, 0.0)

# Ajouter une caméra et une lampe puis début de la boucle principale.

light = soya.Light(scene)
light.set_xyz(2.0, 5.0, -1.0)
light.cast_shadow = 1
light.shadow_color = (0.0, 0.0, 0.0, 0.5)

camera = soya.Camera(scene)
camera.set_xyz(0.0, 1.0, 4.0)

soya.set_root_widget(camera)

soya.MainLoop(scene).main_loop()