Merge remote-tracking branch 'origin/SP3-1-Rendering' into SP3-1-Rendering

This commit is contained in:
Brandon Mak 2022-11-07 11:11:56 +08:00
commit 6a6785d296
15 changed files with 453 additions and 19 deletions

View File

@ -50,7 +50,7 @@
Colliders: Colliders:
- Is Trigger: false - Is Trigger: false
Type: Box Type: Box
Half Extents: {x: 0.00427246094, y: 0.000122070312, z: 0.00427246094} Half Extents: {x: 24.7399445, y: 0.25, z: 8.75}
Friction: 0.400000006 Friction: 0.400000006
Bounciness: 0 Bounciness: 0
Density: 1 Density: 1
@ -85,12 +85,28 @@
Colliders: Colliders:
- Is Trigger: false - Is Trigger: false
Type: Box Type: Box
Half Extents: {x: 0.000244140625, y: 0.000244140625, z: 0.000244140625} Half Extents: {x: 1, y: 1, z: 1}
Friction: 0.400000006 Friction: 0.400000006
Bounciness: 0 Bounciness: 0
Density: 1 Density: 1
Position Offset: {x: 0, y: 0.5, z: 0} Position Offset: {x: 0, y: 0.5, z: 0}
Scripts: ~ Scripts:
- Type: PlayerController
drag: 2
currentState: 0
maxMoveVel: 2
moveForce: 50
sprintMultiplier: 2
rotationFactorPerFrame: 1
maxJumpHeight: 4
maxJumpTime: 0.75
fallMultipler: 2
lightMultiper: 0.75
mediumMultiper: 0.5
heavyMultiper: 0.25
- Type: PickAndThrow
throwForce: [200, 300, 200]
item: 5
- EID: 3 - EID: 3
Name: Default Name: Default
IsActive: true IsActive: true
@ -110,7 +126,12 @@
Translate: {x: 0, y: 0, z: 0} Translate: {x: 0, y: 0, z: 0}
Rotate: {x: 0, y: 0, z: 0} Rotate: {x: 0, y: 0, z: 0}
Scale: {x: 1, y: 1, z: 1} Scale: {x: 1, y: 1, z: 1}
Scripts: ~ Scripts:
- Type: SHADE_Scripting.ThirdPersonCamera
armLength: 2
turnSpeedPitch: 0.300000012
turnSpeedYaw: 0.5
pitchClamp: 45
- EID: 9 - EID: 9
Name: Default Name: Default
IsActive: true IsActive: true
@ -135,7 +156,7 @@
Scale: {x: 2, y: 2, z: 2} Scale: {x: 2, y: 2, z: 2}
Renderable Component: Renderable Component:
Mesh: 144838771 Mesh: 144838771
Material: 126974645 Material: 123745521
RigidBody Component: RigidBody Component:
Type: Dynamic Type: Dynamic
Mass: 1 Mass: 1
@ -153,19 +174,21 @@
Colliders: Colliders:
- Is Trigger: false - Is Trigger: false
Type: Box Type: Box
Half Extents: {x: 0.000244140625, y: 0.000244140625, z: 0.000244140625} Half Extents: {x: 1, y: 1, z: 1}
Friction: 0.400000006 Friction: 0.400000006
Bounciness: 0 Bounciness: 0
Density: 1 Density: 1
Position Offset: {x: 0, y: 0.5, z: 0} Position Offset: {x: 0, y: 0.5, z: 0}
- Is Trigger: true - Is Trigger: true
Type: Box Type: Box
Half Extents: {x: 0.00048828125, y: 0.00048828125, z: 0.00048828125} Half Extents: {x: 2, y: 2, z: 2}
Friction: 0.400000006 Friction: 0.400000006
Bounciness: 0 Bounciness: 0
Density: 1 Density: 1
Position Offset: {x: 0, y: 0.5, z: 0} Position Offset: {x: 0, y: 0.5, z: 0}
Scripts: ~ Scripts:
- Type: Item
currCategory: 0
- EID: 6 - EID: 6
Name: AI Name: AI
IsActive: true IsActive: true
@ -174,7 +197,7 @@
Transform Component: Transform Component:
Translate: {x: -8, y: -2, z: 2.5} Translate: {x: -8, y: -2, z: 2.5}
Rotate: {x: 0, y: 0, z: 0} Rotate: {x: 0, y: 0, z: 0}
Scale: {x: 2, y: 2, z: 2} Scale: {x: 1, y: 1, z: 1}
Renderable Component: Renderable Component:
Mesh: 149697411 Mesh: 149697411
Material: 126974645 Material: 126974645
@ -195,21 +218,28 @@
Colliders: Colliders:
- Is Trigger: false - Is Trigger: false
Type: Box Type: Box
Half Extents: {x: 0.000244140625, y: 0.000244140625, z: 0.000244140625} Half Extents: {x: 0.5, y: 0.5, z: 0.5}
Friction: 0.400000006 Friction: 0.400000006
Bounciness: 0 Bounciness: 0
Density: 1 Density: 1
Position Offset: {x: 0, y: 0.5, z: 0} Position Offset: {x: 0, y: 0.5, z: 0}
Scripts: ~ Scripts:
- Type: AIPrototype
movementForceMultiplier: 100
patrolSpeed: 0.400000006
chaseSpeed: 0.800000012
distanceToCapture: 1.20000005
distanceToStartChase: 2
distanceToEndChase: 2.5
- EID: 7 - EID: 7
Name: Default Name: Default
IsActive: true IsActive: true
NumberOfChildren: 0 NumberOfChildren: 0
Components: Components:
Transform Component: Transform Component:
Translate: {x: 3, y: -1, z: -1} Translate: {x: 0, y: -16.8647861, z: -14.039052}
Rotate: {x: 0, y: 0, z: 0} Rotate: {x: -0, y: 0, z: -0}
Scale: {x: 5, y: 5, z: 5} Scale: {x: 28.1434975, y: 28.1434975, z: 28.1434975}
Renderable Component: Renderable Component:
Mesh: 149697411 Mesh: 149697411
Material: 126974645 Material: 126974645

