I'm continuing the discussion from the demo thread about Floating origin / Increased Terrain here so we don't clutter it up
I've (possibly) made some headway in making increased terrain work with floating origin.
Updated Floating Origin script:
https://drive.google.com/file/d/0B8_oQw ... sp=sharing
Code change in IncreasedTerrainDistance:
In private void updatePositionWorldTerrain()
Code: Select all
// world map level transform (for whole world map pixels)
Vector3 offset = FloatingOrigin.instance.totalOffset;
Vector3 worldMapLevelTransform;
worldMapLevelTransform.x = (xdif) * scale;
worldMapLevelTransform.y = offset.y;
worldMapLevelTransform.z = -(ydif) * scale;
// local world level transform (for inter- world map pixels)
float localTransformX = (float)Math.Floor((cameraPos.x) / scale) * scale;
float localTransformZ = (float)Math.Floor((cameraPos.z) / scale) * scale;
// compute composite transform and apply it to terrain object
Vector3 finalTransform = new Vector3(worldMapLevelTransform.x + localTransformX, worldMapLevelTransform.y, worldMapLevelTransform.z + localTransformZ);
terrainGameObject.gameObject.transform.localPosition = finalTransform;
And you can either attach the script to streaming world and it be offset automatically, or put it on a new game object and add the following in updatePositionWorldTerrain()
Code: Select all
// Create Unity Terrain game object
GameObject terrainGameObject = Terrain.CreateTerrainGameObject(null);
terrainGameObject.AddComponent<PositionUpdate>();
PositionUpdate was included in the Zip w/ floating origin if you didn't copy into your assets already
It's not perfect, but it seems to keep the far terrain position close to correct as the player moves around - (though I think the y might need to be offset tiny bit? I left it alone to keep it simple for now) - Iit doesn't do anything about the terrain textures / meshes - they'll still turn to water and so on if the player changes height.
edit: It seems just resetting the water height each update before updating the textures solves the water issue.
I just copied the relevant code from generateWorldTerrain() and put it in update:
Code: Select all
secondaryCamera.transform.SetParent(Camera.main.transform); // make 2nd camera inherit transform from main camera
}
//## new water height code:
Vector3 vecWaterHeight = new Vector3(0.0f, (TerrainHelper.scaledOceanElevation + 1.0f) * streamingWorld.TerrainScale, 0.0f); // water height level on y-axis (+1.0f some coastlines are incorrect otherwise)
Vector3 vecWaterHeightTransformed = terrainGameObject.transform.TransformPoint(vecWaterHeight); // transform to world coordinates
terrainMaterial.SetFloat("_WaterHeightTransformed", vecWaterHeightTransformed.y);
// Handle moving to new map pixel or first-time init
I'm testing this all without the improved terrain, so I'll need to check later and see how that's working now