Thruster color can be modified globally or per direction

master
mike-f1 2017-03-13 09:17:28 +01:00
parent c6e11e39e3
commit a54eb2f0d1
9 changed files with 175 additions and 2 deletions

View File

@ -37,6 +37,20 @@ Intro::Intro(Graphics::Renderer *r, int width, int height)
for (auto i : ShipType::player_ships) {
SceneGraph::Model *model = Pi::FindModel(ShipType::types[i].modelName)->MakeInstance();
model->SetThrust(vector3f(0.f, 0.f, -0.6f), vector3f(0.f));
if (ShipType::types[i].isGlobalColorDefined) model->SetThrusterColor(ShipType::types[i].globalThrusterColor);
for (int j=0; j<THRUSTER_MAX; j++) {
if (!ShipType::types[i].isDirectionColorDefined[j]) continue;
vector3f dir;
switch (j) {
case THRUSTER_FORWARD: dir = vector3f(0.0, 0.0, 1.0); break;
case THRUSTER_REVERSE: dir = vector3f(0.0, 0.0, -1.0); break;
case THRUSTER_LEFT: dir = vector3f(1.0, 0.0, 0.0); break;
case THRUSTER_RIGHT: dir = vector3f(-1.0, 0.0, 0.0); break;
case THRUSTER_UP: dir = vector3f(1.0, 0.0, 0.0); break;
case THRUSTER_DOWN: dir = vector3f(-1.0, 0.0, 0.0); break;
}
model->SetThrusterColor(dir, ShipType::types[i].directionThrusterColor[j]);
}
const Uint32 numMats = model->GetNumMaterials();
for( Uint32 m=0; m<numMats; m++ ) {
RefCountedPtr<Graphics::Material> mat = model->GetMaterialByIndex(m);

View File

@ -229,7 +229,6 @@ void Ship::Init()
p.Set("fuelMassLeft", m_stats.fuel_tank_mass_left);
// Init of Propulsion:
// TODO: Separe the enum used in ShipType and use it both on Propulsion and ShipType
Propulsion::Init( this, GetModel(), m_type->fuelTankMass, m_type->effectiveExhaustVelocity, m_type->linThrust, m_type->angThrust );
p.Set("shipName", m_shipName);
@ -300,6 +299,21 @@ Ship::Ship(const ShipType::Id &shipId): DynamicBody(),
m_decelerating = false;
SetModel(m_type->modelName.c_str());
// Setting thrusters colors
if (m_type->isGlobalColorDefined) GetModel()->SetThrusterColor(m_type->globalThrusterColor);
for (int i=0; i<THRUSTER_MAX; i++) {
if (!m_type->isDirectionColorDefined[i]) continue;
vector3f dir;
switch (i) {
case THRUSTER_FORWARD: dir = vector3f(0.0, 0.0, 1.0); break;
case THRUSTER_REVERSE: dir = vector3f(0.0, 0.0, -1.0); break;
case THRUSTER_LEFT: dir = vector3f(1.0, 0.0, 0.0); break;
case THRUSTER_RIGHT: dir = vector3f(-1.0, 0.0, 0.0); break;
case THRUSTER_UP: dir = vector3f(1.0, 0.0, 0.0); break;
case THRUSTER_DOWN: dir = vector3f(-1.0, 0.0, 0.0); break;
}
GetModel()->SetThrusterColor(dir, m_type->directionThrusterColor[i]);
}
SetLabel("UNLABELED_SHIP");
m_skin.SetRandomColors(Pi::rng);
m_skin.SetDecal(m_type->manufacturer);

View File

@ -51,6 +51,8 @@ ShipType::ShipType(const Id &_id, const std::string &path)
Json::Reader reader;
Json::Value data;
isGlobalColorDefined = false;
auto fd = FileSystem::gameDataFiles.ReadFile(path);
if (!fd) {
Output("couldn't open ship def '%s'\n", path.c_str());
@ -87,6 +89,85 @@ ShipType::ShipType(const Id &_id, const std::string &path)
linThrust[THRUSTER_RIGHT] = data.get("right_thrust", 0.0f).asFloat();
angThrust = data.get("angular_thrust", 0.0f).asFloat();
// Parse global thrusters color
bool error = false;
int parse = 0;
for( Json::Value::iterator thruster_color = data["thruster_global_color"].begin() ; thruster_color != data["thruster_global_color"].end() ; ++thruster_color ) {
const std::string colorchannel = thruster_color.key().asString();
if (colorchannel.length()!=1) {
error = true;
break;
}
if (colorchannel.at(0) == 'r') {
globalThrusterColor.r = data["thruster_global_color"].get(colorchannel, 0).asInt();
parse++;
continue;
} else if (colorchannel.at(0) == 'g') {
globalThrusterColor.g = data["thruster_global_color"].get(colorchannel, 0).asInt();
parse++;
continue;
} else if (colorchannel.at(0) == 'b') {
globalThrusterColor.b = data["thruster_global_color"].get(colorchannel, 0).asInt();
parse++;
continue;
} else {
// No 'r', no 'g', no 'b', no good :/
error = true;
break;
}
}
if (error==true) {
isGlobalColorDefined = false;
Output("In file \"%s.json\" global thrusters custom color must be \"r\",\"g\" and \"b\"\n", modelName.c_str());
} else {
globalThrusterColor.a = 255;
isGlobalColorDefined = true;
}
// Parse direction thrusters color
for (int i=0; i<THRUSTER_MAX; i++) isDirectionColorDefined[i]=false;
error = false;
for( Json::Value::iterator thruster_color = data["thruster_direction_color"].begin() ; thruster_color != data["thruster_direction_color"].end() ; ++thruster_color ) {
const std::string th_color_dir = thruster_color.key().asString();
Json::Value dir_color = data["thruster_direction_color"].get(th_color_dir, 0);
Color color;
if (!dir_color.isMember("r")||!dir_color.isMember("g")||!dir_color.isMember("b")) {
error = true;
continue /* for */;
} else {
color.r = dir_color["r"].asInt();
color.g = dir_color["g"].asInt();
color.b = dir_color["b"].asInt();
color.a = 255;
}
if (th_color_dir.find("forward")!=std::string::npos) {
isDirectionColorDefined[THRUSTER_FORWARD]=true;
directionThrusterColor[THRUSTER_FORWARD]= color;
}
if (th_color_dir.find("retro")!=std::string::npos) {
isDirectionColorDefined[THRUSTER_REVERSE]=true;
directionThrusterColor[THRUSTER_REVERSE]= color;
}
if (th_color_dir.find("left")!=std::string::npos) {
isDirectionColorDefined[THRUSTER_LEFT]=true;
directionThrusterColor[THRUSTER_LEFT]= color;
}
if (th_color_dir.find("right")!=std::string::npos) {
isDirectionColorDefined[THRUSTER_RIGHT]=true;
directionThrusterColor[THRUSTER_RIGHT]= color;
}
if (th_color_dir.find("up")!=std::string::npos) {
isDirectionColorDefined[THRUSTER_UP]=true;
directionThrusterColor[THRUSTER_UP]= color;
}
if (th_color_dir.find("down")!=std::string::npos) {
isDirectionColorDefined[THRUSTER_DOWN]=true;
directionThrusterColor[THRUSTER_DOWN]= color;
}
}
if (error==true) {
for (int i=0; i<THRUSTER_MAX; i++) isDirectionColorDefined[i]=false;
Output("In file \"%s.json\" directional thrusters custom color must be \"r\",\"g\" and \"b\"\n", modelName.c_str());
}
// invert values where necessary
linThrust[THRUSTER_FORWARD] *= -1.f;
linThrust[THRUSTER_LEFT] *= -1.f;

View File

@ -41,6 +41,10 @@ struct ShipType {
float angThrust;
std::map<std::string, int> slots;
std::map<std::string, bool> roles;
Color globalThrusterColor; // Override default color for thrusters
bool isGlobalColorDefined; // If globalThrusterColor is filled with... a color :)
Color directionThrusterColor[THRUSTER_MAX];
bool isDirectionColorDefined[THRUSTER_MAX];
double thrusterUpgrades[4];
int capacity; // tonnes
int hullMass;

View File

@ -9,6 +9,8 @@
#include "graphics/VertexArray.h"
#include "StringF.h"
#include "json/JsonUtils.h"
#include "FindNodeVisitor.h"
#include "Thruster.h"
namespace SceneGraph {
@ -472,6 +474,54 @@ void Model::SetThrust(const vector3f &lin, const vector3f &ang)
m_renderData.angthrust[2] = ang.z;
}
void Model::SetThrusterColor(const vector3f &dir, const Color &color)
{
assert(m_root!=nullptr);
FindNodeVisitor thrusterFinder(FindNodeVisitor::MATCH_NAME_FULL, "thrusters");
m_root->Accept(thrusterFinder);
const std::vector<Node*> &results = thrusterFinder.GetResults();
Group* thrusters = static_cast<Group*>(results.at(0));
for (unsigned int i=0; i<thrusters->GetNumChildren(); i++) {
MatrixTransform *mt = static_cast<MatrixTransform*>(thrusters->GetChildAt(i));
Thruster* my_thruster = static_cast<Thruster*>(mt->GetChildAt(0));
if (my_thruster==nullptr) continue;
float dot = my_thruster->GetDirection().Dot(dir);
if (dot>0.99) my_thruster->SetColor(color);
}
}
void Model::SetThrusterColor(const std::string &name, const Color &color)
{
assert(m_root!=nullptr);
FindNodeVisitor thrusterFinder(FindNodeVisitor::MATCH_NAME_FULL, name);
m_root->Accept(thrusterFinder);
const std::vector<Node*> &results = thrusterFinder.GetResults();
//Hope there's only 1 result...
Thruster* my_thruster = static_cast<Thruster*>(results.at(0));
if (my_thruster!=nullptr) my_thruster->SetColor(color);
}
void Model::SetThrusterColor(const Color &color)
{
assert(m_root!=nullptr);
FindNodeVisitor thrusterFinder(FindNodeVisitor::MATCH_NAME_FULL, "thrusters");
m_root->Accept(thrusterFinder);
const std::vector<Node*> &results = thrusterFinder.GetResults();
Group* thrusters = static_cast<Group*>(results.at(0));
for (unsigned int i=0; i<thrusters->GetNumChildren(); i++) {
MatrixTransform *mt = static_cast<MatrixTransform*>(thrusters->GetChildAt(i));
Thruster* my_thruster = static_cast<Thruster*>(mt->GetChildAt(0));
assert(my_thruster!=nullptr);
my_thruster->SetColor(color);
}
}
class SaveVisitorJson : public NodeVisitor {
public:
SaveVisitorJson(Json::Value &jsonObj) : m_jsonArray(jsonObj) {}

View File

@ -153,6 +153,10 @@ public:
//special for ship model use
void SetThrust(const vector3f &linear, const vector3f &angular);
void SetThrusterColor(const vector3f &dir, const Color &color);
void SetThrusterColor(const std::string &name, const Color &color);
void SetThrusterColor(const Color &color);
void SaveToJson(Json::Value &jsonObj) const;
void LoadFromJson(const Json::Value &jsonObj);

View File

@ -41,6 +41,7 @@ struct RenderData
{
float linthrust[3]; // 1.0 to -1.0
float angthrust[3]; // 1.0 to -1.0
Color customColor;
float boundingRadius; //updated by model and passed to submodels
unsigned int nodemask;

View File

@ -22,6 +22,7 @@ Thruster::Thruster(Graphics::Renderer *r, bool _linear, const vector3f &_pos, co
, linearOnly(_linear)
, dir(_dir)
, pos(_pos)
, currentColor(baseColor)
{
//set up materials
Graphics::MaterialDescriptor desc;
@ -49,6 +50,7 @@ Thruster::Thruster(const Thruster &thruster, NodeCopyCache *cache)
, linearOnly(thruster.linearOnly)
, dir(thruster.dir)
, pos(thruster.pos)
, currentColor(thruster.currentColor)
{
}
@ -87,7 +89,7 @@ void Thruster::Render(const matrix4x4f &trans, const RenderData *rd)
}
if (power < 0.001f) return;
m_tMat->diffuse = m_glowMat->diffuse = baseColor * power;
m_tMat->diffuse = m_glowMat->diffuse = currentColor * power;
//directional fade
vector3f cdir = vector3f(trans * -dir).Normalized();

View File

@ -28,6 +28,8 @@ public:
virtual void Render(const matrix4x4f &trans, const RenderData *rd) override;
virtual void Save(NodeDatabase&) override;
static Thruster *Load(NodeDatabase&);
void SetColor(const Color c) { currentColor = c; }
const vector3f &GetDirection() { return dir; }
private:
static Graphics::VertexBuffer* CreateThrusterGeometry(Graphics::Renderer*, Graphics::Material*);
@ -40,6 +42,7 @@ private:
bool linearOnly;
vector3f dir;
vector3f pos;
Color currentColor;
};
}