View File

@ -0,0 +1,40 @@
/*************************************************************************//**
* \file SHFontAsset.h
* \author Brandon Mak
* \date 5 November 2022
* \brief
*
* Copyright (C) 2022 DigiPen Institute of Technology. Reproduction or
* disclosure of this file or its contents without the prior written consent
* of DigiPen Institute of Technology is prohibited.
*****************************************************************************/
#pragma once
#include "SHAssetData.h"
#include "SH_API.h"
#include <vector>
#include <string>
#include "msdf-atlas-gen/msdf-atlas-gen.h"
#include "math/SHMatrix.h"
namespace SHADE
{
struct SH_API SHFontAsset : SHAssetData
{
using GlyphData = std::tuple<msdfgen::unicode_t, SHMatrix>;
/*-----------------------------------------------------------------------*/
/* MEMBER VARIABLES */
/*-----------------------------------------------------------------------*/
//! Name of the shader file (without parent path)
//std::string fontName;
//! Data containing character and uv transformation data and other misc data
std::vector<GlyphData> glyphTransformations;
//! The actual data of the atlas to go into the binary
std::unique_ptr<unsigned char[]> bitmapData;
};
}

View File

@ -0,0 +1,217 @@
#include "SHpch.h"
#include "SHFontCompiler.h"
#include "Graphics/MiddleEnd/TextRendering/SHFreetypeInstance.h"
#include "Assets/Asset Types/SHFontAsset.h"
#include "Math/Vector/SHVec2.h"
#include <fstream>
#include <iostream>
namespace SHADE
{
/***************************************************************************/
/*!
\brief
Given a valid ptr to a font asset and relevant data, initialize the data
inside the font asset. See SHFontAsset for details.
\param fontAsset
The ptr to the font asset.
\param glyphData
Individual glyph data.
\param fontBitmap
Actual bitmap data
\param fontGeometry
Font geometry required to get advance
*/
/***************************************************************************/
void SHFontCompiler::WriteToFontAsset(SHFontAsset* fontAsset, std::vector<msdf_atlas::GlyphGeometry> const& glyphData, msdfgen::Bitmap<msdfgen::byte, 3> const& fontBitmap, msdf_atlas::FontGeometry const& fontGeometry) noexcept
{
if (!fontAsset)
return;
uint32_t numGlyphs = static_cast<uint32_t>(glyphData.size());
for (uint32_t i = 0; i < numGlyphs; ++i)
{
// bounding box of the glyph in atlas
double atlasL = 0.0, atlasR = 0.0, atlasT = 0.0, atlasB = 0.0;
// bounding box of glyph as it should be placed on the baseline
double atlasPL = 0.0, atlasPR = 0.0, atlasPT = 0.0, atlasPB = 0.0;
// initialize the bounding boxes
glyphData[i].getQuadAtlasBounds(atlasL, atlasB, atlasR, atlasT);
glyphData[i].getQuadPlaneBounds(atlasPL, atlasPB, atlasPR, atlasPT);
// normalize the bounding box to (0 - 1).
atlasL /= fontBitmap.width();
atlasR /= fontBitmap.width();
atlasT /= fontBitmap.height();
atlasB /= fontBitmap.height();
// Normalized texture dimensions
SHVec2 const NORMALIZED_TEX_DIMS{ static_cast<float> (atlasR - atlasL), static_cast<float> (atlasT - atlasB) };
// When we render the quad, it has to correctly scale depending on what letter/glyph we are rendering. This is for that scale.
SHVec2 const QUAD_SCALE { static_cast<float> (atlasPR - atlasL), static_cast<float> (atlasT - atlasB) };
// initialize a matrix for uv and quad transformation data
SHMatrix transformData
{
// For scaling the tex coords
NORMALIZED_TEX_DIMS[0], 0.0f, 0.0f, 0.0f,
0.0f, NORMALIZED_TEX_DIMS[1], 0.0f, 0.0f,
// For translating the tex coords
static_cast<float>(atlasL), static_cast<float>(atlasB), 1.0f, 0.0f,
// Stores the transformation for a quad to correctly shape the glyph (first 2 values) and the bearing (last 2)
QUAD_SCALE[0], QUAD_SCALE[1], static_cast<float>(atlasPL), static_cast<float>(atlasPB)
};
// Initialize new data (we want the matrix transposed for shader use)
SHFontAsset::GlyphData newData = std::make_tuple(glyphData[i].getCodepoint(), SHMatrix::Transpose(transformData));
// Push 1 set of data for a character/glyph into the asset.
fontAsset->glyphTransformations.push_back(newData);
}
// copy data from bitmap to asset. Each channel is a 32 bit float and there are 3 channels.
fontAsset->bitmapData = std::make_unique<unsigned char[]>(fontBitmap.width() * fontBitmap.height() * 3 * sizeof (float));
}
/***************************************************************************/
/*!
\brief
Loads and compiles a font to binary format. Returns a path to the binary
data (XQ please confirm kor kor thanks <3).
\param path
Path to the font file (truetype font file) to load.
\return
Path to newly created binary data.
*/
/***************************************************************************/
std::optional<AssetPath> SHFontCompiler::LoadAndCompileFont(AssetPath path) noexcept
{
msdfgen::FontHandle* fontHandle = nullptr;
// XQ I need your help for path manipulation to actually load the msdfgen::FontHandle here. Am I doing this correctly?
fontHandle = msdfgen::loadFont(SHFreetypeInstance::GetFreetypeHandle(), path.string().c_str());
// Compile a font asset
auto* fontAsset = CompileFontToMemory(fontHandle);
// No path to binary format
if (!fontAsset)
return {};
CompileFontToBinary(path, *fontAsset);
return {};
}
/***************************************************************************/
/*!
\brief
This function takes in a font handle and generates a font asset from it.
It first geneates an atlas and all relevant data before creating the
asset.
\param fontHandle
MSDF font handle required to initialize member variables in SHFontAsset.
\return
A pointer to a brand new font asset.
*/
/***************************************************************************/
SHADE::SHFontAsset const* SHFontCompiler::CompileFontToMemory(msdfgen::FontHandle* fontHandle) noexcept
{
// Individual glyph geometry
std::vector<msdf_atlas::GlyphGeometry> glyphData;
// Actual bitmap data
msdfgen::Bitmap<msdfgen::byte, 3> fontBitmap;
// Font geometry required to get advance
msdf_atlas::FontGeometry fontGeometry (&glyphData);
// Load char set
fontGeometry.loadCharset(fontHandle, 1.0, msdf_atlas::Charset::ASCII);
// Apply MSDF edge coloring
const double maxCornerAngle = 3.0;
for (msdf_atlas::GlyphGeometry& glyph : glyphData)
glyph.edgeColoring(&msdfgen::edgeColoringInkTrap, maxCornerAngle, 0);
// configure parameters for atlas generation
msdf_atlas::TightAtlasPacker atlasPacker;
atlasPacker.setDimensionsConstraint(msdf_atlas::TightAtlasPacker::DimensionsConstraint::SQUARE);
atlasPacker.setMinimumScale(64.0);
atlasPacker.setPixelRange(2.0);
atlasPacker.setMiterLimit(1.0);
atlasPacker.pack(glyphData.data(), static_cast<int>(glyphData.size()));
// Get the dimensions after applying parameters
int width = 0, height = 0;
atlasPacker.getDimensions(width, height);
// generate the atlas
msdf_atlas::ImmediateAtlasGenerator<float, 3, msdf_atlas::msdfGenerator, msdf_atlas::BitmapAtlasStorage<float, 3>> generator(width, height);
msdf_atlas::GeneratorAttributes genAttribs;
generator.setAttributes(genAttribs);
generator.setThreadCount(4);
generator.generate(glyphData.data(), static_cast<int>(glyphData.size()));
fontBitmap = std::move(((msdfgen::Bitmap<msdfgen::byte, 3>&&)generator.atlasStorage()));
// at this point we have all the required data to initialize a font asset.
// Dynamically allocate new asset
SHFontAsset* newAsset = new SHFontAsset();
// Now we populate it with data
WriteToFontAsset(newAsset, glyphData, fontBitmap, fontGeometry);
return newAsset;
}
std::string SHFontCompiler::CompileFontToBinary(AssetPath path, SHFontAsset const& asset) noexcept
{
std::string newPath{ path.string() };
newPath = newPath.substr(0, newPath.find_last_of('.'));
newPath += SHADER_BUILT_IN_EXTENSION.data();
std::ofstream file{ newPath, std::ios::binary | std::ios::out | std::ios::trunc };
//file.write(
// reinterpret_cast<char const*>(&data.shaderType), sizeof(uint8_t)
//);
//size_t const byteCount = sizeof(uint32_t) * data.spirvBinary.size();
//file.write(
// reinterpret_cast<char const*>(&byteCount), sizeof(size_t)
//);
//file.write(
// reinterpret_cast<char const*>(data.spirvBinary.data()), byteCount
//);
file.close();
return newPath;
}
}

