internal void Update(float frameTime) { if (_meleeWeaponAnimation == null) { return; } _timer += frameTime; var(r, g, b, a) = Vector4.Clamp(_meleeWeaponAnimation.Color + _meleeWeaponAnimation.ColorDelta * _timer, Vector4.Zero, Vector4.One); _sprite.Color = new Color(r, g, b, a); switch (_meleeWeaponAnimation.ArcType) { case WeaponArcType.Slash: var angle = Angle.FromDegrees(_meleeWeaponAnimation.Width) / 2; Owner.Transform.LocalRotation = _baseAngle + Angle.Lerp(-angle, angle, (float)(_timer / _meleeWeaponAnimation.Length.TotalSeconds)); break; case WeaponArcType.Poke: _sprite.Offset += (_meleeWeaponAnimation.Speed * frameTime, 0); break; } if (_meleeWeaponAnimation.Length.TotalSeconds <= _timer) { Owner.Delete(); } }
/// <summary> /// Update and get current weights of the weapon variants. /// </summary> /// <returns>Weights. Normalized.</returns> Vector4D UpdateAndGetWeaponVariantWeights(MyHandItemDefinition handItemDefinition) { float characterSpeed; Character.AnimationController.Variables.GetValue(MyAnimationVariableStorageHints.StrIdSpeed, out characterSpeed); bool isWalkingState = MyCharacter.IsRunningState(Character.GetCurrentMovementState()) && characterSpeed > Character.Definition.MaxWalkSpeed; bool isShooting = Character.IsShooting(MyShootActionEnum.PrimaryAction) && (!Character.IsSprinting); bool isInIronSight = Character.ZoomMode == MyZoomModeEnum.IronSight && (!Character.IsSprinting); float deltaW = MyEngineConstants.UPDATE_STEP_SIZE_IN_SECONDS / handItemDefinition.BlendTime; float deltaShootW = MyEngineConstants.UPDATE_STEP_SIZE_IN_SECONDS / handItemDefinition.ShootBlend; // blend into the current variant // if currently shooting/ironsight -> use "shooting" blend speed m_weaponPositionVariantWeightCounters.X += !isWalkingState && !isShooting && !isInIronSight ? deltaW : (isShooting || isInIronSight ? -deltaShootW : -deltaW); m_weaponPositionVariantWeightCounters.Y += isWalkingState && !isShooting && !isInIronSight ? deltaW : (isShooting || isInIronSight ? -deltaShootW : -deltaW); m_weaponPositionVariantWeightCounters.Z += isShooting && !isInIronSight ? deltaShootW : (isInIronSight ? -deltaShootW : -deltaW); m_weaponPositionVariantWeightCounters.W += isInIronSight ? deltaShootW : (isShooting ? -deltaShootW : -deltaW); m_weaponPositionVariantWeightCounters = Vector4.Clamp(m_weaponPositionVariantWeightCounters, Vector4.Zero, Vector4.One); Vector4D rtnWeights = new Vector4D(MathHelper.SmoothStep(0, 1, m_weaponPositionVariantWeightCounters.X), MathHelper.SmoothStep(0, 1, m_weaponPositionVariantWeightCounters.Y), MathHelper.SmoothStep(0, 1, m_weaponPositionVariantWeightCounters.Z), MathHelper.SmoothStep(0, 1, m_weaponPositionVariantWeightCounters.W)); double weightSum = rtnWeights.X + rtnWeights.Y + rtnWeights.Z + rtnWeights.W; return(rtnWeights / weightSum); }
private static Struct888 Pack(ref Vector4 vector) { vector = Vector4.Clamp(vector, Vector4.Zero, Vector4.One); vector *= MaxBytes4; vector += Half4; return(new Struct888((byte)vector.X, (byte)vector.Y, (byte)vector.Z)); }
public void PositionAwareFullImage <TPixel>(TestImageProvider <TPixel> provider) where TPixel : unmanaged, IPixel <TPixel> { provider.RunValidatingProcessorTest( c => c.ProcessPixelRowsAsVector4( (span, offset) => { int y = offset.Y; int x = offset.X; for (int i = 0; i < span.Length; i++) { float sine = MathF.Sin(y), cosine = MathF.Cos(x + i), sum = sine + cosine, abs = MathF.Abs(sum), a = 0.5f + (abs / 2); // Max value for sin(y) + cos(x) is 2 Vector4 v4 = span[i]; float avg = (v4.X + v4.Y + v4.Z) / 3f; var gray = new Vector4(avg, avg, avg, a); span[i] = Vector4.Clamp(gray, Vector4.Zero, Vector4.One); } }), appendPixelTypeToFileName: false); }
public void PositionAwareInBox <TPixel>(TestImageProvider <TPixel> provider) where TPixel : unmanaged, IPixel <TPixel> { provider.RunRectangleConstrainedValidatingProcessorTest( (c, rect) => c.ProcessPixelRowsAsVector4( (span, offset) => { int y = offset.Y; int x = offset.X; for (int i = 0; i < span.Length; i++) { float sine = MathF.Sin(y), cosine = MathF.Cos(x + i), sum = sine + cosine, abs = MathF.Abs(sum), a = 0.5f + (abs / 2); Vector4 v4 = span[i]; float avg = (v4.X + v4.Y + v4.Z) / 3f; var gray = new Vector4(avg, avg, avg, a); span[i] = Vector4.Clamp(gray, Vector4.Zero, Vector4.One); } }, rect)); }
public void FromVector4(Vector4 vector) { vector = Vector4.Clamp(vector, Vector4.Zero, Vector4.One) * Max; this.R = (ushort)MathF.Round(vector.X); this.G = (ushort)MathF.Round(vector.Y); this.B = (ushort)MathF.Round(vector.Z); }
unsafe public void CopyPixels(Rectangle sourceArea, long cbStride, long cbBufferSize, IntPtr pbBuffer) { source.CopyPixels(sourceArea, cbStride, cbBufferSize, pbBuffer); Vector4 vb = vec0, vg = vec1, vr = vec2, vdiv = new Vector4(1f / byte.MaxValue); Vector4 vmin = Vector4.Zero, vmax = new Vector4(byte.MaxValue), vrnd = new Vector4(0.5f); float fmax = vmax.X; int chan = channels; for (int y = 0; y < sourceArea.Height; y++) { byte *ip = (byte *)pbBuffer + y * cbStride, ipe = ip + sourceArea.Width * chan; while (ip < ipe) { var v0 = new Vector4(ip[0], ip[1], ip[2], fmax) * vdiv; float f0 = Vector4.Dot(v0, vb); float f1 = Vector4.Dot(v0, vg); float f2 = Vector4.Dot(v0, vr); v0 = Vector4.Clamp(new Vector4(f0, f1, f2, 0f) * vmax + vrnd, vmin, vmax); ip[0] = (byte)v0.X; ip[1] = (byte)v0.Y; ip[2] = (byte)v0.Z; ip += chan; } } }
public void FromVector4(Vector4 vector) { vector = Vector4.Clamp(vector, Vector4.Zero, Vector4.One); this.R = vector.X; this.G = vector.Y; this.B = vector.Z; this.A = vector.W; }
public Cmyk(Vector4 vector) { vector = Vector4.Clamp(vector, Min, Max); this.C = vector.X; this.M = vector.Y; this.Y = vector.Z; this.K = vector.W; }
public Rgba64(Vector4 vector) { vector = Vector4.Clamp(vector, Vector4.Zero, Vector4.One) * Max; this.R = (ushort)MathF.Round(vector.X); this.G = (ushort)MathF.Round(vector.Y); this.B = (ushort)MathF.Round(vector.Z); this.A = (ushort)MathF.Round(vector.W); }
private static ushort Pack(ref Vector4 vector) { vector = Vector4.Clamp(vector, Vector4.Zero, Vector4.One); return((ushort)((((int)Math.Round(vector.W * 15F) & 0x0F) << 12) | (((int)Math.Round(vector.X * 15F) & 0x0F) << 8) | (((int)Math.Round(vector.Y * 15F) & 0x0F) << 4) | ((int)Math.Round(vector.Z * 15F) & 0x0F))); }
private static Vector4 DivideRound(Vector4 dividend, Vector4 divisor) { // sign(dividend) = max(min(dividend, 1), -1) var sign = Vector4.Clamp(dividend, NegativeOne, Vector4.One); // AlmostRound(dividend/divisor) = dividend/divisior + 0.5*sign(dividend) return((dividend / divisor) + (sign * Offset)); }
public void SetPixel(int x, int y, Vector4 color) { var clampedColor = Vector4.Clamp(color, Vector4.Zero, Vector4.One); var linearRgbColor = new LinearRGBColor(clampedColor.X, clampedColor.Y, clampedColor.Z); var sRgbColor = _converter.ToRGB(linearRgbColor); SetPixel(x, y, new MonoColor((float)sRgbColor.R, (float)sRgbColor.G, (float)sRgbColor.B)); }
/// <inheritdoc/> public void PackFromVector4(Vector4 vector) { Vector4 clamped = Vector4.Clamp(vector, Vector4.Zero, Vector4.One) * 255F; this.R = (byte)Math.Round(clamped.X); this.G = (byte)Math.Round(clamped.Y); this.B = (byte)Math.Round(clamped.Z); this.A = (byte)Math.Round(clamped.W); }
public void ToZyxBytes(byte[] bytes, int startIndex) { Vector4 vector = Vector4.Clamp(this.backingVector, Vector4.Zero, Vector4.One) * MaxBytes; vector += Half; bytes[startIndex] = (byte)vector.Z; bytes[startIndex + 1] = (byte)vector.Y; bytes[startIndex + 2] = (byte)vector.X; }
private static ulong Pack(ref Vector4 vector) { vector = Vector4.Clamp(vector, Vector4.Zero, Vector4.One); return((ulong)( (uint)FloatHelper.PackFloatToFloat16(vector.X) | ((uint)FloatHelper.PackFloatToFloat16(vector.Y) << 16) | ((uint)FloatHelper.PackFloatToFloat16(vector.Z) << 32) | ((uint)FloatHelper.PackFloatToFloat16(vector.W) << 48))); }
private Vector4 ToByteScaledVector4() { var vector = this.ToVector4(); vector *= MaxBytes; vector += Half; vector = Vector4.Clamp(vector, Vector4.Zero, MaxBytes); return(vector); }
/// <summary> /// Clamps <see cref="ComputeColors.ComputeFloat4"/> value within a specified range [min; max]. /// </summary> /// <param name="key">Input scalar.</param> /// <param name="min">The minimum value.</param> /// <param name="max">The maximum value.</param> public static void ClampFloat4([NotNull] this IComputeColor key, ref Vector4 min, ref Vector4 max) { var asFloat4 = key as ComputeColors.ComputeFloat4; if (asFloat4 != null) { asFloat4.Value = Vector4.Clamp(asFloat4.Value, min, max); } }
private static ushort Pack(ref Vector4 vector) { vector = Vector4.Clamp(vector, Vector4.Zero, Vector4.One); return((ushort)( (((int)Math.Round(vector.X * 31F) & 0x1F) << 10) | (((int)Math.Round(vector.Y * 31F) & 0x1F) << 5) | (((int)Math.Round(vector.Z * 31F) & 0x1F) << 0) | (((int)Math.Round(vector.W) & 0x1) << 15))); }
private static ulong Pack(ref Vector4 vector) { vector = Vector4.Clamp(vector, Vector4.Zero, Vector4.One); return((ulong)( ((uint)Math.Round(vector.X * 65535F) & 65535) | (((uint)Math.Round(vector.Y * 65535F) & 65535) << 16) | (((uint)Math.Round(vector.Z * 65535F) & 65535) << 32) | (((uint)Math.Round(vector.W * 65535F) & 65535) << 48))); }
public void Convolve <TPixel>( Vector2 transformedPoint, int column, ref float ySpanRef, ref float xSpanRef, Buffer2D <TPixel> sourcePixels, Span <Vector4> targetRow) where TPixel : struct, IPixel <TPixel> { // Clamp sampling pixel radial extents to the source image edges Vector2 minXY = transformedPoint - this.extents; Vector2 maxXY = transformedPoint + this.extents; // left, top, right, bottom var extents = new Vector4( MathF.Ceiling(minXY.X - .5F), MathF.Ceiling(minXY.Y - .5F), MathF.Floor(maxXY.X + .5F), MathF.Floor(maxXY.Y + .5F)); extents = Vector4.Clamp(extents, Vector4.Zero, this.maxSourceExtents); int left = (int)extents.X; int top = (int)extents.Y; int right = (int)extents.Z; int bottom = (int)extents.W; if (left == right || top == bottom) { return; } this.CalculateWeights(top, bottom, transformedPoint.Y, ref ySpanRef); this.CalculateWeights(left, right, transformedPoint.X, ref xSpanRef); Vector4 sum = Vector4.Zero; for (int kernelY = 0, y = top; y <= bottom; y++, kernelY++) { float yWeight = Unsafe.Add(ref ySpanRef, kernelY); for (int kernelX = 0, x = left; x <= right; x++, kernelX++) { float xWeight = Unsafe.Add(ref xSpanRef, kernelX); // Values are first premultiplied to prevent darkening of edge pixels. var current = sourcePixels[x, y].ToVector4(); Vector4Utils.Premultiply(ref current); sum += current * xWeight * yWeight; } } // Reverse the premultiplication Vector4Utils.UnPremultiply(ref sum); targetRow[column] = sum; }
public void Clamp1() { Vector4 clamped = new Vector4(-10, 1, 100, 1000); clamped.Clamp(-100, 1000); Assert.AreEqual(-10, clamped.X); Assert.AreEqual(1, clamped.Y); Assert.AreEqual(100, clamped.Z); Assert.AreEqual(1000, clamped.W); }
public void ClampStatic2() { Vector4 clamped = new Vector4(-10, 1, 100, 1000); clamped = Vector4.Clamp(clamped, -1, 0); Assert.AreEqual(-1, clamped.X); Assert.AreEqual(0, clamped.Y); Assert.AreEqual(0, clamped.Z); Assert.AreEqual(0, clamped.W); }
public void ToXyzwBytes(Span <byte> bytes, int startIndex) { Vector4 vector = Vector4.Clamp(this.backingVector, Vector4.Zero, Vector4.One) * MaxBytes; vector += Half; bytes[startIndex] = (byte)vector.X; bytes[startIndex + 1] = (byte)vector.Y; bytes[startIndex + 2] = (byte)vector.Z; bytes[startIndex + 3] = (byte)vector.W; }
private static uint Pack(ref Vector4 vector) { vector = Vector4.Clamp(vector, Vector4.Zero, Vector4.One) * Multiplier; return((uint)( (((int)Math.Round(vector.X) & 0x03FF) << 0) | (((int)Math.Round(vector.Y) & 0x03FF) << 10) | (((int)Math.Round(vector.Z) & 0x03FF) << 20) | (((int)Math.Round(vector.W) & 0x03) << 30))); }
/// <summary> /// Packs a <see cref="Vector4"/> into a uint. /// </summary> /// <param name="vector">The vector containing the values to pack.</param> /// <returns>The <see cref="uint"/> containing the packed values.</returns> private static uint Pack(ref Vector4 vector) { vector = Vector4.Clamp(vector, Vector4.Zero, Vector4.One); vector *= MaxBytes; vector += Half; return((uint)(((byte)vector.X << 24) | ((byte)vector.Y << 16) | ((byte)vector.Z << 8) | (byte)vector.W)); }
private void Pack(ref Vector4 vector) { vector *= MaxBytes; vector += Half; vector = Vector4.Clamp(vector, Vector4.Zero, MaxBytes); this.R = (byte)vector.X; this.G = (byte)vector.Y; this.B = (byte)vector.Z; }
private Vector4 ToByteScaledVector4() { var vector = this.ToVector4(); vector /= 65534; vector *= 255; vector += Half; vector += Round; return(Vector4.Clamp(vector, Vector4.Zero, MaxBytes)); }
/// <summary> /// Packs a <see cref="Vector4"/> into a uint. /// </summary> /// <param name="vector">The vector containing the values to pack.</param> /// <returns>The <see cref="uint"/> containing the packed values.</returns> private static uint Pack(ref Vector4 vector) { vector = Vector4.Clamp(vector, Vector4.Zero, Vector4.One); vector *= MaxBytes; vector += Half; return((uint)(((byte)vector.X << RedShift) | ((byte)vector.Y << GreenShift) | ((byte)vector.Z << BlueShift) | (byte)vector.W << AlphaShift)); }
/// <summary> /// Initializes a new instance of the <see cref="Color"/> struct. /// </summary> /// <param name="vector"> /// The vector containing the components for the packed vector. /// </param> public Color(Vector4 vector) : this() { Vector4 clamped = Vector4.Clamp(vector, Vector4.Zero, Vector4.One) * 255F; this.R = (byte)Math.Round(clamped.X); this.G = (byte)Math.Round(clamped.Y); this.B = (byte)Math.Round(clamped.Z); this.A = (byte)Math.Round(clamped.W); }