図0402a
ここに配置されているオブジェクトはFullTutorial006に配置されているのと同じものです。スプライトオブジェクトのみを取り出したものです。
//ビューの作成
void GameStage::CreateViewLight() {
auto PtrView = CreateViewSingleView>();
//シャドウマップは使用しない
SetShadowmapDraw(false);
}
//シャドウマップは使用しない
SetShadowmapDraw(false);
//--------------------------------------------------------------------------------------
/// 半透明のスプライト
//--------------------------------------------------------------------------------------
class TraceSprite : public GameObject {
bool m_Trace;
Vec2 m_StartScale;
Vec3 m_StartPos;
float m_TotalTime;
//バックアップ頂点データ
vector<VertexPositionColor> m_BackupVertices;
public:
//--------------------------------------------------------------------------------------
/*!
@brief コンストラクタ
@param[in] StagePtr ステージ
@param[in] Trace 透明処理するかどうか
@param[in] StartScale 初期スケール
@param[in] StartPos 初期位置
*/
//--------------------------------------------------------------------------------------
TraceSprite(const shared_ptr<Stage>& StagePtr, bool Trace,
const Vec2& StartScale, const Vec3& StartPos);
//--------------------------------------------------------------------------------------
/*!
@brief デストラクタ
*/
//--------------------------------------------------------------------------------------
virtual ~TraceSprite();
//--------------------------------------------------------------------------------------
/*!
@brief 初期化
@return なし
*/
//--------------------------------------------------------------------------------------
virtual void OnCreate() override;
//--------------------------------------------------------------------------------------
/*!
@brief 更新
@return なし
*/
//--------------------------------------------------------------------------------------
virtual void OnUpdate()override;
};
//--------------------------------------------------------------------------------------
/// 半透明のスプライト
//--------------------------------------------------------------------------------------
TraceSprite::TraceSprite(const shared_ptr<Stage>& StagePtr, bool Trace,
const Vec2& StartScale, const Vec3& StartPos) :
GameObject(StagePtr),
m_Trace(Trace),
m_StartScale(StartScale),
m_StartPos(StartPos),
m_TotalTime(0)
{}
void TraceSprite::OnCreate() {
float HelfSize = 0.5f;
//頂点配列
m_BackupVertices = {
{ VertexPositionColor(Vec3(-HelfSize, HelfSize, 0),Col4(1.0f,0.0f,0.0f,0.0f)) },
{ VertexPositionColor(Vec3(HelfSize, HelfSize, 0), Col4(0.0f, 1.0f, 0.0f, 0.0f)) },
{ VertexPositionColor(Vec3(-HelfSize, -HelfSize, 0), Col4(0.0f, 0.0f, 1.0f, 0.0f)) },
{ VertexPositionColor(Vec3(HelfSize, -HelfSize, 0), Col4(0.0f, 0.0f, 0, 0.0f)) },
};
//インデックス配列
vector<uint16_t> indices = { 0, 1, 2, 1, 3, 2 };
SetAlphaActive(m_Trace);
auto PtrTransform = GetComponent<Transform>();
PtrTransform->SetScale(m_StartScale.x, m_StartScale.y, 1.0f);
PtrTransform->SetRotation(0, 0, 0);
PtrTransform->SetPosition(m_StartPos);
//頂点とインデックスを指定してスプライト作成
AddComponent<PCSpriteDraw>(m_BackupVertices, indices);
}
AddComponent<PCSpriteDraw>(m_BackupVertices, indices);
void TraceSprite::OnUpdate() {
float ElapsedTime = App::GetApp()->GetElapsedTime();
m_TotalTime += ElapsedTime;
if (m_TotalTime >= XM_PI) {
m_TotalTime = 0;
}
vector<VertexPositionColor> NewVertices;
for (size_t i = 0; i < m_BackupVertices.size(); i++) {
Col4 col = m_BackupVertices[i].color;
col.w = sin(m_TotalTime);
auto v = VertexPositionColor(
m_BackupVertices[i].position,
col
);
NewVertices.push_back(v);
}
auto PtrDraw = GetComponent<PCSpriteDraw>();
PtrDraw->UpdateVertices(NewVertices);
}
auto PtrDraw = GetComponent<PCSpriteDraw>();
PtrDraw->UpdateVertices(NewVertices);
図0402b
それに対して、以下のようにUV値を設定すると、テクスチャを分割して張り付けるようになります。ScoreSpriteクラスの数字のテクスチャの貼り付け方はこのような形です。
図0402c
そして下図はタイリング処理です。1つのテクスチャを一つの面に複数貼り付けます。下図がそのイメージです。ScrollSpriteクラスがそうですね。
図0402d
void ScoreSprite::OnUpdate() {
vector<VertexPositionTexture> NewVertices;
UINT Num;
int VerNum = 0;
for (UINT i = m_NumberOfDigits; i > 0; i--) {
UINT Base = (UINT)pow(10, i);
Num = ((UINT)m_Score) % Base;
Num = Num / (Base / 10);
Vec2 UV0 = m_BackupVertices[VerNum].textureCoordinate;
UV0.x = (float)Num / 10.0f;
auto v = VertexPositionTexture(
m_BackupVertices[VerNum].position,
UV0
);
NewVertices.push_back(v);
Vec2 UV1 = m_BackupVertices[VerNum + 1].textureCoordinate;
UV1.x = UV0.x + 0.1f;
v = VertexPositionTexture(
m_BackupVertices[VerNum + 1].position,
UV1
);
NewVertices.push_back(v);
Vec2 UV2 = m_BackupVertices[VerNum + 2].textureCoordinate;
UV2.x = UV0.x;
v = VertexPositionTexture(
m_BackupVertices[VerNum + 2].position,
UV2
);
NewVertices.push_back(v);
Vec2 UV3 = m_BackupVertices[VerNum + 3].textureCoordinate;
UV3.x = UV0.x + 0.1f;
v = VertexPositionTexture(
m_BackupVertices[VerNum + 3].position,
UV3
);
NewVertices.push_back(v);
VerNum += 4;
}
auto PtrDraw = GetComponent<PTSpriteDraw>();
PtrDraw->UpdateVertices(NewVertices);
}
図0402e
void ScrollSprite::OnCreate() {
//中略
//頂点とインデックスを指定してスプライト作成
auto PtrDraw = AddComponent<PTSpriteDraw>(m_BackupVertices, indices);
PtrDraw->SetSamplerState(SamplerState::LinearWrap);
PtrDraw->SetTextureResource(m_TextureKey);
}
void ScrollSprite::OnUpdate() {
float ElapsedTime = App::GetApp()->GetElapsedTime();
m_TotalTime += ElapsedTime;
if (m_TotalTime > 1.0f) {
m_TotalTime = 0;
}
vector<VertexPositionTexture> NewVertices;
for (size_t i = 0; i < m_BackupVertices.size(); i++) {
Vec2 UV = m_BackupVertices[i].textureCoordinate;
if (UV.x == 0.0f) {
UV.x = m_TotalTime;
}
else if (UV.x == 4.0f) {
UV.x += m_TotalTime;
}
auto v = VertexPositionTexture(
m_BackupVertices[i].position,
UV
);
NewVertices.push_back(v);
}
auto PtrDraw = GetComponent<PTSpriteDraw>();
PtrDraw->UpdateVertices(NewVertices);
}