Browse Source

Fix parallax not being uniform between terrain with a different ileSize

ShiroSmith 5 years ago
parent
commit
e22fba5fb2
2 changed files with 7 additions and 6 deletions
  1. 2 2
      hrt/prefab/terrain/Terrain.hx
  2. 5 4
      hrt/shader/Terrain.hx

+ 2 - 2
hrt/prefab/terrain/Terrain.hx

@@ -528,7 +528,7 @@ class Terrain extends Object3D {
 		else
 			terrain.weightMapResolution = new h2d.col.IPoint(Math.round(tileSizeX * weightMapPixelPerMeter), Math.round(tileSizeY * weightMapPixelPerMeter));
 
-		terrain.parallaxAmount = parallaxAmount / 10;
+		terrain.parallaxAmount = parallaxAmount;
 		terrain.parallaxMinStep = parallaxMinStep;
 		terrain.parallaxMaxStep = parallaxMaxStep;
 		terrain.heightBlendStrength = heightBlendStrength;
@@ -552,7 +552,7 @@ class Terrain extends Object3D {
 		super.updateInstance(ctx, null);
 
 		#if editor
-		terrain.parallaxAmount = parallaxAmount / 10.0;
+		terrain.parallaxAmount = parallaxAmount;
 		terrain.parallaxMinStep = parallaxMinStep;
 		terrain.parallaxMaxStep = parallaxMaxStep;
 		terrain.heightBlendStrength = heightBlendStrength;

+ 5 - 4
hrt/shader/Terrain.hx

@@ -60,15 +60,15 @@ class Terrain extends hxsl.Shader {
 				var terrainUV = (calculatedUV * (heightMapSize - 1)) / heightMapSize;
 				// Blend with the heightpixel of the adjacent chunk
 				if( input.position.x == primSize.x ) terrainUV.x += 0.5 / heightMapSize.x;
-				if( input.position.y == primSize.y ) terrainUV.y += 0.5 / heightMapSize.y; 
+				if( input.position.y == primSize.y ) terrainUV.y += 0.5 / heightMapSize.y;
 				transformedPosition += vec3(0,0,textureLod(heightMap, terrainUV, 0).r) * global.modelView.mat3();
 			}
 			transformedTangent = vec4(tangent * global.modelView.mat3(),tangent.dot(tangent) > 0.5 ? 1. : -1.);
 			var tanX = transformedTangent.xyz.normalize() * -transformedTangent.w;
 			var tanY = transformedNormal.cross(tanX).normalize();
 			TBN = mat3(tanX, tanY, transformedNormal);
-			tangentViewPos = camera.position * TBN;
-			tangentFragPos = transformedPosition * TBN;
+			tangentViewPos = (camera.position * TBN);
+			tangentFragPos = (transformedPosition * TBN);
 		}
 
 		function getWeight( i : IVec3,  uv : Vec2 ) : Vec3 {
@@ -91,10 +91,11 @@ class Terrain extends hxsl.Shader {
 		var i : IVec3;
 		function getPOMUV( i : IVec3, uv : Vec2 ) : Vec2 {
 			var viewNS = normalize(tangentViewPos - tangentFragPos);
+			viewNS.xy /= viewNS.z;
 			var numLayers = mix(float(maxStep), float(minStep), viewNS.dot(transformedNormal));
 			var layerDepth = 1 / numLayers;
 			var curLayerDepth = 0.;
-			var delta = (viewNS.xy / viewNS.z) * parallaxAmount / numLayers;
+			var delta = (viewNS.xy * parallaxAmount / primSize) / numLayers;
 			var curUV = uv;
 			var depth = getDepth(i, curUV);
 			var curDepth = depth.dot(w);