View File

@ -0,0 +1,22 @@
#pragma once
#include "Assets/SHAssetMacros.h"
#include "msdf-atlas-gen/msdf-atlas-gen.h"
namespace SHADE
{
class SHFontAsset;
class SHFontCompiler
{
private:
static void WriteToFontAsset (SHFontAsset* fontAsset, std::vector<msdf_atlas::GlyphGeometry> const& glyphData, msdfgen::Bitmap<msdfgen::byte, 3> const& fontBitmap, msdf_atlas::FontGeometry const& fontGeometry) noexcept;
public:
static std::optional<AssetPath> LoadAndCompileFont (AssetPath path) noexcept;
static SHFontAsset const* CompileFontToMemory (msdfgen::FontHandle* fontHandle) noexcept;
static std::string CompileFontToBinary (AssetPath path, SHFontAsset const& asset) noexcept;
};
}

View File

@ -0,0 +1,25 @@
#include "SHpch.h"
#include "SHFontLoader.h"
#include "Assets/Asset Types/SHFontAsset.h"
#include "Graphics/MiddleEnd/TextRendering/SHFreetypeInstance.h"
namespace SHADE
{
SHADE::SHAssetData* SHFontLoader::Load(AssetPath path)
{
return nullptr;
}
void SHFontLoader::Write(SHAssetData const* data, AssetPath path)
{
/*
Stuff to write to binary file :
- Interleaved per character data
- codepoint (actual character)
- 4x4 Matrix data (stores UV transform + extra data)
- Actual atlas texture
*/
}
}

