Skip to content

Commit

Permalink
kramv - enable uvPreview for shapes of Key::Num6
Browse files Browse the repository at this point in the history
  • Loading branch information
alecazam committed May 7, 2022
1 parent 57ed987 commit c4b2e4d
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 37 deletions.
3 changes: 3 additions & 0 deletions kramv/KramRenderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ class KTXImage;
// unload gltf model
- (void)unloadModel;

// called from view and renderer in render loop
- (void)updateAnimationState:(MTKView*)view;

// can play animations in gltf models
@property (nonatomic) BOOL playAnimations;

Expand Down
52 changes: 24 additions & 28 deletions kramv/KramRenderer.mm
Original file line number Diff line number Diff line change
Expand Up @@ -722,6 +722,23 @@ - (void)releaseAllPendingTextures
}
}

- (void)updateAnimationState:(MTKView*)view
{
bool animateDisplay = self.playAnimations;

// animate the uvPreviw until it reaches endPoint, no scrubber yet
_showSettings->updateUVPreviewState();

if (_showSettings->uvPreviewFrames > 0) {
_showSettings->uvPreviewFrames--;
animateDisplay = true;
}

view.enableSetNeedsDisplay = !animateDisplay;
view.paused = !animateDisplay;
}


- (void)updateModelSettings:(const string &)fullFilename
{
_showSettings->isModel = true;
Expand Down Expand Up @@ -1545,33 +1562,9 @@ - (void)_updateGameState
float4x4 panTransform =
matrix4x4_translation(-_showSettings->panX, _showSettings->panY, 0.0);

// interpolate this, also need to draw wireframe
// this is an animated effect, that overlays the shape uv wires over the image
// but it needs to set needsDisplay until animation finishes

// TODO: need to reset these when shape changes
static float delta = 1.0 / 60.0;
static float uvPreviewAmount = 0.0;

// hack to see uvPreview
//_showSettings->isUVPreview = true;

if (_showSettings->is3DView && _showSettings->isUVPreview) {
uvPreviewAmount += delta;

if (uvPreviewAmount > 1.0) {
delta = -1.0 / 60.0;
uvPreviewAmount = 1.0;
}
else if (uvPreviewAmount < 0.0) {
delta = 1.0 / 60.0;
uvPreviewAmount = 0.0;
}
}
else {
uvPreviewAmount = 0.0;
}
uniforms.uvPreview = uvPreviewAmount;
uniforms.isUVPreview = _showSettings->uvPreview > 0.0;
uniforms.uvPreview = _showSettings->uvPreview;

// scale
float zoom = _showSettings->zoom;
Expand Down Expand Up @@ -1669,8 +1662,11 @@ - (void)_setUniformsLevel:(UniformsLevel &)uniforms mipLOD:(int32_t)mipLOD
- (void)drawInMTKView:(nonnull MTKView *)view
{
@autoreleasepool {
/// Per frame updates here
// Per frame updates here

// update per frame state
[self updateAnimationState:view];

// TODO: move this out, needs to get called off mouseMove, but don't want to
// call drawMain
[self drawSample];
Expand Down Expand Up @@ -2107,7 +2103,7 @@ - (void)drawMain:(id<MTLCommandBuffer>)commandBuffer
}

// Draw uv wire overlay
if (_showSettings->is3DView && _showSettings->isUVPreview) {
if (_showSettings->is3DView && _showSettings->uvPreview > 0.0) {
// need to force color in shader or it's still sampling texture
// also need to add z offset

Expand Down
25 changes: 25 additions & 0 deletions kramv/KramViewerBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,31 @@ void ShowSettings::advanceDebugMode(bool decrement)
// clear color of farPlane.
}

void ShowSettings::updateUVPreviewState()
{
if (is3DView) {
if (isUVPreview) {
if (uvPreview < 1.0)
uvPreview += uvPreviewStep;
}
else
{
if (uvPreview > 0.0)
uvPreview -= uvPreviewStep;
}

uvPreview = saturate(uvPreview);
}
else {
uvPreview = 0.0;
}

// stop the frame update
if (uvPreview == 0.0f || uvPreview == 1.0f) {
uvPreviewFrames = 0;
}
}

void printChannels(string &tmp, const string &label, float4 c,
int32_t numChannels, bool isFloat, bool isSigned)
{
Expand Down
6 changes: 6 additions & 0 deletions kramv/KramViewerBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,10 @@ class ShowSettings {
// Can collapse 3d to 2d and overlay the uv
bool isUVPreview = false;

uint32_t uvPreviewFrames = 0;
float uvPreviewStep = 1.0f / 10.0f;
float uvPreview = 0.0f;

// the 2d view doesn't want to inset pixels for clamp, or point sampling is
// thrown off expecially on small 4x4 textures
#if USE_PERSPECTIVE
Expand Down Expand Up @@ -240,6 +244,8 @@ class ShowSettings {

const char *meshNumberName(uint32_t meshNumber) const;

void updateUVPreviewState();

float imageAspectRatio() const {
float ar = 1.0f;
if (meshNumber == 0 && !isModel && imageBoundsY > 0)
Expand Down
34 changes: 25 additions & 9 deletions kramv/KramViewerMain.mm
Original file line number Diff line number Diff line change
Expand Up @@ -503,6 +503,7 @@ @implementation MyMTKView {
int32_t _fileFolderIndex;

Action* _actionPlay;
Action* _actionShapeUVPreview;
Action* _actionHelp;
Action* _actionInfo;
Action* _actionHud;
Expand Down Expand Up @@ -687,6 +688,7 @@ - (NSStackView *)_addButtons
Action("", "", Key::A), // sep

Action(" ", "Play", Key::Space), // TODO: really need icon on this
Action("6", "Shape UVPreview", Key::Num6),
Action("S", "Shape", Key::S),
Action("C", "Shape Channel", Key::C),
Action("L", "Lighting", Key::L),
Expand Down Expand Up @@ -732,6 +734,7 @@ - (NSStackView *)_addButtons
&_actionFit,

&_actionPlay,
&_actionShapeUVPreview,
&_actionShapeMesh,
&_actionShapeChannel,
&_actionLighting,
Expand Down Expand Up @@ -2055,11 +2058,14 @@ - (void)showFileTable
scrollView.hidden = NO;
}

- (void)updateHudVisibility {
- (void)updateHudVisibility
{
_hudLabel.hidden = _hudHidden || !_showSettings->isHudShown;
_hudLabel2.hidden = _hudHidden || !_showSettings->isHudShown;
}



- (bool)handleEventAction:(const Action*)action isShiftKeyDown:(bool)isShiftKeyDown
{
// Some data depends on the texture data (isSigned, isNormal, ..)
Expand All @@ -2070,6 +2076,8 @@ - (bool)handleEventAction:(const Action*)action isShiftKeyDown:(bool)isShiftKeyD
// f.e. clamped values don't need to re-render
string text;

Renderer* renderer = (Renderer*)self.delegate;

if (action == _actionVertical) {
bool isVertical =
_buttonStack.orientation == NSUserInterfaceLayoutOrientationVertical;
Expand Down Expand Up @@ -2162,23 +2170,31 @@ - (bool)handleEventAction:(const Action*)action isShiftKeyDown:(bool)isShiftKeyD
}
else if (action == _actionPlay) {
if (!action->isHidden) {
Renderer* renderer = (Renderer*)self.delegate;


renderer.playAnimations = !renderer.playAnimations;

text = renderer.playAnimations ? "Play" : "Pause";
isChanged = true;

self.enableSetNeedsDisplay = !renderer.playAnimations;
self.paused = !renderer.playAnimations;
[renderer updateAnimationState:self];
}
else {
self.enableSetNeedsDisplay = YES;
self.paused = YES;
[renderer updateAnimationState:self];
}

}

else if (action == _actionShapeUVPreview) {

// toggle state
_showSettings->isUVPreview = !_showSettings->isUVPreview;
text = _showSettings->isUVPreview ? "Show UVPreview" : "Hide UvPreview";
isChanged = true;

_showSettings->uvPreviewFrames = 10;

// also need to call this in display link, for when it reaches end
[renderer updateAnimationState:self];
}

else if (action == _actionShapeChannel) {
_showSettings->advanceShapeChannel(isShiftKeyDown);

Expand Down

0 comments on commit c4b2e4d

Please sign in to comment.