Example #1
0
  /**
   * Notification that the script has completed the setup and should go about its own internal
   * initialization.
   */
  public void initialize() {
    touchTime = (SFTime) fields.get("touchTime");

    // Listen to events on touchTime
    touchTime.addX3DEventListener(this);

    // Create nodes directly in the parent scene
    X3DScene scene = (X3DScene) browser.getExecutionContext();

    X3DShapeNode shape = (X3DShapeNode) scene.createNode("Shape");
    X3DGeometryNode box = (X3DGeometryNode) scene.createNode("Box");
    X3DNode touchSensor = scene.createNode("TouchSensor");

    shape.setGeometry(box);

    // Create a Group to hold the nodes
    X3DGroupingNode group = (X3DGroupingNode) scene.createNode("Group");

    // Add the shape and sensor to the group
    group.addChild(shape);
    group.addChild(touchSensor);

    // Add the nodes to the scene
    scene.addRootNode(group);

    // Get a handle to the toplevel execution context
    scene.addRoute(touchSensor, "touchTime", selfRef, "touchTime");
  }
Example #2
0
  /**
   * Output a specific entity to the specified stream.
   *
   * @param model The world model to export
   * @param entityID The entity to export
   * @param mainScene The X3D scene to write to
   */
  public X3DNode export(WorldModel model, int entityID, X3DScene mainScene, String worldURL) {

    // get the entity
    Entity entity = model.getEntity(entityID);

    if (entity == null) {
      errorReporter.messageReport("Cannot find model to export: " + entityID);
      return null;
    }

    if (entity.isController()) {
      return null;
    }

    try {

      X3DNode group;

      // if the entity has a position then place it
      if (entity instanceof PositionableEntity) {

        double[] position = new double[3];
        float[] rotation = new float[4];
        float[] scale = new float[3];

        ((PositionableEntity) entity).getPosition(position);
        ((PositionableEntity) entity).getRotation(rotation);
        ((PositionableEntity) entity).getScale(scale);

        float[] pos = new float[] {(float) position[0], (float) position[1], (float) position[2]};

        // create the transform group node
        group = mainScene.createNode("Transform");
        SFVec3f translationField = (SFVec3f) (group.getField("translation"));
        translationField.setValue(pos);
        SFRotation rotationField = (SFRotation) (group.getField("rotation"));
        rotationField.setValue(rotation);
        SFVec3f scaleField = (SFVec3f) (group.getField("scale"));
        scaleField.setValue(scale);

      } else {

        // create the transform group node
        group = mainScene.createNode("Group");
      }

      // create the inline node
      String url = worldURL + entity.getModelURL();

      X3DNode inline = mainScene.createNode("Inline");
      MFString urlField = (MFString) (inline.getField("url"));
      urlField.setValue(1, new String[] {url});

      // grab the child node to append to
      MFNode childrenField = (MFNode) (group.getField("children"));

      // add inline to the group/transform
      childrenField.append(inline);

      return group;

    } catch (Exception ex) {
      errorReporter.errorReport("Error.", ex);
    }

    return null;
  }