/// <summary> /// Draws this object with the specified material. /// </summary> public void Draw(DrawingContext context, Material material) { var graphics = context.graphics; var applyTexture = UseModelTextures.HasValue ? UseModelTextures.Value : model.UseModelTextures; if (applyTexture) { ApplyTextures(material); } ApplySkinTransform(material); material.world = worldTransform; material.BeginApply(context); // SetVertexBuffer isn't doing a good job filtering out same vertex buffer due to // multiple vertex buffer binding. Doing it manually here. context.SetVertexBuffer(vertexBuffer, vertexOffset); // On contrast, setting indices has no overhead for the same index buffers. graphics.Indices = indexBuffer; graphics.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, numVertices, startIndex, primitiveCount); material.EndApply(context); // The input material might be shared between different objects, // some of which might not even have a texture. // Since we have modified the material texture, we need to store it. // // We also modified other textures like normal maps, but these textures // aren't likely to be used by other object types, so don't restore them. if (applyTexture) { material.texture = null; } }
/// <summary> /// Draws this object with the specified material. /// </summary> public void Draw(DrawingContext context, Material material) { #if WINRT throw new NotSupportedException(); #else if (model.template == null) { return; } VertexBuffer instanceBuffer; int instanceCount; model.GetInstanceBuffer(ref context.matrices.cameraPosition, false, out instanceBuffer, out instanceCount); if (instanceBuffer == null) { return; } var materialGroup = material as MaterialGroup; if (materialGroup == null) { return; } if (materialGroup.Find <InstancedMaterialPart>() == null) { return; } VertexBuffer vertexBuffer; IndexBuffer indexBuffer; int vertexOffset; int numVertices; int startIndex; int primitiveCount; model.template.GetVertexBuffer(index, out vertexBuffer, out vertexOffset, out numVertices); model.template.GetIndexBuffer(index, out indexBuffer, out startIndex, out primitiveCount); if (vertexBuffer == null) { return; } context.SetVertexBuffer(null, 0); Bindings[0] = new VertexBufferBinding(vertexBuffer, vertexOffset, 0); Bindings[1] = new VertexBufferBinding(instanceBuffer, 0, 1); model.GraphicsDevice.SetVertexBuffers(Bindings); model.GraphicsDevice.Indices = indexBuffer; model.template.PrepareMaterial(index, material); material.world = model.AbsoluteTransform; material.BeginApply(context); model.GraphicsDevice.DrawInstancedPrimitives(PrimitiveType.TriangleList, 0, 0, numVertices, startIndex, primitiveCount, instanceCount); material.EndApply(context); #endif }
/// <summary> /// Draws this object with the specified material. /// </summary> public void Draw(DrawingContext context, Material material) { GraphicsDevice.Indices = IndexBuffer; context.SetVertexBuffer(VertexBuffer, 0); material.world = surface.AbsoluteTransform; material.BeginApply(context); GraphicsDevice.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, VertexCount, StartIndex, PrimitiveCount); material.EndApply(context); }
public void Draw(DrawingContext context, Material material) { #if WINDOWS_PHONE var view = context.matrices.view; var projection = context.matrices.projection; context.matrices.view = context.matrices.projection = material.world = Matrix.Identity; #endif if (Texture != null) { material.texture = Texture; } material.BeginApply(context); context.SetVertexBuffer(vertexBuffer, 0); GraphicsDevice.Indices = indexBuffer; #if WINDOWS_PHONE GraphicsDevice.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, 4, 0, 2); context.matrices.view = view; context.matrices.projection = projection; #else // Apply a vertex pass through material in case the specified material does // not have a vertex shader. // // NOTE: There is no legal way to determine the current shader profile. If you are mix using a // 2_0 vs and 3_0 ps, DrawIndexedPrimitives is going to throw an InvalidOperationException, // in that case, catch that exception and try to draw with 3_0 vs. try { // Use vs 2_0 vertexPassThrough2.BeginApply(context); GraphicsDevice.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, 4, 0, 2); } catch (InvalidOperationException) { if (context.graphics.GraphicsProfile != GraphicsProfile.HiDef) { throw; } // Use vs 3_0 if (vertexPassThrough3 == null) { vertexPassThrough3 = new VertexPassThrough3Material(GraphicsDevice); } vertexPassThrough3.BeginApply(context); GraphicsDevice.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, 4, 0, 2); } #endif material.EndApply(context); }
/// <summary> /// Draws this sprite with the specified material. /// </summary> public void Draw(DrawingContext context, Material material) { float width, height; float left, right, top, bottom; float textureWidth, textureHeight; if (texture != null) { textureWidth = (float)texture.Width; textureHeight = (float)texture.Height; } else if (size != null) { textureWidth = size.Value.X; textureHeight = size.Value.Y; } else { return; } if (sourceRectangle.HasValue) { var rect = sourceRectangle.Value; left = rect.X / textureWidth; right = (rect.X + rect.Width) / textureWidth; bottom = rect.Y / textureHeight; top = (rect.Y + rect.Height) / textureHeight; width = rect.Width; height = rect.Height; } else { width = textureWidth; height = textureHeight; left = 0f; right = 1f; bottom = 0f; top = 1f; } if (flipX) { var temp = left; left = right; right = temp; } if (flipY) { var temp = top; top = bottom; bottom = temp; } UpdateAnchorPoint(); SharedVertices[0].Position = new Vector3(-anchorPoint.X, -anchorPoint.Y, 0); SharedVertices[1].Position = new Vector3(width - anchorPoint.X, -anchorPoint.Y, 0); SharedVertices[2].Position = new Vector3(width - anchorPoint.X, height - anchorPoint.Y, 0); SharedVertices[3].Position = new Vector3(-anchorPoint.X, height - anchorPoint.Y, 0); SharedVertices[0].TextureCoordinate = new Vector2(left, top); SharedVertices[1].TextureCoordinate = new Vector2(right, top); SharedVertices[2].TextureCoordinate = new Vector2(right, bottom); SharedVertices[3].TextureCoordinate = new Vector2(left, bottom); SharedVertices[0].Color = SharedVertices[1].Color = SharedVertices[2].Color = SharedVertices[3].Color = color * alpha; if (vertexBuffer == null) { GetBuffers(context.graphics, out vertexBuffer, out indexBuffer); } context.SetVertexBuffer(null, 0); vertexBuffer.SetData(SharedVertices); Matrix worldTransform = AbsoluteTransform; ApplyScaleFactor(ref worldTransform); #if !WINDOWS_PHONE // Apply a basic effect in case the specified material does not have a vertex shader if (basicEffect == null) { basicEffect = GetBasicEffect(context.graphics); } basicEffect.World = worldTransform; basicEffect.View = context.matrices.view; basicEffect.Projection = context.matrices.projection; basicEffect.CurrentTechnique.Passes[0].Apply(); #endif // Override material texture with the sprite texture Texture2D originalTexture = null; if (texture != null) { originalTexture = material.texture; material.texture = texture; } material.world = worldTransform; material.BeginApply(context); context.SetVertexBuffer(vertexBuffer, 0); context.graphics.Indices = indexBuffer; context.graphics.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, 4, 0, 2); material.EndApply(context); if (originalTexture != null) { material.texture = originalTexture; } }