Jelajahi Sumber

added fog noise

Nicolas Cannasse 5 tahun lalu
induk
melakukan
2899431bd3
1 mengubah file dengan 67 tambahan dan 2 penghapusan
  1. 67 2
      hrt/prefab/rfx/GenFog.hx

+ 67 - 2
hrt/prefab/rfx/GenFog.hx

@@ -19,16 +19,27 @@ class GenFogShader extends PbrShader {
 		@const var usePosition : Bool;
 		@param var position : Vec3;
 
+		@const var useNoise : Bool;
+		@param var noiseTex : Sampler2D;
+		@param var noiseScale : Float;
+		@param var noiseSpeed : Float;
+		@param var noiseAmount : Vec3;
+
 		function fragment() {
 			var origin = getPosition();
 			var amount = 0.;
 
-			if( distanceScale != 0 ) {
+			if( useNoise ) {
+				var noise = noiseTex.get( origin.xy * noiseScale + vec2(global.time * noiseSpeed, noiseScale * origin.z) * vec2(1,-1) );
+				origin += (noise.rgb - 0.5) * noiseAmount;
+			}
+
+			if( distanceOpacity != 0 ) {
 				var distance = (origin - (usePosition ? position : camera.position)).length() - cameraDistance;
 				amount += clamp((distance - startDistance) * distanceScale, 0, 1) * distanceOpacity;
 			}
 
-			if( heightScale != 0 ) {
+			if( heightOpacity != 0 ) {
 				var height = origin.z;
 				if( usePosition ) height -= position.z;
 				amount += clamp((height - startHeight) * heightScale, 0, 1) * heightOpacity;
@@ -46,6 +57,14 @@ class GenFogShader extends PbrShader {
 
 }
 
+typedef GenFogNoise = {
+	var texture : String;
+	var speed : Float;
+	var scale : Float;
+	var amount : Float;
+	var distAmount : Float;
+}
+
 typedef GenFogProps = {
  	var startDistance : Float;
 	var endDistance : Float;
@@ -63,6 +82,8 @@ typedef GenFogProps = {
 	var endColor : Int;
 	var renderMode : String;
 
+	var ?noise : GenFogNoise;
+
 	var posX : Float;
 	var posY : Float;
 	var posZ : Float;
@@ -123,6 +144,15 @@ class GenFog extends RendererFX {
 			fogPass.shader.position.set(p.posX, p.posY, p.posZ);
 			fogPass.shader.usePosition = p.usePosition;
 
+			fogPass.shader.useNoise = p.noise != null && p.noise.texture != null;
+			if( p.noise != null && p.noise.texture != null ) {
+				fogPass.shader.noiseTex = hxd.res.Loader.currentInstance.load(p.noise.texture).toTexture();
+				fogPass.shader.noiseTex.wrap = Repeat;
+				fogPass.shader.noiseScale = 1 / p.noise.scale;
+				fogPass.shader.noiseSpeed = p.noise.speed / p.noise.scale;
+				fogPass.shader.noiseAmount.set(p.noise.amount * p.noise.distAmount, p.noise.amount * p.noise.distAmount, p.noise.amount);
+			}
+
 
 			fogPass.setGlobals(ctx);
 			fogPass.render();
@@ -166,6 +196,41 @@ class GenFog extends RendererFX {
 
 			</dl>
 		'),props);
+		var props : GenFogProps = props;
+		if( props.noise == null ) {
+			var e = ctx.properties.add(new hide.Element('
+			<div class="group" name="Noise">
+			<dl><dt></dt><dd><a class="button" href="#">Add</a></dd></dl>
+			</div>
+			'));
+			e.find("a.button").click(function(_) {
+				props.noise = {
+					texture : null,
+					amount : 1,
+					scale : 1,
+					speed : 1,
+					distAmount : 0.5,
+				};
+				ctx.rebuildProperties();
+			});
+		} else {
+			var e = ctx.properties.add(new hide.Element('
+			<div class="group" name="Noise">
+			<dl>
+				<dt>Texture</dt><dd><input type="texturepath" field="texture"/></dd>
+				<dt>Amount</dt><dd><input type="range" min="0" max="10" field="amount"/></dd>
+				<dt>Scale</dt><dd><input type="range" min="0" max="10" field="scale"/></dd>
+				<dt>Speed</dt><dd><input type="range" min="0" max="10" field="speed"/></dd>
+				<dt>Dist.Amount</dt><dd><input type="range" min="0" max="10" field="distAmount"/></dd>
+				<dt></dt><dd><a class="button" href="#">Remove</a></dd>
+			</dl>
+			</div>
+			'),props.noise);
+			e.find("a.button").click(function(_) {
+				props.noise = null;
+				ctx.rebuildProperties();
+			});
+		}
 	}
 	#end