cube appearance matches light

pull/1/head
cosmonaut 2020-08-26 16:07:31 -07:00
parent eb51182760
commit 92379f1ae9
5 changed files with 115 additions and 55 deletions

View File

@ -0,0 +1,15 @@
using Microsoft.Xna.Framework;
using Encompass;
namespace KavTest.Components
{
public struct OverrideAlbedoComponent : IComponent
{
public Vector3 Color { get; }
public OverrideAlbedoComponent(Vector3 color)
{
Color = color;
}
}
}

View File

@ -22,6 +22,7 @@ namespace KavTest.Spawners
AddComponent(entity, new PointLightComponent(message.Color, message.Intensity));
AddComponent(entity, new MoveAlongCurve3DComponent(message.Curve));
AddComponent(entity, new ModelComponent(LightBulbModel));
AddComponent(entity, new OverrideAlbedoComponent(message.Color.ToVector3()));
}
}
}

View File

@ -3,6 +3,7 @@ using System.IO;
using Encompass;
using KavTest.Components;
using KavTest.Engines;
using KavTest.Extensions;
using KavTest.Messages;
using KavTest.Renderers;
using KavTest.Spawners;
@ -81,62 +82,17 @@ namespace KavTest
new Vector3(-1, 1, 1)
));
var lightMovementSpline = new SplineCurve3D(
ImmutableArray.Create<ICurve3D>(
new QuadraticBezierCurve3D(
new System.Numerics.Vector3(-5, 2, -5),
new System.Numerics.Vector3(20, 50, -20),
new System.Numerics.Vector3(10, 20, 10)
),
new QuadraticBezierCurve3D(
new System.Numerics.Vector3(10, 20, 10),
new System.Numerics.Vector3(-20, -50, -10),
new System.Numerics.Vector3(-5, 2, -5)
)
),
ImmutableArray.Create<float>(
3f,
3f
),
true
);
for (var i = 0; i < 64; i++)
{
var start = RandomHelper.RandomVector3(-5, 5);
WorldBuilder.SendMessage(new LightBulbSpawnMessage(
new Transform3D(new Vector3(-5, 2, -5), Quaternion.Identity, new Vector3(0.25f, 0.25f, 0.25f)),
Color.White,
300f,
lightMovementSpline
new Transform3D(start.ToXNAVector(), Quaternion.Identity, new Vector3(0.1f, 0.1f, 0.1f)),
RandomHelper.RandomColor(),
5f,
RandomHelper.RandomLoop(start, -15, 15, 3, 10)
));
// WorldBuilder.SendMessage(new LightBulbSpawnMessage(
// new Transform3D(new Vector3(5, 2, -5)),
// Color.Blue,
// 300f
// ));
// WorldBuilder.SendMessage(new LightBulbSpawnMessage(
// new Transform3D(new Vector3(-5, -2, -5)),
// Color.Red,
// 300f
// ));
// WorldBuilder.SendMessage(new LightBulbSpawnMessage(
// new Transform3D(new Vector3(-5, 2, 5)),
// Color.Yellow,
// 300f
// ));
// WorldBuilder.SendMessage(new LightBulbSpawnMessage(
// new Transform3D(new Vector3(-5, 2, -10)),
// Color.Orange,
// 300f
// ));
// WorldBuilder.SendMessage(new LightBulbSpawnMessage(
// new Transform3D(new Vector3(-10, 2, -5)),
// Color.CornflowerBlue,
// 300f
// ));
}
var directionalLightEntity = WorldBuilder.CreateEntity();
WorldBuilder.SetComponent(directionalLightEntity, new Transform3DComponent(

View File

@ -3,6 +3,7 @@ using System.Collections.Generic;
using Encompass;
using Kav;
using KavTest.Components;
using KavTest.Extensions;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
@ -20,6 +21,22 @@ namespace KavTest.Renderers
{
var transformComponent = GetComponent<Transform3DComponent>(entity);
var modelComponent = GetComponent<ModelComponent>(entity);
if (HasComponent<OverrideAlbedoComponent>(entity))
{
var overrideAlbedoComponent = GetComponent<OverrideAlbedoComponent>(entity);
foreach (var mesh in modelComponent.Model.Meshes)
{
foreach (var meshPart in mesh.MeshParts)
{
if (meshPart.Effect is GBufferEffect gBufferEffect)
{
gBufferEffect.AlbedoTexture = null;
gBufferEffect.Albedo = overrideAlbedoComponent.Color;
}
}
}
}
yield return (modelComponent.Model, transformComponent.Transform.TransformMatrix);
}
}

View File

@ -0,0 +1,71 @@
using System;
using System.Collections.Immutable;
using System.Numerics;
using MoonTools.Curve;
namespace KavTest
{
public static class RandomHelper
{
private static Random s_random = new Random();
public static double RandomDouble(double min, double max)
{
return (s_random.NextDouble() * (max - min)) + min;
}
public static float RandomFloat(float min, float max)
{
return (float)(RandomDouble(min, max));
}
public static int RandomInt(int min, int max)
{
return (int)(Math.Floor(RandomDouble(min, max)));
}
public static SplineCurve3D RandomLoop(Vector3 start, float minMotion, float maxMotion, float minTime, float maxTime)
{
var midpoint = start + RandomVector3(minMotion, maxMotion);
var controlPointTranslation = RandomVector3(minMotion, maxMotion);
var curveOne = new QuadraticBezierCurve3D(
start,
start + controlPointTranslation,
midpoint
);
var curveTwo = new QuadraticBezierCurve3D(
midpoint,
start - controlPointTranslation, // co-linear control points produce smooth curves
start
);
return new SplineCurve3D(
ImmutableArray.Create<ICurve3D>(
curveOne,
curveTwo
),
ImmutableArray.Create<float>(
RandomFloat(minTime, maxTime / 2),
RandomFloat(minTime, maxTime / 2)
),
true
);
}
public static Vector3 RandomVector3(float min, float max)
{
return new Vector3(
RandomFloat(min, max),
RandomFloat(min, max),
RandomFloat(min, max)
);
}
public static Microsoft.Xna.Framework.Color RandomColor()
{
var randomColor = RandomVector3(0f, 1f);
return new Microsoft.Xna.Framework.Color(randomColor.X, randomColor.Y, randomColor.Z);
}
}
}