17: SCRIPTS Y PROTOTIPOS


Script

SINTAXIS:

Script {
    exposedField MFString url          []
    field        SFBool   directOutput FALSE
    field        SFBool   mustEvaluate FALSE

    eventIn  tipoEvento nombreEvento
    field    tipoCampo  nombreCampo  ValorInicial
    eventOut tipoEvento nombreEvento
}

EJEMPLO:

#VRML V2.0 utf8
DEF TAPIZ Transform {
    children # PRIMITIVA PARA EL TAPIZ
}
DEF CORDEL Transform {
    children [
        # PRIMITIVA PARA EL CORDEL
        DEF TS TouchSensor { }
    ]
}
DEF SC Script {
    eventIn  SFTime  toque
    eventOut SFVec3f escala
    eventOut SFVec3f desplaz
    field    SFInt32 estado 0
    url "vrmlscript:
        function toque() {
            if (estado==0) {
                estado = 1;
                escala = new SFVec3f(1,1,1);
                desplaz = new SFVec3f(0,-1.9,-.01);
            } else {
                estado = 0;
                escala = new SFVec3f(1,.15,1);
                desplaz = new SFVec3f(0,-.3,-.01);
            }
        }
    "

}
ROUTE TS.touchTime TO SC.toque
ROUTE SC.escala    TO TAPIZ.set_scale
ROUTE SC.desplaz   TO CORDEL.set_translation

ESQUEMA:

PROTO

SINTAXIS:

PROTO nombre [
    eventIn  tipoEvento nombreEvento
    field    tipoCampo  nombreCampo  ValorInicial
    eventOut tipoEvento nombreEvento
]
{ código del prototipo }

EJEMPLO:

#VRML V2.0 utf8

# DEFINICIÓN
PROTO Pared [
    field SFVec3f tam 2 2 2
    field SFVec2f esc 1 1
    field SFVec2f tra 0 0
]
{
    Shape {
        appearance Appearance {
            material Material {
                ambientIntensity 0.6
            }
            texture ImageTexture {
                url "pared_ladrillos.gif"
            }
            textureTransform TextureTransform {
                scale IS esc
                
translation IS tra
            }
        }
        geometry Box {size IS tam}
    }
}

# UTILIZACION
Transform {
    translation -3 0 0
    children Pared {tam 0.5 1.2 2 esc 10 10}
}
Pared {}
Transform {
    translation 3 0 0
    children Pared {esc 15 15}
}

EXTERNPROTO

EJEMPLO:

#VRML V2.0 utf8

EXTERNPROTO Pared2 [field SFVec2f esc] "fichero.wrl#Pared"

# UTILIZACION
Transform {
    translation -3 0 0
    children Pared2 {esc 10 10}
}
Pared2 {}
Transform {
    translation 3 0 0
    children Pared2 {esc 15 15}
}