use of com.jme3.math.ColorRGBA in project jmonkeyengine by jMonkeyEngine.
the class TestWalkingChar method createLight.
private void createLight() {
Vector3f direction = new Vector3f(-0.1f, -0.7f, -1).normalizeLocal();
DirectionalLight dl = new DirectionalLight();
dl.setDirection(direction);
dl.setColor(new ColorRGBA(1f, 1f, 1f, 1.0f));
rootNode.addLight(dl);
}
use of com.jme3.math.ColorRGBA in project jmonkeyengine by jMonkeyEngine.
the class FbxMaterial method createMaterial.
private Material createMaterial() {
Material m = new Material(scene.assetManager, "Common/MatDefs/Light/Lighting.j3md");
m.setName(name);
ambientColor.multLocal(ambientFactor);
diffuseColor.multLocal(diffuseFactor);
specularColor.multLocal(specularFactor);
m.setColor("Ambient", new ColorRGBA(ambientColor.x, ambientColor.y, ambientColor.z, 1));
m.setColor("Diffuse", new ColorRGBA(diffuseColor.x, diffuseColor.y, diffuseColor.z, 1));
m.setColor("Specular", new ColorRGBA(specularColor.x, specularColor.y, specularColor.z, 1));
m.setFloat("Shininess", shininessExponent);
m.setBoolean("UseMaterialColors", true);
// TODO replace with right way in JME to set "Aplha Test"
m.setFloat("AlphaDiscardThreshold", 0.5f);
m.getAdditionalRenderState().setBlendMode(BlendMode.Alpha);
return m;
}
use of com.jme3.math.ColorRGBA in project jmonkeyengine by jMonkeyEngine.
the class FbxMaterial method toJmeObject.
@Override
protected Material toJmeObject() {
ColorRGBA ambient = null;
ColorRGBA diffuse = null;
ColorRGBA specular = null;
ColorRGBA transp = null;
ColorRGBA emissive = null;
float shininess = 1f;
boolean separateTexCoord = false;
Texture diffuseMap = null;
Texture specularMap = null;
Texture normalMap = null;
Texture transpMap = null;
Texture emitMap = null;
Texture aoMap = null;
FbxTexture fbxDiffuseMap = null;
Object diffuseColor = properties.getProperty("DiffuseColor");
if (diffuseColor != null) {
if (diffuseColor instanceof ColorRGBA) {
diffuse = ((ColorRGBA) diffuseColor).clone();
} else if (diffuseColor instanceof FbxTexture) {
FbxTexture tex = (FbxTexture) diffuseColor;
fbxDiffuseMap = tex;
diffuseMap = tex.getJmeObject();
diffuseMap.getImage().setColorSpace(ColorSpace.sRGB);
}
}
Object diffuseFactor = properties.getProperty("DiffuseFactor");
if (diffuseFactor != null && diffuseFactor instanceof Float) {
float factor = (Float) diffuseFactor;
if (diffuse != null) {
multRGB(diffuse, factor);
} else {
diffuse = new ColorRGBA(factor, factor, factor, 1f);
}
}
Object specularColor = properties.getProperty("SpecularColor");
if (specularColor != null) {
if (specularColor instanceof ColorRGBA) {
specular = ((ColorRGBA) specularColor).clone();
} else if (specularColor instanceof FbxTexture) {
FbxTexture tex = (FbxTexture) specularColor;
specularMap = tex.getJmeObject();
specularMap.getImage().setColorSpace(ColorSpace.sRGB);
}
}
Object specularFactor = properties.getProperty("SpecularFactor");
if (specularFactor != null && specularFactor instanceof Float) {
float factor = (Float) specularFactor;
if (specular != null) {
multRGB(specular, factor);
} else {
specular = new ColorRGBA(factor, factor, factor, 1f);
}
}
Object transparentColor = properties.getProperty("TransparentColor");
if (transparentColor != null) {
if (transparentColor instanceof ColorRGBA) {
transp = ((ColorRGBA) transparentColor).clone();
} else if (transparentColor instanceof FbxTexture) {
FbxTexture tex = (FbxTexture) transparentColor;
transpMap = tex.getJmeObject();
transpMap.getImage().setColorSpace(ColorSpace.sRGB);
}
}
Object transparencyFactor = properties.getProperty("TransparencyFactor");
if (transparencyFactor != null && transparencyFactor instanceof Float) {
float factor = (Float) transparencyFactor;
if (transp != null) {
transp.a *= factor;
} else {
transp = new ColorRGBA(1f, 1f, 1f, factor);
}
}
Object emissiveColor = properties.getProperty("EmissiveColor");
if (emissiveColor != null) {
if (emissiveColor instanceof ColorRGBA) {
emissive = ((ColorRGBA) emissiveColor).clone();
} else if (emissiveColor instanceof FbxTexture) {
FbxTexture tex = (FbxTexture) emissiveColor;
emitMap = tex.getJmeObject();
emitMap.getImage().setColorSpace(ColorSpace.sRGB);
}
}
Object emissiveFactor = properties.getProperty("EmissiveFactor");
if (emissiveFactor != null && emissiveFactor instanceof Float) {
float factor = (Float) emissiveFactor;
if (emissive != null) {
multRGB(emissive, factor);
} else {
emissive = new ColorRGBA(factor, factor, factor, 1f);
}
}
Object ambientColor = properties.getProperty("AmbientColor");
if (ambientColor != null && ambientColor instanceof ColorRGBA) {
ambient = ((ColorRGBA) ambientColor).clone();
}
Object ambientFactor = properties.getProperty("AmbientFactor");
if (ambientFactor != null && ambientFactor instanceof Float) {
float factor = (Float) ambientFactor;
if (ambient != null) {
multRGB(ambient, factor);
} else {
ambient = new ColorRGBA(factor, factor, factor, 1f);
}
}
Object shininessFactor = properties.getProperty("Shininess");
if (shininessFactor != null) {
if (shininessFactor instanceof Float) {
shininess = (Float) shininessFactor;
} else if (shininessFactor instanceof FbxTexture) {
// TODO: support shininess textures
}
}
Object bumpNormal = properties.getProperty("NormalMap");
if (bumpNormal != null) {
if (bumpNormal instanceof FbxTexture) {
// TODO: check all meshes that use this material have tangents
// otherwise shading errors occur
FbxTexture tex = (FbxTexture) bumpNormal;
normalMap = tex.getJmeObject();
normalMap.getImage().setColorSpace(ColorSpace.Linear);
}
}
Object aoColor = properties.getProperty("DiffuseColor2");
if (aoColor != null) {
if (aoColor instanceof FbxTexture) {
FbxTexture tex = (FbxTexture) aoColor;
if (tex.getUvSet() != null && fbxDiffuseMap != null) {
if (!tex.getUvSet().equals(fbxDiffuseMap.getUvSet())) {
separateTexCoord = true;
}
}
aoMap = tex.getJmeObject();
aoMap.getImage().setColorSpace(ColorSpace.sRGB);
}
}
assert ambient == null || ambient.a == 1f;
assert diffuse == null || diffuse.a == 1f;
assert specular == null || specular.a == 1f;
assert emissive == null || emissive.a == 1f;
assert transp == null || (transp.r == 1f && transp.g == 1f && transp.b == 1f);
// to handle it. Gotta disable specularity then.
if (shininess < 1f) {
shininess = 1f;
specular = ColorRGBA.Black;
}
// Try to guess if we need to enable alpha blending.
// FBX does not specify this explicitly.
boolean useAlphaBlend = false;
if (diffuseMap != null && diffuseMap == transpMap) {
// jME3 already uses alpha from diffuseMap
// (if alpha blend is enabled)
useAlphaBlend = true;
transpMap = null;
} else if (diffuseMap != null && transpMap != null && diffuseMap != transpMap) {
// TODO: potential bug here. Alpha from diffuse may
// leak unintentionally.
useAlphaBlend = true;
} else if (transpMap != null) {
// We have alpha map but no diffuse map, OK.
useAlphaBlend = true;
}
if (transp != null && transp.a != 1f) {
// Consolidate transp into diffuse
// (jME3 doesn't use a separate alpha color)
// TODO: potential bug here. Alpha from diffuse may
// leak unintentionally.
useAlphaBlend = true;
if (diffuse != null) {
diffuse.a = transp.a;
} else {
diffuse = transp;
}
}
Material mat = new Material(assetManager, "Common/MatDefs/Light/Lighting.j3md");
mat.setName(name);
// TODO: load this from FBX material.
mat.setReceivesShadows(true);
if (useAlphaBlend) {
// No idea if this is a transparent or translucent model, gotta guess..
mat.setTransparent(true);
mat.setFloat("AlphaDiscardThreshold", 0.01f);
mat.getAdditionalRenderState().setBlendMode(BlendMode.Alpha);
}
mat.getAdditionalRenderState().setFaceCullMode(FaceCullMode.Off);
// Set colors.
if (ambient != null || diffuse != null || specular != null) {
// If either of those is set, we have to set them all.
// NOTE: default specular is black, unless it is set explicitly.
mat.setBoolean("UseMaterialColors", true);
mat.setColor("Ambient", /*ambient != null ? ambient :*/
ColorRGBA.White);
mat.setColor("Diffuse", diffuse != null ? diffuse : ColorRGBA.White);
mat.setColor("Specular", specular != null ? specular : ColorRGBA.Black);
}
if (emissive != null) {
mat.setColor("GlowColor", emissive);
}
// Set shininess.
if (shininess > 1f) {
// Convert shininess from
// Phong (FBX shading model) to Blinn (jME3 shading model).
float blinnShininess = (shininess * 5.1f) + 1f;
mat.setFloat("Shininess", blinnShininess);
}
// Set textures.
if (diffuseMap != null) {
mat.setTexture("DiffuseMap", diffuseMap);
}
if (specularMap != null) {
mat.setTexture("SpecularMap", specularMap);
}
if (normalMap != null) {
mat.setTexture("NormalMap", normalMap);
}
if (transpMap != null) {
// mat.setTexture("AlphaMap", transpMap);
}
if (emitMap != null) {
mat.setTexture("GlowMap", emitMap);
}
if (aoMap != null) {
mat.setTexture("LightMap", aoMap);
if (separateTexCoord) {
mat.setBoolean("SeparateTexCoord", true);
}
}
return mat;
}
use of com.jme3.math.ColorRGBA in project jmonkeyengine by jMonkeyEngine.
the class FbxMesh method toIRMesh.
/**
* Convert FBXMesh to IRMesh.
*/
public IrMesh toIRMesh() {
IrMesh newMesh = new IrMesh();
newMesh.polygons = new IrPolygon[polygons.length];
int polygonVertexIndex = 0;
int positionIndex = 0;
FbxLayer layer0 = layers[0];
FbxLayer layer1 = layers.length > 1 ? layers[1] : null;
for (int i = 0; i < polygons.length; i++) {
FbxPolygon polygon = polygons[i];
IrPolygon irPolygon = new IrPolygon();
irPolygon.vertices = new IrVertex[polygon.indices.length];
for (int j = 0; j < polygon.indices.length; j++) {
positionIndex = polygon.indices[j];
IrVertex irVertex = new IrVertex();
irVertex.pos = positions[positionIndex];
if (layer0 != null) {
irVertex.norm = (Vector3f) layer0.getVertexData(FbxLayerElement.Type.Normal, i, polygonVertexIndex, positionIndex, 0);
irVertex.tang = (Vector3f) layer0.getVertexData(FbxLayerElement.Type.Tangent, i, polygonVertexIndex, positionIndex, 0);
irVertex.bitang = (Vector3f) layer0.getVertexData(FbxLayerElement.Type.Binormal, i, polygonVertexIndex, positionIndex, 0);
irVertex.uv0 = (Vector2f) layer0.getVertexData(FbxLayerElement.Type.UV, i, polygonVertexIndex, positionIndex, 0);
irVertex.color = (ColorRGBA) layer0.getVertexData(FbxLayerElement.Type.Color, i, polygonVertexIndex, positionIndex, 0);
irVertex.material = (Integer) layer0.getVertexData(FbxLayerElement.Type.Material, i, polygonVertexIndex, positionIndex, 0);
irVertex.smoothing = (Integer) layer0.getVertexData(FbxLayerElement.Type.Smoothing, i, polygonVertexIndex, positionIndex, 0);
}
if (layer1 != null) {
irVertex.uv1 = (Vector2f) layer1.getVertexData(FbxLayerElement.Type.UV, i, polygonVertexIndex, positionIndex, 0);
}
if (boneIndices != null) {
ArrayList<Integer> boneIndicesForVertex = boneIndices[positionIndex];
ArrayList<Float> boneWeightsForVertex = boneWeights[positionIndex];
if (boneIndicesForVertex != null) {
irVertex.boneWeightsIndices = toBoneWeightIndices(boneIndicesForVertex, boneWeightsForVertex);
}
}
irPolygon.vertices[j] = irVertex;
polygonVertexIndex++;
}
newMesh.polygons[i] = irPolygon;
}
// Ensure "inspection vertex" specifies that mesh has bone indices / weights
if (boneIndices != null && newMesh.polygons[0].vertices[0] == null) {
newMesh.polygons[0].vertices[0].boneWeightsIndices = new IrBoneWeightIndex[0];
}
return newMesh;
}
use of com.jme3.math.ColorRGBA in project jmonkeyengine by jMonkeyEngine.
the class SceneLoader method startElement.
@Override
public void startElement(String uri, String localName, String qName, Attributes attribs) throws SAXException {
if (qName.equals("scene")) {
if (elementStack.size() != 0) {
throw new SAXException("dotScene parse error: 'scene' element must be the root XML element");
}
String version = attribs.getValue("formatVersion");
if (version == null || (!version.equals("1.0.0") && !version.equals("1.0.1"))) {
logger.log(Level.WARNING, "Unrecognized version number" + " in dotScene file: {0}", version);
}
} else if (qName.equals("nodes")) {
if (root != null) {
throw new SAXException("dotScene parse error: nodes element was specified twice");
}
if (sceneName == null) {
root = new com.jme3.scene.Node("OgreDotScene" + (++sceneIdx));
} else {
root = new com.jme3.scene.Node(sceneName + "-scene_node");
}
node = root;
} else if (qName.equals("externals")) {
checkTopNode("scene");
} else if (qName.equals("item")) {
checkTopNode("externals");
} else if (qName.equals("file")) {
checkTopNode("item");
// NOTE: This part of the file is ignored, it is parsed
// by SceneMaterialLoader in the first pass.
} else if (qName.equals("node")) {
String curElement = elementStack.peek();
if (!curElement.equals("node") && !curElement.equals("nodes")) {
throw new SAXException("dotScene parse error: " + "node element can only appear under 'node' or 'nodes'");
}
parseNode(attribs);
} else if (qName.equals("property")) {
if (node != null) {
String type = attribs.getValue("type");
String name = attribs.getValue("name");
String data = attribs.getValue("data");
if (type.equals("BOOL")) {
node.setUserData(name, Boolean.parseBoolean(data) || data.equals("1"));
} else if (type.equals("FLOAT")) {
node.setUserData(name, Float.parseFloat(data));
} else if (type.equals("STRING")) {
node.setUserData(name, data);
} else if (type.equals("INT")) {
node.setUserData(name, Integer.parseInt(data));
}
}
} else if (qName.equals("entity")) {
checkTopNode("node");
parseEntity(attribs);
} else if (qName.equals("camera")) {
checkTopNode("node");
parseCamera(attribs);
} else if (qName.equals("clipping")) {
checkTopNode("camera");
parseCameraClipping(attribs);
} else if (qName.equals("position")) {
if (elementStack.peek().equals("node")) {
node.setLocalTranslation(SAXUtil.parseVector3(attribs));
} else if (elementStack.peek().equals("camera")) {
cameraNode.setLocalTranslation(SAXUtil.parseVector3(attribs));
}
} else if (qName.equals("quaternion") || qName.equals("rotation")) {
node.setLocalRotation(parseQuat(attribs));
} else if (qName.equals("scale")) {
node.setLocalScale(SAXUtil.parseVector3(attribs));
} else if (qName.equals("light")) {
parseLight(attribs);
} else if (qName.equals("colourDiffuse") || qName.equals("colorDiffuse")) {
if (elementStack.peek().equals("light")) {
if (light != null) {
light.setColor(parseColor(attribs));
}
} else {
checkTopNode("environment");
}
} else if (qName.equals("colourAmbient") || qName.equals("colorAmbient")) {
if (elementStack.peek().equals("environment")) {
ColorRGBA color = parseColor(attribs);
if (!color.equals(ColorRGBA.Black) && !color.equals(ColorRGBA.BlackNoAlpha)) {
// Lets add an ambient light to the scene.
AmbientLight al = new AmbientLight();
al.setColor(color);
root.addLight(al);
}
}
} else if (qName.equals("normal") || qName.equals("direction")) {
checkTopNode("light");
parseLightNormal(attribs);
} else if (qName.equals("lightAttenuation")) {
parseLightAttenuation(attribs);
} else if (qName.equals("spotLightRange") || qName.equals("lightRange")) {
parseLightSpotLightRange(attribs);
}
elementStack.push(qName);
}
Aggregations