View File

@ -0,0 +1,24 @@
/*************************************************************************//**
* \file SHSFontLoader.h
* \author Loh Xiao Qi
* \date 23 10 2022
* \brief
*
* Copyright (C) 2022 DigiPen Institute of Technology. Reproduction or
* disclosure of this file or its contents without the prior written consent
* of DigiPen Institute of Technology is prohibited.
*****************************************************************************/
#pragma once
#include "Assets/Libraries/Loaders/SHAssetLoader.h"
namespace SHADE
{
struct SHFontLoader : SHAssetLoader
{
SHAssetData* Load(AssetPath path) override;
void Write(SHAssetData const* data, AssetPath path) override;
};
}

View File

@ -79,7 +79,8 @@ constexpr std::string_view META_EXTENSION {".shmeta"};
constexpr std::string_view AUDIO_EXTENSION {".ogg"}; constexpr std::string_view AUDIO_EXTENSION {".ogg"};
constexpr std::string_view AUDIO_WAV_EXTENSION {".wav"}; constexpr std::string_view AUDIO_WAV_EXTENSION {".wav"};
constexpr std::string_view SHADER_EXTENSION{ ".shshader" }; constexpr std::string_view SHADER_EXTENSION{ ".shshader" };
constexpr std::string_view SHADER_BUILT_IN_EXTENSION{".shshaderb"}; constexpr std::string_view SHADER_BUILT_IN_EXTENSION{ ".shshaderb" };
constexpr std::string_view FONT_EXTENSION{ ".shfont" };
constexpr std::string_view SCRIPT_EXTENSION {".cs"}; constexpr std::string_view SCRIPT_EXTENSION {".cs"};
constexpr std::string_view SCENE_EXTENSION {".shade"}; constexpr std::string_view SCENE_EXTENSION {".shade"};
constexpr std::string_view PREFAB_EXTENSION {".shprefab"}; constexpr std::string_view PREFAB_EXTENSION {".shprefab"};
@ -91,6 +92,7 @@ constexpr std::string_view EXTENSIONS[] = {
AUDIO_EXTENSION, AUDIO_EXTENSION,
SHADER_EXTENSION, SHADER_EXTENSION,
SHADER_BUILT_IN_EXTENSION, SHADER_BUILT_IN_EXTENSION,
FONT_EXTENSION,
MATERIAL_EXTENSION, MATERIAL_EXTENSION,
TEXTURE_EXTENSION, TEXTURE_EXTENSION,
MODEL_EXTENSION, MODEL_EXTENSION,

View File

@ -117,16 +117,22 @@ namespace SHADE
auto* scriptEngine = static_cast<SHScriptEngine*>(SHSystemManager::GetSystem<SHScriptEngine>()); auto* scriptEngine = static_cast<SHScriptEngine*>(SHSystemManager::GetSystem<SHScriptEngine>());
scriptEngine->GenerateScriptsCsProjFile(); scriptEngine->GenerateScriptsCsProjFile();
} }
ImGui::BeginDisabled(SHSystemManager::GetSystem<SHEditor>()->editorState != SHEditor::State::STOP);
if (ImGui::Selectable("Build Scripts - Debug")) if (ImGui::Selectable("Build Scripts - Debug"))
{ {
auto* scriptEngine = static_cast<SHScriptEngine*>(SHSystemManager::GetSystem<SHScriptEngine>()); auto* scriptEngine = static_cast<SHScriptEngine*>(SHSystemManager::GetSystem<SHScriptEngine>());
SHSerialization::SerializeSceneToFile(SHSceneManager::GetCurrentSceneAssetID());
scriptEngine->BuildScriptAssembly(true, true); scriptEngine->BuildScriptAssembly(true, true);
SHSceneManager::RestartScene(SHSceneManager::GetCurrentSceneAssetID());
} }
if (ImGui::Selectable("Build Scripts - Release")) if (ImGui::Selectable("Build Scripts - Release"))
{ {
auto* scriptEngine = static_cast<SHScriptEngine*>(SHSystemManager::GetSystem<SHScriptEngine>()); auto* scriptEngine = static_cast<SHScriptEngine*>(SHSystemManager::GetSystem<SHScriptEngine>());
SHSerialization::SerializeSceneToFile(SHSceneManager::GetCurrentSceneAssetID());
scriptEngine->BuildScriptAssembly(false, true); scriptEngine->BuildScriptAssembly(false, true);
SHSceneManager::RestartScene(SHSceneManager::GetCurrentSceneAssetID());
} }
ImGui::EndDisabled();
ImGui::EndMenu(); ImGui::EndMenu();
} }

