Use a non-default shader

From JReality Wiki
Jump to: navigation, search

Source file: NonDefaultShaderExample


Run as Java webstart


Part of the shading strategy in jReality is to support different shaders. There are several different polygon shaders, for example, supported by the standard jReality backends. Although the default shader is most commonly used, there are also shaders named "twoSide", "implode", and "constant".


The following example shows how to use the "twoSide" polygon shader. It basically allows you to specify a different polygon shader to be used for front-, resp. back-, facing polygons. In this example we've left all the attributes unchanged except the diffuse color. But you could also change the specular properties of the surface. Unfortunately it's not possible to attach different textures for the front and back surfaces (at least for the JOGL backend).


 
...
public class NonDefaultShaderExample {
 
	public static void main(String[] args)	{
		SceneGraphComponent world = SceneGraphUtility.createFullSceneGraphComponent("world");
		world.setGeometry(SphereUtility.sphericalPatch(0, 0.0, 180.0, 90.0, 40, 40, 1.0));
		Appearance ap = world.getAppearance();
		DefaultGeometryShader dgs = (DefaultGeometryShader) 
   			ShaderUtility.createDefaultGeometryShader(ap, true);
		dgs.setShowLines(true);
		dgs.setShowPoints(false);
		TwoSidePolygonShader tsps = (TwoSidePolygonShader) dgs.createPolygonShader("twoSide");
		ImplodePolygonShader dps = (ImplodePolygonShader) tsps.createFront("implode");
		DefaultPolygonShader dps2 = (DefaultPolygonShader) tsps.createBack("default");
		dps.setImplodeFactor(-.5);
		dps.setDiffuseColor(new Color(0,204,204));
		dps2.setDiffuseColor(new Color(204,204,0));
		DefaultLineShader dls = (DefaultLineShader) dgs.getLineShader();
		dls.setTubeDraw(true);
		dls.setTubeRadius(.005);
		dls.setDiffuseColor(Color.red);
 
		JRViewer.display(world);
	}
 
}

NOTE: Currently it isn't possible to set a non-default shader directly using the setAttribute() method of the Appearance class.

Previous: Use the default polygon shader Developer Tutorial: Contents Next: Use a 2d texture