Sharedwww / talks / 2006-05-09-sage-digipen / tutorial / modeling-shadow-1.pyOpen in CoCalc
Author: William A. Stein
1
# Soya 3D tutorial
2
# Copyright (C) 2001-2004 Jean-Baptiste LAMY
3
#
4
# This program is free software; you can redistribute it and/or modify
5
# it under the terms of the GNU General Public License as published by
6
# the Free Software Foundation; either version 2 of the License, or
7
# (at your option) any later version.
8
#
9
# This program is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
# GNU General Public License for more details.
13
#
14
# You should have received a copy of the GNU General Public License
15
# along with this program; if not, write to the Free Software
16
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17
18
19
# modeling-shadow-1: Modeling : shadows !
20
21
# Shadows must be enabled both on the light that casts the shadow, and on the shape
22
# that projects it.
23
24
# The shadow algorithm assume that non-double sided faces are part of closed volumes, so
25
# you should take this into account when designing your models.
26
# Nothing is assumed for double sided faces, though, so in doubt, use double sided faces.
27
28
# To add shadow to an animated character, see also tutorial lesson
29
# character-animation-shadow-cellshading-1.
30
31
32
# Imports and inits Soya (see lesson basic-1.py).
33
34
import sys, os, os.path, soya, soya.cube
35
36
soya.init()
37
soya.path.append(os.path.join(os.path.dirname(sys.argv[0]), "data"))
38
39
# Creates the scene.
40
41
scene = soya.World()
42
43
# Loads the sword model.
44
45
sword = soya.World.get("sword")
46
47
# Creates a simple shape shapifier object. A shapifier is an object that says how a world
48
# is compiled into a shape. When no shapifier is specified, Soya uses the default shapifier
49
# (an instance of SimpleShapifier, that does not include shadows).
50
51
shapifier = soya.SimpleShapifier()
52
53
# Sets the 'shadow' shapifier property to true.
54
55
shapifier.shadow = 1
56
57
# Assigns the shapifier to the sword.
58
59
sword.shapifier = shapifier
60
61
# Compiles the sword model to a shadowed shape. Notice that is you save the sword now,
62
# the shapifier would be saved with it too, and thus you can use 'soya.Shape.get("sword")'.
63
64
sword_shape = sword.shapify()
65
66
# Create a rotating volume class, and a rotating volume.
67
68
class RotatingVolume(soya.Volume):
69
def __init__(self, parent = None, shape = None):
70
soya.Volume.__init__(self, parent, shape)
71
72
def advance_time(self, proportion):
73
soya.Volume.advance_time(self, proportion)
74
75
self.rotate_lateral(proportion * 2.0)
76
77
78
volume = RotatingVolume(scene, sword_shape)
79
volume.set_xyz(1.0, -1.0, 0.0)
80
volume.rotate_vertical(90.0)
81
82
# Creates a wall, to receive the shadow.
83
# Notice that the wall can be any object (including e.g. a landscape,...) : receiving
84
# shadows doesn't require any particular property.
85
86
wall_model = soya.World()
87
wall_face = soya.Face(wall_model, [
88
soya.Vertex(wall_model, 0.0, -5.0, -5.0),
89
soya.Vertex(wall_model, 0.0, -5.0, 5.0),
90
soya.Vertex(wall_model, 0.0, 5.0, 5.0),
91
soya.Vertex(wall_model, 0.0, 5.0, -5.0),
92
])
93
wall_face.double_sided = 1
94
wall = soya.Volume(scene, wall_model.shapify())
95
wall.set_xyz(-1.0, 0.0, 0.0)
96
97
# Creates a light.
98
99
light = soya.Light(scene)
100
light.set_xyz(5.0, 0.0, 0.0)
101
102
# Sets the light's shadow color. Semitransparent color (alpha < 1.0) are often used.
103
104
light.shadow_color = (0.0, 0.0, 0.0, 0.5)
105
106
# Enables shadows for this light
107
108
light.cast_shadow = 1
109
110
# Creates a camera.
111
112
camera = soya.Camera(scene)
113
camera.set_xyz(0.0, 0.0, 3.5)
114
soya.set_root_widget(camera)
115
116
soya.Idler(scene).idle()
117