2
0
Эх сурвалжийг харах

added normalMap & specularTexture loading from FBX into HMD

ncannasse 7 жил өмнө
parent
commit
b25b642c87

+ 2 - 1
h3d/mat/Material.hx

@@ -14,7 +14,8 @@ class Material extends BaseMaterial {
 	public var textureShader(default, null) : h3d.shader.Texture;
 	public var specularShader(default, null) : h3d.shader.SpecularTexture;
 	public var texture(get, set) : h3d.mat.Texture;
-	public var specularTexture(get,set) : h3d.mat.Texture;
+	public var specularTexture(get, set) : h3d.mat.Texture;
+	public var normalMap : h3d.mat.Texture;
 
 	public var color(get, set) : Vector;
 	public var specularAmount(get, set) : Float;

+ 33 - 18
hxd/fmt/fbx/HMDOut.hx

@@ -474,23 +474,16 @@ class HMDOut extends BaseLibrary {
 				// get texture
 				var texture = getSpecChild(m, "DiffuseColor");
 				if( texture != null ) {
-					var path = texture.get("FileName").props[0].toString();
-					if( path != "" ) {
-						path = path.split("\\").join("/");
-						if( !absoluteTexturePath ) {
-							if( filePath != null && StringTools.startsWith(path.toLowerCase(), filePath) )
-								path = path.substr(filePath.length);
-							else {
-								// relative resource path
-								var k = path.split("/res/");
-								if( k.length > 1 ) {
-									k.shift();
-									path = k.join("/res/");
-								}
-							}
-						}
-						mat.diffuseTexture = path;
-					}
+					var path = makeTexturePath(texture);
+					if( path != null ) mat.diffuseTexture = path;
+				}
+
+				// get other textures
+				mat.normalMap = makeTexturePath(getSpecChild(m, "NormalMap"));
+				mat.specularTexture = makeTexturePath(getSpecChild(m, "SpecularFactor"));
+				if( mat.normalMap != null || mat.specularTexture != null ) {
+					if( mat.props == null ) mat.props = [];
+					mat.props.push(HasExtraTextures);
 				}
 
 				// get alpha map
@@ -504,7 +497,7 @@ class HMDOut extends BaseLibrary {
 							// if that's the same file, we're doing alpha blending
 							if( mat.blendMode == null && ext != "jpg" && ext != "jpeg" ) mat.blendMode = Alpha;
 						} else
-							throw "TODO : alpha texture";
+							throw "Alpha texture that is different from diffuse is not supported in HMD";
 					}
 				}
 
@@ -557,6 +550,28 @@ class HMDOut extends BaseLibrary {
 		}
 	}
 
+	function makeTexturePath( tex : FbxNode ) {
+		if( tex == null )
+			return null;
+		var path = tex.get("FileName").props[0].toString();
+		if( path == "" )
+			return null;
+		path = path.split("\\").join("/");
+		if( !absoluteTexturePath ) {
+			if( filePath != null && StringTools.startsWith(path.toLowerCase(), filePath) )
+				path = path.substr(filePath.length);
+			else {
+				// relative resource path
+				var k = path.split("/res/");
+				if( k.length > 1 ) {
+					k.shift();
+					path = k.join("/res/");
+				}
+			}
+		}
+		return path;
+	}
+
 	function makeSkin( skin : h3d.anim.Skin, obj : TmpObject ) {
 		var s = new Skin();
 		s.name = obj.model.getName();

+ 3 - 0
hxd/fmt/hmd/Data.hx

@@ -41,6 +41,7 @@ typedef Index<T> = Int;
 enum Property<T> {
 	CameraFOVY( v : Float ) : Property<Float>;
 	HasMaterialFlags;
+	HasExtraTextures;
 }
 
 typedef Properties = Null<Array<Property<Dynamic>>>;
@@ -139,6 +140,8 @@ class Material {
 	public var name : String;
 	public var props : Properties;
 	public var diffuseTexture : Null<String>;
+	public var specularTexture : Null<String>;
+	public var normalMap : Null<String>;
 	public var blendMode : h3d.mat.BlendMode;
 	public var culling : h3d.mat.Data.Face;
 	public var killAlpha : Null<Float>;

+ 4 - 0
hxd/fmt/hmd/Library.hx

@@ -266,6 +266,10 @@ class Library {
 			mat.texture = loadTexture(m.diffuseTexture);
 			if( mat.texture == null ) mat.texture = h3d.mat.Texture.fromColor(0xFF00FF);
 		}
+		if( m.specularTexture != null )
+			mat.specularTexture = loadTexture(m.specularTexture);
+		if( m.normalMap != null )
+			mat.normalMap = loadTexture(m.normalMap);
 		mat.blendMode = m.blendMode;
 		mat.mainPass.culling = m.culling;
 		if( m.killAlpha != null ) {

+ 6 - 0
hxd/fmt/hmd/Reader.hx

@@ -19,6 +19,8 @@ class Reader {
 			return CameraFOVY(i.readFloat());
 		case 1:
 			return HasMaterialFlags;
+		case 2:
+			return HasExtraTextures;
 		case unk:
 			throw "Unknown property #" + unk;
 		}
@@ -145,6 +147,10 @@ class Reader {
 			if( m.killAlpha == 1 ) m.killAlpha = null;
 			if( m.props != null && m.props.indexOf(HasMaterialFlags) >= 0 )
 				m.flags = haxe.EnumFlags.ofInt(i.readInt32());
+			if( m.props != null && m.props.indexOf(HasExtraTextures) >= 0 ) {
+				m.specularTexture = readName();
+				m.normalMap = readName();
+			}
 			d.materials.push(m);
 		}
 

+ 5 - 1
hxd/fmt/hmd/Writer.hx

@@ -15,7 +15,7 @@ class Writer {
 		switch( p ) {
 		case CameraFOVY(v):
 			out.writeFloat(v);
-		case HasMaterialFlags:
+		case HasMaterialFlags, HasExtraTextures:
 		}
 	}
 
@@ -136,6 +136,10 @@ class Writer {
 			writeFloat(m.killAlpha == null ? 1 : m.killAlpha);
 			if( m.props != null && m.props.indexOf(HasMaterialFlags) >= 0 )
 				out.writeInt32(m.flags.toInt());
+			if( m.props != null && m.props.indexOf(HasExtraTextures) >= 0 ) {
+				writeName(m.specularTexture);
+				writeName(m.normalMap);
+			}
 		}
 
 		out.writeInt32(d.models.length);