📄 material.java
字号:
/* * $RCSfile: Material.java,v $ * * Copyright (c) 2007 Sun Microsystems, Inc. All rights reserved. * * Use is subject to license terms. * * $Revision: 1.5 $ * $Date: 2007/02/09 17:18:10 $ * $State: Exp $ */package javax.media.j3d;import javax.vecmath.Color3f;/** * The Material object defines the appearance of an object under * illumination. * If the Material object in an Appearance object is <code>null</code>, * lighting is disabled for all nodes that use that Appearance object. * <P> * The properties that can be set for a Material object are: * <P><UL> * <LI>Ambient color - the ambient RGB color reflected off the surface * of the material. The range of values is 0.0 to 1.0. The default ambient * color is (0.2, 0.2, 0.2).<p></LI> * <LI>Diffuse color - the RGB color of the material when illuminated. * The range of values is 0.0 to 1.0. The default diffuse color is * (1.0, 1.0, 1.0).<p></LI> * <LI>Specular color - the RGB specular color of the material (highlights). * The range of values is 0.0 to 1.0. The default specular color * is (1.0, 1.0, 1.0).<p></LI> * <LI>Emissive color - the RGB color of the light the material emits, if * any. The range of values is 0.0 to 1.0. The default emissive * color is (0.0, 0.0, 0.0).<p></LI> * <LI>Shininess - the material's shininess, in the range [1.0, 128.0] * with 1.0 being not shiny and 128.0 being very shiny. Values outside * this range are clamped. The default value for the material's * shininess is 64.<p></LI> * <LI>Color target - the material color target for per-vertex colors, * one of: AMBIENT, EMISSIVE, DIFFUSE, SPECULAR, or AMBIENT_AND_DIFFUSE. * The default target is DIFFUSE.<p></LI> * </UL> * * The Material object also enables or disables lighting. */public class Material extends NodeComponent { /** * For material object, specifies that Material allows reading * individual component field information. */ public static final int ALLOW_COMPONENT_READ = CapabilityBits.MATERIAL_ALLOW_COMPONENT_READ; /** * For material object, specifies that Material allows reading * individual component field information. */ public static final int ALLOW_COMPONENT_WRITE = CapabilityBits.MATERIAL_ALLOW_COMPONENT_WRITE; /** * Specifies that per-vertex colors replace the ambient material color. * @see #setColorTarget * * @since Java 3D 1.3 */ public static final int AMBIENT = 0; /** * Specifies that per-vertex colors replace the emissive material color. * @see #setColorTarget * * @since Java 3D 1.3 */ public static final int EMISSIVE = 1; /** * Specifies that per-vertex colors replace the diffuse material color. * This is the default target. * @see #setColorTarget * * @since Java 3D 1.3 */ public static final int DIFFUSE = 2; /** * Specifies that per-vertex colors replace the specular material color. * @see #setColorTarget * * @since Java 3D 1.3 */ public static final int SPECULAR = 3; /** * Specifies that per-vertex colors replace both the ambient and the * diffuse material color. * @see #setColorTarget * * @since Java 3D 1.3 */ public static final int AMBIENT_AND_DIFFUSE = 4; // Array for setting default read capabilities private static final int[] readCapabilities = { ALLOW_COMPONENT_READ }; /** * Constructs and initializes a Material object using default parameters. * The default values are as follows: * <ul> * lighting enable : true<br> * ambient color : (0.2, 0.2, 0.2)<br> * emmisive color : (0.0, 0.0, 0.0)<br> * diffuse color : (1.0, 1.0, 1.0)<br> * specular color : (1.0, 1.0, 1.0)<br> * shininess : 64<br> * color target : DIFFUSE * </ul> */ public Material() { // Just use the defaults // set default read capabilities setDefaultReadCapabilities(readCapabilities); } /** * Constructs and initializes a new material object using the specified * parameters. Lighting is enabled by default. * @param ambientColor the material's ambient color * @param emissiveColor the material's emissive color * @param diffuseColor the material's diffuse color when illuminated by a * light * @param specularColor the material's specular color when illuminated * to generate a highlight * @param shininess the material's shininess in the * range [1.0, 128.0] with 1.0 being not shiny and 128.0 being very shiny. * Values outside this range are clamped. */ public Material(Color3f ambientColor, Color3f emissiveColor, Color3f diffuseColor, Color3f specularColor, float shininess) { // set default read capabilities setDefaultReadCapabilities(readCapabilities); ((MaterialRetained)this.retained).createMaterial(ambientColor, emissiveColor, diffuseColor, specularColor, shininess); } /** * Creates a retained mode MaterialRetained object that this * Material component object will point to. */ void createRetained() { this.retained = new MaterialRetained(); this.retained.setSource(this); } /** * Sets this material's ambient color. * This specifies how much ambient light is reflected by * the surface. * The ambient color in this Material object may be overridden by * per-vertex colors in some cases. If vertex colors are present * in the geometry, and lighting is enabled, and the colorTarget * is either AMBIENT or AMBIENT_AND_DIFFUSE, and vertex colors are * not being ignored, then the vertex colors are used in place of * this Material's ambient color in the lighting equation. * * @param color the material's ambient color * @exception CapabilityNotSetException if appropriate capability is * not set and this object is part of live or compiled scene graph * * @see RenderingAttributes#setIgnoreVertexColors * @see #setColorTarget */ public void setAmbientColor(Color3f color) { if (isLiveOrCompiled()) if (!this.getCapability(ALLOW_COMPONENT_WRITE)) throw new CapabilityNotSetException(J3dI18N.getString("Material0")); if (isLive()) { ((MaterialRetained)this.retained).setAmbientColor(color); } else { ((MaterialRetained)this.retained).initAmbientColor(color); } } /** * Sets this material's ambient color. * This specifies how much ambient light is reflected by * the surface. * The ambient color in this Material object may be overridden by * per-vertex colors in some cases. If vertex colors are present * in the geometry, and lighting is enabled, and the colorTarget * is either AMBIENT or AMBIENT_AND_DIFFUSE, and vertex colors are * not being ignored, then the vertex colors are used in place of * this Material's ambient color in the lighting equation. * * @param r the new ambient color's red component * @param g the new ambient color's green component * @param b the new ambient color's blue component * @exception CapabilityNotSetException if appropriate capability is * not set and this object is part of live or compiled scene graph * * @see RenderingAttributes#setIgnoreVertexColors * @see #setColorTarget */ public void setAmbientColor(float r, float g, float b) { if (isLiveOrCompiled()) if (!this.getCapability(ALLOW_COMPONENT_WRITE)) throw new CapabilityNotSetException(J3dI18N.getString("Material0")); if (isLive()) { ((MaterialRetained)this.retained).setAmbientColor(r,g,b); } else { ((MaterialRetained)this.retained).initAmbientColor(r,g,b); } } /** * Retrieves this material's ambient color. * @param color that will contain the material's ambient color * @exception CapabilityNotSetException if appropriate capability is * not set and this object is part of live or compiled scene graph */ public void getAmbientColor(Color3f color) { if (isLiveOrCompiled()) if (!this.getCapability(ALLOW_COMPONENT_READ)) throw new CapabilityNotSetException(J3dI18N.getString("Material2")); ((MaterialRetained)this.retained).getAmbientColor(color); } /** * Sets this material's emissive color. * This is the color of light, if any, that the material emits. * The emissive color in this Material object may be overridden by * per-vertex colors in some cases. If vertex colors are present * in the geometry, and lighting is enabled, and the colorTarget * is EMISSIVE, and vertex colors are * not being ignored, then the vertex colors are used in place of * this Material's emissive color in the lighting equation. * * @param color the new emissive color * @exception CapabilityNotSetException if appropriate capability is * not set and this object is part of live or compiled scene graph * * @see RenderingAttributes#setIgnoreVertexColors * @see #setColorTarget */ public void setEmissiveColor(Color3f color) { if (isLiveOrCompiled()) if (!this.getCapability(ALLOW_COMPONENT_WRITE)) throw new CapabilityNotSetException(J3dI18N.getString("Material0")); if (isLive()) ((MaterialRetained)this.retained).setEmissiveColor(color); else ((MaterialRetained)this.retained).initEmissiveColor(color); } /** * Sets this material's emissive color. * This is the color of light, if any, that the material emits. * The emissive color in this Material object may be overridden by * per-vertex colors in some cases. If vertex colors are present * in the geometry, and lighting is enabled, and the colorTarget * is EMISSIVE, and vertex colors are * not being ignored, then the vertex colors are used in place of * this Material's emissive color in the lighting equation. * * @param r the new emissive color's red component * @param g the new emissive color's green component * @param b the new emissive color's blue component * @exception CapabilityNotSetException if appropriate capability is * not set and this object is part of live or compiled scene graph * * @see RenderingAttributes#setIgnoreVertexColors * @see #setColorTarget */ public void setEmissiveColor(float r, float g, float b) { if (isLiveOrCompiled()) if (!this.getCapability(ALLOW_COMPONENT_WRITE)) throw new CapabilityNotSetException(J3dI18N.getString("Material0")); if (isLive()) ((MaterialRetained)this.retained).setEmissiveColor(r,g,b); else ((MaterialRetained)this.retained).initEmissiveColor(r,g,b); } /** * Retrieves this material's emissive color and stores it in the * argument provided. * @param color the vector that will receive this material's emissive color * @exception CapabilityNotSetException if appropriate capability is * not set and this object is part of live or compiled scene graph */ public void getEmissiveColor(Color3f color) { if (isLiveOrCompiled()) if (!this.getCapability(ALLOW_COMPONENT_READ)) throw new CapabilityNotSetException(J3dI18N.getString("Material2")); ((MaterialRetained)this.retained).getEmissiveColor(color); } /** * Sets this material's diffuse color. * This is the color of the material when illuminated by a light source. * The diffuse color in this Material object may be overridden by * per-vertex colors in some cases. If vertex colors are present * in the geometry, and lighting is enabled, and the colorTarget * is either DIFFUSE or AMBIENT_AND_DIFFUSE, and vertex colors are * not being ignored, then the vertex colors are used in place of * this Material's diffuse color in the lighting equation. * * @param color the new diffuse color * @exception CapabilityNotSetException if appropriate capability is * not set and this object is part of live or compiled scene graph * * @see RenderingAttributes#setIgnoreVertexColors * @see #setColorTarget */ public void setDiffuseColor(Color3f color) { if (isLiveOrCompiled()) if (!this.getCapability(ALLOW_COMPONENT_WRITE)) throw new CapabilityNotSetException(J3dI18N.getString("Material0")); if (isLive()) ((MaterialRetained)this.retained).setDiffuseColor(color); else ((MaterialRetained)this.retained).initDiffuseColor(color); } /** * Sets this material's diffuse color. * This is the color of the material when illuminated by a light source. * The diffuse color in this Material object may be overridden by * per-vertex colors in some cases. If vertex colors are present * in the geometry, and lighting is enabled, and the colorTarget
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -