-
Notifications
You must be signed in to change notification settings - Fork 0
Tutorial 03 Materials
A basic material is just a color or a texture without any lighting enabled. This renders a green cube:
SimpleMaterial simple = new SimpleMaterial();
simple.setUseColor(true);
mCube.setMaterial(simple);
mCube.setColor(0xff009900);
This renders a textured cube:
SimpleMaterial simple = new SimpleMaterial();
mCube.setMaterial(simple);
Bitmap texture = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.mytexture);
mCube.addTexture(mTextureManager.addTexture(texture));
This material can be used in combination with a light. For more info check out this Wikipedia article.
mLight = new DirectionalLight();
mLight.setPosition(0, 5, -3);
GouraudMaterial gouraud = new GouraudMaterial();
gouraud.setSpecularColor(0xffffff00); // yellow
gouraud.setUseColor(true);
mCube.setMaterial(gouraud);
mCube.addLight(mLight);
Use this material to get nice specular highlights. See this article on Wikipedia.
PhongMaterial phong = new PhongMaterial();
phong.setSpecularColor(0xffffffff); // white
phong.setAmbientcolor(0xffffff00); // yellow
phong.setShininess(0.5f);
phong.setUseColor(true);
mCube.setMaterial(phong);
mCube.addLight(mLight);
As of pull request #438 you can apply image maps directly to the stock materials to simplify alpha, specular, and normal mapping.
Map support by material:
PhongMaterial
- Bump/Norm, Spec, Alpha
GouraudMaterial
- Spec, Alpha
SimpleMaterial
- Alpha
Here is an example of how mapping is utilized:
PhongMaterial myMappedMaterial = new PhongMaterial();
myMappedMaterial.addTexture(mTextureManager.addTexture(BitmapFactory.decodeResource(mContext.getResources(), R.drawable.diffuseTexture), TextureType.DIFFUSE));
myMappedMaterial.addTexture(mTextureManager.addTexture(BitmapFactory.decodeResource(mContext.getResources(), R.drawable.alphaTexture), TextureType.ALPHA)); //Black and white alpha map where white is opaque
myMappedMaterial.addTexture(mTextureManager.addTexture(BitmapFactory.decodeResource(mContext.getResources(), R.drawable.normalTexture), TextureType.BUMP)); //RGB normal map
BaseObject3D mObject = newBaseObject3D();
mObject.setMaterial(myMappedMaterial);
mObject.addLight(new PointLight());
addChild(mObject);
This is a reflective material. It requires 6 textures. Check this Wikipedia article for more information.
Bitmap[] textures = new Bitmap[6];
String ns = "com.mynamespace.myapp:drawable/";
String dns = "com.mynamespace.myapp";
String prefix = "";
textures[0] = BitmapFactory.decodeResource(mContext.getResources(), mContext.getResources().getIdentifier(ns + prefix + "_posx", null, dns));
textures[1] = BitmapFactory.decodeResource(mContext.getResources(), mContext.getResources().getIdentifier(ns + prefix + "_negx", null, dns));
textures[2] = BitmapFactory.decodeResource(mContext.getResources(), mContext.getResources().getIdentifier(ns + prefix + "_posy", null, dns));
textures[3] = BitmapFactory.decodeResource(mContext.getResources(), mContext.getResources().getIdentifier(ns + prefix + "_negy", null, dns));
textures[4] = BitmapFactory.decodeResource(mContext.getResources(), mContext.getResources().getIdentifier(ns + prefix + "_posz", null, dns));
textures[5] = BitmapFactory.decodeResource(mContext.getResources(), mContext.getResources().getIdentifier(ns + prefix + "_negz", null, dns));
TextureInfo tInfo = mTextureManager.addCubemapTextures(textures);
CubeMapMaterial mat = new CubeMapMaterial();
mat.addTexture(tInfo);
mCube.setMaterial(mat);
DirectionalLight light = new DirectionalLight();
light.setPosition(3, 5, -3);
mCube.addLight(mLight);
This one is more economic then the cube map material. It requires one reflection texture only. It can be combined with either a color:
SphereMapMaterial material = new SphereMapMaterial();
material.setSphereMapStrength(2);
material.setUseColor(true);
Bitmap sphereMap = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.my_sphere_map);
myObject.setMaterial(material);
myObject.setColor(0xFF999999);
myObject.addLight(mLight);
myObject.addTexture(mTextureManager.addTexture(sphereMap, TextureType.SPHERE_MAP));
… or it can be combined with a normal diffuse texture:
SphereMapMaterial material = new SphereMapMaterial();
material.setSphereMapStrength(.4f);
Bitmap sphereMap = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.my_sphere_map);
Bitmap texture = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.my_texture);
myObject.setMaterial(material);
myObject.setColor(0xFF999999);
myObject.addLight(mLight);
myObject.addTexture(mTextureManager.addTexture(sphereMap, TextureType.SPHERE_MAP));
myObject.addTexture(mTextureManager.addTexture(texture, TextureType.DIFFUSE));
See https://github.com/MasDennis/Rajawali/wiki/Tutorial-14-Bump-Normal-Mapping
See https://github.com/MasDennis/Rajawali/wiki/Tutorial-11-Particles
See https://github.com/MasDennis/Rajawali/wiki/Tutorial-09-Creating-a-Custom-Material---GLSL-Shader
For a nice cartoon-style effect. Read more about this in this Wikipedia article.
ToonMaterial toonMat = new ToonMaterial();
toonMat.setToonColors(0xffffffff, 0xff000000, 0xff666666, 0xff000000);
myObject.setMaterial(toonMat);
myObject.addLight(mLight);