View File

@ -42,6 +42,7 @@ of DigiPen Institute of Technology is prohibited.
#include "Graphics/SHVkUtil.h" #include "Graphics/SHVkUtil.h"
#include "Graphics/RenderGraph/SHRenderGraphNodeCompute.h" #include "Graphics/RenderGraph/SHRenderGraphNodeCompute.h"
#include "../Meshes/SHPrimitiveGenerator.h" #include "../Meshes/SHPrimitiveGenerator.h"
#include "Graphics/MiddleEnd/TextRendering/SHFreetypeInstance.h"
namespace SHADE namespace SHADE
{ {
@ -310,6 +311,8 @@ namespace SHADE
lightingSubSystem = resourceManager.Create<SHLightingSubSystem>(); lightingSubSystem = resourceManager.Create<SHLightingSubSystem>();
lightingSubSystem->Init(device, descPool); lightingSubSystem->Init(device, descPool);
SHFreetypeInstance::Init();
} }
void SHGraphicsSystem::InitBuiltInResources(void) void SHGraphicsSystem::InitBuiltInResources(void)
@ -380,6 +383,7 @@ namespace SHADE
void SHGraphicsSystem::Exit(void) void SHGraphicsSystem::Exit(void)
{ {
SHFreetypeInstance::Exit();
} }
#pragma endregion INIT_EXIT #pragma endregion INIT_EXIT

