Beispiel #1
0
		public int Add(TangentKey key0, ref Vector3 normal, Vector3 tangent)
		{
			TangentMix tmix;
			if (!this.mix.TryGetValue(key0, out tmix))
			{
				tmix = new TangentMix();
				this.mix.Add(key0, tmix);
			}

			var i = tmix.IndexOf(ref normal, ref tangent);
			if (i < 0)
			{
				i = this.Count;
				this.count = this.Count + 1;
				tmix.Add(ref normal, ref tangent, i);
			}
			return i;
		}
Beispiel #2
0
		private void CalculateTangentsTrianleList(TangentMixer t, TangentMixer b)
		{
			for (int i = 0; i < this.indices.Count; i += 3)
			{
				Vector3 p2 = this.Mesh.Vertices[this.indices[i + 2].Vertex];
				Vector3 p1 = this.Mesh.Vertices[this.indices[i + 1].Vertex];
				Vector3 p0 = this.Mesh.Vertices[this.indices[i].Vertex];

				Vector3 n2 = this.Mesh.Normals[this.indices[i + 2].Normal];
				Vector3 n1 = this.Mesh.Normals[this.indices[i + 1].Normal];
				Vector3 n0 = this.Mesh.Normals[this.indices[i].Normal];

				Vector3 v1 = p1 - p0;
				Vector3 v2 = p2 - p0;

				var pu0 = this.Mesh.IsUV0StreamAvailable?this.Mesh.UV0[this.indices[i].UV0]:Vector2.Zero;
				var pu1 = this.Mesh.IsUV0StreamAvailable?this.Mesh.UV0[this.indices[i + 1].UV0]:Vector2.Zero;
				var pu2 = this.Mesh.IsUV0StreamAvailable ? this.Mesh.UV0[this.indices[i + 2].UV0] : Vector2.Zero;

				var u1 = pu1 - pu0;
				var u2 = pu2 - pu0;

				var det = u1.X * u2.Y - u2.X * u1.Y;
				if (det == 0)
				{
					det = 1;
				}
				det = 1 / det;

				var tangent =
					Vector3.Normalize(
						new Vector3(
							(v1.X * u2.Y - v2.X * u1.Y) * det, (v1.Y * u2.Y - v2.Y * u1.Y) * det, (v1.Z * u2.Y - v2.Z * u1.Y) * det));
				var bitangent =
					Vector3.Normalize(
						new Vector3(
							(-v1.X * u2.X + v2.X * u1.X) * det, (-v1.Y * u2.X + v2.Y * u1.X) * det, (-v1.Z * u2.X + v2.Z * u1.X) * det));

				var key0 = new TangentKey(p0, pu0);
				var key1 = new TangentKey(p1, pu1);
				var key2 = new TangentKey(p2, pu2);

				{
					var old = this.indices[i]; 
					old.Tangent = t.Add(key0, ref n0, tangent);
					old.Binormal = b.Add(key0, ref n0, bitangent);
					this.indices[i] = old;
				}
				{
					var old = this.indices[i+1];
					old.Tangent = t.Add(key1, ref n1, tangent);
					old.Binormal = b.Add(key1, ref n1, bitangent);
					this.indices[i+1] = old;
				}
				{
					var old = this.indices[i + 2];
					old.Tangent = t.Add(key2, ref n2, tangent);
					old.Binormal = b.Add(key2, ref n2, bitangent);
					this.indices[i + 2] = old;
				}
			}
		}
Beispiel #3
0
		public bool Equals(TangentKey other)
		{
			return other.Position.Equals(this.Position) && other.UV0.Equals(this.UV0);
		}