internal float SignedDistanceLocal(Vector3 position, float lodVoxelSize) { float distance = position.Length(); if (distance > .1 && distance >= InnerRadius - lodVoxelSize) { if (distance > OuterRadius + lodVoxelSize) { return(float.PositiveInfinity); } float signedDistance = distance - m_radius; int face; Vector2 tex; MyCubemapHelpers.CalculateSampleTexcoord(ref position, out face, out tex); Vector3 Norm; float value = GetValueForPositionInternal(face, ref tex, lodVoxelSize, distance, out Norm); if (m_detail.Matches(Norm.Z)) { float dtx = tex.X * m_detail.Factor; float dty = tex.Y * m_detail.Factor; dtx -= (float)Math.Floor(dtx); dty -= (float)Math.Floor(dty); value += m_detail.GetValue(dtx, dty, Norm.Z); } return((signedDistance - value) * Norm.Z); } return(-lodVoxelSize); }
public void GetPositionParams(ref Vector3 pos, float lodSize, out MaterialSampleParams ps, bool skipCache = false) { Vector3 localPos = pos - this.m_planetShape.Center(); ps.DistanceToCenter = localPos.Length(); ps.LodSize = lodSize; if (ps.DistanceToCenter < 0.01f) { ps.SurfaceDepth = 0f; ps.Gravity = Vector3.Down; ps.Latitude = 0f; ps.Longitude = 0f; ps.Texcoord = Vector2.One / 2f; ps.Face = 0; ps.Normal = Vector3.Backward; ps.SampledHeight = 0f; } else { ps.Gravity = localPos / ps.DistanceToCenter; MyCubemapHelpers.CalculateSampleTexcoord(ref localPos, out ps.Face, out ps.Texcoord); ps.SampledHeight = !skipCache?this.m_planetShape.GetValueForPositionWithCache(ps.Face, ref ps.Texcoord, out ps.Normal) : this.m_planetShape.GetValueForPositionCacheless(ps.Face, ref ps.Texcoord, out ps.Normal); ps.SurfaceDepth = this.m_planetShape.SignedDistanceWithSample(lodSize, ps.DistanceToCenter, ps.SampledHeight) * ps.Normal.Z; ps.Latitude = ps.Gravity.Y; Vector2 vector2 = new Vector2(-ps.Gravity.X, -ps.Gravity.Z); vector2.Normalize(); ps.Longitude = vector2.Y; if (-ps.Gravity.X > 0f) { ps.Longitude = 2f - ps.Longitude; } } }
/** * Get the height for a position using the coefficient cache. Calculated position is for LOD0. */ public double GetDistanceToSurfaceCacheless(Vector3 localPos) { float length = localPos.Length(); if (length.IsZero()) { Debug.Fail("Cannot project to surface from the center of the planet!"); return(0); } if (!length.IsValid()) { Debug.Fail("Cannot project from such number!"); return(0); } int face; Vector2 tex; MyCubemapHelpers.CalculateSampleTexcoord(ref localPos, out face, out tex); Vector3 norm; float value = GetValueForPositionCacheless(face, ref tex, out norm); value += Radius; return(length - value); }
internal float SignedDistanceLocalCacheless(Vector3 position) { float distance = position.Length(); if (distance > .1 && distance >= InnerRadius - 1) { if (distance > OuterRadius + 1) { return(float.PositiveInfinity); } float signedDistance = distance - m_radius; int face; Vector2 tex; MyCubemapHelpers.CalculateSampleTexcoord(ref position, out face, out tex); Vector3 normal; float value = GetValueForPositionCacheless(face, ref tex, out normal); if (m_detail.Matches(normal.Z)) { float dtx = tex.X * m_detail.Factor; float dty = tex.Y * m_detail.Factor; dtx -= (float)Math.Floor(dtx); dty -= (float)Math.Floor(dty); value += m_detail.GetValue(dtx, dty, normal.Z); } return((signedDistance - value) * normal.Z); } return(-1); }
private byte GetOcclusionForPosition(ref Vector3 localPos, float lodVoxelSize) { int face; Vector2 texcoord; Vector3 localPosition = localPos - m_planetShape.Center(); var distanceToCenter = localPosition.Length(); MyCubemapHelpers.CalculateSampleTexcoord(ref localPosition, out face, out texcoord); Vector3 normal; float sampledHeight = m_planetShape.GetValueForPositionWithCache(face, ref texcoord, out normal); var surfaceDepth = m_planetShape.SignedDistanceWithSample(lodVoxelSize, distanceToCenter, sampledHeight) * normal.Z; if (m_occlusionMap != null && surfaceDepth > -(lodVoxelSize * 1.5f)) { if (m_biomePixelSize < lodVoxelSize) { return(m_occlusionMap[face].GetValue(texcoord.X, texcoord.Y)); } else { return(ComputeMapBlend(texcoord, face, ref m_occlusionBC, m_occlusionMap[face])); } } return(0); }
public Vector3 GetColorShift(Vector3 position, byte material, float maxDepth = 1f) { if (maxDepth >= 1f) { int num; Vector2 vector2; List <PlanetOre> list; MyVoxelMaterialDefinition voxelMaterialDefinition = MyDefinitionManager.Static.GetVoxelMaterialDefinition(material); if ((voxelMaterialDefinition == null) || (voxelMaterialDefinition.ColorKey == null)) { return(Vector3.Zero); } MyCubemapHelpers.CalculateSampleTexcoord(ref position - this.m_planetShape.Center(), out num, out vector2); if (this.m_oreMap == null) { return(Vector3.Zero); } byte key = this.m_oreMap.Faces[num].GetValue(vector2.X, vector2.Y); if (!this.m_ores.TryGetValue(key, out list)) { return(Vector3.Zero); } float num3 = (float)MathHelper.Saturate(this.m_perlin.GetValue((double)(vector2.X * 1000f), (double)(vector2.Y * 1000f), 0.0)); using (List <PlanetOre> .Enumerator enumerator = list.GetEnumerator()) { while (true) { if (!enumerator.MoveNext()) { break; } PlanetOre current = enumerator.Current; float colorInfluence = current.ColorInfluence; colorInfluence = 256f; if ((colorInfluence >= 1f) && ((colorInfluence >= current.Start) && ((current.Start <= maxDepth) && (current.TargetColor != null)))) { Vector3 targetShift = current.TargetColor.Value; Color violet = Color.Violet; if (targetShift == Vector3.Backward) { targetShift = new Vector3(0f, 1f, -0.08f); } else { targetShift = this.m_targetShift; } return((Vector3)((num3 * targetShift) * (1f - (current.Start / colorInfluence)))); } } } } return(Vector3.Zero); }
public double GetDistanceToSurfaceWithCache(Vector3 localPos) { int num2; Vector2 vector; Vector3 vector2; float f = localPos.Length(); if (f.IsZero(0.0001f)) { return(0.0); } MyCubemapHelpers.CalculateSampleTexcoord(ref localPos, out num2, out vector); return((double)(f - (this.GetValueForPositionWithCache(num2, ref vector, out vector2) + this.Radius))); }
public void GetPositionParams(ref Vector3 pos, float lodSize, out MaterialSampleParams ps, bool skipCache = false) { Vector3 localPosition = pos - m_planetShape.Center(); ps.DistanceToCenter = localPosition.Length(); ps.LodSize = lodSize; if (ps.DistanceToCenter < 0.01f) { ps.SurfaceDepth = 0; ps.Gravity = Vector3.Down; ps.Latitude = 0; ps.Longitude = 0; ps.Texcoord = Vector2.One / 2; ps.Face = 0; ps.Normal = Vector3.Backward; ps.SampledHeight = 0; return; } ps.Gravity = localPosition / ps.DistanceToCenter; MyCubemapHelpers.CalculateSampleTexcoord(ref localPosition, out ps.Face, out ps.Texcoord); // this guarantess texcoord in [0,1) if (skipCache) { ps.SampledHeight = m_planetShape.GetValueForPositionCacheless(ps.Face, ref ps.Texcoord, out ps.Normal); } else { ps.SampledHeight = m_planetShape.GetValueForPositionWithCache(ps.Face, ref ps.Texcoord, out ps.Normal); } ps.SurfaceDepth = m_planetShape.SignedDistanceWithSample(lodSize, ps.DistanceToCenter, ps.SampledHeight) * ps.Normal.Z; ps.Latitude = ps.Gravity.Y; Vector2 lon = new Vector2(-ps.Gravity.X, -ps.Gravity.Z); lon.Normalize(); ps.Longitude = lon.Y; if (-ps.Gravity.X > 0) { ps.Longitude = 2 - ps.Longitude; } }
public unsafe void ComputeCombinedMaterialAndSurface(Vector3 position, bool useCache, out MySurfaceParams props) { if (this.Closed) { props = new MySurfaceParams(); } else { MyPlanetMaterialProvider.MaterialSampleParams @params; int num2; Vector2 vector2; position -= this.Shape.Center(); float num = position.Length(); @params.Gravity = position / num; props.Latitude = @params.Gravity.Y; Vector2 vector = new Vector2([email protected], [email protected]); vector.Normalize(); props.Longitude = vector.Y; if ([email protected] > 0f) { props.Longitude = 2f - props.Longitude; } MyCubemapHelpers.CalculateSampleTexcoord(ref position, out num2, out vector2); float altitude = useCache ? this.Shape.GetValueForPositionWithCache(num2, ref vector2, out props.Normal) : this.Shape.GetValueForPositionCacheless(num2, ref vector2, out props.Normal); @params.SampledHeight = altitude; @params.SurfaceDepth = 0f; @params.Texcoord = vector2; @params.LodSize = 1f; @params.Latitude = props.Latitude; @params.Longitude = props.Longitude; @params.Face = num2; @params.Normal = props.Normal; props.Position = (@params.Gravity * (this.Radius + altitude)) + this.Shape.Center(); MyPlanetMaterialProvider.MaterialSampleParams *paramsPtr1 = (MyPlanetMaterialProvider.MaterialSampleParams *) ref @params; props.Gravity = paramsPtr1->Gravity = [email protected]; @params.DistanceToCenter = props.Position.Length(); MyPlanetMaterialProvider.PlanetMaterial layeredMaterialForPosition = this.Material.GetLayeredMaterialForPosition(ref @params, out props.Biome); props.Material = (layeredMaterialForPosition.FirstOrDefault != null) ? layeredMaterialForPosition.FirstOrDefault.Index : 0; props.Normal = @params.Normal; props.HeightRatio = this.Shape.AltitudeToRatio(altitude); } }
/** * Project a position in space to the surface of the heightmap. */ public void ProjectToSurface(Vector3 localPos, out Vector3 surface) { float length = localPos.Length(); if (length.IsZero()) { Debug.Fail("Cannot project to surface from the center of the planet!"); surface = localPos; return; } Vector3 dir = localPos / length; int face; Vector2 tex; MyCubemapHelpers.CalculateSampleTexcoord(ref localPos, out face, out tex); Vector3 norm; float value = GetValueForPositionCacheless(face, ref tex, out norm); value += Radius; surface = value * dir; }