Panda3D Manual: MaterialsMaterialsMaterials affect how the surfaces of models appear when lights are enabled in Panda. These have various effects such as how shiny an object appears, the brightness of it's colors etc. Material properties are combined with textures and lighting to get the final look of an object. It must be emphasized that materials only work when lights are applied to an object. Otherwise, materials have no effect. Explanation of LightingWhen light strikes a 3D model, light reflects off the model. If there were no light reflecting off the model, the model would appear pitch black. The light reflecting off the model is what causes it to have a non-black color onscreen. In the real world, light is incredibly complicated --- so complicated, that it is infeasible to do realistic calculations. Instead, Panda3D leaves it in your hands, giving you some basic tools to express how much light you want reflecting from each surface. The tools provided are lights and materials. Lights are used to express how much light is striking the model. Materials are used to express how much of the light striking the model is reflected. Panda3D separates the light striking the model into two general categories: nondirectional, and directional. Directional light is light that comes straight from a particular lamp. Because we know where it's coming from, we also know what direction it is coming from. Nondirectional light is light that maybe came from somewhere, bounced around a bit, and then eventually hit the model. Because we don't know exactly where it came from, we don't know what direction it is coming from. Panda3D handles nondirectional and directional light separately. There are four kinds of lights in Panda3D: ambient, point, diffuse, and directional. The ambient light only creates nondirectional light. The other three create directional light. When light strikes the surface of the model, it is the Material that governs how much of it reflects. The Material consists of four values:
Default Behavior and Explicit BehaviorIf the model does not have an explicit material, does not have a flat color, and does not have vertex colors, the behavior is this:
If the model does not have an explicit material, but it does have a flat color or a vertex color, the behavior is this:
When you set an explicit material on an object, the behavior is as follows:
It is possible to mix-and-match explicit with default behavior. For example, you can specify an explicit specular color, but not specify an explicit ambient, diffuse, or emissive color. If you do that, the behavior would be:
Creating and Using MaterialsTo use explicit materials, import the Materials module when you first begin your script. Then creating Materials is a matter of creating instances of the Material class and setting the relevant properties: from pandac.PandaModules import Material from pandac.PandaModules import VBase4 #VBase4 has to be imported in order to set colors in the methods myMaterial = Material() myMaterial.setShininess(5.0) #Make this material shiny myMaterial.setAmbient(VBase4(0,0,1,1)) #Make this material blue myNode = loader.loadModel("panda") #Load the model to apply the material to myNode.setMaterial(myMaterial) #Apply the material to this nodePath Material PropertiesThe following table details the properties available in a material, its effects as well as the relevant setter method. Most of these properties have additional get and clear methods as well.
Other Material MethodsBesides the setter methods covered above, you can also get material properties using their get methods, such as Properties can also be reset by using the clear methods: Additionally you can check if a material has a property with the has methods: Materials have two other methods that have not been covered yet, Related Classes© Carnegie Mellon University 2010 |