To make this work, you simply use a texture node. These go in the Shape object.
Here is a simple example:
brickWall.wrl
#VRML V2.0 utf8
#brickWall.wrl
#andy Harris
# demonstrates use of textures
Shape {
appearance DEF theTexture Appearance {
texture ImageTexture {
url ["brick1.jpg"]
} # end texture
} # end appearance
geometry Box {
size 1 2 3
} # end geometry
} # end shape
NavigationInfo {
type "EXAMINE"
} # end navInfo
As you can see, The texture mode is reminiscent of the material node.
It essentially goes in the same place in the appearance node. You can
have both a texture and a material, but the texture will over-write
the material node. You might choose to still have a material in case
something goes wrong with the texture.
The texture node itself is quite simple. It basically consists of another node, which will define exactly what type of texture is mapped to the object. For now, we will be using ImageTexture nodes.
In the imageTexture, the most important node is the URL node. This is a multi-string node. Note the square brackets. Inside the brackets, you place the url of the image you want to have mapped to the shape. The url must be placed inside double quotes, and must be a valid url of a graphic file in standard url format. It can point to a gif or to a jpeg. Animated gifs will NOT work (although later I will show you an alternative.) Gif files with transparent backgrounds are fine, and are used quite frequently.
Here is a slightly more involved example:
an immersive texture map
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
You can see that there are three main ways that VRML tends to map images to surfaces. It can do a rectangular 'stretch and squash' as you can see on the box shape. On spheres and parts of cones, it does a 'distortion' to try and map the entire rectangular shape to the non-rectangular surface. As you can see, we can use this distortion to our advantage.
If you examine the bottom of the cone with an image mapped onto it, you will notice that VRML also occasionally 'clips' an image. In effect, it draws a rectangle around an image, and simply shows the parts of the image that are are on the surface. So the bottom of the cone is not distorted, but only the center of it is shown.
As usual, you will really need to look carefully at this yourself to understand the phenomenon. It really isn't that difficult to understand once you've played with it a little bit.
#VRML V2.0 utf8
#tree.wrl
# illustrates how to make a simple tree with textures.
# andy Harris, 12/98
Transform {
children [
Shape {
appearance Appearance {
texture ImageTexture {
url [ "tree.gif" ]
} # end texture
} # end appearance
geometry Box {
size 5 5 .01
} # end box
} # end shape
Shape {
appearance Appearance {
texture ImageTexture {
url [ "tree.gif" ]
} # end texture
} # end appearance
geometry Box {
size .01 5 5
} # end box
} # end shape
] # end children
} # end tree
This illustrates how you can generate a reasonably nice tree figure
with very little effort. The technique can be used in other ways, to
quickly make 'cardboard cutouts' of three-dimensional objects. This
is best used for things that will not be central to your scene, as it
will not stand up to close scrutiny, but it does have its uses.
To do this, make an image (or two,) with a transparent background. Try to center the image along the y axis.
When you design the object in VRML, make it two boxes in the same space. Make one of the boxes REALLY tiny in the z axis, and the other really small in the X axis. Map your texture onto both of the boxes.
#VRML V2.0 utf8
#billboard.wrl
# illustrates how to make a simple tree with textures.
# adding the billboard node to make the tree appear dimensional
# andy Harris, 12/98
Billboard {
axisOfRotation 0 1 0
children [
Shape {
appearance Appearance {
texture ImageTexture {
url [ "tree.gif" ]
} # end texture
} # end appearance
geometry Box {
size 5 5 .01
} # end box
} # end shape
] # end children
} # end tree
#VRML V2.0 utf8
#brickTrans.wrl
#illustrates texture transformation
#default, no transformation of textures
Transform {
translation 0 0 0
children [
Shape {
appearance Appearance {
texture ImageTexture {
url ["brick1.jpg"]
} # end texture
} # end appearance
geometry Box {
size 1 1 1
} # end geometry
} # end shape
] # end children
} # end transform
#left hand box
#tries to stretch texture to new size. Note the distortion
Transform {
translation -2 0 0
children [
Shape {
appearance Appearance {
texture ImageTexture {
url ["brick1.jpg"]
} # end texture
} # end appearance
geometry Box {
size .75 5 1
} # end geometry
} # end shape
] # end children
} # end transform
#right hand box
#use texture transform to add scale to texture map
#corrects the texture
Transform {
translation 2 0 0
children [
Shape {
appearance Appearance {
texture ImageTexture {
url ["brick1.jpg"]
} # end texture
#note the addition of a textureTransform node here
textureTransform TextureTransform {
scale .75 5
} # end textureTransform
} # end appearance
geometry Box {
size .75 5 1
} # end geometry
} # end shape
] # end children
} # end transform
#bottom box
#default size geometry, but 'zooms in' the texture for more detail
Transform {
translation 0 -2 0
children [
Shape {
appearance Appearance {
texture ImageTexture {
url ["brick1.jpg"]
} # end texture
textureTransform TextureTransform {
scale .5 .5
} # end textTrans
} # end appearance
geometry Box {
size 1 1 1
} # end geometry
} # end shape
] # end children
} # end transform
#top box
#shows how we can also rotate a texture
Transform {
translation 0 2 0
children [
Shape {
appearance Appearance {
texture ImageTexture {
url ["brick1.jpg"]
} # end texture
textureTransform TextureTransform {
scale .5 .5
rotation -0.5
} # end textTrans
} # end appearance
geometry Box {
size 1 1 1
} # end geometry
} # end shape
] # end children
} # end transform
The box in the center shows the default mapping of the brick graphic
onto a 1x1x1 box. The graphic was designed to be square, so it works
out pretty well on a regular cube. However, the left hand box is
designed to be a fraction of the width and five times the height of
the default. When we map exactly the same graphic onto the 'tall and
skinny' box, we see that the default behavior of the texture causes us
some problems. The texture is 'squashed and stretched' to fit the new
size, giving us tall and skinny bricks. With some textures, this
behavior is fine, but other textures such as bricks really demand more
control over size. If we put the two shapes in the same world, they
would clearly not appear to be made of the same kind of bricks.
#VRML V2.0 utf8
#texPal.wrl
#Attaching a texture palette to an indexed faceset
Shape {
appearance Appearance {
material Material {
diffuseColor 1 1 1
} # end material
texture ImageTexture {
url ["texPal.gif"]
} # end texture
} # end appearance
geometry IndexedFaceSet {
solid TRUE
coord Coordinate {
point [
# the pointset hasn't changed
#base of pyramid
-2 0 2, # point 0, left and close
-2 0 -2, # point 1, left and back
2 0 -2, # point 2, right and back
2 0 2, # point 3, right and close
#top of pyramid
0 2 0, #point 4, top of pyramid
] # end point
} # end coord
coordIndex [
#base
#note that now it is CRUCIAL that I go counter-clockwise
#from the OUTSIDE.
0, 1, 2, 3, 0, -1, #bottom
0, 4, 1, -1, # left side
1, 4, 2, -1, # back side
2, 4, 3, -1, # right side
3, 4, 0, -1, # front side
] # end coordIndex
texCoord TextureCoordinate {
point [
0 1, #0 upper left
.5 1, #1 upper middle
1, 1, #2 upper right
0 .5, #3 mid left
.5 .5, #4 center
1, .5 #5 mid right
0 0, #6 lower left
.5 0, #7 lower middle
1, 0, #8 lower right
]
} # end textcoord
texCoordIndex[
#Photo on bottom
1 4 5 2 1 -1,
#wood on left
4 0 3 -1,
#sky on back
7 3 6 -1,
#grass on right
8 4 7 -1,
#wierd on front
8 1 6 -1
] # end
} # end geometry
} # end shape
NavigationInfo{
type "EXAMINE"
} # end NavigationInfo