Testing textures

This commit is contained in:
2023-05-11 23:57:08 -07:00
parent 642f1408ff
commit f44fe25011
7 changed files with 76 additions and 173 deletions

View File

@ -15,5 +15,5 @@ TiledSpriteAnimation::TiledSpriteAnimation(TiledSprite *sprite) :
void TiledSpriteAnimation::tick(float_t delta) {
SimpleAnimation::tick(delta);
this->sprite->setTile(frame);
this->sprite->tile = frame;
}

View File

@ -8,84 +8,36 @@
using namespace Dawn;
TiledSprite::TiledSprite(SceneItem *item) : SceneItemComponent(item) {
TiledSprite::TiledSprite(SceneItem *item) :
SceneItemComponent(item),
tile(-1),
tileset(nullptr),
meshHost(nullptr)
{
}
glm::vec2 TiledSprite::getUV0() {
assertNotNull(this->tileset);
auto tile = this->tileset->getTile(tileIndex);
return glm::vec2(
(this->flipState & TILED_SPRITE_FLIP_X) == 0 ? tile.uv0.x : tile.uv1.x,
(this->flipState & TILED_SPRITE_FLIP_Y) == 0 ? tile.uv0.y : tile.uv1.y
);
}
glm::vec2 TiledSprite::getUV1() {
assertNotNull(this->tileset);
auto tile = this->tileset->getTile(tileIndex);
return glm::vec2(
(this->flipState & TILED_SPRITE_FLIP_X) == 0 ? tile.uv1.x : tile.uv0.x,
(this->flipState & TILED_SPRITE_FLIP_Y) == 0 ? tile.uv1.y : tile.uv0.y
);
}
void TiledSprite::setTileset(Tileset *tileset) {
assertNotNull(tileset);
this->tileset = tileset;
this->setTile(0);
}
void TiledSprite::setTilesetAndSize(TilesetGrid *tileset, glm::vec2 center) {
this->setTileset(tileset);
this->setSize(glm::vec2(tileset->divX, tileset->divY), center);
}
void TiledSprite::setTilesetAndSize(TilesetGrid *tileset) {
this->setTileset(tileset);
this->setSize(glm::vec2(tileset->divX, tileset->divY));
}
void TiledSprite::setTile(int32_t tileIndex) {
if(tileIndex == this->tileIndex) return;
this->tileIndex = tileIndex;
if(this->host != nullptr) {
QuadMesh::bufferCoordinates(
&this->host->mesh, this->getUV0(), this->getUV1(), 0
);
}
}
void TiledSprite::setSize(glm::vec2 size, glm::vec2 center) {
this->xy0 = -center;
this->xy1 = size - center;
if(this->host != nullptr) {
QuadMesh::bufferPositions(
&this->host->mesh, this->xy0, this->xy1, 0
);
}
}
void TiledSprite::setSize(glm::vec2 size) {
this->setSize(size, size / 2.0f);
}
std::vector<SceneItemComponent*> TiledSprite::getDependencies() {
this->host = this->item->getComponent<MeshHost>();
if(this->meshHost == nullptr) {
this->meshHost = this->item->getComponent<QuadMeshHost>();
}
return std::vector<SceneItemComponent*>{
this->host
return {
this->meshHost
};
}
void TiledSprite::onStart() {
SceneItemComponent::onStart();
assertNotNull(this->host);
assertNotNull(this->tileset);
QuadMesh::initQuadMesh(&this->host->mesh,
this->xy0, this->getUV0(),
this->xy1, this->getUV1(),
0
);
useEffect([&]{
if(this->meshHost == nullptr || this->tileset == nullptr) return;
auto tile = this->tileset->getTile(this->tile);
this->meshHost->uv0 = tile.uv0;
this->meshHost->uv1 = tile.uv1;
}, {
&this->tile,
&this->meshHost,
&this->tileset
})();
}

View File

@ -4,85 +4,21 @@
// https://opensource.org/licenses/MIT
#pragma once
#include "scene/SceneItemComponent.hpp"
#include "scene/components/display/mesh/MeshRenderer.hpp"
#include "scene/components/display/mesh/MeshHost.hpp"
#include "display/mesh/QuadMesh.hpp"
#include "display/Tileset.hpp"
#define TILED_SPRITE_FLIP_Y FLAG_DEFINE(0)
#define TILED_SPRITE_FLIP_X FLAG_DEFINE(1)
#include "scene/components/display/mesh/QuadMeshHost.hpp"
namespace Dawn {
class TiledSprite : public SceneItemComponent {
protected:
MeshHost *host = nullptr;
Tileset *tileset = nullptr;
flag_t flipState = TILED_SPRITE_FLIP_Y;
int32_t tileIndex = -1;
glm::vec2 xy0 = glm::vec2(0, 0);
glm::vec2 xy1 = glm::vec2(1, 1);
glm::vec2 getUV0();
glm::vec2 getUV1();
public:
// @optional
StateProperty<Tileset*> tileset;
// @optional
StateProperty<QuadMeshHost*> meshHost;
// @optional
StateProperty<int32_t> tile;
TiledSprite(SceneItem *item);
std::vector<SceneItemComponent*> getDependencies() override;
std::vector<SceneItemComponent*> getDependencies();
void onStart() override;
/**
* Sets which tileset to use for this sprite.
*
* @param tileset Tileset to use.
*/
void setTileset(Tileset *tileset);
/**
* Sets the tileset for the sprite, and autosizes the sprite based on
* this tileset.
*
* @param gridTileset Tileset to use.
* @param center The center offset of the sprite.
*/
void setTilesetAndSize(TilesetGrid *gridTileset, glm::vec2 center);
/**
* Sets the tileset for the sprite, and autosizes the sprite based on
* this tileset. This will put the sprite centered on its origin.
*
* @param gridTileset Tileset to use.
*/
void setTilesetAndSize(TilesetGrid *gridTileset);
/**
* Updates the selected tile.
*
* @param tile Tile to use.
*/
void setTile(int32_t tile);
/**
* Adjust how the sprite is flippxed.
*
* @param flippedState Flipped axis flags.
*/
void setFlippedState(flag_t flippedState);
/**
* Sets the dimensions of this tiled sprite.
*
* @param size Size of the sprite.
* @param center Negative center offset.
*/
void setSize(glm::vec2 size, glm::vec2 center);
/**
* Sets the size of this sprite. This will center the sprite on its origin
*
* @param size Size of the sprite.
*/
void setSize(glm::vec2 size);
};
}

View File

@ -21,4 +21,7 @@ set(LIMINAL_ASSETS_DIR ${DAWN_ASSETS_DIR}/games/liminal)
tool_vnscene(${LIMINAL_ASSETS_DIR}/test.xml)
tool_prefab(${LIMINAL_ASSETS_DIR}/VNTextbox.xml)
tool_truetype(font_main ${DAWN_ASSETS_DIR}/ark-pixel.ttf)
tool_truetype(font_main ${DAWN_ASSETS_DIR}/ark-pixel.ttf)
tool_prefab(${LIMINAL_ASSETS_DIR}/prefabs/EthPrefab.xml)
tool_texture(texture_eth ${LIMINAL_ASSETS_DIR}/textures/eth.png)

View File

@ -10,6 +10,7 @@
#include "prefabs/VNTextbox.hpp"
#include "display/TextureRenderTarget.hpp"
#include "scene/components/ui/UIImage.hpp"
#include "prefabs/EthPrefab.hpp"
namespace Dawn {
class HelloWorldScene : public Scene {
@ -19,51 +20,59 @@ namespace Dawn {
UICanvas *canvas;
TextureRenderTarget *renderTarget;
Texture text;
TilesetGrid grid;
int32_t test = 0;
void stage() override {
canvas = UICanvas::create(this);
// canvas = UICanvas::create(this);
camera = Camera::create(this);
glm::vec3 off = glm::vec3(90000, 100, 100);
glm::vec3 off = glm::vec3(0, 0, 0);
camera->transform->lookAt(glm::vec3(10, 10, 10) + off, glm::vec3(0, 0, 0) + off);
camNew = Camera::create(this);
auto camTexture = camNew->item->addComponent<CameraTexture>();
camNew->fov = 0.436332f;
camNew->transform->lookAt(glm::vec3(10, 10, 10), glm::vec3(0, 0, 0));
camTexture->renderTarget.setSize(1024, 1024);
// auto textbox = VNTextbox::create(this);
// textbox->transform.setParent(canvas->transform);
auto cube = SimpleSpinningCubePrefab::create(this);
// camNew = Camera::create(this);
// auto camTexture = camNew->item->addComponent<CameraTexture>();
// camNew->fov = 0.436332f;
// camNew->transform->lookAt(glm::vec3(10, 10, 10), glm::vec3(0, 0, 0));
// camTexture->renderTarget.setSize(1024, 1024);
auto textbox = VNTextbox::create(this);
textbox->transform.setParent(canvas->transform);
// auto uiTest = this->createSceneItem();
// uiTest->transform.setParent(canvas->transform);
// auto image = uiTest->addComponent<UIImage>();
// image->texture = camTexture->renderTarget.getTexture();
// image->alignment = glm::vec4(0, 0, 50, 0);
// image->alignX = UI_COMPONENT_ALIGN_START;
// image->alignUnitRight = UI_COMPONENT_ALIGN_UNIT_PERCENT;
// image->alignY = UI_COMPONENT_ALIGN_STRETCH;
auto uiTest = this->createSceneItem();
uiTest->transform.setParent(canvas->transform);
auto image = uiTest->addComponent<UIImage>();
image->texture = camTexture->renderTarget.getTexture();
image->alignment = glm::vec4(0, 0, 50, 0);
image->alignX = UI_COMPONENT_ALIGN_START;
image->alignUnitRight = UI_COMPONENT_ALIGN_UNIT_PERCENT;
image->alignY = UI_COMPONENT_ALIGN_STRETCH;
this->grid = TilesetGrid(
1, 26,
741, 20540,
0, 0,
0, 0
);
auto eth = EthPrefab::create(this);
// eth->tiledSprite->tileset = &grid;
struct Color colors[] = {
COLOR_BLUE, COLOR_MAGENTA, COLOR_WHITE,
COLOR_MAGENTA, COLOR_CORNFLOWER_BLUE, COLOR_MAGENTA,
COLOR_BLACK, COLOR_MAGENTA, COLOR_BLUE
};
text.setSize(3, 3);
text.buffer(colors);
textbox->border->texture = &text;
// struct Color colors[] = {
// COLOR_BLUE, COLOR_MAGENTA, COLOR_WHITE,
// COLOR_MAGENTA, COLOR_CORNFLOWER_BLUE, COLOR_MAGENTA,
// COLOR_BLACK, COLOR_MAGENTA, COLOR_BLUE
// };
// text.setSize(3, 3);
// text.buffer(colors);
// textbox->border->texture = &text;
}
std::vector<Asset*> getRequiredAssets() override {
auto assMan = &this->game->assetManager;
std::vector<Asset*> assets;
vectorAppend(&assets, SimpleSpinningCubePrefab::prefabAssets(assMan));
vectorAppend(&assets, VNTextbox::prefabAssets(assMan));
vectorAppend(&assets, EthPrefab::prefabAssets(assMan));
return assets;
}

View File

@ -107,7 +107,7 @@ std::vector<struct ShaderPassItem> SimpleTexturedShader::getPassItems(
Material *material,
Camera *camera
) {
SimpleTexturedMaterial *simpleMaterial = dynamic_cast<SimpleTexturedMaterial*>(material);
auto simpleMaterial = dynamic_cast<SimpleTexturedMaterial*>(material);
assertNotNull(simpleMaterial);
struct ShaderPassItem onlyPass;

View File

@ -18,7 +18,10 @@ int32_t TextureTool::start() {
// Load input file
File in(flags["input"]);
if(!in.open(FILE_MODE_READ)) return 1;
if(!in.open(FILE_MODE_READ)) {
std::cout << "Failed to open input file " << in.filename << std::endl;
return 1;
}
int w, h, channels;
auto imageRaw = stbi_load_from_file(in.file, &w, &h, &channels, STBI_rgb_alpha);