Browse Source

[gradient] Gradient now use Repeat wrap by default

To configure that at the prefab level, use the new wrap parameter
in getTextureFromValue
Clement Espeute 1 year ago
parent
commit
80c026373e
4 changed files with 10 additions and 6 deletions
  1. 0 2
      hrt/impl/Gradient.hx
  2. 3 2
      hrt/impl/TextureType.hx
  3. 7 1
      hrt/shader/GradientMap.hx
  4. 0 1
      hrt/shgraph/nodes/Sampler.hx

+ 0 - 2
hrt/impl/Gradient.hx

@@ -193,8 +193,6 @@ class Gradient {
             texture.uploadPixels(genPixels());
         }
 
-        texture.wrap = Repeat;
-
 		#if !editor
         cache.set(hash, texture);
 		#end

+ 3 - 2
hrt/impl/TextureType.hx

@@ -8,10 +8,10 @@ enum abstract TextureType(String) from String to String {
 }
 
 class Utils {
-    public static function getTextureFromValue(val : Any) : h3d.mat.Texture {
+    public static function getTextureFromValue(val : Any, wrap : h3d.mat.Data.Wrap = Repeat) : h3d.mat.Texture {
         if (Std.isOfType(val, String)) {
             var t = hxd.res.Loader.currentInstance.load(val).toTexture();
-            t.wrap = Repeat;
+            t.wrap = wrap;
             return t;
         }
         else if (Type.typeof(val) == TObject) {
@@ -24,6 +24,7 @@ class Utils {
                         var gradData = Utils.getGradientData((val:Dynamic));
                         if (gradData != null) {
                             var t = Gradient.textureFromData(gradData);
+							t.wrap = wrap;
                             return t;
                         }
                     }

+ 7 - 1
hrt/shader/GradientMap.hx

@@ -8,7 +8,13 @@ class GradientMap extends hxsl.Shader {
 
 		function fragment() {
 			var t = USE_ALPHA ? pixelColor.a : dot(pixelColor.rgb*pixelColor.rgb, vec3(0.2126, 0.7152, 0.0722));
-			pixelColor.rgb = gradient.get(vec2(t, 0.5)).rgb;
+
+			// Force clamping values (compatibility for gradients that are now in repeat mode by default)
+			var uv2 = vec2(t, 0.5);
+			var size = gradient.size();
+			uv2 = clamp(uv2, 0.5 / size, (size - vec2(0.5)) / size);
+
+			pixelColor.rgb = gradient.get(uv2).rgb;
 		}
 	};
 }

+ 0 - 1
hrt/shgraph/nodes/Sampler.hx

@@ -40,7 +40,6 @@ class Sampler extends ShaderNodeHxsl {
 			var uv2 = uv;
 			if (wrap == 0) {
 				var size = texture.size();
-				uv2 = saturate(uv2);
 				uv2 = clamp(uv2, 0.5 / size, (size - vec2(0.5)) / size);
 			}