View File

@ -0,0 +1,7 @@
#include "SHpch.h"
#include "SHFontAtlasData.h"
namespace SHADE
{
}

View File

@ -0,0 +1,7 @@
#pragma once
namespace SHADE
{
}

View File

@ -0,0 +1,27 @@
#include "SHpch.h"
#include "SHFreetypeInstance.h"
#include "msdf-atlas-gen/msdf-atlas-gen.h"
#include "msdfgen-ext.h"
namespace SHADE
{
msdfgen::FreetypeHandle* SHFreetypeInstance::freetypeHandle = nullptr;
void SHFreetypeInstance::Init(void) noexcept
{
// initialize freetype
freetypeHandle = msdfgen::initializeFreetype();
}
void SHFreetypeInstance::Exit(void) noexcept
{
msdfgen::deinitializeFreetype(freetypeHandle);
}
msdfgen::FreetypeHandle* SHFreetypeInstance::GetFreetypeHandle(void) noexcept
{
return freetypeHandle;
}
}

View File

@ -0,0 +1,23 @@
#pragma once
namespace msdfgen
{
class FreetypeHandle;
}
namespace SHADE
{
class SHFreetypeInstance
{
private:
//! Only need this to be initialized once
static msdfgen::FreetypeHandle* freetypeHandle;
public:
static void Init (void) noexcept;
static void Exit (void) noexcept;
static msdfgen::FreetypeHandle* GetFreetypeHandle(void) noexcept;
};
}

View File

@ -168,7 +168,7 @@ namespace YAML
case SHCollider::Type::BOX: case SHCollider::Type::BOX:
{ {
if (node[HalfExtents].IsDefined()) if (node[HalfExtents].IsDefined())
rhs.SetBoundingBox(node[HalfExtents].as<SHVec3>()); rhs.SetBoundingBox(node[HalfExtents].as<SHVec3>() * 2.0f);
} }
break; break;
case SHCollider::Type::SPHERE: case SHCollider::Type::SPHERE:

View File

@ -15,7 +15,7 @@ public class PlayerController : Script
TOTAL TOTAL
} }
public RigidBody rb; public RigidBody rb { get; set; }
private Transform tranform; private Transform tranform;
private Camera cam; private Camera cam;
private PickAndThrow pat; private PickAndThrow pat;