SpriteBatch 使用注意点

本文介绍SpriteBatch类作为OpenGL简易封装的应用细节。此组件主要用于优化2D矩形纹理的批量绘制操作,通过批处理提高渲染效率。它适用于GL1.0和GL2.0环境,并在后者下使用自定义着色器。注意此类为托管资源,需手动释放,且在OpenGL上下文丢失后会自动重新加载。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

SpriteBatch 使用注意点

SpriteBatch 本质上是 OpenGL 的简易封装的载体。

用于绘制引用 texture(region) 的 2D 【矩型】,该类将会批处理绘制命令来优化它们,然后交给 GPU 处理。

该类也是 managed 的,即需要手动调用 dispose 方法的,在 OpenGL 上下文丢失时这种情况下, SpriteBatch 所使用的 OpenGL 资源将会无效。

当用户切换到另外一个 app 或接听一个电话时,OpenGL 上下文将会被丢失。当 OpenGL 上下文恢复时,该类会被自动重新加载。

该类似一个非常重量级的对象,因此在程序中尽量只存在一个实例。

该类能运行在 GL1.0 和 GL2.0 上,但是当在 2.0 的环境下,它会使用 2.0 自定义的着色器来绘制提供的精灵,你所指定的着色器将不会起作用

在MonoGame的环境下,请帮我重写一下这个PreDraw方法:public override bool PreDraw(SpriteBatch spriteBatch, Color lightColor) { float fade = Utils.InverseLerp(0f, 35f, Time, true); if (Time >= Lifetime - 45f) fade = Utils.InverseLerp(Lifetime, Lifetime - 45f, Time, true); Texture2D noiseTexture = ModContent.GetTexture("CalamityMod/ExtraTextures/VoronoiShapes"); Vector2 drawPosition = projectile.Center - Main.screenPosition; Vector2 origin2 = noiseTexture.Size() * 0.5f; if (NoTelegraph) { spriteBatch.EnterShaderRegion(); GameShaders.Misc["CalamityMod:DoGPortal"].UseOpacity(fade); GameShaders.Misc["CalamityMod:DoGPortal"].UseColor(new Color(0.2f, 1f, 1f, 0f)); GameShaders.Misc["CalamityMod:DoGPortal"].UseSecondaryColor(new Color(1f, 0.2f, 1f, 0f)); GameShaders.Misc["CalamityMod:DoGPortal"].Apply(); spriteBatch.Draw(noiseTexture, drawPosition, null, Color.White, 0f, origin2, 3.5f, SpriteEffects.None, 0f); spriteBatch.ExitShaderRegion(); return false; } Texture2D laserTelegraph = ModContent.GetTexture("CalamityMod/ExtraTextures/LaserWallTelegraphBeam"); float yScale = 2f; if (TelegraphDelay < TelegraphFadeTime) { yScale = MathHelper.Lerp(0f, 2f, TelegraphDelay / 15f); } if (TelegraphDelay > TelegraphTotalTime - TelegraphFadeTime) { yScale = MathHelper.Lerp(2f, 0f, (TelegraphDelay - (TelegraphTotalTime - TelegraphFadeTime)) / 15f); } Vector2 scaleInner = new Vector2(TelegraphWidth / laserTelegraph.Width, yScale); Vector2 origin = laserTelegraph.Size() * new Vector2(0f, 0.5f); Vector2 scaleOuter = scaleInner * new Vector2(1f, 1.9f); Color colorOuter = Color.Lerp(Color.Cyan, Color.Purple, TelegraphDelay / TelegraphTotalTime * 4f % 1f); // Iterate through purple and cyan once and then flash. Color colorInner = Color.Lerp(colorOuter, Color.White, 0.85f); colorOuter *= 0.7f; colorInner *= 0.7f; spriteBatch.Draw(laserTelegraph, projectile.Center - Main.screenPosition, null, colorInner, projectile.AngleTo(Destination), origin, scaleInner, SpriteEffects.None, 0f); spriteBatch.Draw(laserTelegraph, projectile.Center - Main.screenPosition, null, colorOuter, projectile.AngleTo(Destination), origin, scaleOuter, SpriteEffects.None, 0f); spriteBatch.EnterShaderRegion(); GameShaders.Misc["CalamityMod:DoGPortal"].UseOpacity(fade); GameShaders.Misc["CalamityMod:DoGPortal"].UseColor(Color.Cyan); GameShaders.Misc["CalamityMod:DoGPortal"].UseSecondaryColor(Color.Fuchsia); GameShaders.Misc["CalamityMod:DoGPortal"].Apply(); spriteBatch.Draw(noiseTexture, drawPosition, null, Color.White, 0f, origin2, 2.7f, SpriteEffects.None, 0f); spriteBatch.ExitShaderRegion(); return false; } }
最新发布
03-24
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值