private Texture2D processTexture(Texture2D texture) { //Some programs such as Photoshop save PNG and other RGBA images without premultiplied alpha if (!Constants.Instance.PremultipliedAlpha) { //XNA 4.0+ assumes that all RGBA images have premultiplied alpha channels. //Code snippet borrowed from http://xboxforums.create.msdn.com/forums/p/62320/383015.aspx#485897 Byte4[] data = new Byte4[texture.Width * texture.Height]; texture.GetData<Byte4>(data); for (int i = 0; i < data.Length; i++) { Vector4 vec = data[i].ToVector4(); float alpha = vec.W / 255.0f; int a = (int)(vec.W); int r = (int)(alpha * vec.X); int g = (int)(alpha * vec.Y); int b = (int)(alpha * vec.Z); uint packed = (uint)( (a << 24) + (b << 16) + (g << 8) + r ); data[i].PackedValue = packed; } texture.SetData<Byte4>(data); } return texture; }
static public Byte4 PackNormalB4(ref Vector3 normal) { uint packedValue = PackNormal(ref normal); Byte4 b4 = new Byte4(); b4.PackedValue = packedValue; return b4; }
internal void AddSprite(Vector2 clipOffset, Vector2 clipScale, Vector2 texOffset, Vector2 texScale, Byte4 color) { MySpritesRenderer.m_spriteInstanceList.Add(new MyVertexFormatSpritePositionTextureColor( new HalfVector4(clipOffset.X, clipOffset.Y, clipScale.X, clipScale.Y), new HalfVector4(texOffset.X, texOffset.Y, texScale.X, texScale.Y), color)); }
////////////////////////////////////////////////////////////////////////// /// <summary> /// 返回一个菱形Texture2D /// </summary> public static Texture2D GetDiamond(int width, int height, Color color, Game game) { Texture2D ttDmd = new Texture2D(game.GraphicsDevice, width, height); Byte4[] btDmd = new Byte4[width * height]; Vector2 vtO = new Vector2(width / 2f, height / 2f); // Point ptPos = new Point(i / ttRound.Width, i % ttRound.Width); // ptPos.X = Row number, ptPos.Y = Col number for (int x = 0; x <= width / 2; x++) { for (int y = 0; y <= height / 2; y++) { if (width / 2f * y + height / 2f * x >= width * height / 4f) { uint packed = (uint)( (color.A << 24) + (color.B << 16) + (color.G << 8) + color.R ); btDmd[Vector2ToIndex(new Vector2(x, y), width)].PackedValue = packed; if (x != 0) btDmd[Vector2ToIndex(new Vector2(width - x, y), width)].PackedValue = packed; if (y != 0) btDmd[Vector2ToIndex(new Vector2(x, height - y), width)].PackedValue = packed; if (x != 0 && y != 0) btDmd[Vector2ToIndex(new Vector2(width - x, height - y), width)].PackedValue = packed; } } } ttDmd.SetData<Byte4>(btDmd); return ttDmd; }
internal void AddSprite(Vector2 clipOffset, Vector2 clipScale, Vector2 texOffset, Vector2 texScale, Vector2 origin, Vector2 tangent, Byte4 color) { MySpritesRenderer.StackTop().m_instances.Add(new MyVertexFormatSpritePositionTextureRotationColor( new HalfVector4(clipOffset.X, clipOffset.Y, clipScale.X, clipScale.Y), new HalfVector4(texOffset.X, texOffset.Y, texScale.X, texScale.Y), new HalfVector4(origin.X, origin.Y, tangent.X, tangent.Y), color)); }
/// <summary> /// Initializes a new instance of the <see cref="VertexPositionNormalBlendable"/> struct. /// </summary> /// <param name="position"> /// The position of the vertex. /// </param> /// <param name="normal"> /// The normal of the vertex. /// </param> /// <param name="boneWeight"> /// The bone weightings that apply to the vertex. /// </param> /// <param name="boneIndices"> /// The bone IDs that apply to the vertex. /// </param> public VertexPositionNormalBlendable( Vector3 position, Vector3 normal, Vector4 boneWeight, Byte4 boneIndices) { this.Position = position; this.Normal = normal; this.BoneWeights = boneWeight; this.BoneIndices = boneIndices; }
public InstancedVertexPositionNormalTextureBumpSkin(VertexPositionNormalTextureBumpSkin source, Matrix meshToObject, byte index) { Position = source.Position; Normal = source.Normal; TextureCoordinate1 = source.TextureCoordinate; TextureCoordinate2 = source.TextureCoordinate; Tangent = source.Tangent; Binormal = source.Binormal; BoneIndices = new Byte4(source.BoneIndex0, source.BoneIndex1, source.BoneIndex2, source.BoneIndex3); BoneWeights = source.BoneWeights; }
internal void AddQuad(Vector3 v0, Vector3 v1, Vector3 v2, Vector3 v3, Color color) { var bcolor = new Byte4(color.R, color.G, color.B, color.A); Add(new MyVertexFormatPositionColor(v0, bcolor)); Add(new MyVertexFormatPositionColor(v1, bcolor)); Add(new MyVertexFormatPositionColor(v1, bcolor)); Add(new MyVertexFormatPositionColor(v2, bcolor)); Add(new MyVertexFormatPositionColor(v2, bcolor)); Add(new MyVertexFormatPositionColor(v3, bcolor)); Add(new MyVertexFormatPositionColor(v3, bcolor)); Add(new MyVertexFormatPositionColor(v0, bcolor)); }
private static void MultiplyAlpha(Texture2D ret) { var data = new Byte4[ret.Width*ret.Height]; ret.GetData(data); for (int i = 0; i < data.Length; i++) { Vector4 vec = data[i].ToVector4(); float alpha = vec.W/255.0f; var a = (int) (vec.W); var r = (int) (alpha*vec.X); var g = (int) (alpha*vec.Y); var b = (int) (alpha*vec.Z); var packed = (uint) ((a << 24) + (b << 16) + (g << 8) + r); data[i].PackedValue = packed; } ret.SetData(data); }
internal void AddBoundingBox(BoundingBox bb, Color color) { var v0 = bb.Center - bb.HalfExtents; var v1 = v0 + new Vector3(bb.HalfExtents.X * 2, 0, 0); var v2 = v0 + new Vector3(bb.HalfExtents.X * 2, bb.HalfExtents.Y * 2, 0); var v3 = v0 + new Vector3(0, bb.HalfExtents.Y * 2, 0); var v4 = v0 + new Vector3(0, 0, bb.HalfExtents.Z * 2); var v5 = v4 + new Vector3(bb.HalfExtents.X * 2, 0, 0); var v6 = v4 + new Vector3(bb.HalfExtents.X * 2, bb.HalfExtents.Y * 2, 0); var v7 = v4 + new Vector3(0, bb.HalfExtents.Y * 2, 0); var bcolor = new Byte4(color.R, color.G, color.B, color.A); Add(new MyVertexFormatPositionColor(v0, bcolor)); Add(new MyVertexFormatPositionColor(v1, bcolor)); Add(new MyVertexFormatPositionColor(v1, bcolor)); Add(new MyVertexFormatPositionColor(v2, bcolor)); Add(new MyVertexFormatPositionColor(v2, bcolor)); Add(new MyVertexFormatPositionColor(v3, bcolor)); Add(new MyVertexFormatPositionColor(v0, bcolor)); Add(new MyVertexFormatPositionColor(v3, bcolor)); Add(new MyVertexFormatPositionColor(v4, bcolor)); Add(new MyVertexFormatPositionColor(v5, bcolor)); Add(new MyVertexFormatPositionColor(v5, bcolor)); Add(new MyVertexFormatPositionColor(v6, bcolor)); Add(new MyVertexFormatPositionColor(v6, bcolor)); Add(new MyVertexFormatPositionColor(v7, bcolor)); Add(new MyVertexFormatPositionColor(v4, bcolor)); Add(new MyVertexFormatPositionColor(v7, bcolor)); Add(new MyVertexFormatPositionColor(v0, bcolor)); Add(new MyVertexFormatPositionColor(v4, bcolor)); Add(new MyVertexFormatPositionColor(v1, bcolor)); Add(new MyVertexFormatPositionColor(v5, bcolor)); Add(new MyVertexFormatPositionColor(v2, bcolor)); Add(new MyVertexFormatPositionColor(v6, bcolor)); Add(new MyVertexFormatPositionColor(v3, bcolor)); Add(new MyVertexFormatPositionColor(v7, bcolor)); }
public ModelVertex( Vector3? position, Vector3? normal, Vector3? tangent, Vector3? bitangent, Color[] colors, Vector2[] texCoordsUV, Vector3[] texCoordsUVW, Byte4? boneIndicies, Vector4? boneWeights) { Position = position; Normal = normal; Tangent = tangent; BiTangent = bitangent; Colors = colors; TexCoordsUV = texCoordsUV; TexCoordsUVW = texCoordsUVW; BoneIndices = boneIndicies; BoneWeights = boneWeights; }
// by 火必烈 public static void PreMultiplyAlphas(Texture2D ret) { Byte4[] data = new Byte4[ret.Width * ret.Height]; ret.GetData<Byte4>(data); for (int i = 0; i < data.Length; i++) { Vector4 vec = data[i].ToVector4(); float alpha = vec.W / 255.0f; int a = (int)(vec.W); int r = (int)(alpha * vec.X); int g = (int)(alpha * vec.Y); int b = (int)(alpha * vec.Z); uint packed = (uint)( (a << 24) + (b << 16) + (g << 8) + r ); data[i].PackedValue = packed; } ret.SetData<Byte4>(data); }
internal MyVertexFormatSpritePositionTextureColor(HalfVector4 position, HalfVector4 texcoord, Byte4 color) { ClipspaceOffsetScale = position; TexcoordOffsetScale = texcoord; Color = color; }
public static void SetZ(this Byte4 data, byte value) { data.PackedValue = (data.PackedValue & 0xFF00FFFF) | ((uint)value << 16); }
internal MyVertexFormatPositionColor(Vector3 position, Color color) { Position = position; Color = new Byte4(color.PackedValue); }
internal MyVertexFormatPositionPackedColor(Vector3 position, Color color) { Position = new HalfVector4(position.X, position.Y, position.Z, 1); Color = new Byte4(color.PackedValue); }
internal MyVertexFormatPositionPackedColor(HalfVector4 position, Byte4 color) { Position = position; Color = color; }
internal MyVertexFormatTexcoordNormalTangentTexindices(Vector2 texcoord, Vector3 normal, Vector3 tangent, Byte4 texIndices) { Texcoord = new HalfVector2(texcoord.X, texcoord.Y); Normal = VF_Packer.PackNormalB4(ref normal); Vector4 T = new Vector4(tangent, 1); Tangent = VF_Packer.PackTangentSignB4(ref T); TexIndices = texIndices; }
internal MyVertexFormatTexcoordNormalTangent(HalfVector2 texcoord, Vector3 normal, Vector4 tangent) { Texcoord = texcoord; Normal = VF_Packer.PackNormalB4(ref normal); Tangent = VF_Packer.PackTangentSignB4(ref tangent); }
public void TestMemberFn_GetHashCode_i () { HashSet<Int32> hs = new HashSet<Int32>(); var rand = new System.Random(); var buff = new Byte[4]; UInt32 collisions = 0; for(Int32 i = 0; i < Settings.NumTests; ++i) { rand.NextBytes(buff); BitConverter.ToUInt32(buff, 0); UInt32 packed = BitConverter.ToUInt32(buff, 0); var packedObj = new Byte4(); packedObj.PackedValue = packed; Int32 hc = packedObj.GetHashCode (); if(hs.Contains(hc)) ++collisions; hs.Add(hc); } Assert.That(collisions, Is.LessThan(10)); }
public void TestMemberFn_ToString_i() { var testCase = new Byte4(); testCase.PackFrom(0.656f, 0.125f, 0.222f, 0.861f); String s = testCase.ToString (); Assert.That(s, Is.EqualTo("01000001")); }
public static byte GetX(this Byte4 data) { return((byte)(data.PackedValue & 0xFF)); }
public static byte GetW(this Byte4 data) { return((byte)((data.PackedValue >> 0x18) & 0xFF)); }
public static void SetX(this Byte4 data, byte value) { data.PackedValue = (data.PackedValue & 0xFFFFFF00) | (uint)value; }
internal MyVertexFormatSpritePositionTextureRotationColor(HalfVector4 position, HalfVector4 texcoord, HalfVector4 originTangent, Byte4 color) { ClipspaceOffsetScale = position; TexcoordOffsetScale = texcoord; OriginTangent = originTangent; Color = color; }
internal MyVertexFormatPositionTextureSkinning(HalfVector4 position, HalfVector2 texcoord, Byte4 indices, Vector4 weights) { Position = position; Texcoord = texcoord; BoneIndices = indices; BoneWeights = new HalfVector4(weights); }
/// <summary> /// Write Byte4 /// </summary> private static void WriteByte4(this BinaryWriter writer, ref Byte4 val) { writer.Write(val.PackedValue); }
internal MyVertexFormatTexcoordNormalTangent(HalfVector2 texcoord, Byte4 normal, Byte4 tangent) { Texcoord = texcoord; Normal = normal; Tangent = tangent; }
/// <summary> /// Write Byte4 /// </summary> private void WriteByte4(Byte4 val) { m_writer.Write(val.PackedValue); }
internal MyVertexFormatNormal(Byte4 normal, Byte4 normalMorph) { Normal = normal; NormalMorph = normalMorph; }
public bool ExportData(string tagName, Byte4[] vctArr) { WriteTag(tagName); if (vctArr == null) { m_writer.Write(0); return true; } m_writer.Write(vctArr.Length); foreach (Byte4 vctVal in vctArr) { WriteByte4(vctVal); } return true; }
internal MyVertexFormatPositionPackedColor(Vector3 position, Byte4 color) { Position = new HalfVector4(position.X, position.Y, position.Z, 1); Color = color; }
public bool ExportDataPackedAsB4(string tagName, Vector3[] vctArr) { WriteTag(tagName); if (vctArr == null) { m_writer.Write(0); return true; } m_writer.Write(vctArr.Length); foreach (Vector3 vctVal in vctArr) { Vector3 v = vctVal; Byte4 vct = new Byte4(); vct.PackedValue = VF_Packer.PackNormal(ref v); WriteByte4(vct); } return true; }
internal MyVertexFormatPositionColor(Vector3 position, Byte4 color) { Position = position; Color = color; }
public static void MakeRect3D(CRenderContext rc, CGfxMeshPrimitives result) { var dpDesc = new CDrawPrimitiveDesc(); dpDesc.SetDefault(); dpDesc.NumPrimitives = 2; result.PushAtomLOD(0, ref dpDesc); List <Vector3> posArray = new List <Vector3>(); List <Vector3> normalArray = new List <Vector3>(); List <Vector4> tangentArray = new List <Vector4>(); List <Vector2> uvArray = new List <Vector2>(); List <UInt32> indexArray = new List <UInt32>(); List <Byte4> Colors = new List <Byte4>(); Byte4 color = new Byte4(); color.X = 255; color.Y = 255; color.Z = 255; color.W = 255; Colors.Add(color); Colors.Add(color); Colors.Add(color); Colors.Add(color); // -> tileCount * tileCount * 2 triangles float x0 = -0.5f; float x1 = 0.5f; float y0 = -0.5f; float y1 = 0.5f; float V0 = 0.0f; float V1 = 1.0f; float U0 = 0.0f; float U1 = 1.0f; posArray.Add(new Vector3(x0, y0, 0)); posArray.Add(new Vector3(x1, y0, 0)); posArray.Add(new Vector3(x1, y1, 0)); posArray.Add(new Vector3(x0, y1, 0)); normalArray.Add(new Vector3(0, 0, 1)); normalArray.Add(new Vector3(0, 0, 1)); normalArray.Add(new Vector3(0, 0, 1)); normalArray.Add(new Vector3(0, 0, 1)); uvArray.Add(new Vector2(U1, V1)); uvArray.Add(new Vector2(U0, V1)); uvArray.Add(new Vector2(U0, V0)); uvArray.Add(new Vector2(U1, V0)); var targent = CalculationTargent(posArray[0], posArray[3], posArray[1], normalArray[0], uvArray[0], uvArray[3], uvArray[1]); tangentArray.Add(new Vector4(targent.X, targent.Y, targent.Z, 0)); targent = CalculationTargent(posArray[1], posArray[0], posArray[2], normalArray[1], uvArray[1], uvArray[0], uvArray[2]); tangentArray.Add(new Vector4(targent.X, targent.Y, targent.Z, 0)); targent = CalculationTargent(posArray[2], posArray[1], posArray[3], normalArray[2], uvArray[2], uvArray[1], uvArray[3]); tangentArray.Add(new Vector4(targent.X, targent.Y, targent.Z, 0)); targent = CalculationTargent(posArray[3], posArray[0], posArray[2], normalArray[3], uvArray[3], uvArray[0], uvArray[2]); tangentArray.Add(new Vector4(targent.X, targent.Y, targent.Z, 0)); indexArray.Add(0); indexArray.Add(1); indexArray.Add(2); indexArray.Add(0); indexArray.Add(2); indexArray.Add(3); unsafe { UInt32 resourceSize = 0; var vbDesc = new CVertexBufferDesc(); var mesh = result.GeometryMesh; fixed(Vector3 *pV3 = &posArray.ToArray()[0]) { vbDesc.InitData = (IntPtr)pV3; vbDesc.Stride = (UInt32)sizeof(Vector3); vbDesc.ByteWidth = (UInt32)(sizeof(Vector3) * posArray.Count); var vb = rc.CreateVertexBuffer(vbDesc); mesh.BindVertexBuffer(EVertexSteamType.VST_Position, vb); resourceSize += vbDesc.ByteWidth; } fixed(Byte4 *pc = &Colors.ToArray()[0]) { vbDesc.InitData = (IntPtr)pc; vbDesc.Stride = (UInt32)sizeof(Byte4); vbDesc.ByteWidth = (UInt32)(sizeof(Byte4) * Colors.Count); var vb = rc.CreateVertexBuffer(vbDesc); mesh.BindVertexBuffer(EVertexSteamType.VST_Color, vb); resourceSize += vbDesc.ByteWidth; } fixed(Vector3 *pV3 = &normalArray.ToArray()[0]) { vbDesc.InitData = (IntPtr)pV3; vbDesc.Stride = (UInt32)sizeof(Vector3); vbDesc.ByteWidth = (UInt32)(sizeof(Vector3) * normalArray.Count); var vb = rc.CreateVertexBuffer(vbDesc); mesh.BindVertexBuffer(EVertexSteamType.VST_Normal, vb); resourceSize += vbDesc.ByteWidth; } fixed(Vector4 *pV4 = &tangentArray.ToArray()[0]) { vbDesc.InitData = (IntPtr)pV4; vbDesc.Stride = (UInt32)sizeof(Vector4); vbDesc.ByteWidth = (UInt32)(sizeof(Vector4) * tangentArray.Count); var vb = rc.CreateVertexBuffer(vbDesc); mesh.BindVertexBuffer(EVertexSteamType.VST_Tangent, vb); resourceSize += vbDesc.ByteWidth; } fixed(Vector2 *pV = &uvArray.ToArray()[0]) { vbDesc.InitData = (IntPtr)pV; vbDesc.Stride = (UInt32)sizeof(Vector2); vbDesc.ByteWidth = (UInt32)(sizeof(Vector2) * uvArray.Count); var vb = rc.CreateVertexBuffer(vbDesc); mesh.BindVertexBuffer(EVertexSteamType.VST_UV, vb); resourceSize += vbDesc.ByteWidth; } fixed(UInt32 *pV = &indexArray.ToArray()[0]) { var ibDesc = new CIndexBufferDesc(); ibDesc.InitData = (IntPtr)pV; ibDesc.Type = EIndexBufferType.IBT_Int32; ibDesc.ByteWidth = (UInt32)(sizeof(UInt32) * indexArray.Count); var ib = rc.CreateIndexBuffer(ibDesc); mesh.BindIndexBuffer(ib); resourceSize += ibDesc.ByteWidth; } result.ResourceState.ResourceSize = (UInt32)(resourceSize); mesh.Dirty = true; } result.ResourceState.StreamState = EStreamingState.SS_Valid; result.ResourceState.KeepValid = true; }
public void AddDefaultColorBuff(CRenderContext rc, CGfxMeshPrimitives result) { if (result == null) { return; } var mesh = result.GeometryMesh; if (mesh == null) { return; } CVertexBuffer colorbuff = mesh.GetVertexBuffer(EVertexSteamType.VST_Color); if (colorbuff != null) { return; } CVertexBuffer posbuff = mesh.GetVertexBuffer(EVertexSteamType.VST_Position); if (posbuff == null) { return; } var blob = new Support.CBlobObject(); posbuff.GetBufferData(rc, blob); int vertNum = 0; unsafe { vertNum = (int)blob.Size / sizeof(Vector3); } if (vertNum == 0) { return; } Support.NativeList <Byte4> Colors = new Support.NativeList <Byte4>(); var color = new Byte4(); color.X = 255; color.Y = 255; color.Z = 255; color.W = 255; for (int i = 0; i < vertNum; i++) { Colors.Add(color); } var dpDesc = new CDrawPrimitiveDesc(); dpDesc.SetDefault(); dpDesc.NumPrimitives = (UInt32)vertNum; result.SetAtom(0, 0, ref dpDesc); UInt32 resourceSize = 0; unsafe { var vbDesc = new CVertexBufferDesc(); vbDesc.CPUAccess = (UInt32)ECpuAccess.CAS_WRITE; { vbDesc.InitData = Colors.UnsafeAddressAt(0); vbDesc.Stride = (UInt32)sizeof(Byte4); vbDesc.ByteWidth = (UInt32)(sizeof(Byte4) * Colors.Count); var vb = rc.CreateVertexBuffer(vbDesc); mesh.BindVertexBuffer(EVertexSteamType.VST_Color, vb); resourceSize += vbDesc.ByteWidth; } result.ResourceState.ResourceSize = (UInt32)(resourceSize); mesh.Dirty = true; } result.ResourceState.StreamState = EStreamingState.SS_Valid; result.ResourceState.KeepValid = true; }
internal MyVertexFormatPositionSkinning(HalfVector4 position, Byte4 indices, Vector4 weights) { Position = position; BoneIndices = indices; BoneWeights = new HalfVector4(weights); }