/* * @return an blend state for doing alpha transparency */ public static BlendState getFontBlend() { final BlendState as1 = new BlendState(); as1.setBlendEnabled(true); as1.setSourceFunction(BlendState.SourceFunction.SourceAlpha); as1.setDestinationFunction(BlendState.DestinationFunction.OneMinusSourceAlpha); return as1; }
@Override protected void initWindow() { _canvas.setTitle("Shapes Example"); wrapCount = 5; addMesh(new Arrow("Arrow", 3, 1)); addMesh(new AxisRods("AxisRods", true, 3, 0.5)); addMesh(new Box("Box", new Vector3(), 3, 3, 3)); addMesh(new Capsule("Capsule", 5, 5, 5, 2, 5)); addMesh(new Cone("Cone", 8, 8, 2, 4)); addMesh(new Cylinder("Cylinder", 8, 8, 2, 4)); addMesh(new Disk("Disk", 8, 8, 3)); addMesh(new Dodecahedron("Dodecahedron", 3)); addMesh(new Dome("Dome", 8, 8, 3)); addMesh(new Hexagon("Hexagon", 3)); addMesh(new Icosahedron("Icosahedron", 3)); addMesh(new MultiFaceBox("MultiFaceBox", new Vector3(), 3, 3, 3)); addMesh(new Octahedron("Octahedron", 3)); addMesh(new PQTorus("PQTorus", 5, 4, 1.5, .5, 128, 8)); addMesh(new Pyramid("Pyramid", 2, 4)); addMesh(new Quad("Quad", 3, 3)); addMesh(new RoundedBox("RoundedBox", new Vector3(3, 3, 3))); addMesh(new Sphere("Sphere", 16, 16, 3)); addMesh(new GeoSphere("GeoSphere", true, 3, 3, TextureMode.Original)); addMesh(new StripBox("StripBox", new Vector3(), 3, 3, 3)); addMesh(new Teapot("Teapot")); addMesh(new Torus("Torus", 16, 8, 1.0, 2.5)); addMesh(new Tube("Tube", 2, 3, 4)); addMesh(createLines()); final TextureState ts = new TextureState(); ts.setTexture( TextureManager.load( "images/ardor3d_white_256.jpg", Texture.MinificationFilter.Trilinear, Format.Guess, true)); _root.setRenderState(ts); final BlendState bs = new BlendState(); bs.setBlendEnabled(true); _root.setRenderState(bs); // Set up a reusable pick results _pickResults = new PrimitivePickResults(); _pickResults.setCheckDistance(true); // Set up our pick label _text = BasicText.createDefaultTextLabel("", "pick"); _text.setTranslation(10, 10, 0); _text.getSceneHints().setCullHint(CullHint.Always); _root.attachChild(_text); }
/** Create setup to use as fallback if fancy water is not supported. */ private void createFallbackData() { fallbackTextureState = new TextureState(); fallbackTextureState.setEnabled(true); fallbackTexture = TextureManager.load( fallbackMapTextureString, Texture.MinificationFilter.Trilinear, TextureStoreFormat.GuessCompressedFormat, true); fallbackTextureState.setTexture(fallbackTexture, 0); fallbackTexture.setWrap(Texture.WrapMode.Repeat); fallbackTextureStateMatrix = new Matrix4(); as1 = new BlendState(); as1.setBlendEnabled(true); as1.setTestEnabled(true); as1.setSourceFunction(BlendState.SourceFunction.SourceAlpha); as1.setDestinationFunction(BlendState.DestinationFunction.OneMinusSourceAlpha); as1.setEnabled(true); }
@Override public void draw(Renderer r) { if (hasSelection()) { if (r.isProcessingQueue()) { { setDefaultColor(faceColor); WireframeState ws = new WireframeState(); ws.setEnabled(false); setRenderState(ws); CullState cs = new CullState(); cs.setCullFace(com.ardor3d.renderer.state.CullState.Face.None); setRenderState(cs); BlendState bs = new BlendState(); bs.setBlendEnabled(true); bs.setSourceFunctionAlpha(SourceFunction.SourceAlpha); bs.setDestinationFunction(DestinationFunction.One); bs.setTestEnabled(false); setRenderState(bs); updateGeometricState(0); super.draw(r); } { setDefaultColor(wireColor); WireframeState ws = new WireframeState(); ws.setEnabled(true); ws.setFace(Face.Front); setRenderState(ws); CullState cs = new CullState(); cs.setCullFace(com.ardor3d.renderer.state.CullState.Face.Back); setRenderState(cs); BlendState bs = new BlendState(); bs.setBlendEnabled(false); setRenderState(bs); updateGeometricState(0); super.draw(r); } if (bbvn != null) bbvn.draw(r); } else { super.draw(r); } } }
@Override protected void initExample() { _canvas.setTitle("Shapes Example"); wrapCount = 5; addMesh(new Arrow("Arrow", 3, 1)); addMesh(new AxisRods("AxisRods", true, 3, 0.5)); addMesh(new Box("Box", new Vector3(), 3, 3, 3)); addMesh(new Capsule("Capsule", 5, 5, 5, 2, 5)); addMesh(new Cone("Cone", 8, 8, 2, 4)); addMesh(new Cylinder("Cylinder", 8, 8, 2, 4)); addMesh(new Disk("Disk", 8, 8, 3)); addMesh(new Dodecahedron("Dodecahedron", 3)); addMesh(new Dome("Dome", 8, 8, 3)); addMesh(new Hexagon("Hexagon", 3)); addMesh(new Icosahedron("Icosahedron", 3)); addMesh(new MultiFaceBox("MultiFaceBox", new Vector3(), 3, 3, 3)); addMesh(new Octahedron("Octahedron", 3)); addMesh(new PQTorus("PQTorus", 5, 4, 1.5, .5, 128, 8)); addMesh(new Pyramid("Pyramid", 2, 4)); addMesh(new Quad("Quad", 3, 3)); addMesh(new RoundedBox("RoundedBox", new Vector3(3, 3, 3))); addMesh(new Sphere("Sphere", 16, 16, 3)); addMesh(new GeoSphere("GeoSphere", true, 3, 3, TextureMode.Original)); addMesh(new StripBox("StripBox", new Vector3(), 3, 3, 3)); addMesh(new Teapot("Teapot")); addMesh(new Torus("Torus", 16, 8, 1.0, 2.5)); addMesh(new Tube("Tube", 2, 3, 4)); addMesh(createLines()); final TextureState ts = new TextureState(); ts.setTexture( TextureManager.load( "images/ardor3d_white_256.jpg", Texture.MinificationFilter.Trilinear, TextureStoreFormat.GuessCompressedFormat, true)); _root.setRenderState(ts); final BlendState bs = new BlendState(); bs.setBlendEnabled(true); _root.setRenderState(bs); // Set up a reusable pick results _pickResults = new BoundingPickResults(); _pickResults.setCheckDistance(true); // Set up our pick label _text = BasicText.createDefaultTextLabel("", "pick"); _text.setTranslation(10, 10, 0); _text.getSceneHints().setCullHint(CullHint.Always); _root.attachChild(_text); // Set up picked pulse _pickedControl = new SpatialController<Spatial>() { ColorRGBA curr = new ColorRGBA(); float val = 0; boolean add = true; public void update(final double time, final Spatial caller) { val += time * (add ? 1 : -1); if (val < 0) { val = -val; add = true; } else if (val > 1) { val = 1 - (val - (int) val); add = false; } curr.set(val, val, val, 1.0f); final MaterialState ms = (MaterialState) caller.getLocalRenderState(StateType.Material); ms.setAmbient(curr); } }; }
/** * Find and apply the given material to the given Mesh. * * @param materialName our material name * @param mesh the mesh to apply material to. */ public void applyMaterial(final String materialName, final Mesh mesh) { if (materialName == null) { logger.warning("materialName is null"); return; } Element mat = _dataCache.getBoundMaterial(materialName); if (mat == null) { logger.warning("material not bound: " + materialName + ", trying search with id."); mat = _colladaDOMUtil.findTargetWithId(materialName); } if (mat == null || !"material".equals(mat.getName())) { logger.warning("material not found: " + materialName); return; } final String originalMaterial = mat.getAttributeValue("id"); MaterialInfo mInfo = null; if (!_dataCache.getMaterialInfoMap().containsKey(originalMaterial)) { mInfo = new MaterialInfo(); mInfo.setMaterialName(originalMaterial); _dataCache.getMaterialInfoMap().put(originalMaterial, mInfo); } _dataCache.getMeshMaterialMap().put(mesh, originalMaterial); final Element child = mat.getChild("instance_effect"); final Element effectNode = _colladaDOMUtil.findTargetWithId(child.getAttributeValue("url")); if (effectNode == null) { logger.warning( "material effect not found: " + mat.getChild("instance_material").getAttributeValue("url")); return; } if ("effect".equals(effectNode.getName())) { /* * temp cache for textures, we do not want to add textures twice (for example, transparant map might point * to diffuse texture) */ final HashMap<String, Texture> loadedTextures = new HashMap<String, Texture>(); final Element effect = effectNode; // XXX: For now, just grab the common technique: if (effect.getChild("profile_COMMON") != null) { if (mInfo != null) { mInfo.setProfile("COMMON"); } final Element technique = effect.getChild("profile_COMMON").getChild("technique"); String type = "blinn"; if (technique.getChild(type) == null) { type = "phong"; if (technique.getChild(type) == null) { type = "lambert"; if (technique.getChild(type) == null) { type = "constant"; if (technique.getChild(type) == null) { ColladaMaterialUtils.logger.warning( "COMMON material has unusuable techinque. " + child.getAttributeValue("url")); return; } } } } final Element blinnPhongLambert = technique.getChild(type); if (mInfo != null) { mInfo.setTechnique(type); } final MaterialState mState = new MaterialState(); // TODO: implement proper transparency handling Texture diffuseTexture = null; ColorRGBA transparent = new ColorRGBA(1.0f, 1.0f, 1.0f, 1.0f); float transparency = 1.0f; boolean useTransparency = false; String opaqueMode = "A_ONE"; /* * place holder for current property, we import material properties in fixed order (for texture order) */ Element property = null; /* Diffuse property */ property = blinnPhongLambert.getChild("diffuse"); if (property != null) { final Element propertyValue = (Element) property.getChildren().get(0); if ("color".equals(propertyValue.getName())) { final ColorRGBA color = _colladaDOMUtil.getColor(propertyValue.getText()); mState.setDiffuse(MaterialFace.FrontAndBack, color); } else if ("texture".equals(propertyValue.getName()) && _loadTextures) { diffuseTexture = populateTextureState(mesh, propertyValue, effect, loadedTextures, mInfo, "diffuse"); } } /* Ambient property */ property = blinnPhongLambert.getChild("ambient"); if (property != null) { final Element propertyValue = (Element) property.getChildren().get(0); if ("color".equals(propertyValue.getName())) { final ColorRGBA color = _colladaDOMUtil.getColor(propertyValue.getText()); mState.setAmbient(MaterialFace.FrontAndBack, color); } else if ("texture".equals(propertyValue.getName()) && _loadTextures) { populateTextureState(mesh, propertyValue, effect, loadedTextures, mInfo, "ambient"); } } /* Transparent property */ property = blinnPhongLambert.getChild("transparent"); if (property != null) { final Element propertyValue = (Element) property.getChildren().get(0); if ("color".equals(propertyValue.getName())) { transparent = _colladaDOMUtil.getColor(propertyValue.getText()); // TODO: use this useTransparency = true; } else if ("texture".equals(propertyValue.getName()) && _loadTextures) { populateTextureState(mesh, propertyValue, effect, loadedTextures, mInfo, "transparent"); } opaqueMode = property.getAttributeValue("opaque", "A_ONE"); } /* Transparency property */ property = blinnPhongLambert.getChild("transparency"); if (property != null) { final Element propertyValue = (Element) property.getChildren().get(0); if ("float".equals(propertyValue.getName())) { transparency = Float.parseFloat(propertyValue.getText().replace(",", ".")); // TODO: use this if (_flipTransparency) { transparency = 1f - transparency; } useTransparency = true; } else if ("texture".equals(propertyValue.getName()) && _loadTextures) { populateTextureState( mesh, propertyValue, effect, loadedTextures, mInfo, "transparency"); } } /* Emission property */ property = blinnPhongLambert.getChild("emission"); if (property != null) { final Element propertyValue = (Element) property.getChildren().get(0); if ("color".equals(propertyValue.getName())) { mState.setEmissive( MaterialFace.FrontAndBack, _colladaDOMUtil.getColor(propertyValue.getText())); } else if ("texture".equals(propertyValue.getName()) && _loadTextures) { populateTextureState(mesh, propertyValue, effect, loadedTextures, mInfo, "emissive"); } } /* Specular property */ property = blinnPhongLambert.getChild("specular"); if (property != null) { final Element propertyValue = (Element) property.getChildren().get(0); if ("color".equals(propertyValue.getName())) { mState.setSpecular( MaterialFace.FrontAndBack, _colladaDOMUtil.getColor(propertyValue.getText())); } else if ("texture".equals(propertyValue.getName()) && _loadTextures) { populateTextureState(mesh, propertyValue, effect, loadedTextures, mInfo, "specular"); } } /* Shininess property */ property = blinnPhongLambert.getChild("shininess"); if (property != null) { final Element propertyValue = (Element) property.getChildren().get(0); if ("float".equals(propertyValue.getName())) { float shininess = Float.parseFloat(propertyValue.getText().replace(",", ".")); if (shininess >= 0.0f && shininess <= 1.0f) { final float oldShininess = shininess; shininess *= 128; logger.finest( "Shininess - " + oldShininess + " - was in the [0,1] range. Scaling to [0, 128] - " + shininess); } else if (shininess < 0 || shininess > 128) { final float oldShininess = shininess; shininess = MathUtils.clamp(shininess, 0, 128); logger.warning( "Shininess must be between 0 and 128. Shininess " + oldShininess + " was clamped to " + shininess); } mState.setShininess(MaterialFace.FrontAndBack, shininess); } else if ("texture".equals(propertyValue.getName()) && _loadTextures) { populateTextureState(mesh, propertyValue, effect, loadedTextures, mInfo, "shininess"); } } /* Reflectivity property */ float reflectivity = 1.0f; property = blinnPhongLambert.getChild("reflectivity"); if (property != null) { final Element propertyValue = (Element) property.getChildren().get(0); if ("float".equals(propertyValue.getName())) { reflectivity = Float.parseFloat(propertyValue.getText().replace(",", ".")); } } /* Reflective property. Texture only */ property = blinnPhongLambert.getChild("reflective"); if (property != null) { final Element propertyValue = (Element) property.getChildren().get(0); if ("texture".equals(propertyValue.getName()) && _loadTextures) { final Texture reflectiveTexture = populateTextureState( mesh, propertyValue, effect, loadedTextures, mInfo, "reflective"); reflectiveTexture.setEnvironmentalMapMode(Texture.EnvironmentalMapMode.SphereMap); reflectiveTexture.setApply(ApplyMode.Combine); reflectiveTexture.setCombineFuncRGB(CombinerFunctionRGB.Interpolate); // color 1 reflectiveTexture.setCombineSrc0RGB(CombinerSource.CurrentTexture); reflectiveTexture.setCombineOp0RGB(CombinerOperandRGB.SourceColor); // color 2 reflectiveTexture.setCombineSrc1RGB(CombinerSource.Previous); reflectiveTexture.setCombineOp1RGB(CombinerOperandRGB.SourceColor); // interpolate param will come from alpha of constant color reflectiveTexture.setCombineSrc2RGB(CombinerSource.Constant); reflectiveTexture.setCombineOp2RGB(CombinerOperandRGB.SourceAlpha); reflectiveTexture.setConstantColor(new ColorRGBA(1, 1, 1, reflectivity)); } } /* * An extra tag defines some materials not part of the collada standard. Since we're not able to parse * we simply extract the textures from the element (such that shaders etc can at least pick up on them) */ property = technique.getChild("extra"); if (property != null) { getTexturesFromElement(mesh, property, effect, loadedTextures, mInfo); } // XXX: There are some issues with clarity on how to use alpha blending in OpenGL FFP. // The best interpretation I have seen is that if transparent has a texture == diffuse, // Turn on alpha blending and use diffuse alpha. // check to make sure we actually need this. // testing separately for a transparency of 0.0 is to hack around erroneous exports, since // usually // there is no use in exporting something with 100% transparency. if ("A_ONE".equals(opaqueMode) && ColorRGBA.WHITE.equals(transparent) && transparency == 1.0 || transparency == 0.0) { useTransparency = false; } if (useTransparency) { if (diffuseTexture != null) { final BlendState blend = new BlendState(); blend.setBlendEnabled(true); blend.setTestEnabled(true); blend.setSourceFunction(BlendState.SourceFunction.SourceAlpha); blend.setDestinationFunction(BlendState.DestinationFunction.OneMinusSourceAlpha); mesh.setRenderState(blend); } else { final BlendState blend = new BlendState(); blend.setBlendEnabled(true); blend.setTestEnabled(true); transparent.setAlpha(transparent.getAlpha() * transparency); blend.setConstantColor(transparent); blend.setSourceFunction(BlendState.SourceFunction.ConstantAlpha); blend.setDestinationFunction(BlendState.DestinationFunction.OneMinusConstantAlpha); mesh.setRenderState(blend); } mesh.getSceneHints().setRenderBucketType(RenderBucketType.Transparent); } if (mInfo != null) { if (useTransparency) { mInfo.setUseTransparency(useTransparency); if (diffuseTexture == null) { mInfo.setTransparency(transparent.getAlpha() * transparency); } } mInfo.setMaterialState(mState); } mesh.setRenderState(mState); } } else { ColladaMaterialUtils.logger.warning( "material effect not found: " + mat.getChild("instance_material").getAttributeValue("url")); } }