示例#1
0
        public DebugGridRenderer(ITagContainer diContainer)
        {
            Material = new DebugLinesMaterial(diContainer);
            device   = diContainer.GetTag <GraphicsDevice>();

            vertexBuffer = null !;
            GenerateGrid(cellCount: 5, originSize: 5f, gridColor: new IColor(0xFF888888));
        }
示例#2
0
        public DebugSkeletonRenderer(ITagContainer diContainer, ClumpBuffers geometryBuffers, Skeleton skeleton)
        {
            Geometry = geometryBuffers;
            Skeleton = skeleton;
            var camera = diContainer.GetTag <Camera>();

            locationBuffer   = diContainer.GetTag <LocationBuffer>();
            worldBufferRange = locationBuffer.Add(skeleton.Location);

            void LinkTransformsFor(IStandardTransformMaterial m)
            {
                m.LinkTransformsTo(camera);
                m.World.BufferRange = worldBufferRange;
            }

            BoneMaterial = new DebugSkinnedMaterial(diContainer);
            LinkTransformsFor(BoneMaterial);
            BoneMaterial.Pose.Skeleton = skeleton;
            SkinMaterial = new DebugSkinAllMaterial(diContainer);
            LinkTransformsFor(SkinMaterial);
            SkinMaterial.Alpha.Ref  = 1.0f;
            SkinHighlightedMaterial = new DebugSkinSingleMaterial(diContainer);
            LinkTransformsFor(SkinHighlightedMaterial);
            SkinHighlightedMaterial.BoneIndex.Ref = -1;
            LinesMaterial = new DebugLinesMaterial(diContainer);
            LinkTransformsFor(LinesMaterial);
            device = diContainer.GetTag <GraphicsDevice>();

            var vertices     = Enumerable.Empty <ColoredVertex>();
            var skinVertices = Enumerable.Empty <SkinVertex>();
            var indices      = Enumerable.Empty <ushort>();

            foreach (var(bone, index) in skeleton.Bones.Indexed())
            {
                if (bone.Parent == null)
                {
                    continue;
                }
                var to       = bone.GlobalPosition;
                var from     = bone.Parent.GlobalPosition;
                var length   = (to - from).Length();
                var baseSize = length * RhombusBaseSize;

                var normal = MathEx.CmpZero(length)
                    ? Vector3.UnitY
                    : (to - from) / length;
                var tangent    = Vector3.Normalize(normal.SomeOrthogonal()) * baseSize;
                var bitangent  = Vector3.Normalize(Vector3.Cross(normal, tangent)) * baseSize;
                var baseCenter = from + normal * length * RhombusBaseOffset;

                vertices = vertices.Concat(new[]
                {
                    from,
                    baseCenter - tangent - bitangent,
                    baseCenter + tangent - bitangent,
                    baseCenter + tangent + bitangent,
                    baseCenter - tangent + bitangent,
                    to
                }.Select(p => new ColoredVertex(p, Colors[index % Colors.Length])));
                skinVertices = skinVertices.Concat(Enumerable.Repeat(new SkinVertex()
                {
                    bone0   = unchecked ((byte)Skeleton.Parents[index]),
                    weights = Vector4.UnitX
                }, RhombusVertexCount));
                indices = indices.Concat(RhombusIndices.Select(i => (ushort)(i + index * RhombusVertexCount)));
            }

            var vertexArray     = vertices.ToArray();
            var skinVertexArray = skinVertices.ToArray();
            var indexArray      = indices.ToArray();

            vertexBuffer = device.ResourceFactory.CreateBuffer(new BufferDescription(
                                                                   (uint)vertexArray.Length * ColoredVertex.Stride, BufferUsage.VertexBuffer));
            skinBuffer = device.ResourceFactory.CreateBuffer(new BufferDescription(
                                                                 (uint)skinVertexArray.Length * SkinVertex.Stride, BufferUsage.VertexBuffer));
            indexBuffer = device.ResourceFactory.CreateBuffer(new BufferDescription(
                                                                  (uint)indexArray.Length * sizeof(ushort), BufferUsage.IndexBuffer));
            device.UpdateBuffer(vertexBuffer, 0, vertexArray);
            device.UpdateBuffer(skinBuffer, 0, skinVertexArray);
            device.UpdateBuffer(indexBuffer, 0, indexArray);

            var lineVertices = new ColoredVertex[]
            {
                new ColoredVertex(Vector3.Zero, Colors[0]),
                new ColoredVertex(Vector3.UnitX * LineLength, Colors[0]),
                new ColoredVertex(Vector3.Zero, Colors[1]),
                new ColoredVertex(Vector3.UnitY * LineLength, Colors[1]),
                new ColoredVertex(Vector3.Zero, Colors[2]),
                new ColoredVertex(Vector3.UnitZ * LineLength, Colors[2])
            };

            lineBuffer = device.ResourceFactory.CreateBuffer(new BufferDescription(
                                                                 (uint)lineVertices.Length * ColoredVertex.Stride, BufferUsage.VertexBuffer));
            device.UpdateBuffer(lineBuffer, 0, lineVertices);

            var boneDepthsArr = new int[Skeleton.Bones.Count];

            for (int i = 0; i < Skeleton.Bones.Count; i++)
            {
                boneDepthsArr[i] = Skeleton.Parents[i] < 0 ? 0 : boneDepthsArr[Skeleton.Parents[i]] + 1;
            }
            boneDepths = boneDepthsArr;
        }
示例#3
0
 public DebugLineRenderer(ITagContainer diContainer)
 {
     Material   = new DebugLinesMaterial(diContainer);
     meshBuffer = new DynamicLineMeshBuffer <ColoredVertex>(diContainer.GetTag <ResourceFactory>());
 }