Skip to content

Instantly share code, notes, and snippets.

@hachmeister
Created September 12, 2018 09:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hachmeister/4d3b59710f22e7c141e3b0da29323584 to your computer and use it in GitHub Desktop.
Save hachmeister/4d3b59710f22e7c141e3b0da29323584 to your computer and use it in GitHub Desktop.
Bsf Test
#include <BsApplication.h>
#include <Components/BsCAnimation.h>
#include <Components/BsCBone.h>
#include <Components/BsCCamera.h>
#include <Components/BsCRenderable.h>
#include <Importer/BsImporter.h>
#include <Importer/BsMeshImportOptions.h>
#include <Importer/BsTextureImportOptions.h>
#include <Input/BsInput.h>
#include <Material/BsMaterial.h>
#include <Math/BsDegree.h>
#include <Math/BsVector2I.h>
#include <Platform/BsCursor.h>
#include <Resources/BsBuiltinResources.h>
#include <Resources/BsResources.h>
#include <Scene/BsComponent.h>
#include <Scene/BsSceneObject.h>
#include <Utility/BsTime.h>
#include <cmath>
using namespace bs;
class Control : public Component {
public:
Control(const HSceneObject& parent);
private:
void onInitialized() override;
void update() override;
private:
Degree yaw_;
Degree pitch_;
float distance_;
Vector2I screenPos_;
};
Control::Control(const HSceneObject& parent)
: Component(parent), yaw_(180.0f), pitch_(-15.0f), distance_(5.6f) {
setName("Control");
}
void Control::onInitialized() {
gInput().onButtonDown.connect([&](const ButtonEvent& event) {
if (event.isMouse()) {
if (event.buttonCode == ButtonCode::BC_MOUSE_LEFT) {
screenPos_ = gCursor().getScreenPosition();
gCursor().hide();
}
}
});
gInput().onButtonUp.connect([&](const ButtonEvent& event) {
if (event.isMouse()) {
if (event.buttonCode == ButtonCode::BC_MOUSE_LEFT) {
gCursor().setScreenPosition(screenPos_);
gCursor().show();
}
}
});
gInput().onPointerMoved.connect([&](const PointerEvent& event) {
if (event.buttonStates[(UINT32)PointerEventButton::Left]) {
yaw_ -= Degree((float)event.delta.x / 4.0f);
pitch_ -= Degree((float)event.delta.y / 4.0f);
if (pitch_.valueDegrees() > 90.0f) {
pitch_ = 90.0f;
} else if (pitch_.valueDegrees() < -90.0f) {
pitch_ = -90.0f;
}
}
if (event.mouseWheelScrollAmount > 0.0f && distance_ > 2.0f) {
distance_ /= 1.4f;
} else if (event.mouseWheelScrollAmount < 0.0f && distance_ < 15.0f) {
distance_ *= 1.4f;
}
});
}
void Control::update() {
SO()->getParent()->setRotation(Quaternion(pitch_, yaw_, Radian(0.0f), EulerAngleOrder::XYZ));
SO()->setPosition(Vector3(0.0f, 0.0f, distance_));
}
HTexture loadTexture(std::string path, bool isSRGB = true, bool isCubemap = false,
bool isHDR = false, bool mips = true) {
Path srcAssetPath(path.c_str());
Path assetPath = srcAssetPath;
assetPath.setExtension(srcAssetPath.getExtension() + ".asset");
HTexture texture = gResources().load<Texture>(assetPath);
if (texture == nullptr) {
SPtr<ImportOptions> textureImportOptions =
Importer::instance().createImportOptions(srcAssetPath);
if (rtti_is_of_type<TextureImportOptions>(textureImportOptions)) {
TextureImportOptions* importOptions =
static_cast<TextureImportOptions*>(textureImportOptions.get());
importOptions->setGenerateMipmaps(mips);
importOptions->setSRGB(isSRGB);
importOptions->setCPUCached(true);
importOptions->setIsCubemap(isCubemap);
importOptions->setCubemapSourceType(CubemapSourceType::Faces);
if (isHDR) {
importOptions->setFormat(PF_RG11B10F);
}
}
texture = gImporter().import<Texture>(srcAssetPath, textureImportOptions);
gResources().save(texture, assetPath, true);
}
return texture;
}
int main() {
UINT32 width = 1280;
UINT32 height = 720;
VideoMode videoMode(width, height);
Application::startUp(videoMode, "Example", false);
// === Textures & Shader ===
HTexture black = BuiltinResources::getTexture(BuiltinTexture::Black);
HTexture white = BuiltinResources::getTexture(BuiltinTexture::White);
HTexture normal = BuiltinResources::getTexture(BuiltinTexture::Normal);
HShader shader = gBuiltinResources().getBuiltinShader(BuiltinShader::Standard);
// === Human ===
auto options = MeshImportOptions::create();
options->setImportSkin(true);
options->setImportAnimation(true);
auto resources = gImporter().importAll("../human.fbx", options);
HMesh humanModel;
HAnimationClip humanRun;
for (auto& entry : resources) {
if (entry.name == "primary") {
humanModel = static_resource_cast<Mesh>(entry.value);
} else if (entry.name == "Skeleton|Run") {
humanRun = static_resource_cast<AnimationClip>(entry.value);
}
}
HTexture color = loadTexture("../human.png");
HMaterial material = Material::create(shader);
material->setTexture("gAlbedoTex", color);
material->setTexture("gNormalTex", normal);
material->setTexture("gRoughnessTex", white);
material->setTexture("gMetalnessTex", black);
HSceneObject humanNode = SceneObject::create("Human");
HRenderable renderable = humanNode->addComponent<CRenderable>();
renderable->setMesh(humanModel);
renderable->setMaterial(material);
HAnimation animation = humanNode->addComponent<CAnimation>();
HSceneObject handRightNode = SceneObject::create("Hand.R");
handRightNode->setParent(humanNode);
HBone handRightBone = handRightNode->addComponent<CBone>();
handRightBone->setBoneName("Hand.R");
// === Sword ===
options->setImportAnimation(false);
auto res = gImporter().importAll("../sword.fbx", options);
HMesh swordModel;
for (auto& entry : res) {
if (entry.name == "primary") {
std::cout << "Exporting: sword.asset" << std::endl;
swordModel = static_resource_cast<Mesh>(entry.value);
gResources().save(entry.value, "../sword.asset", true, true);
}
}
HTexture swordColor = loadTexture("../sword.png");
HMaterial swordMaterial = Material::create(shader);
swordMaterial->setTexture("gAlbedoTex", swordColor);
swordMaterial->setTexture("gNormalTex", normal);
swordMaterial->setTexture("gRoughnessTex", white);
swordMaterial->setTexture("gMetalnessTex", black);
HSceneObject swordNode = SceneObject::create("Sword");
HRenderable sword = swordNode->addComponent<CRenderable>();
sword->setMesh(swordModel);
sword->setMaterial(swordMaterial);
// handRightNode->setScale(Vector3(100.0f, 100.0f, 100.0f));
// swordNode->setParent(handRightNode);
// === Human Animation ===
animation->play(humanRun);
// === Camera ===
HSceneObject viewNode = SceneObject::create("ViewPoint");
viewNode->setPosition(Vector3(0.0f, 1.6f, 0.0f));
HSceneObject cameraNode = SceneObject::create("SceneCamera");
cameraNode->setParent(viewNode);
SPtr<RenderWindow> window = gApplication().getPrimaryWindow();
HCamera camera = cameraNode->addComponent<CCamera>();
camera->getViewport()->setTarget(window);
camera->setNearClipDistance(0.005f);
camera->setFarClipDistance(1000);
camera->setAspectRatio(width / (float)height);
camera->setHorzFOV(Degree(75));
cameraNode->addComponent<Control>();
// === Main Loop ===
Application::instance().runMainLoop();
Application::shutDown();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment