本文整理汇总了C#中RenderContext.Validate方法的典型用法代码示例。如果您正苦于以下问题:C# RenderContext.Validate方法的具体用法?C# RenderContext.Validate怎么用?C# RenderContext.Validate使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类RenderContext
的用法示例。
在下文中一共展示了RenderContext.Validate方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Render
/// <summary>
/// Clears the current render target (which must be the G-buffer).
/// </summary>
/// <param name="context">The render context.</param>
public void Render(RenderContext context)
{
if (context == null)
throw new ArgumentNullException("context");
context.Validate(_effect);
var graphicsDevice = _effect.GraphicsDevice;
var savedRenderState = new RenderStateSnapshot(graphicsDevice);
graphicsDevice.DepthStencilState = DepthStencilState.None;
graphicsDevice.RasterizerState = RasterizerState.CullNone;
graphicsDevice.BlendState = BlendState.Opaque;
// Clear to maximum depth.
_parameterDepth.SetValue(1.0f);
// The environment is facing the camera.
// --> Set normal = cameraBackward.
var cameraNode = context.CameraNode;
_parameterNormal.SetValue((cameraNode != null) ? (Vector3)cameraNode.ViewInverse.GetColumn(2).XYZ : Vector3.Backward);
// Clear specular to arbitrary value.
_parameterSpecularPower.SetValue(1.0f);
_effect.CurrentTechnique.Passes[0].Apply();
// Draw full-screen quad using clip space coordinates.
graphicsDevice.DrawQuad(
new VertexPositionTexture(new Vector3(-1, 1, 0), new Vector2(0, 0)),
new VertexPositionTexture(new Vector3(1, -1, 0), new Vector2(1, 1)));
savedRenderState.Restore();
}
示例2: Render
/// <summary>
/// Draws the texts.
/// </summary>
/// <param name="context">The render context.</param>
/// <remarks>
/// If <see cref="SpriteBatch"/> or <see cref="SpriteFont"/> are <see langword="null"/>, then
/// <see cref="Render"/> does nothing.
/// </remarks>
/// <exception cref="ArgumentNullException">
/// <paramref name="context"/> is <see langword="null"/>.
/// </exception>
public void Render(RenderContext context)
{
if (context == null)
throw new ArgumentNullException("context");
if (SpriteBatch == null || SpriteFont == null)
return;
context.Validate(SpriteBatch);
if (_texts2D.Count == 0 && _texts3D.Count == 0)
return;
if (_texts3D.Count > 0)
context.ThrowIfCameraMissing();
var savedRenderState = new RenderStateSnapshot(SpriteBatch.GraphicsDevice);
if (EnableDepthTest)
{
SpriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend, SamplerState.LinearClamp, DepthStencilState.Default, RasterizerState.CullNone);
}
else
{
SpriteBatch.Begin();
}
// ----- Draw world space text.
if (_texts3D.Count > 0)
{
CameraNode cameraNode = context.CameraNode;
Matrix44F viewProjection = cameraNode.Camera.Projection * cameraNode.View;
Viewport viewport = SpriteBatch.GraphicsDevice.Viewport;
foreach (var textInfo in _texts3D)
{
// Transform position from world space to the viewport.
Vector3F pos = viewport.ProjectToViewport(textInfo.Position, viewProjection);
if (pos.Z < 0 || pos.Z > 1)
continue;
// Snap to pixels. Also add a small bias in one direction because when we draw text at
// certain positions (e.g. view space origin) and the presentation target width is an
// odd number, the pos will be exactly at pixel centers and due to numerical errors it
// would jitter between pixels if the camera moves slightly.
pos.X = (float)Math.Round(pos.X + 0.01f);
pos.Y = (float)Math.Round(pos.Y + 0.01f);
var textAsString = textInfo.Text as string;
if (!string.IsNullOrEmpty(textAsString))
{
var textOrigin = GetOrigin(textAsString, textInfo.RelativeOrigin);
SpriteBatch.DrawString(SpriteFont, textAsString, new Vector2(pos.X, pos.Y), textInfo.Color, 0, textOrigin, 1.0f, SpriteEffects.None, pos.Z);
}
else
{
var textAsStringBuilder = textInfo.Text as StringBuilder;
if (textAsStringBuilder != null && textAsStringBuilder.Length > 0)
{
var textOrigin = GetOrigin(textAsStringBuilder, textInfo.RelativeOrigin);
SpriteBatch.DrawString(SpriteFont, textAsStringBuilder, new Vector2(pos.X, pos.Y), textInfo.Color, 0, textOrigin, 1, SpriteEffects.None, pos.Z);
}
}
}
}
// ----- Draw screen space text.
foreach (var textInfo in _texts2D)
{
var textAsString = textInfo.Text as string;
if (!string.IsNullOrEmpty(textAsString))
{
var textOrigin = GetOrigin(textAsString, textInfo.RelativeOrigin);
SpriteBatch.DrawString(SpriteFont, textAsString, (Vector2)textInfo.Position, textInfo.Color, 0, textOrigin, 1, SpriteEffects.None, 0);
}
else
{
var textAsStringBuilder = textInfo.Text as StringBuilder;
if (textAsStringBuilder != null && textAsStringBuilder.Length > 0)
{
var textOrigin = GetOrigin(textAsStringBuilder, textInfo.RelativeOrigin);
SpriteBatch.DrawString(SpriteFont, textAsStringBuilder, (Vector2)textInfo.Position, textInfo.Color, 0, textOrigin, 1, SpriteEffects.None, 0);
}
}
}
SpriteBatch.End();
savedRenderState.Restore();
//.........这里部分代码省略.........
示例3: Render
public override void Render(IList<SceneNode> nodes, RenderContext context, RenderOrder order)
{
ThrowIfDisposed();
if (nodes == null)
throw new ArgumentNullException("nodes");
if (context == null)
throw new ArgumentNullException("context");
context.Validate(_effect);
context.ThrowIfCameraMissing();
context.ThrowIfGBuffer0Missing();
// Fog is not used in all games. --> Early out, if possible.
int numberOfNodes = nodes.Count;
if (nodes.Count == 0)
return;
if (nodes.Count > 1)
{
// Get a sorted list of all fog nodes.
if (_fogNodes == null)
_fogNodes = new List<SceneNode>();
_fogNodes.Clear();
for (int i = 0; i < numberOfNodes; i++)
{
var node = nodes[i] as FogNode;
if (node != null)
{
_fogNodes.Add(node);
node.SortTag = node.Priority;
}
}
// Sort ascending. (Fog with lower priority is rendered first.)
// Note: Since this list is a list of SceneNodes, we use the AscendingNodeComparer
// instead of the AscendingFogNodeComparer. The Priority was written to the SortTag,
// so this will work.
_fogNodes.Sort(AscendingNodeComparer.Instance);
nodes = _fogNodes;
numberOfNodes = _fogNodes.Count;
}
var graphicsDevice = _effect.GraphicsDevice;
var savedRenderState = new RenderStateSnapshot(graphicsDevice);
graphicsDevice.RasterizerState = RasterizerState.CullNone;
graphicsDevice.DepthStencilState = DepthStencilState.None;
graphicsDevice.BlendState = BlendState.AlphaBlend;
var viewport = graphicsDevice.Viewport;
_parameterViewportSize.SetValue(new Vector2(viewport.Width, viewport.Height));
var cameraNode = context.CameraNode;
var cameraPose = cameraNode.PoseWorld;
GraphicsHelper.GetFrustumFarCorners(cameraNode.Camera.Projection, _cameraFrustumFarCorners);
// Convert frustum far corners from view space to world space.
for (int i = 0; i < _cameraFrustumFarCorners.Length; i++)
_cameraFrustumFarCorners[i] = (Vector3)cameraPose.ToWorldDirection((Vector3F)_cameraFrustumFarCorners[i]);
_parameterFrustumCorners.SetValue(_cameraFrustumFarCorners);
_parameterGBuffer0.SetValue(context.GBuffer0);
// Update SceneNode.LastFrame for all visible nodes.
int frame = context.Frame;
cameraNode.LastFrame = frame;
bool directionalLightIsSet = false;
float scatteringSymmetryStrength = 1;
for (int i = 0; i < numberOfNodes; i++)
{
var node = nodes[i] as FogNode;
if (node == null)
continue;
// FogNode is visible in current frame.
node.LastFrame = frame;
var fog = node.Fog;
if (fog.Density <= Numeric.EpsilonF)
continue;
// Compute actual density and falloff.
float fogDensity = fog.Density;
float heightFalloff = fog.HeightFalloff;
// In previous versions, we gave FogDensity * 2^(-h*y) to the effect. Following code
// avoids numerical problems where this value is numerically 0. This is now handled
// in the shader.
//if (!Numeric.IsZero(heightFalloff))
//{
// float cameraDensity = (float)Math.Pow(2, -heightFalloff * cameraPose.Position.Y);
// // Trick: If the heightFalloff is very large, the e^x function can quickly reach
// // the float limit! If this happens, the shader will not compute any fog and this
// // looks like the fog disappears. To avoid this problem we reduce the heightFalloff
// // to keep the result of e^x always within floating point range.
// const float Limit = 1e-37f;
// if (cameraDensity < Limit)
//.........这里部分代码省略.........
示例4: Render
private void Render(RenderContext context, Vector4F color, Texture2D colorTexture, bool preserveColor)
{
if (context == null)
throw new ArgumentNullException("context");
context.Validate(_effect);
context.ThrowIfCameraMissing();
context.ThrowIfGBuffer0Missing();
var graphicsDevice = _effect.GraphicsDevice;
var savedRenderState = new RenderStateSnapshot(graphicsDevice);
graphicsDevice.DepthStencilState = GraphicsHelper.DepthStencilStateAlways;
graphicsDevice.RasterizerState = RasterizerState.CullNone;
if (preserveColor)
graphicsDevice.BlendState = GraphicsHelper.BlendStateNoColorWrite;
else
graphicsDevice.BlendState = BlendState.Opaque;
if (colorTexture != null)
{
if (TextureHelper.IsFloatingPointFormat(colorTexture.Format))
graphicsDevice.SamplerStates[1] = SamplerState.PointClamp;
else
graphicsDevice.SamplerStates[1] = SamplerState.LinearClamp;
}
var projection = context.CameraNode.Camera.Projection;
bool isPerspective = projection is PerspectiveProjection;
float near = projection.Near * NearBias;
float far = projection.Far * FarBias;
var biasedProjection = isPerspective
? Matrix44F.CreatePerspectiveOffCenter(
projection.Left, projection.Right,
projection.Bottom, projection.Top,
near, far)
: Matrix44F.CreateOrthographicOffCenter(
projection.Left, projection.Right,
projection.Bottom, projection.Top,
near, far);
var viewport = graphicsDevice.Viewport;
_parameterViewportSize.SetValue(new Vector2(viewport.Width, viewport.Height));
_parameterProjection.SetValue((Matrix)biasedProjection);
_parameterCameraFar.SetValue(projection.Far);
_parameterGBuffer0.SetValue(context.GBuffer0);
_parameterColor.SetValue((Vector4)color);
_parameterSourceTexture.SetValue(colorTexture);
_effect.CurrentTechnique = isPerspective ? _techniquePerspective : _techniqueOrthographic;
_effect.CurrentTechnique.Passes[(colorTexture == null) ? 0 : 1].Apply();
graphicsDevice.DrawFullScreenQuad();
graphicsDevice.ResetTextures();
savedRenderState.Restore();
}
示例5: Render
/// <summary>
/// Draws the textures.
/// </summary>
/// <param name="context">The render context.</param>
/// <remarks>
/// If <see cref="SpriteBatch"/> is <see langword="null"/>, then <see cref="Render"/> does
/// nothing.
/// </remarks>
/// <exception cref="ArgumentNullException">
/// <paramref name="context"/> is <see langword="null"/>.
/// </exception>
public void Render(RenderContext context)
{
if (context == null)
throw new ArgumentNullException("context");
if (SpriteBatch == null)
return;
var count = _textures.Count;
if (count == 0)
return;
context.Validate(SpriteBatch);
var savedRenderState = new RenderStateSnapshot(SpriteBatch.GraphicsDevice);
SpriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend, SamplerState.LinearClamp, DepthStencilState.None, RasterizerState.CullNone);
for (int i = 0; i < count; i++)
{
var textureInfo = _textures[i];
if (textureInfo.Texture.IsDisposed)
continue;
if (TextureHelper.IsFloatingPointFormat(textureInfo.Texture.Format))
{
// Floating-point textures must not use linear hardware filtering!
SpriteBatch.GraphicsDevice.SamplerStates[0] = SamplerState.PointClamp;
SpriteBatch.Draw(textureInfo.Texture, textureInfo.Rectangle, Color.White);
SpriteBatch.GraphicsDevice.SamplerStates[0] = SamplerState.LinearClamp;
}
else
{
SpriteBatch.Draw(textureInfo.Texture, textureInfo.Rectangle, Color.White);
}
}
SpriteBatch.End();
savedRenderState.Restore();
}
示例6: Render
public override void Render(IList<SceneNode> nodes, RenderContext context, RenderOrder order)
{
if (nodes == null)
throw new ArgumentNullException("nodes");
if (context == null)
throw new ArgumentNullException("context");
int numberOfNodes = nodes.Count;
if (numberOfNodes == 0)
return;
context.Validate(_effect);
context.ThrowIfCameraMissing();
var graphicsDevice = _effect.GraphicsDevice;
var savedRenderState = new RenderStateSnapshot(graphicsDevice);
graphicsDevice.DepthStencilState = DepthStencilState.None;
graphicsDevice.RasterizerState = RasterizerState.CullNone;
graphicsDevice.BlendState = GraphicsHelper.BlendStateAdd;
var viewport = graphicsDevice.Viewport;
_parameterViewportSize.SetValue(new Vector2(viewport.Width, viewport.Height));
_parameterGBuffer0.SetValue(context.GBuffer0);
_parameterGBuffer1.SetValue(context.GBuffer1);
var cameraNode = context.CameraNode;
var cameraPose = cameraNode.PoseWorld;
Matrix viewProjection = (Matrix)cameraNode.View * cameraNode.Camera.Projection;
// Update SceneNode.LastFrame for all visible nodes.
int frame = context.Frame;
cameraNode.LastFrame = frame;
var isHdrEnabled = context.IsHdrEnabled();
for (int i = 0; i < numberOfNodes; i++)
{
var lightNode = nodes[i] as LightNode;
if (lightNode == null)
continue;
var light = lightNode.Light as ProjectorLight;
if (light == null)
continue;
// LightNode is visible in current frame.
lightNode.LastFrame = frame;
float hdrScale = isHdrEnabled ? light.HdrScale : 1;
_parameterDiffuseColor.SetValue((Vector3)light.Color * light.DiffuseIntensity * hdrScale);
_parameterSpecularColor.SetValue((Vector3)light.Color * light.SpecularIntensity * hdrScale);
_parameterTexture.SetValue(light.Texture);
var lightPose = lightNode.PoseWorld;
_parameterPosition.SetValue((Vector3)(lightPose.Position - cameraPose.Position));
_parameterRange.SetValue(light.Projection.Far);
_parameterAttenuation.SetValue(light.Attenuation);
_parameterTextureMatrix.SetValue((Matrix)(GraphicsHelper.ProjectorBiasMatrix * light.Projection * (lightPose.Inverse * new Pose(cameraPose.Position))));
var rectangle = GraphicsHelper.GetViewportRectangle(cameraNode, viewport, lightNode);
var texCoordTopLeft = new Vector2F(rectangle.Left / (float)viewport.Width, rectangle.Top / (float)viewport.Height);
var texCoordBottomRight = new Vector2F(rectangle.Right / (float)viewport.Width, rectangle.Bottom / (float)viewport.Height);
GraphicsHelper.GetFrustumFarCorners(cameraNode.Camera.Projection, texCoordTopLeft, texCoordBottomRight, _frustumFarCorners);
// Convert frustum far corners from view space to world space.
for (int j = 0; j < _frustumFarCorners.Length; j++)
_frustumFarCorners[j] = (Vector3)cameraPose.ToWorldDirection((Vector3F)_frustumFarCorners[j]);
_parameterFrustumCorners.SetValue(_frustumFarCorners);
bool hasShadow = (lightNode.Shadow != null && lightNode.Shadow.ShadowMask != null);
if (hasShadow)
{
switch (lightNode.Shadow.ShadowMaskChannel)
{
case 0: _parameterShadowMaskChannel.SetValue(new Vector4(1, 0, 0, 0)); break;
case 1: _parameterShadowMaskChannel.SetValue(new Vector4(0, 1, 0, 0)); break;
case 2: _parameterShadowMaskChannel.SetValue(new Vector4(0, 0, 1, 0)); break;
default: _parameterShadowMaskChannel.SetValue(new Vector4(0, 0, 0, 1)); break;
}
_parameterShadowMask.SetValue(lightNode.Shadow.ShadowMask);
}
if (lightNode.Clip != null)
{
var data = lightNode.RenderData as LightRenderData;
if (data == null)
{
data = new LightRenderData();
lightNode.RenderData = data;
}
data.UpdateClipSubmesh(context.GraphicsService, lightNode);
graphicsDevice.DepthStencilState = GraphicsHelper.DepthStencilStateOnePassStencilFail;
graphicsDevice.BlendState = GraphicsHelper.BlendStateNoColorWrite;
_parameterWorldViewProjection.SetValue((Matrix)data.ClipMatrix * viewProjection);
_passClip.Apply();
//.........这里部分代码省略.........
示例7: Render
public override void Render(IList<SceneNode> nodes, RenderContext context, RenderOrder order)
{
ThrowIfDisposed();
if (nodes == null)
throw new ArgumentNullException("nodes");
if (context == null)
throw new ArgumentNullException("context");
int numberOfNodes = nodes.Count;
if (nodes.Count == 0)
return;
context.Validate(_effect);
context.ThrowIfCameraMissing();
var graphicsDevice = context.GraphicsService.GraphicsDevice;
var savedRenderState = new RenderStateSnapshot(graphicsDevice);
graphicsDevice.BlendState = BlendState.AlphaBlend;
graphicsDevice.RasterizerState = RasterizerState.CullNone;
graphicsDevice.DepthStencilState = DepthStencilState.DepthRead;
// Camera properties
var cameraNode = context.CameraNode;
Matrix view = (Matrix)new Matrix44F(cameraNode.PoseWorld.Orientation.Transposed, new Vector3F());
_parameterView.SetValue(view);
Matrix projection = cameraNode.Camera.Projection;
_parameterProjection.SetValue(projection);
// Update SceneNode.LastFrame for all visible nodes.
int frame = context.Frame;
cameraNode.LastFrame = frame;
for (int i = 0; i < numberOfNodes; i++)
{
var node = nodes[i] as GradientTextureSkyNode;
if (node == null)
continue;
// GradientTextureSkyNode is visible in current frame.
node.LastFrame = frame;
_parameterSunDirection.SetValue((Vector3)node.SunDirection);
_parameterTime.SetValue((float)node.TimeOfDay.TotalHours / 24);
_parameterColor.SetValue((Vector4)node.Color);
_parameterFrontTexture.SetValue(node.FrontTexture);
_parameterBackTexture.SetValue(node.BackTexture);
if (node.CieSkyStrength < Numeric.EpsilonF)
{
if (context.IsHdrEnabled())
_passLinear.Apply();
else
_passGamma.Apply();
}
else
{
var p = node.CieSkyParameters;
_parameterAbcd.SetValue(new Vector4(p.A, p.B, p.C, p.D));
_parameterEAndStrength.SetValue(new Vector2(p.E, node.CieSkyStrength));
if (context.IsHdrEnabled())
_passCieLinear.Apply();
else
_passCieGamma.Apply();
}
_submesh.Draw();
}
savedRenderState.Restore();
}
示例8: Render
/// <summary>
/// Draws the points.
/// </summary>
/// <param name="context">The render context.</param>
/// <remarks>
/// If <see cref="Effect"/> is <see langword="null"/>, then <see cref="Render"/> does nothing.
/// </remarks>
/// <exception cref="ArgumentNullException">
/// <paramref name="context"/> is <see langword="null"/>.
/// </exception>
public void Render(RenderContext context)
{
if (context == null)
throw new ArgumentNullException("context");
if (Effect == null)
return;
int numberOfPoints = _points.Count;
if (numberOfPoints <= 0)
return;
context.Validate(Effect);
context.ThrowIfCameraMissing();
// Render state.
var graphicsDevice = context.GraphicsService.GraphicsDevice;
var savedRenderState = new RenderStateSnapshot(graphicsDevice);
graphicsDevice.RasterizerState = RasterizerState.CullNone;
// Reset the texture stages. If a floating point texture is set, we get exceptions
// when a sampler with bilinear filtering is set.
graphicsDevice.ResetTextures();
// Effect parameters.
Effect.Alpha = 1;
Effect.DiffuseColor = new Vector3(1, 1, 1);
Effect.LightingEnabled = false;
Effect.TextureEnabled = false;
Effect.VertexColorEnabled = true;
Effect.World = Matrix.Identity;
Effect.View = Matrix.Identity;
Effect.Projection = Matrix.Identity;
Effect.CurrentTechnique.Passes[0].Apply();
// Get WorldViewProjection matrix.
Matrix view = (Matrix)context.CameraNode.View;
Matrix projection = context.CameraNode.Camera.Projection;
Matrix wvp = Matrix.Multiply(view, projection);
// The x and y point size relative to the viewport.
Viewport viewport = graphicsDevice.Viewport;
float sizeX = PointSize / viewport.Width;
float sizeY = sizeX * viewport.Width / viewport.Height;
// Resize buffer if necessary.
ResizeBuffer(graphicsDevice, numberOfPoints);
// Submit points. The loop is only needed if we have more points than can
// be submitted with one draw call.
var startPointIndex = 0;
while (startPointIndex < numberOfPoints)
{
// Number of points in this batch.
int pointsPerBatch = Math.Min(numberOfPoints - startPointIndex, _buffer.Length / 6);
// Create vertices for points. All positions are directly in clip space!
for (int i = 0; i < pointsPerBatch; i++)
{
var point = _points[startPointIndex + i];
// Transform point position to clip space.
Vector3 positionWorld = (Vector3)point.Position;
Vector3 positionClip;
Vector3.Transform(ref positionWorld, ref wvp, out positionClip);
float w = (float)((double)positionWorld.X * wvp.M14 + (double)positionWorld.Y * wvp.M24 + (double)positionWorld.Z * wvp.M34 + wvp.M44);
// Homogeneous divide.
positionClip /= w;
// 2 triangles create a point quad. Clip space goes from -1 to 1, therefore
// we do not need to divide sizeX and sizeY by 2.
Vector3 bottomLeft = positionClip + new Vector3(-sizeX, +sizeY, 0);
Vector3 bottomRight = positionClip + new Vector3(+sizeX, +sizeY, 0);
Vector3 topLeft = positionClip + new Vector3(-sizeX, -sizeY, 0);
Vector3 topRight = positionClip + new Vector3(+sizeX, -sizeY, 0);
_buffer[i * 6 + 0].Position = bottomLeft;
_buffer[i * 6 + 0].Color = point.Color;
_buffer[i * 6 + 1].Position = bottomRight;
_buffer[i * 6 + 1].Color = point.Color;
_buffer[i * 6 + 2].Position = topLeft;
_buffer[i * 6 + 2].Color = point.Color;
_buffer[i * 6 + 3].Position = bottomRight;
_buffer[i * 6 + 3].Color = point.Color;
_buffer[i * 6 + 4].Position = topRight;
_buffer[i * 6 + 4].Color = point.Color;
_buffer[i * 6 + 5].Position = topLeft;
_buffer[i * 6 + 5].Color = point.Color;
}
//.........这里部分代码省略.........
示例9: Render
public override void Render(IList<SceneNode> nodes, RenderContext context, RenderOrder order)
{
if (nodes == null)
throw new ArgumentNullException("nodes");
if (context == null)
throw new ArgumentNullException("context");
int numberOfNodes = nodes.Count;
if (numberOfNodes == 0)
return;
context.Validate(_effect);
context.ThrowIfCameraMissing();
var graphicsDevice = _effect.GraphicsDevice;
var savedRenderState = new RenderStateSnapshot(graphicsDevice);
graphicsDevice.DepthStencilState = DepthStencilState.None;
graphicsDevice.RasterizerState = RasterizerState.CullNone;
graphicsDevice.BlendState = GraphicsHelper.BlendStateAdd;
var viewport = graphicsDevice.Viewport;
_parameterViewportSize.SetValue(new Vector2(viewport.Width, viewport.Height));
_parameterGBuffer0.SetValue(context.GBuffer0);
_parameterGBuffer1.SetValue(context.GBuffer1);
var cameraNode = context.CameraNode;
Matrix viewProjection = (Matrix)cameraNode.View * cameraNode.Camera.Projection;
var cameraPose = cameraNode.PoseWorld;
GraphicsHelper.GetFrustumFarCorners(cameraNode.Camera.Projection, _cameraFrustumFarCorners);
// Convert frustum far corners from view space to world space.
for (int i = 0; i < _cameraFrustumFarCorners.Length; i++)
_cameraFrustumFarCorners[i] = (Vector3)cameraPose.ToWorldDirection((Vector3F)_cameraFrustumFarCorners[i]);
_parameterFrustumCorners.SetValue(_cameraFrustumFarCorners);
// Update SceneNode.LastFrame for all visible nodes.
int frame = context.Frame;
cameraNode.LastFrame = frame;
var isHdrEnabled = context.IsHdrEnabled();
for (int i = 0; i < numberOfNodes; i++)
{
var lightNode = nodes[i] as LightNode;
if (lightNode == null)
continue;
var light = lightNode.Light as DirectionalLight;
if (light == null)
continue;
// LightNode is visible in current frame.
lightNode.LastFrame = frame;
float hdrScale = isHdrEnabled ? light.HdrScale : 1;
_parameterDiffuseColor.SetValue((Vector3)light.Color * light.DiffuseIntensity * hdrScale);
_parameterSpecularColor.SetValue((Vector3)light.Color * light.SpecularIntensity * hdrScale);
Pose lightPose = lightNode.PoseWorld;
Vector3F lightDirectionWorld = lightPose.ToWorldDirection(Vector3F.Forward);
_parameterLightDirection.SetValue((Vector3)lightDirectionWorld);
bool hasShadow = (lightNode.Shadow != null && lightNode.Shadow.ShadowMask != null);
if (hasShadow)
{
switch (lightNode.Shadow.ShadowMaskChannel)
{
case 0: _parameterShadowMaskChannel.SetValue(new Vector4(1, 0, 0, 0)); break;
case 1: _parameterShadowMaskChannel.SetValue(new Vector4(0, 1, 0, 0)); break;
case 2: _parameterShadowMaskChannel.SetValue(new Vector4(0, 0, 1, 0)); break;
default: _parameterShadowMaskChannel.SetValue(new Vector4(0, 0, 0, 1)); break;
}
_parameterShadowMask.SetValue(lightNode.Shadow.ShadowMask);
}
bool hasTexture = (light.Texture != null);
if (hasTexture)
{
var textureProjection = Matrix44F.CreateOrthographicOffCenter(
-light.TextureOffset.X,
-light.TextureOffset.X + Math.Abs(light.TextureScale.X),
light.TextureOffset.Y,
light.TextureOffset.Y + Math.Abs(light.TextureScale.Y),
1, // Not relevant
2); // Not relevant.
var scale = Matrix44F.CreateScale(Math.Sign(light.TextureScale.X), Math.Sign(light.TextureScale.Y), 1);
_parameterTextureMatrix.SetValue((Matrix)(GraphicsHelper.ProjectorBiasMatrix * scale * textureProjection * lightPose.Inverse));
_parameterTexture.SetValue(light.Texture);
}
if (lightNode.Clip != null)
{
var data = lightNode.RenderData as LightRenderData;
if (data == null)
{
//.........这里部分代码省略.........
示例10: Render
/// <summary>
/// Renders a skybox.
/// </summary>
/// <param name="texture">The cube map with the sky texture.</param>
/// <param name="orientation">The orientation of the skybox.</param>
/// <param name="exposure">The exposure factor that is multiplied to the cube map values to change the brightness.
/// (Usually 1 or higher).</param>
/// <param name="context">
/// The render context. (<see cref="RenderContext.CameraNode"/> needs to be set.)
/// </param>
/// <exception cref="ArgumentNullException">
/// <paramref name="texture"/> or <paramref name="context"/> is <see langword="null"/>.
/// </exception>
public void Render(TextureCube texture, Matrix33F orientation, float exposure, RenderContext context)
{
if (texture == null)
throw new ArgumentNullException("texture");
if (context == null)
throw new ArgumentNullException("context");
context.Validate(_effect);
context.ThrowIfCameraMissing();
var graphicsDevice = context.GraphicsService.GraphicsDevice;
if (graphicsDevice.GraphicsProfile == GraphicsProfile.Reach)
RenderReach(texture, orientation, exposure, context);
else
RenderHiDef(texture, orientation, exposure, context);
}
示例11: UpdateOcclusion
public void UpdateOcclusion(IList<SceneNode> nodes, RenderContext context)
{
// Measures the visibility of the light source by drawing a screen-aligned quad
// using an occlusion query. The query result is used in the next frame.
ThrowIfDisposed();
if (nodes == null)
throw new ArgumentNullException("nodes");
if (context == null)
throw new ArgumentNullException("context");
context.Validate(_basicEffect);
context.ThrowIfCameraMissing();
// Occlusion queries require HiDef profile.
var graphicsDevice = context.GraphicsService.GraphicsDevice;
if (graphicsDevice.GraphicsProfile == GraphicsProfile.Reach)
return;
int numberOfNodes = nodes.Count;
if (nodes.Count == 0)
return;
// Camera properties
var cameraNode = context.CameraNode;
var cameraPose = cameraNode.PoseWorld;
Vector3F cameraRight = cameraPose.Orientation.GetColumn(0); // 1st column vector
Vector3F cameraUp = cameraPose.Orientation.GetColumn(1); // 2nd column vector
Vector3F cameraForward = -cameraPose.Orientation.GetColumn(2); // 3rd column vector (negated)
Matrix44F view = cameraNode.View;
Matrix44F projection = cameraNode.Camera.Projection;
bool isOrthographic = (projection.M33 != 0);
// The following factors are used to estimate the size of a quad in screen space.
// (The equation is described in GraphicsHelper.GetScreenSize().)
float xScale = Math.Abs(projection.M00 / 2);
float yScale = Math.Abs(projection.M11 / 2);
var viewport = graphicsDevice.Viewport;
// Lens flares of directional lights are rendered directly on the screen.
// --> Set up projection transformation for rendering in screen space.
var orthographicProjection = Matrix.CreateOrthographicOffCenter(0, viewport.Width, viewport.Height, 0, 0, 1);
// Set render states for rendering occlusion query geometry (quad).
var savedRenderState = new RenderStateSnapshot(graphicsDevice);
graphicsDevice.BlendState = GraphicsHelper.BlendStateNoColorWrite;
graphicsDevice.DepthStencilState = DepthStencilState.DepthRead;
graphicsDevice.RasterizerState = RasterizerState.CullCounterClockwise;
for (int i = 0; i < numberOfNodes; i++)
{
var node = nodes[i] as LensFlareNode;
if (node == null)
continue;
object dummy;
cameraNode.ViewDependentData.TryGetValue(node, out dummy);
var renderData = dummy as OcclusionData;
if (renderData == null)
{
renderData = new OcclusionData();
cameraNode.ViewDependentData[node] = renderData;
}
if (renderData.OcclusionQuery != null)
{
// Wait until previous occlusion query has completed.
if (!renderData.OcclusionQuery.IsComplete)
continue;
// ----- Read result of previous occlusion query.
int visiblePixels = renderData.OcclusionQuery.PixelCount;
// OcclusionData.TotalPixels is only an approximation.
// --> Clamp pixel count to [0, TotalPixels].
if (visiblePixels > renderData.TotalPixels)
visiblePixels = renderData.TotalPixels;
renderData.VisiblePixels = visiblePixels;
}
// ----- Run new occlusion query.
var lensFlare = node.LensFlare;
// The user can disable the lens flare by setting LensFlare.Intensity to 0.
float intensity = node.Intensity * lensFlare.Intensity;
if (intensity < MinIntensity)
{
renderData.VisiblePixels = 0;
continue;
}
float querySize;
if (lensFlare.IsDirectional)
{
// ----- Directional lights
// Ignore directional lights if camera has orthographic projection.
//.........这里部分代码省略.........
示例12: Render
public override void Render(IList<SceneNode> nodes, RenderContext context, RenderOrder order)
{
ThrowIfDisposed();
if (nodes == null)
throw new ArgumentNullException("nodes");
if (context == null)
throw new ArgumentNullException("context");
// Lens flares are used sparsely in most games. --> Early out, if possible.
int numberOfNodes = nodes.Count;
if (nodes.Count == 0)
return;
context.Validate(_spriteBatch);
context.ThrowIfCameraMissing();
var graphicsDevice = context.GraphicsService.GraphicsDevice;
var savedRenderState = new RenderStateSnapshot(graphicsDevice);
bool hiDef = (graphicsDevice.GraphicsProfile == GraphicsProfile.HiDef);
// Camera properties
var cameraNode = context.CameraNode;
var cameraPose = cameraNode.PoseWorld;
Vector3F cameraForward = -cameraPose.Orientation.GetColumn(2); // 3rd column vector (negated)
Matrix44F view = cameraNode.View;
Matrix44F projection = cameraNode.Camera.Projection;
// The flares are positioned on a line from the origin through the center of
// the screen.
var viewport = graphicsDevice.Viewport;
Vector2F screenCenter = new Vector2F(viewport.Width / 2.0f, viewport.Height / 2.0f);
if (_transformParameter != null)
{
// ----- Original:
// Matrix matrix = (Matrix)(Matrix44F.CreateOrthographicOffCenter(0, viewport.Width, viewport.Height, 0, 0, 1)
// * Matrix44F.CreateTranslation(-0.5f, -0.5f, 0)); // Half-pixel offset (only for Direct3D 9).
// ----- Inlined:
Matrix matrix = new Matrix();
float oneOverW = 1.0f / viewport.Width;
float oneOverH = 1.0f / viewport.Height;
matrix.M11 = oneOverW * 2f;
matrix.M22 = -oneOverH * 2f;
matrix.M33 = -1f;
matrix.M44 = 1f;
#if MONOGAME
matrix.M41 = -1f;
matrix.M42 = 1f;
#else
// Direct3D 9: half-pixel offset
matrix.M41 = -oneOverW - 1f;
matrix.M42 = oneOverH + 1f;
#endif
_transformParameter.SetValue(matrix);
}
// Update SceneNode.LastFrame for all visible nodes.
int frame = context.Frame;
cameraNode.LastFrame = frame;
// Choose current effect technique: Linear vs. Gamma-corrected Writes.
if (_effect != null)
_effect.CurrentTechnique = context.IsHdrEnabled() ? _techniqueLinear : _techniqueGamma;
_spriteBatch.Begin(SpriteSortMode.Texture, BlendState.Additive, null, null, null, _effect);
for (int i = 0; i < numberOfNodes; i++)
{
var node = nodes[i] as LensFlareNode;
if (node == null)
continue;
var lensFlare = node.LensFlare;
float size, intensity;
if (hiDef)
{
// HiDef profile
object dummy;
cameraNode.ViewDependentData.TryGetValue(node, out dummy);
var renderData = dummy as OcclusionData;
if (renderData == null || renderData.VisiblePixels == 0)
continue;
lensFlare.OnGetSizeAndIntensity(node, context, renderData.VisiblePixels, renderData.TotalPixels, out size, out intensity);
}
else
{
// Reach profile
lensFlare.OnGetSizeAndIntensity(node, context, 0, 0, out size, out intensity);
}
if (size <= 0 || intensity < MinIntensity)
continue;
// LensFlareNode is visible in current frame.
node.LastFrame = frame;
// Project position to screen space.
Vector2F screenPosition;
//.........这里部分代码省略.........
示例13: Render
public void Render(RenderContext context)
{
ThrowIfDisposed();
if (context == null)
throw new ArgumentNullException("context");
if (!Enabled)
return;
context.Validate(_spriteBatch);
var graphicsDevice = context.GraphicsService.GraphicsDevice;
var savedRenderState = new RenderStateSnapshot(graphicsDevice);
// ----- Draw points, lines, and text within 3D scene.
if (_autoRenderStates)
{
graphicsDevice.DepthStencilState = DepthStencilState.Default;
graphicsDevice.RasterizerState = GraphicsHelper.RasterizerStateCullCounterClockwise;
graphicsDevice.BlendState = BlendState.Opaque;
}
// No wireframe mode in OpenGLES
bool skipWireframes = (GlobalSettings.PlatformID == PlatformID.Android || GlobalSettings.PlatformID == PlatformID.iOS);
if (_inSceneLineBatch != null)
_inSceneLineBatch.Render(context);
if (_inSceneWireFramePrimitiveBatch != null && !skipWireframes)
_inSceneWireFramePrimitiveBatch.Render(context);
if (_autoRenderStates)
graphicsDevice.RasterizerState = GraphicsHelper.RasterizerStateWireFrame;
if (_inSceneWireFrameTriangleBatch != null && !skipWireframes)
_inSceneWireFrameTriangleBatch.Render(context);
if (_autoRenderStates)
graphicsDevice.RasterizerState = GraphicsHelper.RasterizerStateCullCounterClockwise;
if (_inScenePointBatch != null)
_inScenePointBatch.Render(context);
if (_inSceneTextBatch != null)
_inSceneTextBatch.Render(context);
// ----- Draw opaque objects within 3D scene.
if (_autoRenderStates)
{
graphicsDevice.DepthStencilState = DepthStencilState.Default;
graphicsDevice.RasterizerState = GraphicsHelper.RasterizerStateCullCounterClockwise;
graphicsDevice.BlendState = BlendState.Opaque;
}
if (_opaquePrimitiveBatch != null)
_opaquePrimitiveBatch.Render(context);
if (_opaqueTriangleBatch != null)
_opaqueTriangleBatch.Render(context);
// ----- Draw transparent objects within 3D scene.
if (_autoRenderStates)
{
graphicsDevice.DepthStencilState = DepthStencilState.DepthRead;
graphicsDevice.BlendState = BlendState.AlphaBlend;
}
if (_transparentPrimitiveBatch != null)
_transparentPrimitiveBatch.Render(context);
if (_transparentTriangleBatch != null)
_transparentTriangleBatch.Render(context);
// ----- Draw points, lines, text, textures over scene.
if (_autoRenderStates)
{
graphicsDevice.DepthStencilState = DepthStencilState.None;
graphicsDevice.BlendState = BlendState.Opaque;
}
if (_overSceneWireFramePrimitiveBatch != null && !skipWireframes)
_overSceneWireFramePrimitiveBatch.Render(context);
if (_autoRenderStates)
graphicsDevice.RasterizerState = GraphicsHelper.RasterizerStateWireFrame;
if (_overSceneWireFrameTriangleBatch != null && !skipWireframes)
_overSceneWireFrameTriangleBatch.Render(context);
if (_autoRenderStates)
graphicsDevice.RasterizerState = GraphicsHelper.RasterizerStateCullCounterClockwise;
if (_overSceneLineBatch != null)
_overSceneLineBatch.Render(context);
if (_overScenePointBatch != null)
_overScenePointBatch.Render(context);
if (_overScene3DTextBatch != null)
_overScene3DTextBatch.Render(context);
//.........这里部分代码省略.........
示例14: Render
/// <inheritdoc/>
public override void Render(IList<SceneNode> nodes, RenderContext context, RenderOrder order)
{
ThrowIfDisposed();
if (nodes == null)
throw new ArgumentNullException("nodes");
if (context == null)
throw new ArgumentNullException("context");
int numberOfNodes = nodes.Count;
if (nodes.Count == 0)
return;
context.Validate(_effect);
context.ThrowIfCameraMissing();
// Update SceneNode.LastFrame for all visible nodes.
int frame = context.Frame;
var cameraNode = context.CameraNode;
cameraNode.LastFrame = frame;
bool reach = (context.GraphicsService.GraphicsDevice.GraphicsProfile == GraphicsProfile.Reach);
for (int i = 0; i < numberOfNodes; i++)
{
var node = nodes[i] as SkyboxNode;
if (node == null)
continue;
// SkyboxNode is visible in current frame.
node.LastFrame = frame;
if (node.Texture != null)
{
if (reach)
RenderReach(node, context);
else
RenderHiDef(node, context);
}
}
}
示例15: Render
public override void Render(IList<SceneNode> nodes, RenderContext context, RenderOrder order)
{
ThrowIfDisposed();
if (nodes == null)
throw new ArgumentNullException("nodes");
if (context == null)
throw new ArgumentNullException("context");
int numberOfNodes = nodes.Count;
if (nodes.Count == 0)
return;
context.Validate(_effect);
var originalRenderTarget = context.RenderTarget;
var originalViewport = context.Viewport;
var graphicsDevice = context.GraphicsService.GraphicsDevice;
var savedRenderState = new RenderStateSnapshot(graphicsDevice);
graphicsDevice.BlendState = BlendState.Opaque;
graphicsDevice.RasterizerState = RasterizerState.CullNone;
graphicsDevice.DepthStencilState = DepthStencilState.None;
int frame = context.Frame;
float deltaTime = (float)context.DeltaTime.TotalSeconds;
for (int nodeIndex = 0; nodeIndex < numberOfNodes; nodeIndex++)
{
var cloudNode = nodes[nodeIndex] as CloudLayerNode;
if (cloudNode == null)
continue;
var cloudMap = cloudNode.CloudMap as LayeredCloudMap;
if (cloudMap == null)
continue;
// We update the cloud map only once per frame.
if (cloudMap.LastFrame == frame)
continue;
cloudMap.LastFrame = frame;
var layers = cloudMap.Layers;
var animationTimes = cloudMap.AnimationTimes;
var sources = cloudMap.SourceLayers;
var targets = cloudMap.TargetLayers;
var renderTargets = cloudMap.LayerTextures;
// Animate the cloud map layers.
for (int i = 0; i < LayeredCloudMap.NumberOfTextures; i++)
{
if (layers[i] == null || layers[i].Texture != null)
continue;
if (cloudMap.Random == null)
cloudMap.Random = new Random(cloudMap.Seed);
// Make sure there is a user-defined texture or data for procedural textures.
if (sources[i] == null)
{
// Each octave is 128 x 128 (= 1 / 4 of the 512 * 512 noise texture).
sources[i] = new PackedTexture(null, _noiseTexture, cloudMap.Random.NextVector2F(0, 1), new Vector2F(0.25f));
targets[i] = new PackedTexture(null, _noiseTexture, cloudMap.Random.NextVector2F(0, 1), new Vector2F(0.25f));
renderTargets[i] = new RenderTarget2D(graphicsDevice, 128, 128, false, SurfaceFormat.Alpha8, DepthFormat.None);
}
// Update animation time.
animationTimes[i] += deltaTime * layers[i].AnimationSpeed;
// Update source and target if animation time is beyond 1.
if (animationTimes[i] > 1)
{
// Wrap animation time.
animationTimes[i] = animationTimes[i] % 1;
// Swap source and target.
MathHelper.Swap(ref sources[i], ref targets[i]);
// Set target to a new random part of the noise texture.
targets[i].Offset = cloudMap.Random.NextVector2F(0, 1);
}
// Lerp source and target together to get the final noise texture.
graphicsDevice.SetRenderTarget(renderTargets[i]);
_parameterViewportSize.SetValue(new Vector2(graphicsDevice.Viewport.Width, graphicsDevice.Viewport.Height));
_parameterTextures[0].SetValue(sources[i].TextureAtlas);
_parameterTextures[1].SetValue(targets[i].TextureAtlas);
_parameterTexture0Parameters.SetValue(new Vector4(sources[i].Scale.X, sources[i].Scale.Y, sources[i].Offset.X, sources[i].Offset.Y));
_parameterTexture1Parameters.SetValue(new Vector4(targets[i].Scale.X, targets[i].Scale.Y, targets[i].Offset.X, targets[i].Offset.Y));
_parameterLerp.SetValue(animationTimes[i]);
_passLerp.Apply();
graphicsDevice.DrawFullScreenQuad();
}
// Initialize the cloud map.
if (cloudMap.Texture == null || cloudMap.Size != cloudMap.Texture.Width)
{
cloudMap.Texture.SafeDispose();
//.........这里部分代码省略.........