Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Several minor fixes to ModelWidget #21117

Merged
merged 6 commits into from
Oct 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 23 additions & 56 deletions OpenRA.Mods.Cnc/Widgets/ModelWidget.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,19 +95,16 @@ public override Widget Clone()
return new ModelWidget(this);
}

IModel cachedVoxel;
string cachedPalette;
string cachedPlayerPalette;
string cachedNormalsPalette;
string cachedShadowPalette;
float cachedScale;
WRot cachedRotation;
float[] cachedLightAmbientColor = new float[] { 0, 0, 0 };
float[] cachedLightDiffuseColor = new float[] { 0, 0, 0 };
int cachedLightPitch;
int cachedLightYaw;
WRot cachedLightSource;
WAngle cachedCameraAngle;
PaletteReference paletteReference;
WRot cachedCameraRotation;
PaletteReference paletteReferencePlayer;
PaletteReference paletteReferenceNormals;
PaletteReference paletteReferenceShadow;
Expand All @@ -123,8 +120,14 @@ public override void Draw()
public override void PrepareRenderables()
{
var voxel = GetVoxel();
if (voxel == null)
return;

var palette = GetPalette();
var playerPalette = GetPlayerPalette();
if (string.IsNullOrEmpty(palette) && string.IsNullOrEmpty(playerPalette))
return;

var normalsPalette = GetNormalsPalette();
var shadowPalette = GetShadowPalette();
var scale = GetScale();
Expand All @@ -135,20 +138,10 @@ public override void PrepareRenderables()
var lightYaw = GetLightYaw();
var cameraAngle = GetCameraAngle();

if (voxel == null || palette == null)
return;

if (voxel != cachedVoxel)
cachedVoxel = voxel;

if (palette != cachedPalette)
{
if (string.IsNullOrEmpty(palette) && string.IsNullOrEmpty(playerPalette))
return;

var paletteName = string.IsNullOrEmpty(palette) ? playerPalette : palette;
paletteReference = WorldRenderer.Palette(paletteName);
cachedPalette = paletteName;
paletteReference = WorldRenderer.Palette(playerPalette);
cachedPalette = palette;
}

if (playerPalette != cachedPlayerPalette)
Expand All @@ -169,65 +162,39 @@ public override void PrepareRenderables()
cachedShadowPalette = shadowPalette;
}

if (scale != cachedScale)
cachedScale = scale;

if (rotation != cachedRotation)
cachedRotation = rotation;

if (lightPitch != cachedLightPitch)
if (lightPitch != cachedLightPitch || lightYaw != cachedLightYaw)
{
cachedLightPitch = lightPitch;

if (lightYaw != cachedLightYaw)
cachedLightYaw = lightYaw;

if (cachedLightAmbientColor[0] != lightAmbientColor[0] || cachedLightAmbientColor[1] != lightAmbientColor[1] || cachedLightAmbientColor[2] != lightAmbientColor[2])
cachedLightAmbientColor = lightAmbientColor;

if (cachedLightDiffuseColor[0] != lightDiffuseColor[0] || cachedLightDiffuseColor[1] != lightDiffuseColor[1] || cachedLightDiffuseColor[2] != lightDiffuseColor[2])
cachedLightDiffuseColor = lightDiffuseColor;
cachedLightSource = new WRot(WAngle.Zero, new WAngle(256 - lightPitch), new WAngle(lightYaw));
}

if (cameraAngle != cachedCameraAngle)
{
cachedCameraAngle = cameraAngle;

if (cachedVoxel == null)
return;
cachedCameraRotation = new WRot(WAngle.Zero, cameraAngle - new WAngle(256), new WAngle(256));
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You remove cachedVoxel in the 4th commit. Why is this separated to the first commit?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can merge both together if you want, this was just the first change I made on this file, see the commit description. (This condition was always false.)

var animation = new ModelAnimation(
cachedVoxel,
voxel,
() => WVec.Zero,
() => cachedRotation,
() => rotation,
() => false,
() => 0,
true);

var animations = new ModelAnimation[] { animation };

var renderer = WorldRenderer.World.WorldActor.Trait<ModelRenderer>();

var preview = new ModelPreview(
renderer,
new ModelAnimation[] { animation }, WVec.Zero, 0,
cachedScale,
new WAngle(cachedLightPitch),
new WAngle(cachedLightYaw),
cachedLightAmbientColor,
cachedLightDiffuseColor,
cachedCameraAngle,
paletteReference,
paletteReferenceNormals,
paletteReferenceShadow);

var screenBounds = animation.ScreenBounds(WPos.Zero, WorldRenderer, scale);
IdealPreviewSize = new int2(screenBounds.Width, screenBounds.Height);
var origin = RenderOrigin + new int2(RenderBounds.Size.Width / 2, RenderBounds.Size.Height / 2);

var camera = new WRot(WAngle.Zero, cachedCameraAngle - new WAngle(256), new WAngle(256));
var renderer = WorldRenderer.World.WorldActor.Trait<ModelRenderer>();
var modelRenderable = new UIModelRenderable(
renderer,
animations, WPos.Zero, origin, 0, camera, scale,
WRot.None, cachedLightAmbientColor, cachedLightDiffuseColor,
paletteReferencePlayer, paletteReferenceNormals, paletteReferenceShadow);
animations, WPos.Zero, origin, 0, cachedCameraRotation, scale,
cachedLightSource, lightAmbientColor, lightDiffuseColor,
paletteReferencePlayer ?? paletteReference, paletteReferenceNormals, paletteReferenceShadow);

renderable = modelRenderable.PrepareRender(WorldRenderer);
}
Expand Down
2 changes: 2 additions & 0 deletions mods/common/chrome/assetbrowser.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,8 @@ Background@ASSETBROWSER_PANEL:
Height: PARENT_BOTTOM
Palette: colorpicker
PlayerPalette: colorpicker
LightPitch: 256
LightYaw: 0
Label@ERROR:
Width: PARENT_RIGHT
Height: PARENT_BOTTOM
Expand Down