public override void Draw(IDrawDevice device) { // Perform Camera space transformation Vector3 posBefore = this.GameObj.Transform.Pos; Vector3 posTemp = posBefore; float scaleTemp = 1.0f; device.PreprocessCoords(this, ref posTemp, ref scaleTemp); // Draw debug text VertexC1P3T2[] textVertices; textVertices = null; Font.GenericMonospace10.Res.EmitTextVertices( string.Format("Position (world): {0:0}, {1:0}, {2:0}", posBefore.X, posBefore.Y, posBefore.Z), ref textVertices, posTemp.X, posTemp.Y, posTemp.Z); device.AddVertices(Font.GenericMonospace10.Res.Material, BeginMode.Quads, textVertices); textVertices = null; Font.GenericMonospace10.Res.EmitTextVertices( string.Format("Position (cam): {0:0}, {1:0}, {2:0}", posTemp.X, posTemp.Y, posTemp.Z), ref textVertices, posTemp.X, posTemp.Y + 10, posTemp.Z); device.AddVertices(Font.GenericMonospace10.Res.Material, BeginMode.Quads, textVertices); textVertices = null; Font.GenericMonospace10.Res.EmitTextVertices( string.Format("Scale: {0:F}", scaleTemp), ref textVertices, posTemp.X, posTemp.Y + 20, posTemp.Z); device.AddVertices(Font.GenericMonospace10.Res.Material, BeginMode.Quads, textVertices); // Draw position indicator device.AddVertices(new BatchInfo(DrawTechnique.Alpha, ColorRgba.Red.WithAlpha(0.25f)), BeginMode.Quads, new VertexP3[] { new VertexP3(posTemp.X - 50.0f * scaleTemp, posTemp.Y - 50.0f * scaleTemp, posTemp.Z), new VertexP3(posTemp.X + 50.0f * scaleTemp, posTemp.Y - 50.0f * scaleTemp, posTemp.Z), new VertexP3(posTemp.X + 50.0f * scaleTemp, posTemp.Y + 50.0f * scaleTemp, posTemp.Z), new VertexP3(posTemp.X - 50.0f * scaleTemp, posTemp.Y + 50.0f * scaleTemp, posTemp.Z) }); }
public override void Draw(IDrawDevice device) { Texture mainTex = this.RetrieveMainTex(); ColorRgba mainClr = this.RetrieveMainColor(); DrawTechnique tech = this.RetrieveDrawTechnique(); Rect uvRect; if (mainTex != null) { if (this.rectMode == UVMode.WrapBoth) uvRect = new Rect(mainTex.UVRatio.X * this.rect.W / mainTex.PixelWidth, mainTex.UVRatio.Y * this.rect.H / mainTex.PixelHeight); else if (this.rectMode == UVMode.WrapHorizontal) uvRect = new Rect(mainTex.UVRatio.X * this.rect.W / mainTex.PixelWidth, mainTex.UVRatio.Y); else if (this.rectMode == UVMode.WrapVertical) uvRect = new Rect(mainTex.UVRatio.X, mainTex.UVRatio.Y * this.rect.H / mainTex.PixelHeight); else uvRect = new Rect(mainTex.UVRatio.X, mainTex.UVRatio.Y); } else uvRect = new Rect(1.0f, 1.0f); this.PrepareVerticesLight(ref this.verticesLight, device, mainClr, uvRect, tech); if (this.customMat != null) device.AddVertices(this.customMat, VertexMode.Quads, this.verticesLight); else device.AddVertices(this.sharedMat, VertexMode.Quads, this.verticesLight); }
public override void Draw(IDrawDevice device) { Texture mainTex = this.RetrieveMainTex(); ColorRgba mainClr = this.RetrieveMainColor(); DrawTechnique tech = this.RetrieveDrawTechnique(); Rect uvRect; if (mainTex != null) { uvRect = new Rect(mainTex.UVRatio.X, mainTex.UVRatio.Y); } else { uvRect = new Rect(1.0f, 1.0f); } this.PrepareVerticesLight(ref this.verticesLight, device, mainClr, uvRect, tech); if (this.customMat != null) { device.AddVertices(this.customMat, VertexMode.Quads, this.verticesLight); } else { device.AddVertices(this.sharedMat, VertexMode.Quads, this.verticesLight); } }
public override void Draw(IDrawDevice device) { Texture mainTex = this.RetrieveMainTex(); ColorRgba mainClr = this.RetrieveMainColor(); Rect uvRect; if (mainTex != null) { if (this.spriteIndex < 0) { uvRect = new Rect(mainTex.UVRatio.X, mainTex.UVRatio.Y); } else { mainTex.LookupAtlas(this.spriteIndex, out uvRect); } } else { uvRect = new Rect(1.0f, 1.0f); } this.PrepareVertices(ref this.vertices, device, mainClr, uvRect); if (this.customMat != null) { device.AddVertices(this.customMat, VertexMode.Quads, this.vertices); } else { device.AddVertices(this.sharedMat, VertexMode.Quads, this.vertices); } }
private void DrawHorizontalGraph(Canvas canvas, float[] values, ColorRgba[] colors, ref VertexC1P3[] vertices, float x, float y, float w, float h) { if (h > 0.0f) { h--; } if (h < 0.0f) { h++; } IDrawDevice device = canvas.DrawDevice; ColorRgba baseColor = canvas.State.ColorTint * canvas.State.MaterialDirect.MainColor; float sampleXRatio = w / (float)(values.Length - 1); if (vertices == null) { vertices = new VertexC1P3[MathF.Max(values.Length, 16)]; } else if (vertices.Length < values.Length) { vertices = new VertexC1P3[MathF.Max(vertices.Length * 2, values.Length, 16)]; } for (int i = 0; i < values.Length; i++) { vertices[i].Pos.X = x + 0.5f + i * sampleXRatio; vertices[i].Pos.Y = y + 0.5f + (1.0f - values[i]) * h; vertices[i].Pos.Z = 0.0f; vertices[i].Color = baseColor * colors[i]; } device.AddVertices(canvas.State.MaterialDirect, VertexMode.LineStrip, vertices, values.Length); }
public override void Draw(IDrawDevice device) { var mainClr = RetrieveMainColor(); PrepareVertices(device, mainClr); device.AddVertices(sharedMat, VertexMode.Quads, _vertices); }
private void RenderTexturedBackground(IDrawDevice device) { if (!cachedTexturedBackground.IsAvailable) { return; } float timeMult = Time.TimeMult; backgroundX += timeMult * 1.2f; backgroundY += timeMult * -0.2f + timeMult * MathF.Sin(backgroundPhase) * 0.6f; backgroundPhase += timeMult * 0.001f; Vector3 renderPos = new Vector3(0, 0, 600); // Fit the target rect to actual pixel coordinates to avoid unnecessary filtering offsets renderPos.X = MathF.Round(renderPos.X); renderPos.Y = MathF.Round(renderPos.Y); if (MathF.RoundToInt(device.TargetSize.X) != (MathF.RoundToInt(device.TargetSize.X) / 2) * 2) { renderPos.X += 0.5f; } if (MathF.RoundToInt(device.TargetSize.Y) != (MathF.RoundToInt(device.TargetSize.Y) / 2) * 2) { // AMD Bugfix? renderPos.Y -= 0.004f; } // Reserve the required space for vertex data in our locally cached buffer int neededVertices = 4; if (cachedVertices == null || cachedVertices.Length < neededVertices) { cachedVertices = new VertexC1P3T2[neededVertices]; } // Render it as world-space fullscreen quad cachedVertices[0].Pos = new Vector3(renderPos.X, renderPos.Y, renderPos.Z); cachedVertices[1].Pos = new Vector3(renderPos.X + device.TargetSize.X, renderPos.Y, renderPos.Z); cachedVertices[2].Pos = new Vector3(renderPos.X + device.TargetSize.X, renderPos.Y + device.TargetSize.Y, renderPos.Z); cachedVertices[3].Pos = new Vector3(renderPos.X, renderPos.Y + device.TargetSize.Y, renderPos.Z); cachedVertices[0].TexCoord = new Vector2(0.0f, 0.0f); cachedVertices[1].TexCoord = new Vector2(1f, 0.0f); cachedVertices[2].TexCoord = new Vector2(1f, 1f); cachedVertices[3].TexCoord = new Vector2(0.0f, 1f); cachedVertices[0].Color = cachedVertices[1].Color = cachedVertices[2].Color = cachedVertices[3].Color = ColorRgba.White; // Setup custom pixel shader BatchInfo material = device.RentMaterial(); material.Technique = texturedBackgroundShader; material.MainTexture = cachedTexturedBackground; material.SetValue("horizonColor", horizonColor); material.SetValue("shift", new Vector2(backgroundX, backgroundY)); material.SetValue("parallaxStarsEnabled", 0f); device.AddVertices(material, VertexMode.Quads, cachedVertices, 0, 4); }
public override void Draw(IDrawDevice device) { if (gameobj == null) { return; } float timeMult = Time.TimeMult; for (int j = 0; j < circleEffectData.Length; j++) { ref CircleEffect circle = ref circleEffectData[j]; if (circle.Alpha <= 0f) { continue; } int segmentNum = MathF.Clamp(MathF.RoundToInt(MathF.Pow(circle.Radius, 0.65f) * 2.5f), 4, 32); float angle = 0.0f; for (int i = 0; i < segmentNum; i++) { vertices[i].Pos.X = circle.Pos.X + (float)Math.Sin(angle) * circle.Radius; vertices[i].Pos.Y = circle.Pos.Y - (float)Math.Cos(angle) * circle.Radius; vertices[i].Pos.Z = circle.Pos.Z - 10f; vertices[i].Color = new ColorRgba(1f, circle.Alpha); angle += (MathF.TwoPi / segmentNum); } device.AddVertices(material, VertexMode.LineLoop, vertices, 0, segmentNum); circle.Radius -= timeMult * 0.8f; circle.Alpha -= timeMult * 0.03f; }
private void RenderSinglePass(Rect viewportRect, Pass p) { this.drawDevice.VisibilityMask = this.visibilityMask & p.VisibilityMask; this.drawDevice.RenderMode = p.MatrixMode; this.drawDevice.Target = p.Output; this.drawDevice.ViewportRect = p.Output.IsAvailable ? new Rect(p.Output.Res.Width, p.Output.Res.Height) : viewportRect; if (p.Input == null) { // Render Scene this.drawDevice.PrepareForDrawcalls(); try { this.CollectDrawcalls(); p.NotifyCollectDrawcalls(this.drawDevice); } catch (Exception e) { Log.Core.WriteError("There was an error while {0} was collecting drawcalls: {1}", this.ToString(), Log.Exception(e)); } this.drawDevice.Render(p.ClearFlags, p.ClearColor, p.ClearDepth); } else { Profile.TimePostProcessing.BeginMeasure(); this.drawDevice.PrepareForDrawcalls(); Texture mainTex = p.Input.MainTexture.Res; Vector2 uvRatio = mainTex != null ? mainTex.UVRatio : Vector2.One; Vector2 inputSize = mainTex != null ? new Vector2(mainTex.PixelWidth, mainTex.PixelHeight) : Vector2.One; Rect targetRect; if (DualityApp.ExecEnvironment == DualityApp.ExecutionEnvironment.Editor && !this.drawDevice.Target.IsAvailable) { targetRect = Rect.Align(Alignment.Center, this.drawDevice.TargetSize.X * 0.5f, this.drawDevice.TargetSize.Y * 0.5f, inputSize.X, inputSize.Y); } else { targetRect = new Rect(this.drawDevice.TargetSize); } IDrawDevice device = this.drawDevice; { VertexC1P3T2[] vertices = new VertexC1P3T2[4]; vertices[0].Pos = new Vector3(targetRect.LeftX, targetRect.TopY, 0.0f); vertices[1].Pos = new Vector3(targetRect.RightX, targetRect.TopY, 0.0f); vertices[2].Pos = new Vector3(targetRect.RightX, targetRect.BottomY, 0.0f); vertices[3].Pos = new Vector3(targetRect.LeftX, targetRect.BottomY, 0.0f); vertices[0].TexCoord = new Vector2(0.0f, 0.0f); vertices[1].TexCoord = new Vector2(uvRatio.X, 0.0f); vertices[2].TexCoord = new Vector2(uvRatio.X, uvRatio.Y); vertices[3].TexCoord = new Vector2(0.0f, uvRatio.Y); device.AddVertices(p.Input, VertexMode.Quads, vertices); } this.drawDevice.Render(p.ClearFlags, p.ClearColor, p.ClearDepth); Profile.TimePostProcessing.EndMeasure(); } }
/// <summary> /// Adds a parameterized set of vertices to the drawing devices rendering schedule. /// </summary> /// <typeparam name="T">The type of vertex data to add.</typeparam> /// <param name="device"></param> /// <param name="material">The <see cref="Duality.Resources.Material"/> to use for rendering the vertices.</param> /// <param name="vertexMode">The vertices drawing mode.</param> /// <param name="vertices"> /// A vertex data buffer that stores the vertices to add. Ownership of the buffer /// remains at the callsite, while the <see cref="IDrawDevice"/> copies the required /// data into internal storage. /// </param> public static void AddVertices <T>(this IDrawDevice device, ContentRef <Material> material, VertexMode vertexMode, params T[] vertices) where T : struct, IVertexData { device.AddVertices <T>( material.IsAvailable ? material.Res.Info : Material.Checkerboard.Res.Info, vertexMode, vertices, vertices.Length); }
/// <summary> /// Adds a parameterized set of vertices to the drawing devices rendering schedule. /// </summary> /// <typeparam name="T">The type of vertex data to add.</typeparam> /// <param name="device"></param> /// <param name="material">The <see cref="Duality.Resources.Material"/> to use for rendering the vertices.</param> /// <param name="vertexMode">The vertices drawing mode.</param> /// <param name="vertexBuffer"> /// A vertex data buffer that stores the vertices to add. Ownership of the buffer /// remains at the callsite, while the <see cref="IDrawDevice"/> copies the required /// data into internal storage. /// </param> /// <param name="vertexCount">The number of vertices to add, from the beginning of the buffer.</param> public static void AddVertices <T>(this IDrawDevice device, ContentRef <Material> material, VertexMode vertexMode, T[] vertexBuffer, int vertexCount) where T : struct, IVertexData { device.AddVertices <T>( material.IsAvailable ? material.Res.Info : Material.Checkerboard.Res.Info, vertexMode, vertexBuffer, vertexCount); }
/// <inheritdoc/> public override void Draw(IDrawDevice device) { Texture mainTex = this.RetrieveMainTex(); Rect uvRect; this.GetUVRect(mainTex, this.spriteIndex, out uvRect); this.PrepareVertices(ref this.vertices, device, this.colorTint, uvRect); if (this.customMat != null) { device.AddVertices(this.customMat, VertexMode.Quads, this.vertices); } else { device.AddVertices(this.sharedMat, VertexMode.Quads, this.vertices); } }
/// <summary> /// Adds a parameterized set of vertices to the drawing devices rendering schedule. /// </summary> /// <typeparam name="T">The type of vertex data to add.</typeparam> /// <param name="device"></param> /// <param name="material">The <see cref="Duality.Drawing.BatchInfo"/> to use for rendering the vertices.</param> /// <param name="vertexMode">The vertices drawing mode.</param> /// <param name="vertices"> /// A vertex data buffer that stores the vertices to add. Ownership of the buffer /// remains at the callsite, while the <see cref="IDrawDevice"/> copies the required /// data into internal storage. /// </param> public static void AddVertices <T>(this IDrawDevice device, BatchInfo material, VertexMode vertexMode, params T[] vertices) where T : struct, IVertexData { device.AddVertices <T>( material, vertexMode, vertices, vertices.Length); }
public override void Draw(IDrawDevice device) { Transform transform = this.GameObj.Transform; Vector3 posTemp = transform.Pos; float scaleTemp = 1.0f; device.PreprocessCoords(ref posTemp, ref scaleTemp); Vector2 xDot, yDot; MathF.GetTransformDotVec(transform.Angle, scaleTemp * transform.Scale, out xDot, out yDot); //MathF.TransformDotVec(ref edge1, ref xDot, ref yDot); this.vertices.Count = this.circleStepCount * 2; VertexC1P3T2[] vertexData = this.vertices.Data; for (int i = 0; i < this.circleStepCount; i++) { float percentage = (float)i / (float)(this.circleStepCount - 1); float angle = MathF.DegToRad(this.angleRangeDegree) * (percentage - 0.5f); Vector2 offsetOuter = Vector2.FromAngleLength(angle, this.outerRadius); Vector2 offsetInner = Vector2.FromAngleLength(angle, this.innerRadius); MathF.TransformDotVec(ref offsetOuter, ref xDot, ref yDot); MathF.TransformDotVec(ref offsetInner, ref xDot, ref yDot); float alpha = 1.0f; if (this.texHAlphaFade > 0.0f) { if (percentage < this.texHAlphaFade) { alpha = MathF.Clamp(percentage / this.texHAlphaFade, 0.0f, 1.0f); } else if (percentage > 1.0f - this.texHAlphaFade) { alpha = MathF.Clamp((1.0f - percentage) / this.texHAlphaFade, 0.0f, 1.0f); } } float horizontalOffsetAnim = ((float)Time.GameTimer.TotalSeconds * this.texHOffsetAnimSpeed) % 1.0f; vertexData[i * 2 + 0].Pos.X = posTemp.X + offsetInner.X; vertexData[i * 2 + 0].Pos.Y = posTemp.Y + offsetInner.Y; vertexData[i * 2 + 0].Pos.Z = posTemp.Z; vertexData[i * 2 + 0].TexCoord.X = percentage + horizontalOffsetAnim; vertexData[i * 2 + 0].TexCoord.Y = 1.0f; vertexData[i * 2 + 0].Color = ColorRgba.White.WithAlpha(alpha); vertexData[i * 2 + 1].Pos.X = posTemp.X + offsetOuter.X; vertexData[i * 2 + 1].Pos.Y = posTemp.Y + offsetOuter.Y; vertexData[i * 2 + 1].Pos.Z = posTemp.Z; vertexData[i * 2 + 1].TexCoord.X = percentage + horizontalOffsetAnim; vertexData[i * 2 + 1].TexCoord.Y = 0.0f; vertexData[i * 2 + 1].Color = ColorRgba.White.WithAlpha(alpha); } device.AddVertices(this.sharedMat, VertexMode.TriangleStrip, vertexData, this.vertices.Count); }
public override void Draw(IDrawDevice device) { Texture mainTex = this.RetrieveMainTex(); ColorRgba mainClr = this.RetrieveMainColor(); DrawTechnique tech = this.RetrieveDrawTechnique(); Rect uvRect; if (mainTex != null) { if (this.rectMode == UVMode.WrapBoth) { uvRect = new Rect(mainTex.UVRatio.X * this.rect.W / mainTex.PixelWidth, mainTex.UVRatio.Y * this.rect.H / mainTex.PixelHeight); } else if (this.rectMode == UVMode.WrapHorizontal) { uvRect = new Rect(mainTex.UVRatio.X * this.rect.W / mainTex.PixelWidth, mainTex.UVRatio.Y); } else if (this.rectMode == UVMode.WrapVertical) { uvRect = new Rect(mainTex.UVRatio.X, mainTex.UVRatio.Y * this.rect.H / mainTex.PixelHeight); } else { uvRect = new Rect(mainTex.UVRatio.X, mainTex.UVRatio.Y); } } else { uvRect = new Rect(1.0f, 1.0f); } this.PrepareVerticesLight(ref this.verticesLight, device, mainClr, uvRect, tech); if (this.customMat != null) { device.AddVertices(this.customMat, VertexMode.Quads, this.verticesLight); } else { device.AddVertices(this.sharedMat, VertexMode.Quads, this.verticesLight); } }
public override void Draw(IDrawDevice device) { Texture mainTex = this.RetrieveMainTex(); ColorRgba mainClr = this.RetrieveMainColor(); Rect uvRect; this.GetAnimData(mainTex, out uvRect); this.PrepareVertices(ref this.vertices, device, mainClr, uvRect); if (this.customMat != null) { device.AddVertices(this.customMat, VertexMode.Quads, this.vertices); } else { device.AddVertices(this.sharedMat, VertexMode.Quads, this.vertices); } }
public void RenderTexturedBackground(IDrawDevice device, ref TileMapLayer layer, int cacheIndex, float x, float y) { if (!cachedTexturedBackground.IsAvailable || cachedTexturedBackgroundAnimated) { RecreateTexturedBackground(ref layer); } // Fit the input material rect to the output size according to rendering step config Vector3 renderPos = new Vector3(device.ViewerPos.X - device.TargetSize.X / 2, device.ViewerPos.Y - device.TargetSize.Y / 2, layer.Depth); // Fit the target rect to actual pixel coordinates to avoid unnecessary filtering offsets renderPos.X = MathF.Round(renderPos.X); renderPos.Y = MathF.Round(renderPos.Y); if (MathF.RoundToInt(device.TargetSize.X) != (MathF.RoundToInt(device.TargetSize.X) / 2) * 2) { renderPos.X += 0.5f; } if (MathF.RoundToInt(device.TargetSize.Y) != (MathF.RoundToInt(device.TargetSize.Y) / 2) * 2) { // AMD Bugfix? renderPos.Y -= 0.004f; } // Reserve the required space for vertex data in our locally cached buffer int neededVertices = 4; if (cachedVertices == null || cachedVertices.Length < neededVertices) { cachedVertices = new VertexC1P3T2[neededVertices]; } // Render it as world-space fullscreen quad cachedVertices[0].Pos = new Vector3(renderPos.X, renderPos.Y, renderPos.Z); cachedVertices[1].Pos = new Vector3(renderPos.X + device.TargetSize.X, renderPos.Y, renderPos.Z); cachedVertices[2].Pos = new Vector3(renderPos.X + device.TargetSize.X, renderPos.Y + device.TargetSize.Y, renderPos.Z); cachedVertices[3].Pos = new Vector3(renderPos.X, renderPos.Y + device.TargetSize.Y, renderPos.Z); cachedVertices[0].TexCoord = new Vector2(0f, 0f); cachedVertices[1].TexCoord = new Vector2(1f, 0f); cachedVertices[2].TexCoord = new Vector2(1f, 1f); cachedVertices[3].TexCoord = new Vector2(0f, 1f); cachedVertices[0].Color = cachedVertices[1].Color = cachedVertices[2].Color = cachedVertices[3].Color = ColorRgba.White; // Setup custom pixel shader BatchInfo material = device.RentMaterial(); material.Technique = texturedBackgroundShader; material.MainTexture = cachedTexturedBackground; material.SetValue("horizonColor", layer.BackgroundColor); material.SetValue("shift", new Vector2(x, y)); material.SetValue("parallaxStarsEnabled", layer.ParallaxStarsEnabled ? 1f : 0f); device.AddVertices(material, VertexMode.Quads, cachedVertices, 0, 4); }
protected override void Draw(IDrawDevice inDevice) { base.Draw(inDevice); if (!String.IsNullOrWhiteSpace(_fText.SourceText) && _batchInfo != null) { _textVertices[0].TexCoord.X = 0; _textVertices[1].TexCoord.X = _visibleWidth; _textVertices[2].TexCoord.X = _visibleWidth; _textVertices[3].TexCoord.X = 0; //if (_scrollbar == null || !_scrollbar.Active) if (!_isScrollbarRequired) { _textVertices[0].TexCoord.Y = 0; _textVertices[1].TexCoord.Y = 0; _textVertices[2].TexCoord.Y = 1; _textVertices[3].TexCoord.Y = 1; } else { _textVertices[0].TexCoord.Y = _scrollComponent.Value; _textVertices[1].TexCoord.Y = _textVertices[0].TexCoord.Y; _textVertices[2].TexCoord.Y = (_scrollComponent.Value + _visibleHeight); _textVertices[3].TexCoord.Y = _textVertices[2].TexCoord.Y; } Vector2 uvRatio = _batchInfo.MainTexture.Res.UVRatio; _textVertices[0].TexCoord *= uvRatio * _uvUnit; _textVertices[1].TexCoord *= uvRatio * _uvUnit; _textVertices[2].TexCoord *= uvRatio * _uvUnit; _textVertices[3].TexCoord *= uvRatio * _uvUnit; _textVertices[0].Color = _textColor; _textVertices[0].Pos = _points[5].SceneCoords; _textVertices[0].Pos.Z += DELTA_Z; _textVertices[1].Color = _textColor; _textVertices[1].Pos = _points[6].SceneCoords; _textVertices[1].Pos.Z += DELTA_Z; _textVertices[2].Color = _textColor; _textVertices[2].Pos = _points[10].SceneCoords; _textVertices[2].Pos.Z += DELTA_Z; _textVertices[3].Color = _textColor; _textVertices[3].Pos = _points[9].SceneCoords; _textVertices[3].Pos.Z += DELTA_Z; inDevice.AddVertices(_batchInfo, VertexMode.Quads, _textVertices); } }
public override void Draw(IDrawDevice device) { Transform transform = this.GameObj.Transform; Vector3 posTemp = transform.Pos; float scaleTemp = 1.0f; device.PreprocessCoords(ref posTemp, ref scaleTemp); Vector2 xDot, yDot; MathF.GetTransformDotVec(transform.Angle, scaleTemp * transform.Scale, out xDot, out yDot); float angle = endPos.Angle; Vector2 startLeft = Vector2.FromAngleLength(angle - MathF.RadAngle90, this.startWidth * 0.5f); Vector2 startRight = Vector2.FromAngleLength(angle + MathF.RadAngle90, this.startWidth * 0.5f); Vector2 endLeft = this.endPos + Vector2.FromAngleLength(angle - MathF.RadAngle90, this.endWidth * 0.5f); Vector2 endRight = this.endPos + Vector2.FromAngleLength(angle + MathF.RadAngle90, this.endWidth * 0.5f); MathF.TransformDotVec(ref startLeft, ref xDot, ref yDot); MathF.TransformDotVec(ref startRight, ref xDot, ref yDot); MathF.TransformDotVec(ref endLeft, ref xDot, ref yDot); MathF.TransformDotVec(ref endRight, ref xDot, ref yDot); this.vertices[0].Pos.X = posTemp.X + startLeft.X; this.vertices[0].Pos.Y = posTemp.Y + startLeft.Y; this.vertices[0].Pos.Z = posTemp.Z; this.vertices[0].TexCoord.X = 0.0f; this.vertices[0].TexCoord.Y = 0.0f; this.vertices[0].Color = m_colorStart; this.vertices[1].Pos.X = posTemp.X + startRight.X; this.vertices[1].Pos.Y = posTemp.Y + startRight.Y; this.vertices[1].Pos.Z = posTemp.Z; this.vertices[1].TexCoord.X = 0.0f; this.vertices[1].TexCoord.Y = 0.0f; this.vertices[1].Color = m_colorStart; this.vertices[2].Pos.X = posTemp.X + endRight.X; this.vertices[2].Pos.Y = posTemp.Y + endRight.Y; this.vertices[2].Pos.Z = posTemp.Z; this.vertices[2].TexCoord.X = 0.0f; this.vertices[2].TexCoord.Y = 0.0f; this.vertices[2].Color = m_colorEnd; this.vertices[3].Pos.X = posTemp.X + endLeft.X; this.vertices[3].Pos.Y = posTemp.Y + endLeft.Y; this.vertices[3].Pos.Z = posTemp.Z; this.vertices[3].TexCoord.X = 0.0f; this.vertices[3].TexCoord.Y = 0.0f; this.vertices[3].Color = m_colorEnd; device.AddVertices(new BatchInfo(DrawTechnique.Add, ColorRgba.White), VertexMode.Quads, this.vertices); }
public override void Draw(IDrawDevice device) { device.AddVertices(_material, VertexMode.Quads, new VertexC1P3T2 { Pos = Vector3.Zero, TexCoord = Vector2.Zero } ); _canvas = _canvas ?? new Canvas(device); _canvas.DrawRect(0, 0, 1, 1); }
private void RenderSinglePass(Pass p) { this.drawDevice.VisibilityMask = this.visibilityMask & p.VisibilityMask; this.drawDevice.RenderMode = p.MatrixMode; this.drawDevice.Target = p.Output; if (p.Input == null) { // Render Scene this.drawDevice.BeginRendering(p.ClearFlags, p.ClearColor, p.ClearDepth); try { this.CollectDrawcalls(); p.NotifyCollectDrawcalls(this.DrawDevice); } catch (Exception e) { Log.Core.WriteError("There was an error while {0} was collecting drawcalls: {1}", this.ToString(), Log.Exception(e)); } this.drawDevice.EndRendering(); } else { Performance.TimePostProcessing.BeginMeasure(); this.drawDevice.BeginRendering(p.ClearFlags, p.ClearColor, p.ClearDepth); Texture mainTex = p.Input.MainTexture.Res; Vector2 uvRatio = mainTex != null ? mainTex.UVRatio : Vector2.One; Vector2 inputSize = mainTex != null ? new Vector2(mainTex.PixelWidth, mainTex.PixelHeight) : Vector2.One; Rect targetRect; if (DualityApp.ExecEnvironment == DualityApp.ExecutionEnvironment.Editor && !this.drawDevice.Target.IsAvailable) { targetRect = Rect.AlignCenter(this.drawDevice.TargetSize.X * 0.5f, this.drawDevice.TargetSize.Y * 0.5f, inputSize.X, inputSize.Y); } else { targetRect = new Rect(this.drawDevice.TargetSize); } IDrawDevice device = this.DrawDevice; device.AddVertices(p.Input, VertexMode.Quads, new VertexC1P3T2(targetRect.MinX, targetRect.MinY, 0.0f, 0.0f, 0.0f), new VertexC1P3T2(targetRect.MaxX, targetRect.MinY, 0.0f, uvRatio.X, 0.0f), new VertexC1P3T2(targetRect.MaxX, targetRect.MaxY, 0.0f, uvRatio.X, uvRatio.Y), new VertexC1P3T2(targetRect.MinX, targetRect.MaxY, 0.0f, 0.0f, uvRatio.Y)); this.drawDevice.EndRendering(); Performance.TimePostProcessing.EndMeasure(); } }
public override void Draw(IDrawDevice device) { if (animHidden) { return; } Texture mainTex = RetrieveMainTex(); DrawTechnique tech = RetrieveDrawTechnique(); Rect uvRect, uvRectNext; bool smoothShaderInput = (tech != null && tech.PreferredVertexFormat == VertexC1P3T4A1.Declaration); GetAnimData(mainTex, smoothShaderInput, out uvRect, out uvRectNext); if (!smoothShaderInput) { PrepareVertices(ref vertices, device, this.colorTint, uvRect); if (customMat != null) { device.AddVertices(customMat, VertexMode.Quads, vertices, 0, 4); } else { if (flipMode == 0) { device.AddVertices(sharedMat, VertexMode.Quads, vertices, 0, 4); } else { BatchInfo material = device.RentMaterial(sharedMat.Res.Info); material.SetValue("normalMultiplier", new Vector2((flipMode & FlipMode.Horizontal) == 0 ? 1 : -1f, (flipMode & FlipMode.Vertical) == 0 ? 1 : -1f)); device.AddVertices(material, VertexMode.Quads, vertices, 0, 4); } } } else { PrepareVerticesSmooth(ref verticesSmooth, device, curAnimFrameFade, this.colorTint, uvRect, uvRectNext); if (customMat != null) { device.AddVertices(customMat, VertexMode.Quads, verticesSmooth, 0, 4); } else { if (flipMode == 0) { device.AddVertices(sharedMat, VertexMode.Quads, verticesSmooth, 0, 4); } else { BatchInfo material = device.RentMaterial(sharedMat.Res.Info); material.SetValue("normalMultiplier", new Vector2((flipMode & FlipMode.Horizontal) == 0 ? 1 : -1f, (flipMode & FlipMode.Vertical) == 0 ? 1 : -1f)); device.AddVertices(material, VertexMode.Quads, verticesSmooth, 0, 4); } } } }
public override void Draw(IDrawDevice device) { // When rendering a sprite that uses dynamic lighting, make sure to update // the devices shared / global lighting variables this frame Light.UpdateLighting(device); Texture mainTex = this.RetrieveMainTex(); DrawTechnique tech = this.RetrieveDrawTechnique(); Rect uvRect; this.GetUVRect(mainTex, this.spriteIndex, out uvRect); this.PrepareVerticesLight(ref this.verticesLight, device, this.colorTint, uvRect, tech); if (this.customMat != null) { device.AddVertices(this.customMat, VertexMode.Quads, this.verticesLight); } else { device.AddVertices(this.sharedMat, VertexMode.Quads, this.verticesLight); } }
public override void Draw(IDrawDevice device) { Texture mainTex = this.RetrieveMainTex(); ColorRgba mainClr = this.RetrieveMainColor(); DrawTechnique tech = this.RetrieveDrawTechnique(); Rect uvRect; Rect uvRectNext; bool smoothShaderInput = tech != null && tech.PreferredVertexFormat == VertexC1P3T4A1.Declaration; this.GetAnimData(mainTex, tech, smoothShaderInput, out uvRect, out uvRectNext); if (!smoothShaderInput) { this.PrepareVertices(ref this.vertices, device, mainClr, uvRect); if (this.customMat != null) { device.AddVertices(this.customMat, VertexMode.Quads, this.vertices); } else { device.AddVertices(this.sharedMat, VertexMode.Quads, this.vertices); } } else { this.PrepareVerticesSmooth(ref this.verticesSmooth, device, this.curAnimFrameFade, mainClr, uvRect, uvRectNext); if (this.customMat != null) { device.AddVertices(this.customMat, VertexMode.Quads, this.verticesSmooth); } else { device.AddVertices(this.sharedMat, VertexMode.Quads, this.verticesSmooth); } } }
public override void Draw(IDrawDevice device) { Texture mainTex = this.RetrieveMainTex(); ColorRgba mainClr = this.RetrieveMainColor(); DrawTechnique tech = this.RetrieveDrawTechnique(); Rect uvRect; Rect uvRectNext; bool smoothShaderInput = tech != null && tech.PreferredVertexFormat == VertexC1P3T4A4A1.Declaration; this.GetAnimData(mainTex, tech, smoothShaderInput, out uvRect, out uvRectNext); if (!smoothShaderInput) { this.PrepareVerticesLight(ref this.verticesLight, device, mainClr, uvRect, tech); if (this.customMat != null) device.AddVertices(this.customMat, VertexMode.Quads, this.verticesLight); else device.AddVertices(this.sharedMat, VertexMode.Quads, this.verticesLight); } else { this.PrepareVerticesLightSmooth(ref this.verticesLightSmooth, device, this.CurrentFrameProgress, mainClr, uvRect, uvRectNext, tech); if (this.customMat != null) device.AddVertices(this.customMat, VertexMode.Quads, this.verticesLightSmooth); else device.AddVertices(this.sharedMat, VertexMode.Quads, this.verticesLightSmooth); } }
protected override void Draw(IDrawDevice inDevice) { /** * This is needed to allow to display the widget correctly in the editor **/ if (FrozenUtilities.IsDualityEditor) { OnSkinChange(); } if (_skin.Res != null) { inDevice.AddVertices(_skin.Res.Material, VertexMode.Quads, _vertices); } }
private void RenderSinglePass(Rect viewportRect, Pass p) { this.drawDevice.VisibilityMask = this.visibilityMask & p.VisibilityMask; this.drawDevice.RenderMode = p.MatrixMode; this.drawDevice.Target = p.Output; this.drawDevice.ViewportRect = p.Output.IsAvailable ? new Rect(p.Output.Res.Width, p.Output.Res.Height) : viewportRect; if (p.Input == null) { // Render Scene this.drawDevice.BeginRendering(p.ClearFlags, p.ClearColor, p.ClearDepth); try { this.CollectDrawcalls(); p.NotifyCollectDrawcalls(this.drawDevice); } catch (Exception e) { Log.Core.WriteError("There was an error while {0} was collecting drawcalls: {1}", this.ToString(), Log.Exception(e)); } this.drawDevice.EndRendering(); } else { Profile.TimePostProcessing.BeginMeasure(); this.drawDevice.BeginRendering(p.ClearFlags, p.ClearColor, p.ClearDepth); Texture mainTex = p.Input.MainTexture.Res; Vector2 uvRatio = mainTex != null ? mainTex.UVRatio : Vector2.One; Rect targetRect = new Rect(this.drawDevice.TargetSize); IDrawDevice device = this.drawDevice; device.AddVertices(p.Input, VertexMode.Quads, new VertexC1P3T2(targetRect.MinimumX, targetRect.MinimumY, 0.0f, 0.0f, 0.0f), new VertexC1P3T2(targetRect.MaximumX, targetRect.MinimumY, 0.0f, uvRatio.X, 0.0f), new VertexC1P3T2(targetRect.MaximumX, targetRect.MaximumY, 0.0f, uvRatio.X, uvRatio.Y), new VertexC1P3T2(targetRect.MinimumX, targetRect.MaximumY, 0.0f, 0.0f, uvRatio.Y)); this.drawDevice.EndRendering(); Profile.TimePostProcessing.EndMeasure(); } }
public override void Draw(IDrawDevice device) { ColorRgba mainClr = this.RetrieveMainColor(); List <Texture> texList = this.RetrieveMainTex(); for (int i = 0; i < texList.Count; i++) { Rect uvRect; if (texList[i] != null) { if (this.rectMode == UVMode.WrapBoth) { uvRect = new Rect(texList[i].UVRatio.X * this.rect.W / texList[i].PixelWidth, texList[i].UVRatio.Y * this.rect.H / texList[i].PixelHeight); } else if (this.rectMode == UVMode.WrapHorizontal) { uvRect = new Rect(texList[i].UVRatio.X * this.rect.W / texList[i].PixelWidth, texList[i].UVRatio.Y); } else if (this.rectMode == UVMode.WrapVertical) { uvRect = new Rect(texList[i].UVRatio.X, texList[i].UVRatio.Y * this.rect.H / texList[i].PixelHeight); } else { uvRect = new Rect(texList[i].UVRatio.X, texList[i].UVRatio.Y); } } else { uvRect = new Rect(1.0f, 1.0f); } this.PrepareVertices(ref this.vertices, device, mainClr, uvRect); foreach (var item in this.sharedMat) { device.AddVertices(item, VertexMode.Quads, this.vertices); } } }
void ICmpRenderer.Draw(IDrawDevice device) { if (_inEditor) { if (FXArea != null) { Canvas c = new Canvas(device); FXArea.DrawInEditor(c); } } else { if (_particlesAlive > 0) { Vector3 posTemp = FXArea.GameObj.Transform.Pos; float scaleTemp = 1f; device.PreprocessCoords(ref posTemp, ref scaleTemp); int index = 0; foreach (Particle p in _particles) { if (p.IsAlive) { bool toDraw = DrawParticlesOffScreen || device.IsCoordInView(p.Position); if (toDraw) { p.UpdateVertices(device, posTemp, scaleTemp); Array.Copy(p.Vertices, 0, _particleVertices, index * 4, 4); } index++; } } device.AddVertices(_particleMaterial.Material, VertexMode.Quads, _particleVertices, index * 4); } } }
private void RenderAttachment(IDrawDevice device, ContentRef <Material> material, float[] boneVertices, GameObject gameObject, ColorRgba color, float[] uvs, float depth) { Vector3 position = gameObject.Transform.Pos; float actualScale = 1f * gameObject.Transform.Scale; device.PreprocessCoords(ref position, ref actualScale); var vertices = _vertexCache.Next(); vertices[0].Pos.X = position.X + boneVertices[RegionAttachment.X1] * actualScale; vertices[0].Pos.Y = position.Y + boneVertices[RegionAttachment.Y1] * actualScale; vertices[1].Pos.X = position.X + boneVertices[RegionAttachment.X2] * actualScale; vertices[1].Pos.Y = position.Y + boneVertices[RegionAttachment.Y2] * actualScale; vertices[2].Pos.X = position.X + boneVertices[RegionAttachment.X3] * actualScale; vertices[2].Pos.Y = position.Y + boneVertices[RegionAttachment.Y3] * actualScale; vertices[3].Pos.X = position.X + boneVertices[RegionAttachment.X4] * actualScale; vertices[3].Pos.Y = position.Y + boneVertices[RegionAttachment.Y4] * actualScale; vertices[0].TexCoord.X = uvs[RegionAttachment.X1]; vertices[0].TexCoord.Y = uvs[RegionAttachment.Y1]; vertices[1].TexCoord.X = uvs[RegionAttachment.X2]; vertices[1].TexCoord.Y = uvs[RegionAttachment.Y2]; vertices[2].TexCoord.X = uvs[RegionAttachment.X3]; vertices[2].TexCoord.Y = uvs[RegionAttachment.Y3]; vertices[3].TexCoord.X = uvs[RegionAttachment.X4]; vertices[3].TexCoord.Y = uvs[RegionAttachment.Y4]; vertices[0].Pos.Z = position.Z + depth; vertices[1].Pos.Z = position.Z + depth; vertices[2].Pos.Z = position.Z + depth; vertices[3].Pos.Z = position.Z + depth; vertices[0].Color = material.Res.MainColor * color; vertices[1].Color = material.Res.MainColor * color; vertices[2].Color = material.Res.MainColor * color; vertices[3].Color = material.Res.MainColor * color; device.AddVertices(material, VertexMode.Quads, vertices); }
public override void Draw(IDrawDevice device) { this.PrepareVertices(ref this.vertices, device); device.AddVertices(this.sharedMat, BeginMode.Quads, this.vertices); }
public override void Draw(IDrawDevice device) { // Determine basic working data Tilemap tilemap = this.ActiveTilemap; Tileset tileset = tilemap != null ? tilemap.Tileset.Res : null; Point2 tileCount = tilemap != null ? tilemap.Size : new Point2(1, 1); Vector2 tileSize = tileset != null ? tileset.TileSize : Tileset.DefaultTileSize; // Early-out, if insufficient if (tilemap == null) return; if (tileset == null) return; // Determine the total size and origin of the rendered Tilemap Vector2 renderTotalSize = tileCount * tileSize; Vector2 renderOrigin = Vector2.Zero; this.origin.ApplyTo(ref renderOrigin, ref renderTotalSize); MathF.TransformCoord(ref renderOrigin.X, ref renderOrigin.Y, this.GameObj.Transform.Angle, this.GameObj.Transform.Scale); // Determine Tile visibility TilemapCulling.TileInput cullingIn = new TilemapCulling.TileInput { // Remember: All these transform values are in world space TilemapPos = this.GameObj.Transform.Pos + new Vector3(renderOrigin), TilemapScale = this.GameObj.Transform.Scale, TilemapAngle = this.GameObj.Transform.Angle, TileCount = tileCount, TileSize = tileSize }; TilemapCulling.TileOutput cullingOut = TilemapCulling.GetVisibleTileRect(device, cullingIn); int renderedTileCount = cullingOut.VisibleTileCount.X * cullingOut.VisibleTileCount.Y; // Determine rendering parameters Material material = (tileset != null ? tileset.RenderMaterial : null) ?? Material.Checkerboard.Res; ColorRgba mainColor = material.MainColor * this.colorTint; // Reserve the required space for vertex data in our locally cached buffer if (this.vertices == null) this.vertices = new RawList<VertexC1P3T2>(); this.vertices.Count = renderedTileCount * 4; VertexC1P3T2[] vertexData = this.vertices.Data; // Determine and adjust data for Z offset generation float depthPerTile = -cullingIn.TileSize.Y * cullingIn.TilemapScale * this.tileDepthScale; if (this.tileDepthMode == TileDepthOffsetMode.Flat) depthPerTile = 0.0f; float originDepthOffset = Rect.Align(this.origin, 0, 0, 0, tileCount.Y * depthPerTile).Y; if (this.tileDepthMode == TileDepthOffsetMode.World) originDepthOffset += (this.GameObj.Transform.Pos.Y / (float)tileSize.Y) * depthPerTile; cullingOut.RenderOriginView.Z += this.offset + this.tileDepthOffset * depthPerTile + originDepthOffset; // Prepare vertex generation data Vector2 tileXStep = cullingOut.XAxisView * cullingIn.TileSize.X; Vector2 tileYStep = cullingOut.YAxisView * cullingIn.TileSize.Y; Vector3 renderPos = cullingOut.RenderOriginView; Point2 tileGridPos = cullingOut.VisibleTileStart; // Prepare vertex data array for batch-submitting IReadOnlyGrid<Tile> tiles = tilemap.Tiles; TileInfo[] tileData = tileset.TileData.Data; int submittedTileCount = 0; int vertexBaseIndex = 0; for (int tileIndex = 0; tileIndex < renderedTileCount; tileIndex++) { Tile tile = tiles[tileGridPos.X, tileGridPos.Y]; if (tile.Index < tileData.Length) { Rect uvRect = tileData[tile.Index].TexCoord0; bool visualEmpty = tileData[tile.Index].IsVisuallyEmpty; int tileBaseOffset = tileData[tile.Index].DepthOffset; float localDepthOffset = (tile.DepthOffset + tileBaseOffset) * depthPerTile; if (!visualEmpty) { vertexData[vertexBaseIndex + 0].Pos.X = renderPos.X; vertexData[vertexBaseIndex + 0].Pos.Y = renderPos.Y; vertexData[vertexBaseIndex + 0].Pos.Z = renderPos.Z + localDepthOffset; vertexData[vertexBaseIndex + 0].TexCoord.X = uvRect.X; vertexData[vertexBaseIndex + 0].TexCoord.Y = uvRect.Y; vertexData[vertexBaseIndex + 0].Color = mainColor; vertexData[vertexBaseIndex + 1].Pos.X = renderPos.X + tileYStep.X; vertexData[vertexBaseIndex + 1].Pos.Y = renderPos.Y + tileYStep.Y; vertexData[vertexBaseIndex + 1].Pos.Z = renderPos.Z + localDepthOffset + depthPerTile; vertexData[vertexBaseIndex + 1].TexCoord.X = uvRect.X; vertexData[vertexBaseIndex + 1].TexCoord.Y = uvRect.Y + uvRect.H; vertexData[vertexBaseIndex + 1].Color = mainColor; vertexData[vertexBaseIndex + 2].Pos.X = renderPos.X + tileXStep.X + tileYStep.X; vertexData[vertexBaseIndex + 2].Pos.Y = renderPos.Y + tileXStep.Y + tileYStep.Y; vertexData[vertexBaseIndex + 2].Pos.Z = renderPos.Z + localDepthOffset + depthPerTile; vertexData[vertexBaseIndex + 2].TexCoord.X = uvRect.X + uvRect.W; vertexData[vertexBaseIndex + 2].TexCoord.Y = uvRect.Y + uvRect.H; vertexData[vertexBaseIndex + 2].Color = mainColor; vertexData[vertexBaseIndex + 3].Pos.X = renderPos.X + tileXStep.X; vertexData[vertexBaseIndex + 3].Pos.Y = renderPos.Y + tileXStep.Y; vertexData[vertexBaseIndex + 3].Pos.Z = renderPos.Z + localDepthOffset; vertexData[vertexBaseIndex + 3].TexCoord.X = uvRect.X + uvRect.W; vertexData[vertexBaseIndex + 3].TexCoord.Y = uvRect.Y; vertexData[vertexBaseIndex + 3].Color = mainColor; bool vertical = tileData[tile.Index].IsVertical; if (vertical) { vertexData[vertexBaseIndex + 0].Pos.Z += depthPerTile; vertexData[vertexBaseIndex + 3].Pos.Z += depthPerTile; } submittedTileCount++; vertexBaseIndex += 4; } } tileGridPos.X++; renderPos.X += tileXStep.X; renderPos.Y += tileXStep.Y; if ((tileGridPos.X - cullingOut.VisibleTileStart.X) >= cullingOut.VisibleTileCount.X) { tileGridPos.X = cullingOut.VisibleTileStart.X; tileGridPos.Y++; renderPos = cullingOut.RenderOriginView; renderPos.X += tileYStep.X * (tileGridPos.Y - cullingOut.VisibleTileStart.Y); renderPos.Y += tileYStep.Y * (tileGridPos.Y - cullingOut.VisibleTileStart.Y); renderPos.Z += tileGridPos.Y * depthPerTile; } } // Submit all the vertices as one draw batch device.AddVertices( material, VertexMode.Quads, vertexData, submittedTileCount * 4); Profile.AddToStat(@"Duality\Stats\Render\Tilemaps\NumTiles", renderedTileCount); Profile.AddToStat(@"Duality\Stats\Render\Tilemaps\NumVertices", submittedTileCount * 4); }
public override void Draw(IDrawDevice device) { Vector3 posTemp = this.gameobj.Transform.Pos; float scaleTemp = 1.0f; device.PreprocessCoords(ref posTemp, ref scaleTemp); Vector2 xDot, yDot; MathF.GetTransformDotVec(this.GameObj.Transform.Angle, this.gameobj.Transform.Scale * scaleTemp, out xDot, out yDot); // Apply block alignment Vector2 textOffset = Vector2.Zero; Vector2 textSize = this.text.Size; if (this.text.MaxWidth > 0) textSize.X = this.text.MaxWidth; this.blockAlign.ApplyTo(ref textOffset, textSize); MathF.TransformDotVec(ref textOffset, ref xDot, ref yDot); posTemp.X += textOffset.X; posTemp.Y += textOffset.Y; if (this.text.Fonts != null && this.text.Fonts.Any(r => r.IsAvailable && r.Res.IsPixelGridAligned)) { posTemp.X = MathF.Round(posTemp.X); posTemp.Y = MathF.Round(posTemp.Y); if (MathF.RoundToInt(device.TargetSize.X) != (MathF.RoundToInt(device.TargetSize.X) / 2) * 2) posTemp.X += 0.5f; if (MathF.RoundToInt(device.TargetSize.Y) != (MathF.RoundToInt(device.TargetSize.Y) / 2) * 2) posTemp.Y += 0.5f; } ColorRgba matColor = this.customMat != null ? this.customMat.MainColor : ColorRgba.White; int[] vertLen = this.text.EmitVertices(ref this.vertFont, ref this.vertIcon, posTemp.X, posTemp.Y, posTemp.Z + this.VertexZOffset, this.colorTint * matColor, xDot, yDot); if (this.text.Fonts != null) { for (int i = 0; i < this.text.Fonts.Length; i++) { if (this.text.Fonts[i] != null && this.text.Fonts[i].IsAvailable) { if (this.customMat == null) { device.AddVertices(this.text.Fonts[i].Res.Material, VertexMode.Quads, this.vertFont[i], vertLen[i + 1]); } else { BatchInfo cm = new BatchInfo(this.customMat); cm.Textures = this.text.Fonts[i].Res.Material.Textures; device.AddVertices(cm, VertexMode.Quads, this.vertFont[i], vertLen[i + 1]); } } } } if (this.text.Icons != null && this.iconMat.IsAvailable) { device.AddVertices(this.iconMat, VertexMode.Quads, this.vertIcon, vertLen[0]); } }
public override void Draw(IDrawDevice device) { if (this.particles == null) return; Texture tex = this.RetrieveTexture(); if (tex == null) return; Vector2 particleHalfSize = this.particleSize * 0.5f; float objAngle; float objScale; Vector3 objPos; if (this.worldSpace) { objAngle = 0.0f; objScale = 1.0f; objPos = Vector3.Zero; } else { objAngle = this.GameObj.Transform.Angle; objScale = this.GameObj.Transform.Scale; objPos = this.GameObj.Transform.Pos; } Vector2 objXDot, objYDot; MathF.GetTransformDotVec(objAngle, objScale, out objXDot, out objYDot); if (this.vertexBuffer == null) this.vertexBuffer = new RawList<VertexC1P3T2>(this.particles.Count * 4); this.vertexBuffer.Count = this.vertexBuffer.Count = this.particles.Count * 4; VertexC1P3T2[] vertexData = this.vertexBuffer.Data; Particle[] particleData = this.particles.Data; int particleCount = this.particles.Count; for (int i = 0; i < particleCount; i++) { ColorRgba color = particleData[i].Color; float alpha = (float)color.A / 255.0f; if (this.fadeOutAt < 1.0f) alpha *= MathF.Clamp((1.0f - particleData[i].AgeFactor) / this.fadeOutAt, 0.0f, 1.0f); if (this.fadeInAt > 0.0f) alpha *= MathF.Clamp(particleData[i].AgeFactor / this.fadeInAt, 0.0f, 1.0f); color.A = (byte)(alpha * 255.0f); Rect uvRect; tex.LookupAtlas(particleData[i].SpriteIndex, out uvRect); Vector3 particlePos = particleData[i].Position; MathF.TransformDotVec(ref particlePos, ref objXDot, ref objYDot); particlePos += objPos; float particleAngle = objAngle + particleData[i].Angle; float particleScale = objScale; device.PreprocessCoords(ref particlePos, ref particleScale); Vector2 xDot, yDot; MathF.GetTransformDotVec(particleAngle, particleScale, out xDot, out yDot); Vector2 edgeTopLeft = new Vector2(-particleHalfSize.X, -particleHalfSize.Y); Vector2 edgeBottomLeft = new Vector2(-particleHalfSize.X, particleHalfSize.Y); Vector2 edgeBottomRight = new Vector2(particleHalfSize.X, particleHalfSize.Y); Vector2 edgeTopRight = new Vector2(particleHalfSize.X, -particleHalfSize.Y); MathF.TransformDotVec(ref edgeTopLeft, ref xDot, ref yDot); MathF.TransformDotVec(ref edgeBottomLeft, ref xDot, ref yDot); MathF.TransformDotVec(ref edgeBottomRight, ref xDot, ref yDot); MathF.TransformDotVec(ref edgeTopRight, ref xDot, ref yDot); int vertexBaseIndex = i * 4; vertexData[vertexBaseIndex + 0].Pos.X = particlePos.X + edgeTopLeft.X; vertexData[vertexBaseIndex + 0].Pos.Y = particlePos.Y + edgeTopLeft.Y; vertexData[vertexBaseIndex + 0].Pos.Z = particlePos.Z; vertexData[vertexBaseIndex + 0].TexCoord.X = uvRect.X; vertexData[vertexBaseIndex + 0].TexCoord.Y = uvRect.Y; vertexData[vertexBaseIndex + 0].Color = color; vertexData[vertexBaseIndex + 1].Pos.X = particlePos.X + edgeBottomLeft.X; vertexData[vertexBaseIndex + 1].Pos.Y = particlePos.Y + edgeBottomLeft.Y; vertexData[vertexBaseIndex + 1].Pos.Z = particlePos.Z; vertexData[vertexBaseIndex + 1].TexCoord.X = uvRect.X; vertexData[vertexBaseIndex + 1].TexCoord.Y = uvRect.BottomY; vertexData[vertexBaseIndex + 1].Color = color; vertexData[vertexBaseIndex + 2].Pos.X = particlePos.X + edgeBottomRight.X; vertexData[vertexBaseIndex + 2].Pos.Y = particlePos.Y + edgeBottomRight.Y; vertexData[vertexBaseIndex + 2].Pos.Z = particlePos.Z; vertexData[vertexBaseIndex + 2].TexCoord.X = uvRect.RightX; vertexData[vertexBaseIndex + 2].TexCoord.Y = uvRect.BottomY; vertexData[vertexBaseIndex + 2].Color = color; vertexData[vertexBaseIndex + 3].Pos.X = particlePos.X + edgeTopRight.X; vertexData[vertexBaseIndex + 3].Pos.Y = particlePos.Y + edgeTopRight.Y; vertexData[vertexBaseIndex + 3].Pos.Z = particlePos.Z; vertexData[vertexBaseIndex + 3].TexCoord.X = uvRect.RightX; vertexData[vertexBaseIndex + 3].TexCoord.Y = uvRect.Y; vertexData[vertexBaseIndex + 3].Color = color; } device.AddVertices(this.material, VertexMode.Quads, vertexData, this.vertexBuffer.Count); }
public override void Draw(IDrawDevice device) { Vector3 posTemp = this.gameobj.Transform.Pos; float scaleTemp = 1.0f; device.PreprocessCoords(ref posTemp, ref scaleTemp); Vector2 xDot, yDot; MathF.GetTransformDotVec(this.GameObj.Transform.Angle, this.gameobj.Transform.Scale * scaleTemp, out xDot, out yDot); // Apply block alignment Vector2 textOffset = Vector2.Zero; Vector2 textSize = this.text.Size; if (this.text.MaxWidth > 0) { textSize.X = this.text.MaxWidth; } this.blockAlign.ApplyTo(ref textOffset, textSize); MathF.TransformDotVec(ref textOffset, ref xDot, ref yDot); posTemp.X += textOffset.X; posTemp.Y += textOffset.Y; if (this.text.Fonts != null && this.text.Fonts.Any(r => r.IsAvailable && r.Res.IsPixelGridAligned)) { posTemp.X = MathF.Round(posTemp.X); posTemp.Y = MathF.Round(posTemp.Y); if (MathF.RoundToInt(device.TargetSize.X) != (MathF.RoundToInt(device.TargetSize.X) / 2) * 2) { posTemp.X += 0.5f; } if (MathF.RoundToInt(device.TargetSize.Y) != (MathF.RoundToInt(device.TargetSize.Y) / 2) * 2) { posTemp.Y += 0.5f; } } // Draw design time metrics data if (DualityApp.ExecContext == DualityApp.ExecutionContext.Editor) { bool showLimits = true; bool showLines = false; bool showElements = false; Vector3 metricsOffset = new Vector3(0.0f, 0.0f, 0.01f); Vector3 lineOffset = new Vector3(0.5f, 0.5f, 0.0f); Vector3 tUnitX = Vector3.UnitX; Vector3 tUnitY = Vector3.UnitY; MathF.TransformDotVec(ref tUnitX, ref xDot, ref yDot); MathF.TransformDotVec(ref tUnitY, ref xDot, ref yDot); // Actual text size and maximum text size if (showLimits) { Vector3 textWidth = tUnitX * this.text.Size.X; Vector3 textHeight = tUnitY * this.text.Size.Y; Vector3 textMaxWidth = tUnitX * this.text.MaxWidth; Vector3 textMaxHeight = tUnitY * MathF.Max(this.text.MaxHeight, this.text.Size.Y); ColorRgba clrSize = ColorRgba.Green.WithAlpha(128); ColorRgba clrMaxSize = ColorRgba.Red.WithAlpha(128); device.AddVertices(new BatchInfo(DrawTechnique.Alpha, ColorRgba.White), VertexMode.LineLoop, new VertexC1P3(metricsOffset + lineOffset + posTemp, clrSize), new VertexC1P3(metricsOffset + lineOffset + posTemp + textWidth, clrSize), new VertexC1P3(metricsOffset + lineOffset + posTemp + textWidth + textHeight, clrSize), new VertexC1P3(metricsOffset + lineOffset + posTemp + textHeight, clrSize)); device.AddVertices(new BatchInfo(DrawTechnique.Alpha, ColorRgba.White), VertexMode.LineLoop, new VertexC1P3(metricsOffset + lineOffset + posTemp, clrMaxSize), new VertexC1P3(metricsOffset + lineOffset + posTemp + textMaxWidth, clrMaxSize), new VertexC1P3(metricsOffset + lineOffset + posTemp + textMaxWidth + textMaxHeight, clrMaxSize), new VertexC1P3(metricsOffset + lineOffset + posTemp + textMaxHeight, clrMaxSize)); } // Individual line sizes if (showLines) { ColorRgba clrLineBg = (ColorRgba.Blue + ColorRgba.Red).WithAlpha(64); for (int i = 0; i < this.text.TextMetrics.LineBounds.Count; i++) { Rect lineRect = this.text.TextMetrics.LineBounds[i]; device.AddVertices(new BatchInfo(DrawTechnique.Alpha, ColorRgba.White), VertexMode.Quads, new VertexC1P3(metricsOffset + posTemp + lineRect.TopLeft.X * tUnitX + lineRect.TopLeft.Y * tUnitY, clrLineBg), new VertexC1P3(metricsOffset + posTemp + lineRect.BottomLeft.X * tUnitX + lineRect.BottomLeft.Y * tUnitY, clrLineBg), new VertexC1P3(metricsOffset + posTemp + lineRect.BottomRight.X * tUnitX + lineRect.BottomRight.Y * tUnitY, clrLineBg), new VertexC1P3(metricsOffset + posTemp + lineRect.TopRight.X * tUnitX + lineRect.TopRight.Y * tUnitY, clrLineBg)); } } // Individual line sizes if (showElements) { ColorRgba clrElementBg = (ColorRgba.Blue + ColorRgba.Green).WithAlpha(128); for (int i = 0; i < this.text.TextMetrics.ElementBounds.Count; i++) { Rect elemRect = this.text.TextMetrics.ElementBounds[i]; device.AddVertices(new BatchInfo(DrawTechnique.Alpha, ColorRgba.White), VertexMode.LineLoop, new VertexC1P3(metricsOffset + lineOffset + posTemp + elemRect.TopLeft.X * tUnitX + elemRect.TopLeft.Y * tUnitY, clrElementBg), new VertexC1P3(metricsOffset + lineOffset + posTemp + elemRect.BottomLeft.X * tUnitX + elemRect.BottomLeft.Y * tUnitY, clrElementBg), new VertexC1P3(metricsOffset + lineOffset + posTemp + elemRect.BottomRight.X * tUnitX + elemRect.BottomRight.Y * tUnitY, clrElementBg), new VertexC1P3(metricsOffset + lineOffset + posTemp + elemRect.TopRight.X * tUnitX + elemRect.TopRight.Y * tUnitY, clrElementBg)); } } } ColorRgba matColor = this.customMat != null ? this.customMat.MainColor : ColorRgba.White; int[] vertLen = this.text.EmitVertices(ref this.vertFont, ref this.vertIcon, posTemp.X, posTemp.Y, posTemp.Z, this.colorTint * matColor, xDot, yDot); if (this.text.Fonts != null) { for (int i = 0; i < this.text.Fonts.Length; i++) { if (this.text.Fonts[i] != null && this.text.Fonts[i].IsAvailable) { if (this.customMat == null) { device.AddVertices(this.text.Fonts[i].Res.Material, VertexMode.Quads, this.vertFont[i], vertLen[i + 1]); } else { BatchInfo cm = new BatchInfo(this.customMat); cm.Textures = this.text.Fonts[i].Res.Material.Textures; device.AddVertices(cm, VertexMode.Quads, this.vertFont[i], vertLen[i + 1]); } } } } if (this.text.Icons != null && this.iconMat.IsAvailable) { device.AddVertices(this.iconMat, VertexMode.Quads, this.vertIcon, vertLen[0]); } }
public override void Draw(IDrawDevice device) { Vector3 posTemp = this.gameobj.Transform.Pos; float scaleTemp = 1.0f; device.PreprocessCoords(ref posTemp, ref scaleTemp); Vector2 xDot, yDot; MathF.GetTransformDotVec(this.GameObj.Transform.Angle, this.gameobj.Transform.Scale * scaleTemp, out xDot, out yDot); // Apply block alignment Vector2 textOffset = Vector2.Zero; Vector2 textSize = this.text.Size; if (this.text.MaxWidth > 0) textSize.X = this.text.MaxWidth; this.blockAlign.ApplyTo(ref textOffset, textSize); MathF.TransformDotVec(ref textOffset, ref xDot, ref yDot); posTemp.X += textOffset.X; posTemp.Y += textOffset.Y; if (this.text.Fonts != null && this.text.Fonts.Any(r => r.IsAvailable && r.Res.IsPixelGridAligned)) { posTemp.X = MathF.Round(posTemp.X); posTemp.Y = MathF.Round(posTemp.Y); if (MathF.RoundToInt(device.TargetSize.X) != (MathF.RoundToInt(device.TargetSize.X) / 2) * 2) posTemp.X += 0.5f; if (MathF.RoundToInt(device.TargetSize.Y) != (MathF.RoundToInt(device.TargetSize.Y) / 2) * 2) posTemp.Y += 0.5f; } // Draw design time metrics data if (DualityApp.ExecContext == DualityApp.ExecutionContext.Editor) { bool showLimits = true; bool showLines = false; bool showElements = false; Vector3 metricsOffset = new Vector3(0.0f, 0.0f, 0.01f); Vector3 lineOffset = new Vector3(0.5f, 0.5f, 0.0f); Vector3 tUnitX = Vector3.UnitX; Vector3 tUnitY = Vector3.UnitY; MathF.TransformDotVec(ref tUnitX, ref xDot, ref yDot); MathF.TransformDotVec(ref tUnitY, ref xDot, ref yDot); // Actual text size and maximum text size if (showLimits) { Vector3 textWidth = tUnitX * this.text.Size.X; Vector3 textHeight = tUnitY * this.text.Size.Y; Vector3 textMaxWidth = tUnitX * this.text.MaxWidth; Vector3 textMaxHeight = tUnitY * MathF.Max(this.text.MaxHeight, this.text.Size.Y); ColorRgba clrSize = ColorRgba.Green.WithAlpha(128); ColorRgba clrMaxSize = ColorRgba.Red.WithAlpha(128); device.AddVertices(new BatchInfo(DrawTechnique.Alpha, ColorRgba.White), VertexMode.LineLoop, new VertexC1P3(metricsOffset + lineOffset + posTemp, clrSize), new VertexC1P3(metricsOffset + lineOffset + posTemp + textWidth, clrSize), new VertexC1P3(metricsOffset + lineOffset + posTemp + textWidth + textHeight, clrSize), new VertexC1P3(metricsOffset + lineOffset + posTemp + textHeight, clrSize)); device.AddVertices(new BatchInfo(DrawTechnique.Alpha, ColorRgba.White), VertexMode.LineLoop, new VertexC1P3(metricsOffset + lineOffset + posTemp, clrMaxSize), new VertexC1P3(metricsOffset + lineOffset + posTemp + textMaxWidth, clrMaxSize), new VertexC1P3(metricsOffset + lineOffset + posTemp + textMaxWidth + textMaxHeight, clrMaxSize), new VertexC1P3(metricsOffset + lineOffset + posTemp + textMaxHeight, clrMaxSize)); } // Individual line sizes if (showLines) { ColorRgba clrLineBg = (ColorRgba.Blue + ColorRgba.Red).WithAlpha(64); for (int i = 0; i < this.text.TextMetrics.LineBounds.Count; i++) { Rect lineRect = this.text.TextMetrics.LineBounds[i]; device.AddVertices(new BatchInfo(DrawTechnique.Alpha, ColorRgba.White), VertexMode.Quads, new VertexC1P3(metricsOffset + posTemp + lineRect.TopLeft.X * tUnitX + lineRect.TopLeft.Y * tUnitY, clrLineBg), new VertexC1P3(metricsOffset + posTemp + lineRect.BottomLeft.X * tUnitX + lineRect.BottomLeft.Y * tUnitY, clrLineBg), new VertexC1P3(metricsOffset + posTemp + lineRect.BottomRight.X * tUnitX + lineRect.BottomRight.Y * tUnitY, clrLineBg), new VertexC1P3(metricsOffset + posTemp + lineRect.TopRight.X * tUnitX + lineRect.TopRight.Y * tUnitY, clrLineBg)); } } // Individual line sizes if (showElements) { ColorRgba clrElementBg = (ColorRgba.Blue + ColorRgba.Green).WithAlpha(128); for (int i = 0; i < this.text.TextMetrics.ElementBounds.Count; i++) { Rect elemRect = this.text.TextMetrics.ElementBounds[i]; device.AddVertices(new BatchInfo(DrawTechnique.Alpha, ColorRgba.White), VertexMode.LineLoop, new VertexC1P3(metricsOffset + lineOffset + posTemp + elemRect.TopLeft.X * tUnitX + elemRect.TopLeft.Y * tUnitY, clrElementBg), new VertexC1P3(metricsOffset + lineOffset + posTemp + elemRect.BottomLeft.X * tUnitX + elemRect.BottomLeft.Y * tUnitY, clrElementBg), new VertexC1P3(metricsOffset + lineOffset + posTemp + elemRect.BottomRight.X * tUnitX + elemRect.BottomRight.Y * tUnitY, clrElementBg), new VertexC1P3(metricsOffset + lineOffset + posTemp + elemRect.TopRight.X * tUnitX + elemRect.TopRight.Y * tUnitY, clrElementBg)); } } } ColorRgba matColor = this.customMat != null ? this.customMat.MainColor : ColorRgba.White; int[] vertLen = this.text.EmitVertices(ref this.vertFont, ref this.vertIcon, posTemp.X, posTemp.Y, posTemp.Z, this.colorTint * matColor, xDot, yDot); if (this.text.Fonts != null) { for (int i = 0; i < this.text.Fonts.Length; i++) { if (this.text.Fonts[i] != null && this.text.Fonts[i].IsAvailable) { if (this.customMat == null) { device.AddVertices(this.text.Fonts[i].Res.Material, VertexMode.Quads, this.vertFont[i], vertLen[i + 1]); } else { BatchInfo cm = new BatchInfo(this.customMat); cm.Textures = this.text.Fonts[i].Res.Material.Textures; device.AddVertices(cm, VertexMode.Quads, this.vertFont[i], vertLen[i + 1]); } } } } if (this.text.Icons != null && this.iconMat.IsAvailable) { device.AddVertices(this.iconMat, VertexMode.Quads, this.vertIcon, vertLen[0]); } }
public override void Draw(IDrawDevice device) { Vector3 posTemp = this.gameobj.Transform.Pos; float scaleTemp = 1.0f; device.PreprocessCoords(ref posTemp, ref scaleTemp); Vector2 xDot, yDot; MathF.GetTransformDotVec(this.GameObj.Transform.Angle, this.gameobj.Transform.Scale * scaleTemp, out xDot, out yDot); // Apply block alignment Vector2 textOffset = Vector2.Zero; Vector2 textSize = this.text.Size; if (this.text.MaxWidth > 0) { textSize.X = this.text.MaxWidth; } this.blockAlign.ApplyTo(ref textOffset, textSize); MathF.TransformDotVec(ref textOffset, ref xDot, ref yDot); posTemp.X += textOffset.X; posTemp.Y += textOffset.Y; if (this.text.Fonts != null && this.text.Fonts.Any(r => r.IsAvailable && r.Res.IsPixelGridAligned)) { posTemp.X = MathF.Round(posTemp.X); posTemp.Y = MathF.Round(posTemp.Y); if (MathF.RoundToInt(device.TargetSize.X) != (MathF.RoundToInt(device.TargetSize.X) / 2) * 2) { posTemp.X += 0.5f; } if (MathF.RoundToInt(device.TargetSize.Y) != (MathF.RoundToInt(device.TargetSize.Y) / 2) * 2) { posTemp.Y += 0.5f; } } ColorRgba matColor = this.customMat != null ? this.customMat.MainColor : ColorRgba.White; int[] vertLen = this.text.EmitVertices(ref this.vertFont, ref this.vertIcon, posTemp.X, posTemp.Y, posTemp.Z + this.VertexZOffset, this.colorTint * matColor, xDot, yDot); if (this.text.Fonts != null) { for (int i = 0; i < this.text.Fonts.Length; i++) { if (this.text.Fonts[i] != null && this.text.Fonts[i].IsAvailable) { if (this.customMat == null) { device.AddVertices(this.text.Fonts[i].Res.Material, VertexMode.Quads, this.vertFont[i], vertLen[i + 1]); } else { BatchInfo cm = new BatchInfo(this.customMat); cm.Textures = this.text.Fonts[i].Res.Material.Textures; device.AddVertices(cm, VertexMode.Quads, this.vertFont[i], vertLen[i + 1]); } } } } if (this.text.Icons != null && this.iconMat.IsAvailable) { device.AddVertices(this.iconMat, VertexMode.Quads, this.vertIcon, vertLen[0]); } }
public override void Draw(IDrawDevice device) { Texture mainTex = this.RetrieveMainTex(); ColorRgba mainClr = this.RetrieveMainColor(); Rect uvRect; if (mainTex != null) { if (this.spriteIndex < 0) uvRect = new Rect(mainTex.UVRatio.X, mainTex.UVRatio.Y); else mainTex.LookupAtlas(this.spriteIndex, out uvRect); } else { uvRect = new Rect(1.0f, 1.0f); } this.PrepareVertices(ref this.vertices, device, mainClr, uvRect); if (this.customMat != null) device.AddVertices(this.customMat, VertexMode.Quads, this.vertices); else device.AddVertices(this.sharedMat, VertexMode.Quads, this.vertices); }
protected internal override void OnCollectDrawcalls(Canvas canvas) { base.OnCollectDrawcalls(canvas); IDrawDevice device = canvas.DrawDevice; float scaleTemp = 1.0f; Vector3 posTemp = Vector3.Zero; device.PreprocessCoords(ref posTemp, ref scaleTemp); if (posTemp.Z <= canvas.DrawDevice.NearZ) { return; } float alphaTemp = 0.5f; alphaTemp *= (float)Math.Min(1.0d, ((posTemp.Z - device.NearZ) / (device.NearZ * 5.0f))); if (alphaTemp <= 0.005f) { return; } float stepTemp = 4.0f * this.gridSize * MathF.Max(0.25f, MathF.Pow(2.0f, -MathF.Round(1.0f - MathF.Log(1.0f / scaleTemp, 2.0f)))); float scaledStep = stepTemp * scaleTemp; float viewBoundRad = device.TargetSize.Length * 0.5f; int lineCount = (2 + (int)MathF.Ceiling(viewBoundRad * 2 / scaledStep)) * 4; ColorRgba gridColor = this.FgColor.WithAlpha(alphaTemp); VertexC1P3[] vertices = new VertexC1P3[lineCount * 4]; float beginPos; float pos; int lineIndex; int vertOff = 0; beginPos = posTemp.X % scaledStep - (lineCount / 8) * scaledStep; pos = beginPos; lineIndex = 0; for (int x = 0; x < lineCount; x++) { bool primaryLine = lineIndex % 4 == 0; bool secondaryLine = lineIndex % 4 == 2; vertices[vertOff + x * 2 + 0].Color = primaryLine ? gridColor : gridColor.WithAlpha(alphaTemp * (secondaryLine ? 0.5f : 0.25f)); vertices[vertOff + x * 2 + 0].Pos.X = pos; vertices[vertOff + x * 2 + 0].Pos.Y = -viewBoundRad; vertices[vertOff + x * 2 + 0].Pos.Z = posTemp.Z + 1; vertices[vertOff + x * 2 + 1] = vertices[vertOff + x * 2 + 0]; vertices[vertOff + x * 2 + 1].Pos.Y = viewBoundRad; pos += scaledStep / 4; lineIndex++; } vertOff += lineCount * 2; beginPos = posTemp.Y % scaledStep - (lineCount / 8) * scaledStep; pos = beginPos; lineIndex = 0; for (int y = 0; y < lineCount; y++) { bool primaryLine = lineIndex % 4 == 0; bool secondaryLine = lineIndex % 4 == 2; vertices[vertOff + y * 2 + 0].Color = primaryLine ? gridColor : gridColor.WithAlpha(alphaTemp * (secondaryLine ? 0.5f : 0.25f)); vertices[vertOff + y * 2 + 0].Pos.X = -viewBoundRad; vertices[vertOff + y * 2 + 0].Pos.Y = pos; vertices[vertOff + y * 2 + 0].Pos.Z = posTemp.Z + 1; vertices[vertOff + y * 2 + 1] = vertices[vertOff + y * 2 + 0]; vertices[vertOff + y * 2 + 1].Pos.X = viewBoundRad; pos += scaledStep / 4; lineIndex++; } vertOff += lineCount * 2; device.AddVertices(new BatchInfo(DrawTechnique.Alpha, ColorRgba.White), VertexMode.Lines, vertices); }
public override void Draw(IDrawDevice device) { ColorRgba mainClr = this.RetrieveMainColor(); List<Texture> texList = this.RetrieveMainTex(); for (int i = 0; i < texList.Count; i++) { Rect uvRect; if (texList[i] != null) { if (this.rectMode == UVMode.WrapBoth) uvRect = new Rect(texList[i].UVRatio.X * this.rect.W / texList[i].PixelWidth, texList[i].UVRatio.Y * this.rect.H / texList[i].PixelHeight); else if (this.rectMode == UVMode.WrapHorizontal) uvRect = new Rect(texList[i].UVRatio.X * this.rect.W / texList[i].PixelWidth, texList[i].UVRatio.Y); else if (this.rectMode == UVMode.WrapVertical) uvRect = new Rect(texList[i].UVRatio.X, texList[i].UVRatio.Y * this.rect.H / texList[i].PixelHeight); else uvRect = new Rect(texList[i].UVRatio.X, texList[i].UVRatio.Y); } else uvRect = new Rect(1.0f, 1.0f); this.PrepareVertices(ref this.vertices, device, mainClr, uvRect); foreach (var item in this.sharedMat) { device.AddVertices(item, VertexMode.Quads, this.vertices); } } }
public override void Draw(IDrawDevice device) { if (this.camComp.DrawDevice != device) return; List<VertexC1P3> vertices = new List<VertexC1P3>((2 * this.layerCount * this.starsPerLayer) * 2); float screenBoundRad = this.camComp.ViewBoundingRadius; float minZDist = this.camComp.NearZ + this.camComp.ParallaxRefDist / 50.0f; float stepTemp = this.TileSize; Vector2 minPos = new Vector2(-screenBoundRad, -screenBoundRad); Vector2 maxPos = new Vector2(screenBoundRad, screenBoundRad); // Iterate over star layers for (int layerIndex = 0; layerIndex < this.layerCount; layerIndex++) { StarLayer layer = this.starLayers[layerIndex]; // Determine the layers Z value & perform layer culling if too near float layerZ = this.layerDepth * ((float)layerIndex / (float)this.layerCount) - this.GameObj.Transform.Pos.Z; if (layerZ < 0.0f) layerZ += this.layerDepth * (float)(1 + (int)(-layerZ / this.layerDepth)); layerZ = layerZ % this.layerDepth; if (layerZ <= this.GameObj.Transform.Pos.Z + minZDist) continue; // Calculate transform data Vector3 posTemp = this.GameObj.Transform.Pos + Vector3.UnitZ * layerZ; Vector3 posTempTrail = this.GameObj.Transform.Pos + Vector3.UnitZ * (layerZ + this.GameObj.Transform.Vel.Z * this.trailLength); float scaleTemp = 1.0f; float scaleTempTrail = 1.0f; device.PreprocessCoords(this, ref posTemp, ref scaleTemp); device.PreprocessCoords(this, ref posTempTrail, ref scaleTempTrail); posTempTrail += new Vector3(this.GameObj.Transform.Vel.Xy * this.trailLength); // Prepare this layers transformation to calculate a stars trail position. Vector2 starPosTempTrailDotX; Vector2 starPosTempTrailDotY; MathF.GetTransformDotVec(this.GameObj.Transform.AngleVel * this.trailLength, scaleTempTrail, out starPosTempTrailDotX, out starPosTempTrailDotY); // Iterate over stars for (int starIndex = 0; starIndex < layer.stars.Length; starIndex++) { // Since it's an endless starfield, each star may show up on multiple positions at once. It's tiled. StarInfo star = layer.stars[starIndex]; Vector2 starPosBase = star.pos - this.GameObj.Transform.Pos.Xy; // Move the topleft tiling corner to somewhere near the screens top left if (starPosBase.X > minPos.X) starPosBase.X -= stepTemp * MathF.Ceiling((starPosBase.X - minPos.X) / stepTemp); else if (starPosBase.X < minPos.X - stepTemp) starPosBase.X -= stepTemp * MathF.Ceiling((starPosBase.X - minPos.X) / stepTemp); if (starPosBase.Y > minPos.Y) starPosBase.Y -= stepTemp * MathF.Ceiling((starPosBase.Y - minPos.Y) / stepTemp); else if (starPosBase.Y < minPos.Y - stepTemp) starPosBase.Y -= stepTemp * MathF.Ceiling((starPosBase.Y - minPos.Y) / stepTemp); // Tiling! Vector2 startPos = starPosBase; while (starPosBase.X <= maxPos.X && starPosBase.Y <= maxPos.Y) { // Determine star pos and perform culling Vector3 starPosTemp = posTemp + new Vector3(starPosBase) * scaleTemp; if (starPosTemp.X > -screenBoundRad && starPosTemp.Y > -screenBoundRad && starPosTemp.X < screenBoundRad && starPosTemp.Y < screenBoundRad) { // Determine trail pos Vector3 starPosTempTrail = posTempTrail + new Vector3(starPosBase); MathF.TransformDotVec(ref starPosTempTrail, ref starPosTempTrailDotX, ref starPosTempTrailDotY); // Calculate length factor for the star's alpha value. Reduce alpha if too small or too big float lenTemp = (starPosTemp - starPosTempTrail).Length; if (lenTemp < 1.0f) lenTemp = MathF.Max(0.25f, MathF.Sqrt(lenTemp)); else if (lenTemp > 1.0f) lenTemp = 1.0f / MathF.Pow(lenTemp, 0.25f); // Determine the star's alpha value and generate its vertices (star and trail) float alpha = star.brightness * this.brightness * lenTemp * (1.0f - ((layerZ - minZDist) / (this.layerDepth - minZDist))); vertices.Add(new VertexC1P3(starPosTemp, ColorRgba.White.WithAlpha(alpha))); vertices.Add(new VertexC1P3(starPosTempTrail, ColorRgba.White.WithAlpha(alpha * 0.5f))); } // Advance X / Y grid starPosBase.X += stepTemp; if (starPosBase.X > maxPos.X) { starPosBase.X = startPos.X; starPosBase.Y += stepTemp; } } } } // Draw the stars all at once. Since they're not on the same Z layer, this may lead to wronz Z sorting // when interacting with a complex environment that needs Z-Sorting itsself. For this application, it should be sufficient. device.AddVertices(new BatchInfo(DrawTechnique.Add, ColorRgba.White), BeginMode.Lines, vertices.ToArray()); // (Can be fixed by drawing each layer in its own batch because they can be properly Z-sorted by Duality) }
public override void Draw(IDrawDevice device) { Texture mainTex = this.RetrieveMainTex(); ColorRgba mainClr = this.RetrieveMainColor(); DrawTechnique tech = this.RetrieveDrawTechnique(); Rect uvRect; Rect uvRectNext; bool smoothShaderInput = tech != null && tech.PreferredVertexFormat == DrawTechnique.VertexType_C1P3T4A1; this.GetAnimData(mainTex, tech, smoothShaderInput, out uvRect, out uvRectNext); if (!smoothShaderInput) { this.PrepareVertices(ref this.vertices, device, mainClr, uvRect); if (this.customMat != null) device.AddVertices(this.customMat, VertexMode.Quads, this.vertices); else device.AddVertices(this.sharedMat, VertexMode.Quads, this.vertices); } else { this.PrepareVerticesSmooth(ref this.verticesSmooth, device, this.curAnimFrameFade, mainClr, uvRect, uvRectNext); if (this.customMat != null) device.AddVertices(this.customMat, VertexMode.Quads, this.verticesSmooth); else device.AddVertices(this.sharedMat, VertexMode.Quads, this.verticesSmooth); } }
public void Draw( IDrawDevice device ) { if( GameObj == null || !_loaded || Layers == null || Layers.Count == 0 ) return; Vector3 tempPos = GameObj.Transform.Pos; float tempScale = 1f; device.PreprocessCoords( ref tempPos, ref tempScale ); int halfMapW = W / 2; int halfMapH = H / 2; for (var i = Layers.Values.GetEnumerator(); i.MoveNext();) { var layer = i.Current; if (!layer.Visible) continue; for (int y = 0; y < H; y++) { for (int x = 0; x < W; x++) { // Is renderable tile available? int gid = layer.GetTile(x, y); if (gid <= 0) continue; // Get the correct tileset for this GID var tileset = FindTilesetByGID(gid); if (tileset == null) continue; // Remove tileset's FirstGID from the tile ID // so we get the correct position in the image. int tileX = (gid - tileset.FirstGID) % tileset.W; int tileY = (gid - tileset.FirstGID) / tileset.W; // Let 'em float... float tx = (float)tileX; float ty = (float)tileY; float tw = (float)TileW; float th = (float)TileH; float twp = (float)tileset.WPixel; float thp = (float)tileset.HPixel; var vertices = new VertexC1P3T2[4]; // Get tileset main color and set layer's opacity on it var color = tileset.Image.Res.MainColor.WithAlpha( layer.Opacity ); var uvRatio = tileset.Image.Res.MainTexture.Res.UVRatio; // Texture coordinates var uvRect = new Rect( uvRatio.X * (tx * tw) / twp, uvRatio.Y * (ty * th) / thp, uvRatio.X * tw / twp, uvRatio.Y * th / thp ); // Position float posX = tempPos.X + ((float)x - (float)halfMapW) * (float)TileW; float posY = tempPos.Y + ((float)y - (float)halfMapH) * (float)TileH; // Bottom-left vertices[0] = new VertexC1P3T2(); vertices[0].Pos.X = (posX - tw / 2) * tempScale; vertices[0].Pos.Y = (posY + th / 2) * tempScale; vertices[0].Pos.Z = tempPos.Z; vertices[0].TexCoord.X = uvRect.LeftX; vertices[0].TexCoord.Y = uvRect.BottomY; vertices[0].Color = color; // Top-left vertices[1] = new VertexC1P3T2(); vertices[1].Pos.X = (posX - tw / 2) * tempScale; vertices[1].Pos.Y = (posY - th / 2) * tempScale; vertices[1].Pos.Z = tempPos.Z; vertices[1].TexCoord.X = uvRect.LeftX; vertices[1].TexCoord.Y = uvRect.TopY; vertices[1].Color = color; // Top-right vertices[2] = new VertexC1P3T2(); vertices[2].Pos.X = (posX + tw / 2) * tempScale; vertices[2].Pos.Y = (posY - th / 2) * tempScale; vertices[2].Pos.Z = tempPos.Z; vertices[2].TexCoord.X = uvRect.RightX; vertices[2].TexCoord.Y = uvRect.TopY; vertices[2].Color = color; // Bottom-right vertices[3] = new VertexC1P3T2(); vertices[3].Pos.X = (posX + tw / 2) * tempScale; vertices[3].Pos.Y = (posY + th / 2) * tempScale; vertices[3].Pos.Z = tempPos.Z; vertices[3].TexCoord.X = uvRect.RightX; vertices[3].TexCoord.Y = uvRect.BottomY; vertices[3].Color = color; device.AddVertices(tileset.Image, VertexMode.Quads, vertices); } } } }
public override void Draw(IDrawDevice device) { Texture mainTex = this.RetrieveMainTex(); ColorRgba mainClr = this.RetrieveMainColor(); DrawTechnique tech = this.RetrieveDrawTechnique(); Rect uvRect; if (mainTex != null) uvRect = new Rect(mainTex.UVRatio.X, mainTex.UVRatio.Y); else uvRect = new Rect(1.0f, 1.0f); this.PrepareVerticesLight(ref this.verticesLight, device, mainClr, uvRect, tech); if (this.customMat != null) device.AddVertices(this.customMat, VertexMode.Quads, this.verticesLight); else device.AddVertices(this.sharedMat, VertexMode.Quads, this.verticesLight); }