本文整理汇总了C#中Microsoft.Xna.Framework.Ray类的典型用法代码示例。如果您正苦于以下问题:C# Ray类的具体用法?C# Ray怎么用?C# Ray使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
Ray类属于Microsoft.Xna.Framework命名空间,在下文中一共展示了Ray类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: getMouseWorldPosition
/// <summary>
/// Checks the mouse's position set in the in-game world plane.
/// </summary>
/// <param name="mousePosition">Mouse's position on screen</param>
/// <param name="camera">Camera object</param>
/// <param name="device">Graphics device used in rendering</param>
/// <returns></returns>
public static Vector3 getMouseWorldPosition(Vector2 mousePosition,CameraAndLights camera,GraphicsDevice device)
{
Vector3 nearsource = new Vector3(mousePosition,0f);
Vector3 farsource = new Vector3(mousePosition,CameraAndLights.nearClip);
Vector3 nearPoint = device.Viewport.Unproject(nearsource,
camera.projectionMatrix,
camera.viewMatrix,
Matrix.Identity);
Vector3 farPoint = device.Viewport.Unproject(farsource,
camera.projectionMatrix,
camera.viewMatrix,
Matrix.Identity);
// Create a ray from the near clip plane to the far clip plane.
Vector3 direction = farPoint - nearPoint;
direction.Normalize();
Ray pickRay = new Ray(nearPoint,direction);
Plane floor = new Plane(new Vector3(0f,1f,0f),0f);
float denominator = Vector3.Dot(floor.Normal,pickRay.Direction);
float numerator = Vector3.Dot(floor.Normal,pickRay.Position) + floor.D;
float dist = -(numerator / denominator);
Vector3 mouseWorldPos = nearPoint + direction * dist;
return mouseWorldPos * new Vector3(1f,0f,1f);
}
示例2: CalculateLocalMouse
public override void CalculateLocalMouse(Ray MouseRay, Action<Gem.Render.SceneNode, float> HoverCallback)
{
MouseHover = false;
if (InteractWithMouse == false) return;
var localMouse = GetLocalMouseRay(MouseRay);
var intersection = Mesh.RayIntersection(localMouse);
if (intersection.Intersects)
{
LocalMouse = Vector2.Transform(intersection.UV, UVTransform);
if (AlphaMouse)
{
var pixel = new Color[] { new Color(1.0f, 1.0f, 1.0f, 1.0f) };
var pX = (int)System.Math.Round(LocalMouse.X * Texture.Width);
var pY = (int)System.Math.Round(LocalMouse.Y * Texture.Height);
Texture.GetData<Color>(0, new Rectangle(pX, pY, 1, 1), pixel, 0, 1);
if (pixel[0].A > 0.01f)
HoverCallback(this, intersection.Distance);
}
else
HoverCallback(this, intersection.Distance);
}
}
示例3: GetIntersectingModel
/// <summary>
/// Tests all models in the scene graph for intersection with the relative mouse coordinates.
/// </summary>
/// <returns>The ModelNode object that claims to be hit by the mouse</returns>
/// <remarks>Note that currently this algorithm uses bounding spheres, so for certain objects it will be rather inaccurate.</remarks>
public ModelNode GetIntersectingModel(Matrix projectionMatrix, Matrix viewMatrix, Matrix worldMatrix, GraphicsDevice graphicsDevice, Vector2 mousePosition)
{
// Create 2 positions in screenspace near and far.
Vector3 nearSource = new Vector3(mousePosition, 0f);
Vector3 farSource = new Vector3(mousePosition, 1f);
// Get equivalent positions in world space.
Vector3 nearPoint = graphicsDevice.Viewport.Unproject(nearSource, projectionMatrix, viewMatrix, worldMatrix);
Vector3 farPoint = graphicsDevice.Viewport.Unproject(farSource, projectionMatrix, viewMatrix, worldMatrix);
// Find direction for cursor ray.
Vector3 direction = farPoint - nearPoint;
direction.Normalize();
// and then create a new ray using nearPoint as the source.
Ray cursorRay = new Ray(nearPoint, direction);
foreach (ModelNode modelNode in ModelGraph)
{
ModelNode modelToReturn = modelNode.GetIntersectingModel(cursorRay);
if (modelToReturn != null)
return modelToReturn;
}
return null;
}
示例4: shading
public Vector3 shading(Vector3 pos, Vector3 norm, Ray ray, Actor actor, bool isShade)
{
// 点光源
var l = light - pos;
var l2 = l.LengthSquared();
l.Normalize();
// 視線
var dir = ray.Position - pos;
dir.Normalize();
// ハーフベクトル
var h = dir + l;
h.Normalize();
var ln = Vector3.Dot(l, norm);
var hn = Vector3.Dot(h, norm);
if (ln < 0) ln = 0;
if (hn < 0) hn = 0;
var dst = actor.GetColor(isShade ? 0.0f : ln, hn, pos);
// 光源の色の反映
dst.X *= lightColor.X;
dst.Y *= lightColor.Y;
dst.Z *= lightColor.Z;
// 光の強さの適当な補正
//dst *= Math.Min(1.5f, 500000.0f / (10000.0f + l2));
//dst *= Math.Min(1.0f, l.Y + 0.1f);
return dst;
}
示例5: Setup
public void Setup()
{
r1 = new Ray();
r2 = new Ray(new Vector3(1, 2, 3), new Vector3(4, 5, 6));
r3 = new Ray(new Vector3(2.25f, 3.754321f, 4.387f), new Vector3(-1, -1, -1));
r4 = new Ray(new Vector3(1, 1, 1), new Vector3(1, 1, 1));
}
示例6: CalculateLocalMouse
public override void CalculateLocalMouse(Ray MouseRay, Action<Gem.Render.SceneNode, float> HoverCallback)
{
MouseHover = false;
if (InteractWithMouse == false) return;
MouseRay.Direction = Vector3.Normalize(MouseRay.Direction);
var inverseTransform = Matrix.Invert(Orientation.Transform);
var localMouseSource = Vector3.Transform(MouseRay.Position, inverseTransform);
var forwardPoint = MouseRay.Position + MouseRay.Direction;
forwardPoint = Vector3.Transform(forwardPoint, inverseTransform);
var localMouse = new Ray(localMouseSource, forwardPoint - localMouseSource);
var intersection = Mesh.RayIntersection(localMouse);
if (intersection.Intersects)
{
LocalMouse = Vector2.Transform(intersection.UV, UVTransform);
if (AlphaMouse)
{
var pixel = new Color[] { new Color(1.0f, 1.0f, 1.0f, 1.0f) };
var pX = (int)System.Math.Round(LocalMouse.X * Texture.Width);
var pY = (int)System.Math.Round(LocalMouse.Y * Texture.Height);
Texture.GetData<Color>(0, new Rectangle(pX, pY, 1, 1), pixel, 0, 1);
if (pixel[0].A > 0.01f)
HoverCallback(this, intersection.Distance);
}
else
HoverCallback(this, intersection.Distance);
}
}
示例7: IntersectionInfo
public IntersectionInfo(float distance, Ray originalRay, Vector3 baryCoord, Geomertry geomertry)
{
Distance = distance;
BaryCoord = baryCoord;
OriginalRay = originalRay;
Geomertry = geomertry;
}
示例8: GetCollisionPosition
public Vector3? GetCollisionPosition()
{
MouseState mouseState = Mouse.GetState();
Vector3 nearSource = new Vector3(mouseState.X, mouseState.Y, 0f);
Vector3 farSource = new Vector3(mouseState.X, mouseState.Y, 1f);
Vector3 nearPoint = device.Viewport.Unproject(
nearSource,
camera.projection,
camera.view,
Matrix.Identity);
Vector3 farPoint = device.Viewport.Unproject(
farSource,
camera.projection,
camera.view,
Matrix.Identity);
Vector3 direction = farPoint - nearPoint;
direction.Normalize();
Ray pickRay = new Ray(nearPoint, direction);
Nullable<float> result = pickRay.Intersects(new Plane(Vector3.Up, 0f));
Vector3? resultVector = direction * result;
Vector3? collisionPoint = resultVector + nearPoint;
return collisionPoint;
}
示例9: DetermineSide
public Face DetermineSide(BoundingBox box, Ray ray)
{
Face selectedFace = Face.NONE;
float closestDist = float.MaxValue;
BoundingBox[] sides =
{
new BoundingBox(new Vector3(box.Min.X, box.Min.Y, box.Min.Z), new Vector3(box.Min.X, box.Max.Y, box.Max.Z)), //-x LEFT
new BoundingBox(new Vector3(box.Max.X, box.Min.Y, box.Min.Z), new Vector3(box.Max.X, box.Max.Y, box.Max.Z)), //+x RIGHT
new BoundingBox(new Vector3(box.Min.X, box.Min.Y, box.Min.Z), new Vector3(box.Max.X, box.Min.Y, box.Max.Z)), //-y TOP
new BoundingBox(new Vector3(box.Min.X, box.Max.Y, box.Min.Z), new Vector3(box.Max.X, box.Max.Y, box.Max.Z)), //+y BOTTOM
new BoundingBox(new Vector3(box.Min.X, box.Min.Y, box.Min.Z), new Vector3(box.Max.X, box.Max.Y, box.Min.Z)), //-z BACK
new BoundingBox(new Vector3(box.Min.X, box.Min.Y, box.Max.Z), new Vector3(box.Max.X, box.Max.Y, box.Max.Z)) //+z FRONT
};
for (int i = 0; i < sides.Length; ++i)
{
float? d = ray.Intersects(sides[i]);
if (d.HasValue)
{
if (d.Value < closestDist)
{
closestDist = d.Value;
selectedFace = (Face)i;
}
}
}
return selectedFace;
}
示例10: GetSourisBoxIntercept
public Monstre GetSourisBoxIntercept(List<Monstre> monstres)
{
Vector3 nearScreenPoint = new Vector3(ÉtatSouris.X, ÉtatSouris.Y, 0);
Vector3 farScreenPoint = new Vector3(ÉtatSouris.X, ÉtatSouris.Y, 1);
Vector3 nearWorldPoint = Jeu.PériphériqueGraphique.GraphicsDevice.Viewport.Unproject(nearScreenPoint, ScèneJeu.CaméraJeu.Projection, ScèneJeu.CaméraJeu.Vue, Matrix.Identity);
Vector3 farWorldPoint = Jeu.PériphériqueGraphique.GraphicsDevice.Viewport.Unproject(farScreenPoint, ScèneJeu.CaméraJeu.Projection, ScèneJeu.CaméraJeu.Vue, Matrix.Identity);
Vector3 direction = farWorldPoint - nearWorldPoint;
Ray raySouris = new Ray(nearWorldPoint, direction);
float distancemin = float.MaxValue;
float? distance;
Monstre cible = null;
foreach (Monstre monstre in monstres)
{
foreach (BoundingBox box in monstre.BoxList)
{
distance = raySouris.Intersects(box);
if (distance != null && distance < distancemin)
{
distancemin = (float)distance;
cible = monstre;
}
}
}
return cible;
}
示例11: FindCliff
/// <summary>
/// Determines whether there is a cliff nearby.
/// </summary>
/// <param name="position">Position to look from.</param>
/// <param name="facingDirection">Direction to check in.</param>
/// <param name="filter">Anonymous function to filter out unwanted objects.</param>
/// <param name="space">The space to check for a cliff in.</param>
/// <param name="distance">The distance to check at.</param>
/// <returns>True if a cliff was detected, false otherwise.</returns>
public static bool FindCliff(Vector3 position, Vector3 facingDirection, Func<BroadPhaseEntry, bool> filter, Space space, float distance)
{
// If there is a wall before the requested distance assume there is no cliff.
Ray forwardRay = new Ray(position, new Vector3(facingDirection.X, 0, facingDirection.Z));
RayCastResult forwardResult = new RayCastResult();
space.RayCast(forwardRay, filter, out forwardResult);
if ((forwardResult.HitData.Location - position).Length() < distance)
{
return false;
}
facingDirection.Normalize();
Ray futureDownRay = new Ray(position + new Vector3(facingDirection.X * distance, 0, facingDirection.Z * distance), Vector3.Down);
RayCastResult result = new RayCastResult();
space.RayCast(futureDownRay, filter, out result);
Vector3 drop = result.HitData.Location - futureDownRay.Position;
if (drop.Y < -6.0f)
{
return true;
}
else
{
return false;
}
}
示例12: canSee
public bool canSee()
{
if ((EntityManager.Instance.GetPlayer() as Rabbit).IsHidden)
return false;
Vector2 vecToRabbit = EntityManager.Instance.GetPlayer().Position - agent.Position;
if (vecToRabbit.LengthSquared() < rangeSqrd)
{
vecToRabbit.Normalize();
float relativeAngle = (float)Angles.AngleFromUToV(agent.Heading, vecToRabbit);
if (relativeAngle < angleWidth && relativeAngle > angleWidth * -1)
{
foreach (Wall wall in WallManager.Instance.Walls)
{
float? inter = new Ray(new Vector3(agent.Position, 0),
new Vector3(vecToRabbit, 0)).Intersects(
new BoundingBox(new Vector3(wall.BoundingBox.Left,wall.BoundingBox.Top,-10),
new Vector3(wall.BoundingBox.Right,wall.BoundingBox.Bottom,10)
));
if (inter != null && (float)inter
< range)
return false;
}
return true;
}
}
return false;
}
示例13: GenerateRect
private void GenerateRect()
{
int minX = int.MaxValue;
int minZ = int.MaxValue;
int maxX = int.MinValue;
int maxZ = int.MinValue;
var corners = Frustum.GetCorners();
Plane plane = new Plane(new Vector4(0, 1, 0, 0));
for (int i = 4; i < corners.Length; i++)
{
Ray ray = new Ray(corners[i - 4], corners[i]);
var distance = ray.Intersects(plane);
if (distance.HasValue)
{
var pos2 = ray.Position + ray.Direction * distance.Value;
if ((int)pos2.X > maxX)
maxX = (int)pos2.X;
if ((int)pos2.X < minX)
minX = (int)pos2.X;
if ((int)pos2.Z > maxZ)
maxZ = (int)pos2.Z;
if ((int)pos2.Z < minZ)
minZ = (int)pos2.Z;
}
// Console.WriteLine("distance[{0}]: {1} pos2:{2}", i, distance, pos2.ToString());
}
quadRect = new Rectangle(minX, minZ, Math.Abs(maxX - minX), Math.Abs(maxZ - minZ));
// Console.WriteLine("X: {0},{1} Z:{2},{3}", minX, maxX, minZ, maxZ);
}
示例14: Intersects
// This selects a particular tile given a ray from the camera
public Objects.Tile Intersects(Ray ray)
{
// It's possible that multiple tiles intersect, so we need to create a list
// of potential tiles to select.
LinkedList<Objects.Tile> possibles = new LinkedList<Objects.Tile>();
foreach (Objects.Tile t in m_Tiles)
{
if (t.AABB.Intersects(ray) != null)
{
possibles.AddLast(t);
}
}
// Now select the tile that is the closest to the start position of the ray
Objects.Tile retval = null;
float fBestDist = 999999999.0f;
foreach (Objects.Tile t in possibles)
{
float fDist = Vector3.DistanceSquared(t.Position, ray.Position);
if (fDist < fBestDist)
{
retval = t;
fBestDist = fDist;
}
}
return retval;
}
示例15: GetPickables
public static List<IPickable> GetPickables(Point a_clickPoint)
{
var camera = CameraManager.ActiveCamera;
Vector3 nearSource = camera.Viewport.Unproject(new Vector3(a_clickPoint.X, a_clickPoint.Y, camera.Viewport.MinDepth), camera.Projection, camera.View, Matrix.Identity);
Vector3 farSource = camera.Viewport.Unproject(new Vector3(a_clickPoint.X, a_clickPoint.Y, camera.Viewport.MaxDepth), camera.Projection, camera.View, Matrix.Identity);
Vector3 direction = farSource - nearSource;
direction.Normalize();
var ray = new Ray(nearSource, direction);
var pickables = new List<IPickable>();
var rays = new List<Ray>();
var map = new Dictionary<float?, IPickable>();
foreach (var pickable in ComponentManager.Pickables.Values)
{
BoundingBox boundingBox = pickable.GetBoundingBox();
float? distance;
ray.Intersects(ref boundingBox, out distance);
if (distance != null)
{
map[distance] = pickable;
pickables.Add(pickable);
}
}
return pickables;
}