Skip to content

Commit

Permalink
[#17] : Fixed shader issue
Browse files Browse the repository at this point in the history
  • Loading branch information
ange-yaghi committed Jun 24, 2020
1 parent 1d3010d commit 59b8596
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 22 deletions.
27 changes: 13 additions & 14 deletions engines/basic/shaders/delta_engine_shader.fx
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ float f_diffuse(float3 i, float3 o, float3 h, float3 normal, float power, float
float cos_theta_o = dot(o, normal);

float f_d = (1 + (f_d90 - 1) * pow5(1 - cos_theta_i)) * (1 + (f_d90 - 1) * pow5(1 - cos_theta_o));
return f_d * power * cos_theta_i;
return clamp(f_d * power * cos_theta_i, 0.0, 1.0);
}

float f_specular(float3 i, float3 o, float3 h, float3 normal, float F0, float power, float specularPower) {
Expand All @@ -181,7 +181,7 @@ float f_specular(float3 i, float3 o, float3 h, float3 normal, float F0, float po
float s = pow5(1 - o_dot_h);
float F = F0_scaled + s * (1 - F0_scaled);

return pow(intensity, specularPower) * F * power;
return min(1.0, pow(intensity, specularPower) * F * power);
}

float f_specular_ambient(float3 o, float3 normal, float F0, float power) {
Expand All @@ -191,7 +191,7 @@ float f_specular_ambient(float3 o, float3 normal, float F0, float power) {
float s = pow5(1 - o_dot_n);
float F = F0_scaled + s * (1 - F0_scaled);

return F * power;
return min(1.0, F * power);
}

float linearToSrgb(float u) {
Expand Down Expand Up @@ -236,6 +236,7 @@ float4 PS(VS_OUTPUT input) : SV_Target {
const float FullSpecular = 1 / 0.08;

float3 totalLighting = float3(1.0, 1.0, 1.0);
float3 normal = normalize(input.Normal);

float4 baseColor;
float roughness = 0.5;
Expand All @@ -253,10 +254,11 @@ float4 PS(VS_OUTPUT input) : SV_Target {

if (Lit == 1) {
float3 o = normalize(CameraEye.xyz - input.VertexPosition.xyz);
float cos_theta_o = dot(o, normal);

float3 ambientSpecular = f_specular_ambient(o, input.Normal, IncidentSpecular, SpecularMix) * AmbientLighting;
float3 ambientDiffuse = f_diffuse(o, o, o, input.Normal, DiffuseMix, DiffuseRoughness) * AmbientLighting * baseColor;
float3 ambientMetallic = f_specular_ambient(o, input.Normal, FullSpecular, 1.0) * AmbientLighting.rgb * baseColor.rgb;
float3 ambientSpecular = f_specular_ambient(o, normal, IncidentSpecular, SpecularMix) * AmbientLighting;
float3 ambientDiffuse = f_diffuse(o, o, o, normal, DiffuseMix, DiffuseRoughness) * AmbientLighting * baseColor;
float3 ambientMetallic = f_specular_ambient(o, normal, FullSpecular, 1.0) * AmbientLighting.rgb * baseColor.rgb;

totalLighting = lerp(
ambientSpecular + ambientDiffuse,
Expand All @@ -272,32 +274,29 @@ float4 PS(VS_OUTPUT input) : SV_Target {
float inv_mag = 1.0 / length(i);
i *= inv_mag;

float cos_theta_i = dot(i, input.Normal);
float cos_theta_o = dot(o, input.Normal);
float cos_theta_i = dot(i, normal);

if (cos_theta_i < 0) continue;
if (cos_theta_o < 0) continue;

float3 h = normalize(i + o);
float3 diffuse = f_diffuse(i, o, h, input.Normal, DiffuseMix, DiffuseRoughness) * baseColor.rgb * light.Color;
float3 specular = f_specular(i, o, h, input.Normal, IncidentSpecular, SpecularMix, SpecularPower) * light.Color;
float3 diffuse = f_diffuse(i, o, h, normal, DiffuseMix, DiffuseRoughness) * baseColor.rgb * light.Color;
float3 specular = f_specular(i, o, h, normal, IncidentSpecular, SpecularMix, SpecularPower) * light.Color;
float3 metallic = 0;

if (Metallic > 0) {
metallic = f_specular(i, o, h, input.Normal, FullSpecular, 1, SpecularPower) * light.Color * baseColor.rgb;
metallic = f_specular(i, o, h, normal, FullSpecular, 1, SpecularPower) * light.Color * baseColor.rgb;
}

// Spotlight calculation
float spotCoherence = -dot(i, light.Direction.xyz);
float spotAttenuation = 1.0f;
if (spotCoherence > light.Attenuation0) spotAttenuation = 1.0f;
if (spotCoherence >= light.Attenuation0) spotAttenuation = 1.0f;
else if (spotCoherence < light.Attenuation1) spotAttenuation = 0.0f;
else {
float t = light.Attenuation0 - light.Attenuation1;
if (t == 0) spotAttenuation = 1.0f;
else spotAttenuation = (spotCoherence - light.Attenuation1) / t;
}
spotAttenuation = 1.0;

float falloff = 1.0;
if (light.FalloffEnabled == 1) {
Expand Down
6 changes: 5 additions & 1 deletion engines/basic/src/asset_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,8 @@ ysError dbasic::AssetManager::LoadSceneFile(const char *fname, bool placeInVram)
m_engine->GetDevice()->CreateVertexBuffer(&vertexBuffer, 4 * 1024 * 1024, nullptr, false);
}

std::map<int, int> modelIndexMap;

for (int i = 0; i < fileHeader.ObjectCount; i++) {
ysGeometryExportFile::ObjectOutputHeader header;
file.read((char *)&header, sizeof(ysGeometryExportFile::ObjectOutputHeader));
Expand Down Expand Up @@ -308,10 +310,12 @@ ysError dbasic::AssetManager::LoadSceneFile(const char *fname, bool placeInVram)
}

if (objectType == ysObjectData::ObjectType::Instance) {
newObject->m_geometry = GetModelAsset(header.ParentInstanceIndex + initialIndex);
newObject->m_geometry = GetModelAsset(modelIndexMap[header.ParentInstanceIndex]);
}
}
else if (objectType == ysObjectData::ObjectType::Geometry) {
modelIndexMap[i] = m_modelAssets.GetNumObjects();

// New model asset
ModelAsset *newModelAsset = NewModelAsset();

Expand Down
6 changes: 6 additions & 0 deletions include/yds_animation_target.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
#include "yds_math.h"

#include <algorithm>
#include <assert.h>
#include <cmath>

class ysAnimationCurve;

Expand Down Expand Up @@ -35,6 +37,8 @@ struct TransformTarget {
}

void Accumulate(float t, int index) {
assert(!std::isnan(t) && !std::isinf(t));

unsigned int bit = 0x1 << index;
if ((Animated & bit) == 0) {
Data[index] = t;
Expand All @@ -47,6 +51,8 @@ struct TransformTarget {
}

void AccumulateQuaternion(ysQuaternion &q, float weight) {
assert(ysMath::IsValid(q));

if (Animated == 0) {
Data[0] = ysMath::GetQuatW(q) * weight;
Data[1] = ysMath::GetQuatX(q) * weight;
Expand Down
1 change: 1 addition & 0 deletions physics/include/collision_primitives.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ namespace dphysics {
Collision &operator=(Collision &collision);

bool IsGhost() const;
bool IsResolvable() const;

// Get velocity on impact
ysVector GetContactVelocity() const { return m_initialContactVelocity; }
Expand Down
10 changes: 10 additions & 0 deletions physics/src/collision_primitives.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,16 @@ bool dphysics::Collision::IsGhost() const {
return false;
}

bool dphysics::Collision::IsResolvable() const {
if (m_bodies[0] == nullptr || m_bodies[0]->GetInverseMass() == 0) {
if (m_bodies[1] == nullptr || m_bodies[1]->GetInverseMass() == 0) {
return false;
}
}

return true;
}

void dphysics::Collision::CalculateDesiredDeltaVelocity(float timestep) {
const static float VelocityLimit = 0.25f;

Expand Down
2 changes: 1 addition & 1 deletion physics/src/rigid_body.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

dphysics::RigidBody::RigidBody() {
m_linearDamping = 0.99f;
m_angularDamping = 0.005f;
m_angularDamping = 0.5f;

m_velocity = ysMath::Constants::Zero;
m_angularVelocity = ysMath::Constants::Zero;
Expand Down
8 changes: 4 additions & 4 deletions physics/src/rigid_body_system.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -573,6 +573,7 @@ void dphysics::RigidBodySystem::AdjustVelocities(float timestep) {

if (c->m_sensor) continue;
if (c->IsGhost()) continue;
if (!c->IsResolvable()) continue;

if (c->m_bodies[0] != nullptr) {
if (c->m_bodies[0] == biggestCollision->m_bodies[0]) {
Expand Down Expand Up @@ -691,12 +692,9 @@ void dphysics::RigidBodySystem::AdjustVelocity(Collision *collision, ysVector ve

impulseContact = ysMath::Add(ic, icx);
}
///<UpdateVelWithFriction

///>ImpulseToWorld
// Convert impulse to world coordinates
// Convert impulse to world coordinates
ysVector impulse = ysMath::MatMult(collision->m_contactSpace, impulseContact);
///<ImpulseToWorld

// Split in the impulse into linear and rotational components
ysVector impulsiveTorque = ysMath::Cross(collision->m_relativePosition[0], impulse);
Expand Down Expand Up @@ -744,6 +742,7 @@ void dphysics::RigidBodySystem::ResolveCollisions(float dt) {

if (collision.m_sensor) continue;
if (collision.IsGhost()) continue;
if (!collision.IsResolvable()) continue;

collision.UpdateInternals(dt);

Expand All @@ -769,6 +768,7 @@ void dphysics::RigidBodySystem::ResolveCollisions(float dt) {

if (collision.m_sensor) continue;
if (collision.IsGhost()) continue;
if (!collision.IsResolvable()) continue;

if (collision.m_body1 == biggestCollision->m_body1) {
cp = ysMath::Cross(rotationChange[0], collision.m_relativePosition[0]);
Expand Down
10 changes: 8 additions & 2 deletions src/yds_animation_mixer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

#include <assert.h>
#include <algorithm>
#include <cmath>

ysAnimationChannel::ActionSettings ysAnimationChannel::DefaultSettings;

Expand Down Expand Up @@ -43,7 +44,7 @@ void ysAnimationChannel::Sample() {

if (d < 0) continue;

if (d < t) {
if (d < t && t != 0) {
m_segmentStack[i].Amplitude = (d / t);
}
else {
Expand All @@ -65,6 +66,10 @@ void ysAnimationChannel::Sample() {
}
else if (d > 0) {
float s = 1.0f - d / t;
if (t == 0) {
int a = 0;
}

m_segmentStack[i].Amplitude = m_segmentStack[i].FadeStartAmplitude * s;
}
}
Expand Down Expand Up @@ -207,9 +212,10 @@ void ysAnimationChannel::AddSegmentAtOffset(ysAnimationActionBinding *action, fl
void ysAnimationChannel::Balance() {
int activeSegments = 0;
float totalAmplitude = GetTotalAmplitude(&activeSegments);
if (activeSegments <= 1) return;
if (activeSegments <= 1 || totalAmplitude == 0) return;

float inv = 1 / totalAmplitude;

for (int i = 0; i < SegmentStackSize; ++i) {
if (m_segmentStack[i].IsActive() && m_segmentStack[i].CurrentOffset <= 0) {
m_segmentStack[i].Amplitude *= inv;
Expand Down

0 comments on commit 59b8596

Please sign in to comment.