Browse Source

getTextureFromValue util function

Clement Espeute 3 years ago
parent
commit
997a722c0d
2 changed files with 29 additions and 21 deletions
  1. 28 1
      hrt/impl/TextureType.hx
  2. 1 20
      hrt/prefab/Shader.hx

+ 28 - 1
hrt/impl/TextureType.hx

@@ -4,4 +4,31 @@ package hrt.impl;
 abstract TextureType(String) from String to String {
     var gradient;
     var path;       // Not used as a type inside the json (the playload is a string), default value
-}
+}
+
+class Utils {
+    public static function getTextureFromValue(val : Any) : h3d.mat.Texture {
+        if (Std.isOfType(val, String)) {
+            var t = hxd.res.Loader.currentInstance.load(val).toTexture();
+            t.wrap = Repeat;
+            return t;
+        }
+        else if (Type.typeof(val) == TObject) {
+            var val = (val:Dynamic);
+            if (val.type != null && Std.isOfType(val.type, String)) {
+
+                switch((val.type:String):TextureType) {
+                    case TextureType.gradient:
+                    {
+                        if (val.data.stops != null && val.data.resolution != null) {
+                            var t = Gradient.textureFromData(val.data);
+                            return t;
+                        }
+                    }
+                    default:
+                }
+            }
+        }
+        return null;
+    }
+}

+ 1 - 20
hrt/prefab/Shader.hx

@@ -47,26 +47,7 @@ class Shader extends Prefab {
 					val = new h3d.Vector();
 			case TSampler2D:
 				if( val != null ) {
-					if (Std.isOfType(val, String)) {
-						var t = hxd.res.Loader.currentInstance.load(val).toTexture();
-						t.wrap = Repeat;
-						val = t;
-					}
-					else if (Type.typeof(val) == TObject) {
-						if (val.type != null && Std.isOfType(val.type, String)) {
-
-							switch((val.type:String):TextureType) {
-								case TextureType.gradient:
-								{
-									if (val.data.stops != null && val.data.resolution != null) {
-										var t = Gradient.textureFromData(val.data);
-										val = t; 
-									}
-								}
-								default:
-							}
-						}
-					}
+					val = Utils.getTextureFromValue(val);
 				}
 				else {
 					var childNoise = getOpt(hrt.prefab.l2d.NoiseGenerator, v.name);