parent
7951f5a987
commit
3bbfabed5e
@ -0,0 +1,29 @@ |
|||||||
|
package com.jme3.scene.plugins.gltf; |
||||||
|
|
||||||
|
import com.jme3.asset.ModelKey; |
||||||
|
|
||||||
|
import java.util.HashMap; |
||||||
|
import java.util.Map; |
||||||
|
|
||||||
|
/** |
||||||
|
* Created by Nehon on 08/08/2017. |
||||||
|
*/ |
||||||
|
public class GltfModelKey extends ModelKey { |
||||||
|
|
||||||
|
private Map<String, MaterialAdapter> materialAdapters = new HashMap<>(); |
||||||
|
|
||||||
|
public GltfModelKey(String name) { |
||||||
|
super(name); |
||||||
|
} |
||||||
|
|
||||||
|
public GltfModelKey() { |
||||||
|
} |
||||||
|
|
||||||
|
public void registerMaterialAdapter(String gltfMaterialName, MaterialAdapter adapter) { |
||||||
|
materialAdapters.put(gltfMaterialName, adapter); |
||||||
|
} |
||||||
|
|
||||||
|
public MaterialAdapter getAdapterForMaterial(String gltfMaterialName) { |
||||||
|
return materialAdapters.get(gltfMaterialName); |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,86 @@ |
|||||||
|
package com.jme3.scene.plugins.gltf; |
||||||
|
|
||||||
|
|
||||||
|
import com.jme3.asset.AssetLoadException; |
||||||
|
import com.jme3.asset.AssetManager; |
||||||
|
import com.jme3.material.*; |
||||||
|
import com.jme3.math.*; |
||||||
|
import com.jme3.shader.VarType; |
||||||
|
import com.jme3.texture.Texture; |
||||||
|
|
||||||
|
import java.util.HashMap; |
||||||
|
import java.util.Map; |
||||||
|
|
||||||
|
/** |
||||||
|
* A MaterialAdapter allows to map a GLTF material to a JME material. |
||||||
|
* It maps each gltf parameter to it's matching parameter in the JME material, |
||||||
|
* and allows for some conversion if the JME material model doesn't exactly match the gltf material model |
||||||
|
* Created by Nehon on 08/08/2017. |
||||||
|
*/ |
||||||
|
public abstract class MaterialAdapter { |
||||||
|
|
||||||
|
private Map<String, String> paramsMapping = new HashMap<>(); |
||||||
|
|
||||||
|
/** |
||||||
|
* Should return the material definition used by this material adapter |
||||||
|
* |
||||||
|
* @return |
||||||
|
*/ |
||||||
|
protected abstract String getMaterialDefPath(); |
||||||
|
|
||||||
|
protected abstract MatParam adaptMatParam(Material mat, MatParam param); |
||||||
|
|
||||||
|
public Material getMaterial(AssetManager assetManager) { |
||||||
|
return new Material(assetManager, getMaterialDefPath()); |
||||||
|
} |
||||||
|
|
||||||
|
public void setParam(Material mat, String gltfParamName, Object value) { |
||||||
|
String name = getJmeParamName(gltfParamName); |
||||||
|
if (name == null || value == null) { |
||||||
|
//no mapping registered or value is null, let's ignore this param
|
||||||
|
return; |
||||||
|
} |
||||||
|
MatParam param; |
||||||
|
if (value instanceof Texture) { |
||||||
|
MatParam defParam = mat.getMaterialDef().getMaterialParam(name); |
||||||
|
if (defParam == null) { |
||||||
|
throw new AssetLoadException("Material definition " + getMaterialDefPath() + " has not param with name" + name); |
||||||
|
} |
||||||
|
if (!(defParam instanceof MatParamTexture)) { |
||||||
|
throw new AssetLoadException("param with name" + name + "in material definition " + getMaterialDefPath() + " should be a texture param"); |
||||||
|
} |
||||||
|
param = new MatParamTexture(VarType.Texture2D, name, (Texture) value, ((MatParamTexture) defParam).getColorSpace()); |
||||||
|
param = adaptMatParam(mat, param); |
||||||
|
if (param != null) { |
||||||
|
mat.setTextureParam(param.getName(), param.getVarType(), (Texture) param.getValue()); |
||||||
|
} |
||||||
|
} else { |
||||||
|
param = new MatParam(getVarType(value), name, value); |
||||||
|
param = adaptMatParam(mat, param); |
||||||
|
if (param != null) { |
||||||
|
mat.setParam(param.getName(), param.getVarType(), param.getValue()); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
protected void addParamMapping(String gltfParamName, String jmeParamName) { |
||||||
|
paramsMapping.put(gltfParamName, jmeParamName); |
||||||
|
} |
||||||
|
|
||||||
|
protected String getJmeParamName(String gltfParamName) { |
||||||
|
return paramsMapping.get(gltfParamName); |
||||||
|
} |
||||||
|
|
||||||
|
private VarType getVarType(Object value) { |
||||||
|
if (value instanceof Float) return VarType.Float; |
||||||
|
if (value instanceof Integer) return VarType.Int; |
||||||
|
if (value instanceof Boolean) return VarType.Boolean; |
||||||
|
if (value instanceof ColorRGBA) return VarType.Vector4; |
||||||
|
if (value instanceof Vector4f) return VarType.Vector4; |
||||||
|
if (value instanceof Vector3f) return VarType.Vector3; |
||||||
|
if (value instanceof Vector2f) return VarType.Vector2; |
||||||
|
if (value instanceof Matrix3f) return VarType.Matrix3; |
||||||
|
if (value instanceof Matrix4f) return VarType.Matrix4; |
||||||
|
throw new AssetLoadException("Unsupported material parameter type : " + value.getClass().getSimpleName()); |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,58 @@ |
|||||||
|
package com.jme3.scene.plugins.gltf; |
||||||
|
|
||||||
|
import com.jme3.material.*; |
||||||
|
|
||||||
|
/** |
||||||
|
* Created by Nehon on 08/08/2017. |
||||||
|
*/ |
||||||
|
public class PBRMaterialAdapter extends MaterialAdapter { |
||||||
|
|
||||||
|
|
||||||
|
public PBRMaterialAdapter() { |
||||||
|
addParamMapping("baseColorFactor", "BaseColor"); |
||||||
|
addParamMapping("baseColorTexture", "BaseColorMap"); |
||||||
|
addParamMapping("metallicFactor", "Metallic"); |
||||||
|
addParamMapping("roughnessFactor", "Roughness"); |
||||||
|
addParamMapping("metallicRoughnessTexture", "MetallicRoughnessMap"); |
||||||
|
addParamMapping("normalTexture", "NormalMap"); |
||||||
|
addParamMapping("occlusionTexture", "LightMap"); |
||||||
|
addParamMapping("emisiveTexture", "EmissiveMap"); |
||||||
|
addParamMapping("emisiveFactor", "Emissive"); |
||||||
|
addParamMapping("alphaMode", "alpha"); |
||||||
|
addParamMapping("alphaCutoff", "AlphaDiscardThreshold"); |
||||||
|
addParamMapping("doubleSided", "doubleSided"); |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
protected String getMaterialDefPath() { |
||||||
|
return "Common/MatDefs/Light/PBRLighting.j3md"; |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
protected MatParam adaptMatParam(Material mat, MatParam param) { |
||||||
|
if (param.getName().equals("alpha")) { |
||||||
|
String alphaMode = (String) param.getValue(); |
||||||
|
switch (alphaMode) { |
||||||
|
case "MASK": |
||||||
|
case "BLEND": |
||||||
|
mat.getAdditionalRenderState().setBlendMode(RenderState.BlendMode.Alpha); |
||||||
|
} |
||||||
|
return null; |
||||||
|
} |
||||||
|
if (param.getName().equals("doubleSided")) { |
||||||
|
boolean doubleSided = (boolean) param.getValue(); |
||||||
|
if (doubleSided) { |
||||||
|
//Note that this is not completely right as normals on the back side will be in the wrong direction.
|
||||||
|
mat.getAdditionalRenderState().setFaceCullMode(RenderState.FaceCullMode.Off); |
||||||
|
} |
||||||
|
return null; |
||||||
|
} |
||||||
|
if (param.getName().equals("MetallicRoughnessMap")) { |
||||||
|
//use packed Metallic/Roughness
|
||||||
|
mat.setBoolean("UsePackedMR", true); |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
return param; |
||||||
|
} |
||||||
|
} |
Loading…
Reference in new issue