Pinecoast
 
Basic
Handbag
Cell Phone
Watch
 
Basic 3D Geometry
Messaging
Background
Butterflies
Fog
Color Interpolator
3D Shapes
2D Shapes
Textures
Material
IndexedFaceSet
Layering
Overlay
Shaders 1
Shaders 2
 
     


Using Shaders: 2 Textures

Simple Texture

Sampler variables are used to give shader programs access to textures. The following fragment shader uses a 2D texture called AImage.

uniform sampler2D AImage;
varying float LightIntensity;

void main()
{
    vec3 lightColor = vec3(texture2D(AImage, gl_TexCoord[0].st));
    gl_FragColor = vec4(lightColor * LightIntensity, 1.0);
}


The texture is sent to the shader program by declaring an SFString variable in the X3D with the same name. In addition, the texture must be created by adding an ImageTexture to the Appearance node.

appearance Appearance {
  texture ImageTexture {
    url "image3.png"
  }
  shaders
    ProgramShader {
      language "GLSL"
      programs [
        ShaderProgram {
          field SFVec3f LightPosition 0 0 4
          url "simpletexturev.txt"
        }
        ShaderProgram {
          field SFString AImage "image3.png"
          type "FRAGMENT"
          url "simpletexturef.txt"
        }
      ]
    }
}


The image on the box is just the plain texture, without using the shader program.

MultiTexture


Multiple sampler variables can be added by using a MultiTexture node in the Appearance.

appearance Appearance {
  texture MultiTexture {
    texture [
      ImageTexture {
        url "image3.png"
      }
      ImageTexture {
        url "brick.jpg"
      }
    ]
  }
  shaders
    ProgramShader {
      language "GLSL"
      programs [
        ShaderProgram {
          field SFVec3f LightPosition 0 0 4
          field SFString AImage1 "image3.png"
          field SFString AImage2 "brick.jgp"
          url "multitexturev.txt"
        }
        ShaderProgram {
          type "FRAGMENT"
          url "multitexturef.txt"
        }
      ]
    }
}


As mentionned in the previous tutorial, a ComposedShader and ShaderParts could also be used to produce the same result.
Download tutorial files (same as previous tutorial)



     
 
 

SwirlX3D Viewer


The SwirlX3D Viewer is a free standalone application for viewing VRML, X3D. It also displays sw3d archives created by the SwPublish program.
The professional version also displays files from other formst such as Collada(DAE), 3DS, OBJ, PLY, STL and IGES (IGS).
For more details ...

 

SwTranslator for VRML, X3D and Collada


The SwTranslator can export VRML, X3D, Collada(DAE), 3DS, OBJ, PLY, STL or IGES files to VRML, X3D or Collada. For X3D this offers a migration path for importing high quality 3D models from a variety of sources. For Collada, it offers an opportunity to bring geometry and animation into this increasingly popular format.
For more details ...