diffuse lit mesh sprites

main
cosmonaut 2020-12-06 18:52:41 -08:00
parent 4687d57b28
commit 822b897ffc
8 changed files with 86 additions and 94 deletions

2
Kav

@ -1 +1 @@
Subproject commit 47242e4f52d11b16ec4bc5b630d5d58869dedd92
Subproject commit ca6c91446e7a36c72db4f50ba6d7b6ab91a35605

View File

@ -0,0 +1,16 @@
using Encompass;
using Kav;
namespace KavTest
{
public struct MeshSpriteComponent : IComponent
{
public MeshSprite MeshSprite { get; }
public MeshSpriteComponent(
MeshSprite meshSprite
) {
MeshSprite = meshSprite;
}
}
}

View File

@ -1,29 +0,0 @@
using Encompass;
using KavTest.Components;
using KavTest.Messages;
using Microsoft.Xna.Framework;
namespace KavTest.Spawners
{
public class BillboardSpriteSpawner : Spawner<BillboardSpriteSpawnMessage>
{
protected override void Spawn(in BillboardSpriteSpawnMessage message)
{
var entity = CreateEntity();
var transform = new Transform3D(
message.Position,
Quaternion.Identity,
new Vector3(message.Scale, 1)
);
AddComponent(entity, new Transform3DComponent(transform));
AddComponent(entity, new SpriteComponent(
message.Texture,
message.Origin,
message.Rotation,
message.BillboardConstraint
));
}
}
}

View File

@ -0,0 +1,18 @@
using Encompass;
using KavTest.Components;
using KavTest.Messages;
using Microsoft.Xna.Framework;
namespace KavTest.Spawners
{
public class MeshSpriteSpawner : Spawner<MeshSpriteSpawnMessage>
{
protected override void Spawn(in MeshSpriteSpawnMessage message)
{
var entity = CreateEntity();
AddComponent(entity, new Transform3DComponent(message.Transform));
AddComponent(entity, new MeshSpriteComponent(message.MeshSprite));
}
}
}

View File

@ -171,6 +171,12 @@ namespace KavTest
var mushroomGuyTexture = Texture2D.FromStream(GraphicsDevice, new FileStream("Content/Sprites/mushroomguy.png", FileMode.Open));
var mushroomGuyMeshSprite = new Kav.MeshSprite(
GraphicsDevice,
mushroomGuyTexture,
Kav.SpriteBillboardConstraint.None
);
WorldBuilder.AddEngine(new InputEngine(this));
WorldBuilder.AddEngine(new AngularVelocityEngine());
WorldBuilder.AddEngine(new MoveAlongCurve3DEngine());
@ -180,7 +186,7 @@ namespace KavTest
WorldBuilder.AddEngine(new LightBulbSpawner(lightBulbModel));
WorldBuilder.AddEngine(new StaticModelSpawner());
WorldBuilder.AddEngine(new DirectionalLightSpawner());
WorldBuilder.AddEngine(new BillboardSpriteSpawner());
WorldBuilder.AddEngine(new MeshSpriteSpawner());
WorldBuilder.AddGeneralRenderer(new SceneRenderer(GraphicsDevice), 0);
// WorldBuilder.SendMessage(new RustyBallSpawnMessage(
@ -244,12 +250,9 @@ namespace KavTest
cubeModel
));
WorldBuilder.SendMessage(new BillboardSpriteSpawnMessage(
mushroomGuyTexture,
new Vector3(3, -2, 8),
0f,
new Vector2(mushroomGuyTexture.Width, mushroomGuyTexture.Height) * 0.025f,
Kav.SpriteBillboardConstraint.Full
WorldBuilder.SendMessage(new MeshSpriteSpawnMessage(
new Transform3D(new Vector3(3, 0, 11), Quaternion.Identity, Vector3.One),
mushroomGuyMeshSprite
));
// WorldBuilder.SendMessage(new StaticModelSpawnMessage(

View File

@ -1,32 +0,0 @@
using Encompass;
using Kav;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
namespace KavTest.Messages
{
public struct BillboardSpriteSpawnMessage : IMessage
{
public Texture2D Texture { get; }
public Vector3 Position { get; }
public Vector2 Origin { get; }
public float Rotation { get; }
public Vector2 Scale { get; }
public SpriteBillboardConstraint BillboardConstraint { get; }
public BillboardSpriteSpawnMessage(
Texture2D texture,
Vector3 position,
float rotation,
Vector2 scale,
SpriteBillboardConstraint billboardConstraint
) {
Texture = texture;
Origin = new Vector2(texture.Width / 2, texture.Height);
Position = position;
Rotation = rotation;
Scale = scale;
BillboardConstraint = billboardConstraint;
}
}
}

View File

@ -0,0 +1,19 @@
using Encompass;
using Kav;
namespace KavTest.Messages
{
public struct MeshSpriteSpawnMessage : IMessage
{
public Transform3D Transform { get; }
public MeshSprite MeshSprite { get; }
public MeshSpriteSpawnMessage(
Transform3D transform,
MeshSprite meshSprite
) {
Transform = transform;
MeshSprite = meshSprite;
}
}
}

View File

@ -78,7 +78,9 @@ namespace KavTest.Renderers
}
}
private Kav.DirectionalLight DirectionalLight()
private Kav.DirectionalLight? DirectionalLight()
{
if (SomeComponent<DirectionalLightComponent>())
{
var entity = ReadEntity<DirectionalLightComponent>();
var transformComponent = GetComponent<Transform3DComponent>(entity);
@ -90,25 +92,20 @@ namespace KavTest.Renderers
directionalLightComponent.Intensity
);
}
private IEnumerable<Sprite> Sprites()
else
{
foreach (var entity in ReadEntitiesAsEnumerable<SpriteComponent>())
return null;
}
}
private IEnumerable<(MeshSprite, Matrix)> MeshSpriteTransforms()
{
foreach (var entity in ReadEntitiesAsEnumerable<MeshSpriteComponent>())
{
var transformComponent = GetComponent<Transform3DComponent>(entity);
var spriteComponent = GetComponent<SpriteComponent>(entity);
var spriteComponent = GetComponent<MeshSpriteComponent>(entity);
yield return new Sprite(
spriteComponent.Texture,
transformComponent.Transform.Position,
spriteComponent.Origin,
spriteComponent.Rotation,
new Vector2(
transformComponent.Transform.Scale.X,
transformComponent.Transform.Scale.Y
),
spriteComponent.BillboardConstraint
);
yield return (spriteComponent.MeshSprite, transformComponent.Transform.TransformMatrix);
}
}
@ -200,11 +197,11 @@ namespace KavTest.Renderers
ReadComponent<SkyboxComponent>().Skybox
);
Renderer.BillboardSpriteRender(
Renderer.MeshSpriteRender(
BillboardTarget,
camera,
ModelTransforms,
Sprites(),
MeshSpriteTransforms(),
AmbientLight,
PointLights,
DirectionalLight()