tutorial


Multiple texture change
By Dennis De Vetter

In this tutorial I will show you how to change multiple textures for a Cult3D object, using JAVA. Since a lot
of people are struggling with texture changing and JAVA is the only way 'yet' to do this, here are some tips.



note: You can use Cult3D Designer itself to have the same effect. Have several objects and use Hide/Unhide,
but it only slows down the display, because you have a lot more polygons then.


I will use an example of a project i recently worked on. What we wanted to do is create several Cult3D models
of style furniture, and where possible add some interactivity & texture changing. For this tutorial I will show
you how we created the PETILIA chair.


First the 3D model was created in 3D Studio MAX using scanned images of the real chair as backdrop.



Then we added textures to the chair.
For the future texture change to work, you need an image file as texture.
So we create a texture in a 2D paint software (e.g. Photoshop).
Mind to set the image resolution to a force of 2 (e.g. 64x64, 128x128) and store the bitmap in *.JPG or *.GIF.
Then we made a new material in 3d Studio MAX for the seat.

Diffuse map - type: BITMAP.



And took our own texture for the color of the seat.

navy texture

(64x64 GIF file)


Important: the texture for the seat of the chair will be used in our JAVA code later on, so remember its name.


Now that we have our 3D object with textures it was time to export it to Cult3D Designer.



Under the Textures node you can see our texture for the seat. Here you can resize your original texture.
Since we already had our file in good resolution there was no real need to alter anything here, but it is
always wise to do so. For instance if you don't use square texture you can always alter them here, just
before exporting to Cult3D Designer. We set the Size selection method to "Use max as size" and put
both x&y sliders to 64.

Up to JAVA.
We had everything we needed now to write our JAVA code for the texture change. Except.. yes of course.
We needed other textures ! We created 3 new textures, so in our final Cult3D object you will be able to
switch between 4 different textures.

apple (64x64 GIF-file)
aqua (64x64 GIF-file)
salmon (64x64 GIF-file)


OK.
I will explain the JAVA code one step at a time.
note: click here to see the whole JAVA code.


--START-

// These classes are needed for our JAVA-code to work.
import com.cult3d.*;
import com.cult3d.world.*;
import java.awt.Image;
import java.awt.Graphics;


// This is the declaration of our JAVA code.
// remember to use the same name for both FILE & CLASSNAME (case-sensitive).
public class Petilia extends Thread implements Cult3DScript {

// These are instance variables we need to change the textures.
// notice the variable imgname is an array, because there are several images (textures).
private String texname;
private String [] imgname;
private Texture tex;
private TextureImage img;
private int t_count;

private boolean loaded;

// the constructor. This code is automatically executed when the Cult3D object is loaded
public Petilia() {

// Starts the thread
start();

}

public void run() {

texname = new String("navy");
imgname = new String[4];
imgname[0]="apple.gif";
imgname[1]="aqua.gif";
imgname[2]="salmon.gif";
imgname[3]="navy.gif";
tex = new Texture(texname);
img = (TextureImage)Cult.getImage(imgname[0]);
t_count=0
;
loaded = true;

}

texname holds the name of the texture used in 3d Studio MAX.
imgname is a variable of the String type and holds the names of the 3 other textures + original.

note:
with texname you use the filename of the texture WITHOUT extension. with imgname
you use the filename WITH extension.

tex makes a texture for the petilia seat in memory.
img will be used to retrieve the 3 other textures and store them in memory.
t_count is an integer that will be used to count the number of texture changes already performed
and as a reference to let JAVA know what texture to load.


// The actual texture change function
public void TextureChange(String arg) {

if(loaded) tex.setTexture(img);
t_count = t_count + 1;
if (t_count>3) t_count = 0;
img = (TextureImage)Cult.getImage(imgname[t_count]);

}

When you change back from the last texture to the first texture, the t_count integer is reset.

note: The first new texture is automatically loaded in memory when starting the Cult3D model.
This speeds up the first texture change because the new texture is already in memory.

-END--


The finished JAVA-code was compiled and ready to be used in Cult3D Designer. With our Cult3D
model of the PETILIA chair loaded in Cult3D Designer open "java actions" in the "windows" menu.

Choose add and pick the compiled .class file.
Write the name of the class + .class for the "Startup class".

Now pick "Resources" in the "Windows" menu and add all 4 textures.
(navy.gif, apple.gif, aqua.gif, salmon.gif).

Last thing to do is link the JAVA function "TextureChange" to a mouse event.
Just drag and drop the JAVA function from the "java actions" menu onto the "mouse event".



The FINAL Cult3D object looks like this.


click on the image to load the CULT3D model.

note: we have made some adjustments not explained in this tutorial. The changing text at the left bottom
is also a texture change the interaction is done with the Cult3D Designer.