| 1 |
#X3D V3.2 utf8 |
| 2 |
PROFILE Interactive |
| 3 |
|
| 4 |
# ---------------------------------------------------------------------------- |
| 5 |
# X3D nodes (sound + lighting) to make a thunder (like in a storm) effect. |
| 6 |
# ---------------------------------------------------------------------------- |
| 7 |
|
| 8 |
# Blinking light |
| 9 |
DEF MyLight DirectionalLight { |
| 10 |
ambientIntensity 1.0 |
| 11 |
color 1 1 1 |
| 12 |
direction 0 0 -1 |
| 13 |
global TRUE |
| 14 |
on FALSE |
| 15 |
} |
| 16 |
|
| 17 |
# Sound |
| 18 |
Sound { |
| 19 |
spatialize FALSE |
| 20 |
source DEF MyAudio AudioClip { |
| 21 |
url "../sounds/thunder.wav" |
| 22 |
} |
| 23 |
} |
| 24 |
|
| 25 |
# Script that sets up starting times for everything that happens. |
| 26 |
DEF MyScript Script { |
| 27 |
inputOutput SFTime startLight1AndAudio 0 |
| 28 |
inputOutput SFTime startLight2 0 |
| 29 |
inputOutput SFBool light2Active FALSE |
| 30 |
inputOutput SFBool audioActive FALSE |
| 31 |
|
| 32 |
# Times of the thunder effect. |
| 33 |
# During a single thunder effect: |
| 34 |
# - light blinks between 0 .. blink1Duration, |
| 35 |
# - and then again blinks between blink2Start .. blink2Start + blink2Duration. |
| 36 |
# All times are in seconds. |
| 37 |
|
| 38 |
inputOutput SFTime blink2Start 1.5 |
| 39 |
inputOutput SFTime durationBetweenConst 10 |
| 40 |
inputOutput SFTime durationBetweenRandom1 10 |
| 41 |
inputOutput SFTime durationBetweenRandom2 20 |
| 42 |
|
| 43 |
# send TRUE value to this field to cause thunder now. FALSE is ignored. |
| 44 |
inputOnly SFBool forceThunderNow |
| 45 |
|
| 46 |
url "castlescript: |
| 47 |
function initialize(time) |
| 48 |
{ set up first thunder in the future } |
| 49 |
startLight1AndAudio := time + |
| 50 |
durationBetweenConst + random() * durationBetweenRandom1; |
| 51 |
startLight2 := startLight1AndAudio + blink2Start |
| 52 |
|
| 53 |
function forceThunderNow(value, time) |
| 54 |
when (value, |
| 55 |
startLight1AndAudio := time; |
| 56 |
startLight2 := startLight1AndAudio + blink2Start) |
| 57 |
|
| 58 |
function light2Active(value, time) |
| 59 |
when (and(not(value), not(audioActive)), |
| 60 |
{ Once everything finished (2nd light blink and sound) finished, |
| 61 |
set up next thunder in the future. |
| 62 |
We can only do it once everything finished, as X3D spec says that |
| 63 |
'Any set_startTime events to an active time-dependent node are ignored.' } |
| 64 |
startLight1AndAudio := startLight1AndAudio + |
| 65 |
durationBetweenConst + random() * durationBetweenRandom2; |
| 66 |
startLight2 := startLight1AndAudio + blink2Start) |
| 67 |
|
| 68 |
function audioActive(value, time) |
| 69 |
when (and(not(value), not(light2Active)), |
| 70 |
{ Exactly like light2Active. We have to watch for both light2Active |
| 71 |
and audioActive, as we don't know which one takes longer: light blinking |
| 72 |
or audio sound. } |
| 73 |
startLight1AndAudio := startLight1AndAudio + |
| 74 |
durationBetweenConst + random() * durationBetweenRandom2; |
| 75 |
startLight2 := startLight1AndAudio + blink2Start) |
| 76 |
" } |
| 77 |
|
| 78 |
# Controls the 1st blink of light. |
| 79 |
DEF Light1Timer TimeSensor { cycleInterval 1.0 } |
| 80 |
ROUTE Light1Timer.isActive TO MyLight.on |
| 81 |
# Controls the 2nd blink of light. |
| 82 |
DEF Light2Timer TimeSensor { cycleInterval 1.0 } |
| 83 |
ROUTE Light2Timer.isActive TO MyLight.on |
| 84 |
|
| 85 |
# ROUTEs in and out of the MyScript |
| 86 |
ROUTE MyScript.startLight1AndAudio TO Light1Timer.startTime |
| 87 |
ROUTE MyScript.startLight1AndAudio TO MyAudio.startTime |
| 88 |
ROUTE MyScript.startLight2 TO Light2Timer.startTime |
| 89 |
ROUTE Light2Timer.isActive TO MyScript.light2Active |
| 90 |
ROUTE MyAudio.isActive TO MyScript.audioActive |