Write a texture dragging tool

From JReality Wiki
Jump to: navigation, search

Source file: TextureDragExample

JavaDoc: Texture2D

Run as Java webstart

The following example shows how to write a tool which drags a 2d texture around on its surface, by manipulating the texture matrix associated to it. For an introduction to the tool system in jReality see this this introductory document on the jReality scene graph. (Needed: better documentation for toolconfig.xml)

The example is built on the Use a 2d texture example, so the code for this class has been omitted.

To operate the example:

  • Dragging while holding the left mouse button down, while the cursor is over the geometry drags the texture along the surface.
  • To rotate the object, move the cursor away from the geometry (over the background) before dragging.

Things to notice:

  • The tool sets its activation slot to InputSlot.getDevice("AllDragActivation"). If you look into the file toolconfig.xml you'll see this is mapped to the left mouse. So, our tool only works when you press the left mouse.
  • the code
  addCurrentSlot(InputSlot.getDevice("PointerTransformation"), "drags the texture");

makes sure that the position of the mouse within world space is available via the "PointerTransformation". If you consult toolconfig.xml you'll see this device is a virtual one constructed from the mouse position; it basically describes a vector in world coordinates which is in line with the camera position and the cursor position. Without this slot, no picking takes place, and the perform() method of the tool will not be invoked.

public static void main(String[] args) throws IOException {
   final Texture2D tex = TextureUtility.createTexture(sgc.getAppearance(), POLYGON_SHADER,id);
   Tool t = new AbstractTool(InputSlot.getDevice("AllDragActivation")) {
      private double[] origTexCoords;
      Matrix origTexMatrix;
        addCurrentSlot(InputSlot.getDevice("PointerTransformation"), "drags the texture");
    public void activate(ToolContext tc) {
      origTexCoords = tc.getCurrentPick().getTextureCoordinates();
      origTexMatrix = tex.getTextureMatrix();
    public void perform(ToolContext tc) {
      if (tc.getCurrentPick() == null) return;
      double[] texCoords = tc.getCurrentPick().getTextureCoordinates();
      double[] diff = Rn.subtract(null, origTexCoords, texCoords);
      double[] diff4 = {diff[0], diff[1], 0, 1.0};
      double[] trans = P3.makeTranslationMatrix(null, diff4, Pn.EUCLIDEAN);
      tex.setTextureMatrix(new Matrix(Rn.times(null, origTexMatrix.getArray(), trans)));
    public String getDescription(InputSlot slot) {
      return null;
    public String getDescription() {
      return "A tool which drags a texture around";


Previous: Write a simple tool Developer Tutorial: Contents Next: Write a 3D paint tool