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; }
public TileSceneRenderData(ITagContainer diContainer, TileScene scene, DeviceBuffer counterBuffer) { this.diContainer = diContainer; this.scene = scene; locationBuffer = diContainer.GetTag <LocationBuffer>(); var textureLoader = diContainer.GetTag <IAssetLoader <Texture> >(); var camera = diContainer.GetTag <OrthoCamera>(); var worldLocationRange = locationBuffer.Add(new Location()); var locationRanges = new List <DeviceBufferRange>(scene.Objects.Count + 1) { worldLocationRange }; this.locationRanges = locationRanges; worldMaterials = scene.WorldBuffers.Materials.Select(rwMaterial => { IMapMaterial material; if (rwMaterial.isTextured) { var texMaterial = new MapStandardMaterial(diContainer); (texMaterial.MainTexture.Texture, texMaterial.Sampler.Sampler) = textureLoader.LoadTexture(TextureBasePaths, rwMaterial); material = texMaterial; } else { material = new MapUntexturedMaterial(diContainer); } material.Projection.BufferRange = camera.ProjectionRange; material.View.BufferRange = camera.ViewRange; material.World.Value = Matrix4x4.Identity; material.Uniforms.Ref = ModelStandardMaterialUniforms.Default; material.PixelCounter.Buffer = counterBuffer; AddDisposable(material); return(material as IMaterial); }).ToList() !; objectMaterials = scene.Objects.Select(obj => obj.ClumpBuffers.SubMeshes.Select(subMesh => { var objectLocation = new Location(); objectLocation.LocalPosition = obj.Position; objectLocation.LocalRotation = obj.Rotation; var objectLocationRange = locationBuffer.Add(objectLocation); locationRanges.Add(objectLocationRange); var rwMaterial = subMesh.Material; IMapMaterial material; if (rwMaterial.isTextured) { var texMaterial = new MapStandardMaterial(diContainer); (texMaterial.MainTexture.Texture, texMaterial.Sampler.Sampler) = textureLoader.LoadTexture(TextureBasePaths, rwMaterial); material = texMaterial; } else { material = new MapUntexturedMaterial(diContainer); } material.Projection.BufferRange = camera.ProjectionRange; material.View.BufferRange = camera.ViewRange; material.World.BufferRange = objectLocationRange; material.Uniforms.Ref = ModelStandardMaterialUniforms.Default; material.Uniforms.Ref.vertexColorFactor = 0.0f; material.Uniforms.Ref.tint = rwMaterial.color.ToFColor() * obj.Tint; material.PixelCounter.Buffer = counterBuffer; AddDisposable(material); return(material as IMaterial); }).ToList() as IReadOnlyList <IMaterial>).ToList(); }