public void SetUniforms(Material target) { if (target == null) { return; } if (ParentBody.GetBodyDeformationType() == BodyDeformationType.Spherical) { var celestialBody = ParentBody as CelestialBody; if (celestialBody == null) { throw new Exception("Wow! Celestial body isn't Celestial?!"); } target.SetTexture("_Ground_Diffuse", celestialBody.GroundDiffuse); target.SetTexture("_Ground_Normal", celestialBody.GroundNormal); target.SetTexture("_DetailedNormal", celestialBody.DetailedNormal); } }
protected override void UpdateNode() { TerrainMaterial.renderQueue = (int)ParentBody.RenderQueue + ParentBody.RenderQueueOffset; if (ParentBody.GetBodyDeformationType() == BodyDeformationType.Spherical) { LocalToWorld = Matrix4x4d.ToMatrix4x4d(ParentBody.transform.localToWorldMatrix) * FaceToLocal; } else { LocalToWorld = FaceToLocal; } TangentFrameToWorld = new Matrix3x3d(LocalToWorld.m[0, 0], LocalToWorld.m[0, 1], LocalToWorld.m[0, 2], LocalToWorld.m[1, 0], LocalToWorld.m[1, 1], LocalToWorld.m[1, 2], LocalToWorld.m[2, 0], LocalToWorld.m[2, 1], LocalToWorld.m[2, 2]); var localToCamera = GodManager.Instance.WorldToCamera * LocalToWorld; var invLocalToCamera = localToCamera.Inverse(); DeformedCameraPosition = invLocalToCamera * Vector3d.zero; DeformedFrustumPlanes = Frustum.GetFrustumPlanes(GodManager.Instance.CameraToScreen * localToCamera); // NOTE : Extract frustum planes from LocalToScreen matrix... LocalCameraPosition = Deformation.DeformedToLocal(DeformedCameraPosition); var m = Deformation.LocalToDeformedDifferential(LocalCameraPosition, true); var left = DeformedFrustumPlanes[0].XYZ().Normalized(); var right = DeformedFrustumPlanes[1].XYZ().Normalized(); var fov = (float)MathUtility.Safe_Acos(-left.Dot(right)); SplitDistance = SplitFactor * Screen.width / 1024.0f * Mathf.Tan(40.0f * Mathf.Deg2Rad) / Mathf.Tan(fov / 2.0f); DistanceFactor = (float)Math.Max((new Vector3d(m.m[0, 0], m.m[1, 0], m.m[2, 0])).Magnitude(), (new Vector3d(m.m[0, 1], m.m[1, 1], m.m[2, 1])).Magnitude()); if (SplitDistance < 1.1f || !MathUtility.IsFinite(SplitDistance)) { SplitDistance = 1.1f; } // initializes data structures for horizon occlusion culling if (UseHorizonCulling && LocalCameraPosition.z <= TerrainQuadRoot.ZMax) { var deformedDirection = invLocalToCamera * Vector3d.forward; var localDirection = (Deformation.DeformedToLocal(deformedDirection) - LocalCameraPosition).xy.Normalized(); LocalCameraDirection = new Matrix2x2d(localDirection.y, -localDirection.x, -localDirection.x, -localDirection.y); for (byte i = 0; i < HORIZON_SIZE; ++i) { Horizon[i] = float.NegativeInfinity; } } TerrainQuadRoot.UpdateLOD(); SetUniforms(TerrainMaterial); if (ParentBody.AtmosphereEnabled) { if (ParentBody.Atmosphere != null) { ParentBody.Atmosphere.SetUniforms(TerrainMaterial); } } if (ParentBody.OceanEnabled) { if (ParentBody.Ocean != null) { ParentBody.Ocean.SetUniforms(TerrainMaterial); } else { TerrainMaterial.SetFloat("_Ocean_DrawBRDF", 0.0f); } } else { TerrainMaterial.SetFloat("_Ocean_DrawBRDF", 0.0f); } Deformation.SetUniforms(this, TerrainMaterial); //if (Manager.GetPlantsNode() != null) // Manager.GetPlantsNode().SetUniforms(TerrainMaterial); }
protected override void InitNode() { ParentBody = GetComponentInParent <Body>(); ParentBody.TerrainNodes.Add(this); TerrainMaterial = MaterialHelper.CreateTemp(ParentBody.ColorShader, "TerrainNode"); FaceToLocal = Matrix4x4d.identity; if (ParentBody.GetBodyDeformationType() == BodyDeformationType.Spherical) { var celestialBody = ParentBody as CelestialBody; if (celestialBody == null) { throw new Exception("Wow! Celestial body isn't Celestial?!"); } var faces = new Vector3d[] { new Vector3d(0, 0, 0), new Vector3d(90, 0, 0), new Vector3d(90, 90, 0), new Vector3d(90, 180, 0), new Vector3d(90, 270, 0), new Vector3d(0, 180, 180) }; // If this terrain is deformed into a sphere the face matrix is the rotation of the // terrain needed to make up the spherical planet. In this case there should be 6 terrains, each with a unique face number if (Face - 1 >= 0 && Face - 1 < 6) { FaceToLocal = Matrix4x4d.Rotate(faces[Face - 1]); } LocalToWorld = Matrix4x4d.ToMatrix4x4d(celestialBody.transform.localToWorldMatrix) * FaceToLocal; Deformation = new DeformationSpherical(celestialBody.Size); InitUniforms(TerrainMaterial); if (celestialBody.Atmosphere != null) { celestialBody.Atmosphere.InitUniforms(TerrainMaterial); } } else { LocalToWorld = FaceToLocal; Deformation = new DeformationBase(); } TangentFrameToWorld = new Matrix3x3d(LocalToWorld.m[0, 0], LocalToWorld.m[0, 1], LocalToWorld.m[0, 2], LocalToWorld.m[1, 0], LocalToWorld.m[1, 1], LocalToWorld.m[1, 2], LocalToWorld.m[2, 0], LocalToWorld.m[2, 1], LocalToWorld.m[2, 2]); CreateTerrainQuadRoot(ParentBody.Size); CollectSamplers(); SamplersOrder = new TileSamplerOrder(Samplers); var producers = GetComponentsInChildren <TileProducer>(); var lastProducer = producers[producers.Length - 1]; if (lastProducer.IsLastInSequence == false) { lastProducer.IsLastInSequence = true; Debug.Log(string.Format("{0} probably last in generation sequence, but maybe accidentally not marked as. Fixed!", lastProducer.name)); } }