aboutsummaryrefslogtreecommitdiff
path: root/samplecode
diff options
context:
space:
mode:
authorKevin Lubick <kjlubick@google.com>2018-10-30 15:08:53 -0400
committerSkia Commit-Bot <skia-commit-bot@chromium.org>2018-10-30 19:32:52 +0000
commitd969932474cf533078f8d8139002d2a1d58e2d07 (patch)
tree4ff007a9c2fc4710049dc4df2fff00dff9504c7b /samplecode
parent9877e8407b5ca2b70a0e8e724ba1313ea847bee7 (diff)
downloadskqp-d969932474cf533078f8d8139002d2a1d58e2d07.tar.gz
Refactor Nima code
There were two copies of a Nima "player" and this moves them out of samplecode/ and viewer/ to experimental/ where it is a bit more accessible (e.g. for WebAssembly). Bug: skia: Change-Id: I05419a352f0d13d16b462a374578107513eb1243 Reviewed-on: https://skia-review.googlesource.com/c/166441 Commit-Queue: Kevin Lubick <kjlubick@google.com> Reviewed-by: Brian Osman <brianosman@google.com>
Diffstat (limited to 'samplecode')
-rw-r--r--samplecode/SampleNima.cpp30
-rw-r--r--samplecode/SampleNimaActor.cpp185
-rw-r--r--samplecode/SampleNimaActor.h54
3 files changed, 16 insertions, 253 deletions
diff --git a/samplecode/SampleNima.cpp b/samplecode/SampleNima.cpp
index c9e1d62199..69125186ae 100644
--- a/samplecode/SampleNima.cpp
+++ b/samplecode/SampleNima.cpp
@@ -6,8 +6,11 @@
*/
#include "Sample.h"
-#include "SampleNimaActor.h"
+
+#include "Resources.h"
#include "SkAnimTimer.h"
+#include "nima/NimaActor.h"
+
#include <nima/Animation/ActorAnimationInstance.hpp>
#include <cmath>
@@ -16,8 +19,7 @@ using namespace nima;
class NimaView : public Sample {
public:
NimaView()
- : fActor(nullptr)
- , fAnimation(nullptr) {
+ : fActor(nullptr) {
}
protected:
@@ -31,16 +33,19 @@ protected:
void onOnceBeforeDraw() override {
// Create the actor.
- fActor = std::make_unique<SampleActor>("Robot");
+ std::string nimaPath(GetResourcePath("nima/Robot.nima").c_str());
+ std::string texturePath(GetResourcePath("nima/Robot.png").c_str());
+
+ fActor = std::make_unique<NimaActor>(nimaPath, texturePath);
- // Get the animation.
- fAnimation = fActor->animationInstance("attack");
+ // Also available: dance, jump, idle
+ fActor->setAnimation("attack");
}
void onDrawContent(SkCanvas* canvas) override {
canvas->save();
- canvas->translate(500, 500);
+ canvas->translate(500, 700);
canvas->scale(1, -1);
// Render the actor.
@@ -50,18 +55,15 @@ protected:
}
bool onAnimate(const SkAnimTimer& timer) override {
- // Apply the animation.
- if (fAnimation) {
- float time = std::fmod(timer.secs(), fAnimation->max());
- fAnimation->time(time);
- fAnimation->apply(1.0f);
+ if (fActor) {
+ float time = std::fmod(timer.secs(), fActor->duration());
+ fActor->seek(time);
}
return true;
}
private:
- std::unique_ptr<SampleActor> fActor;
- ActorAnimationInstance* fAnimation;
+ std::unique_ptr<NimaActor> fActor;
typedef Sample INHERITED;
};
diff --git a/samplecode/SampleNimaActor.cpp b/samplecode/SampleNimaActor.cpp
deleted file mode 100644
index 4634a45199..0000000000
--- a/samplecode/SampleNimaActor.cpp
+++ /dev/null
@@ -1,185 +0,0 @@
-/*
- * Copyright 2018 Google Inc.
- *
- * Use of this source code is governed by a BSD-style license that can be
- * found in the LICENSE file.
- */
-
-#include "SampleNimaActor.h"
-
-#include "SkString.h"
-#include "SkVertices.h"
-#include "SkPaint.h"
-#include "SkFilterQuality.h"
-#include "Resources.h"
-#include <algorithm>
-
-using namespace nima;
-
-SampleActor::SampleActor(std::string baseName)
- : fTexture(nullptr)
- , fActorImages()
- , fPaint(nullptr) {
- // Load the NIMA data.
- SkString nimaSkPath = GetResourcePath(("nima/" + baseName + ".nima").c_str());
- std::string nimaPath(nimaSkPath.c_str());
- INHERITED::load(nimaPath);
-
- // Load the image asset.
- fTexture = GetResourceAsImage(("nima/" + baseName + ".png").c_str());
-
- // Create the paint.
- fPaint = std::make_unique<SkPaint>();
- fPaint->setShader(fTexture->makeShader(nullptr));
- fPaint->setFilterQuality(SkFilterQuality::kLow_SkFilterQuality);
-
- // Load the image nodes.
- fActorImages.reserve(m_ImageNodeCount);
- for (uint32_t i = 0; i < m_ImageNodeCount; i ++) {
- fActorImages.emplace_back(m_ImageNodes[i], fTexture, fPaint.get());
- }
-
- // Sort the image nodes.
- std::sort(fActorImages.begin(), fActorImages.end(), [](auto a, auto b) {
- return a.drawOrder() < b.drawOrder();
- });
-}
-
-SampleActor::~SampleActor() {
-}
-
-void SampleActor::render(SkCanvas* canvas) const {
- // Render the image nodes.
- for (auto image : fActorImages) {
- image.render(this, canvas);
- }
-}
-
-SampleActorImage::SampleActorImage(ActorImage* actorImage, sk_sp<SkImage> texture, SkPaint* paint)
- : fActorImage(actorImage)
- , fTexture(texture)
- , fPaint(paint) {
-}
-
-SampleActorImage::~SampleActorImage() {
-}
-
-void SampleActorImage::render(const SampleActor* actor, SkCanvas* canvas) const {
- // Retrieve data from the image.
- uint32_t vertexCount = fActorImage->vertexCount();
- uint32_t vertexStride = fActorImage->vertexStride();
- float* vertexData = fActorImage->vertices();
- uint32_t indexCount = fActorImage->triangleCount() * 3;
- uint16_t* indexData = fActorImage->triangles();
-
- // Don't render if not visible.
- if (!vertexCount || fActorImage->textureIndex() < 0) {
- return;
- }
-
- // Split the vertex data.
- std::vector<SkPoint> positions(vertexCount);
- std::vector<SkPoint> texs(vertexCount);
- for (uint32_t i = 0; i < vertexCount; i ++) {
- uint32_t j = i * vertexStride;
-
- // Get the attributes.
- float* attrPosition = vertexData + j;
- float* attrTex = vertexData + j + 2;
- float* attrBoneIdx = vertexData + j + 4;
- float* attrBoneWgt = vertexData + j + 8;
-
- // Get deformed positions if necessary.
- if (fActorImage->doesAnimationVertexDeform()) {
- attrPosition = fActorImage->animationDeformedVertices() + i * 2;
- }
-
- // Deform the position.
- Vec2D position(attrPosition[0], attrPosition[1]);
- if (fActorImage->connectedBoneCount() > 0) {
- position = deform(position, attrBoneIdx, attrBoneWgt);
- } else {
- position = deform(position, nullptr, nullptr);
- }
-
- // Set the data.
- positions[i].set(position[0], position[1]);
- texs[i].set(attrTex[0] * fTexture->width(), attrTex[1] * fTexture->height());
- }
-
- // Create vertices.
- sk_sp<SkVertices> vertices = SkVertices::MakeCopy(SkVertices::kTriangles_VertexMode,
- vertexCount,
- positions.data(),
- texs.data(),
- nullptr,
- indexCount,
- indexData);
-
- // Determine the blend mode.
- SkBlendMode blendMode;
- switch (fActorImage->blendMode()) {
- case BlendMode::Off: {
- blendMode = SkBlendMode::kSrc;
- break;
- }
- case BlendMode::Normal: {
- blendMode = SkBlendMode::kSrcOver;
- break;
- }
- case BlendMode::Additive: {
- blendMode = SkBlendMode::kPlus;
- break;
- }
- case BlendMode::Multiply: {
- blendMode = SkBlendMode::kMultiply;
- break;
- }
- case BlendMode::Screen: {
- blendMode = SkBlendMode::kScreen;
- break;
- }
- }
-
- // Set the opacity.
- fPaint->setAlpha(static_cast<U8CPU>(fActorImage->renderOpacity() * 255));
-
- // Draw the vertices.
- canvas->drawVertices(vertices, blendMode, *fPaint);
-
- // Reset the opacity.
- fPaint->setAlpha(255);
-}
-
-Vec2D SampleActorImage::deform(const Vec2D& position, float* boneIdx, float* boneWgt) const {
- float px = position[0], py = position[1];
- float px2 = px, py2 = py;
- float influence[6] = { 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f };
-
- // Apply the world transform.
- Mat2D worldTransform = fActorImage->worldTransform();
- px2 = worldTransform[0] * px + worldTransform[2] * py + worldTransform[4];
- py2 = worldTransform[1] * px + worldTransform[3] * py + worldTransform[5];
-
- // Apply deformations based on bone offsets.
- if (boneIdx && boneWgt) {
- float* matrices = fActorImage->boneInfluenceMatrices();
-
- for (uint32_t i = 0; i < 4; i ++) {
- int index = static_cast<int>(boneIdx[i]);
- float weight = boneWgt[i];
- for (int j = 0; j < 6; j ++) {
- influence[j] += matrices[index * 6 + j] * weight;
- }
- }
-
- px = influence[0] * px2 + influence[2] * py2 + influence[4];
- py = influence[1] * px2 + influence[3] * py2 + influence[5];
- } else {
- px = px2;
- py = py2;
- }
-
- // Return the transformed position.
- return Vec2D(px, py);
-}
diff --git a/samplecode/SampleNimaActor.h b/samplecode/SampleNimaActor.h
deleted file mode 100644
index f2a136a2f6..0000000000
--- a/samplecode/SampleNimaActor.h
+++ /dev/null
@@ -1,54 +0,0 @@
-/*
- * Copyright 2018 Google Inc.
- *
- * Use of this source code is governed by a BSD-style license that can be
- * found in the LICENSE file.
- */
-
-#ifndef SampleNimaActor_DEFINED
-#define SampleNimaActor_DEFINED
-
-#include <nima/Actor.hpp>
-#include <nima/ActorImage.hpp>
-#include <nima/Vec2D.hpp>
-
-#include "SkCanvas.h"
-#include "SkImage.h"
-
-class SampleActor;
-class SampleActorImage;
-
-class SampleActor : public nima::Actor {
-public:
- SampleActor(std::string baseName);
- ~SampleActor();
-
- void render(SkCanvas* canvas) const;
-
-private:
- sk_sp<SkImage> fTexture;
- std::vector<SampleActorImage> fActorImages;
- std::unique_ptr<SkPaint> fPaint;
-
- typedef nima::Actor INHERITED;
-};
-
-class SampleActorImage {
-public:
- SampleActorImage(nima::ActorImage* actorImage, sk_sp<SkImage> texture, SkPaint* paint);
- ~SampleActorImage();
-
- void render(const SampleActor* actor, SkCanvas* canvas) const;
-
- int drawOrder() const { return fActorImage->drawOrder(); }
-
-private:
- nima::Vec2D deform(const nima::Vec2D& position, float* boneIdx, float* boneWgt) const;
-
-private:
- nima::ActorImage* fActorImage;
- sk_sp<SkImage> fTexture;
- SkPaint* fPaint;
-};
-
-#endif