View-space position of a sample point - graphics

I am working on implementing Crytek's original SSAO implementation and I have found myself stuck and confused at the part where I need to find the view-space position of the sample. I have implemented a method which I feel should work however, it seems to give me an odd result with blackening occurring at the back. Am I missing something? Would appreciate any insight, thanks in advance.
vec3 depthToPositions(vec2 tc)
{
float depth = texture(depthMap, tc).x;
vec4 clipSpace = vec4(tc * 2.0 - 1.0, depth, 1.0);
vec4 viewSpace = inverse(camera.proj) * clipSpace;
return viewSpace.xyz / viewSpace.w;
}
for(int i = 0; i < ssao.sample_amount; ++i) {
// Mittring, 2007 "Finding next gen CryEngine 2" document suggests to reflect sample
vec3 samplePos = reflect(ssao.samples[i].xyz, plane);
samplePos.xy = samplePos.xy * 0.5 + 0.5; // conver to 0-1 texture coordinates
samplePos = depthToPositions(samplePos.xy); // this is how I am retrieving view-space position of sample
samplePos = viewSpacePositions + samplePos * radius;
vec4 offset = vec4(samplePos, 1.0);
offset = camera.proj * offset;
offset.xyz /= offset.w;
offset.xy = offset.xy * 0.5 + 0.5;
float sampleDepth = texture(gPosition, offset.xy).z;
float rangeCheck = (viewSpacePositions.z - sampleDepth) < radius ? 1.0 : 0.0;
occlusion += (sampleDepth >= samplePos.z + bias ? 1.0 : 0.0) * rangeCheck;
}
Generating samples in C++
for(unsigned int i = 0; i < 64; i++) {
glm::vec4 sample(
randomFloats(generator) * 2.0 - 1.0,
randomFloats(generator) * 2.0 - 1.0,
randomFloats(generator) * 2.0 - 1.0, 0.0);
sample = glm::normalize(sample);
sample *= randomFloats(generator);
float scale = float(i) / 64;
scale = Lerp(0.1f, 1.0f, scale * scale);
sample *= scale;
ssaoKernel.push_back(sample);
}

Related

PBR - Incorrect direct lighting

Based on many internet resources I wrote PBR implementation for directional lighting for my DirectX 11 game engine, but It works incorrectly.
Bellow, you can see a screenshot where I forced metalness to 0.0f and roughness to 1.0f. As you can see there are too many reflections. For example, the grass is reflective very, but roughness is set to 0, so it shouldn't look like that.
Bellow, I visualized ambientLigting and it looks correct.
Unfortunately, directLighting seems completely off and I don't know why. There are too many reflections. It might be because I applied PBR formulas incorrectly for the directional light source, but I don't know how to make it correct.
Here is my PBR source code. I hope you will help me solve this problem or at least give me a hint, where the problem may be because, to be honest, I have no idea at this moment how to fix it.
static const float PI = 3.14159265359f;
static const float3 DIELECTRIC_FACTOR = float3(0.04f, 0.04f, 0.04f);
static const float EPSILON = 0.00001f;
float DistributionGGX(float3 normal, float3 halfway, float roughness)
{
float alpha = roughness * roughness;
float alphaSquare = alpha * alpha;
float cosHalfway = max(dot(normal, halfway), 0.0f);
float cosHalfwaySquare = cosHalfway * cosHalfway;
float denominator = (cosHalfwaySquare * (alphaSquare - 1.0f)) + 1.0f;
denominator = PI * denominator * denominator;
return alphaSquare / denominator;
}
float GeometrySchlickGGX(float cosinus, float roughness)
{
float r = (roughness + 1.0);
float k = (r * r) / 8.0;
float denominator = cosinus * (1.0 - k) + k;
return cosinus / denominator;
}
float GeometrySmith(float3 normal, float roughness, float cosView, float cosLight)
{
return GeometrySchlickGGX(cosView, roughness) * GeometrySchlickGGX(cosLight, roughness);
}
float3 FresnelSchlick(float cosTheta, float3 F0)
{
return F0 + (1.0f - F0) * pow(1.0f - cosTheta, 5.0f);
}
float3 FresnelSchlickRoughness(float cosTheta, float3 F0, float roughness)
{
return F0 + (max(float(1.0f - roughness).xxx, F0) - F0) * pow(1.0f - cosTheta, 5.0f);
}
int GetTextureMipMapLevels(TextureCube input)
{
int width, heigth, levels;
input.GetDimensions(0, width, heigth, levels);
return levels;
}
float3 Pbr(float3 albedo, float3 normal, float metallic, float roughness, float occlusion,
TextureCube irradianceTexture, TextureCube radianceTexture, Texture2D brdfLut,
SamplerState defaultSampler, SamplerState brdfSampler, float3 lightDirection,
float3 lightColor, float3 cameraPosition, float3 pixelPosition, float shadowMultiplier)
{
lightDirection *= -1;
float3 viewDirection = normalize(cameraPosition - pixelPosition);
float3 halfwayDirection = normalize(viewDirection + lightDirection);
float3 reflectionDirection = reflect(-viewDirection, normal);
float3 F0 = lerp(DIELECTRIC_FACTOR, albedo, metallic);
float cosView = max(dot(normal, viewDirection), 0.0f);
float cosLight = max(dot(normal, lightDirection), 0.0f);
float NDF = DistributionGGX(normal, halfwayDirection, roughness);
float G = GeometrySmith(normal, roughness, cosView, cosLight);
float3 F = FresnelSchlick(max(dot(halfwayDirection, viewDirection), 0.0f), F0);
float3 nominator = NDF * G * F;
float denominator = 4 * cosView * cosLight + EPSILON;
float3 specular = nominator / denominator;
float3 kD = lerp(float3(1.0f, 1.0f, 1.0f) - F, float3(0.0f, 0.0f, 0.0f), metallic);
float3 directLighting = (kD * albedo / PI + specular) * lightColor * cosLight;
F = FresnelSchlickRoughness(cosView, F0, roughness);
kD = lerp(float3(1.0f, 1.0f, 1.0f) - F, float3(0.0f, 0.0f, 0.0f), metallic);
float3 irradiance = irradianceTexture.Sample(defaultSampler, normal).rgb;
float3 diffuse = irradiance * albedo;
int radianceLevels = GetTextureMipMapLevels(radianceTexture);
float3 radiance = radianceTexture.SampleLevel(defaultSampler, reflectionDirection, roughness * radianceLevels).rgb;
float2 brdf = brdfLut.Sample(brdfSampler, float2(cosView, roughness)).xy;
float3 specularColor = radiance * (F0 * brdf.x + brdf.y);
float3 ambientLighting = (kD * diffuse + specularColor) * occlusion;
return ambientLighting + (directLighting * shadowMultiplier);
}

How to decode raw_outputs/box_encodings from Tensorflow Object detection ssd-mobilenet without nms

In order to deploy my own ssd-mobile model on android and use NNAPI acceleration , I retrained the model without NMS post processing according to the tensorflow objection detection API.
without NMS, the output raw_outputs/box_encodings are encoded box location, I decode it as follows, but it does not work:
for(int j =0; j < 5; j++)
{
float sk = (float)(0.2 + (0.949 - 0.200) *j * 1.0 / 5*1.0);
float width_a = (float)(sk * Math.sqrt(aspectra[j]));
float height_a = (float)(sk * 1.0 / Math.sqrt(aspectra[j]));
for(int k = 0; k < featuresize[j] ; k++)
{
float center_x_a = (float)((k + 0.5) * 1.0/ featuresize[j]);
float center_y_a = (float)((k + 0.5) * 1.0/ featuresize[j]);
float ty = (float)(outputBox[0][i][0] / 10.);
float tx = (float)(outputBox[0][i][1] / 10.);
float th = (float)(outputBox[0][i][2] / 5.);
float tw = (float)(outputBox[0][i][3] / 5.);
float w =(float)(Math.exp(tw) * width_a);
float h = (float)(Math.exp(th) * height_a);
float y_center = ty * height_a + center_y_a;
float x_ceneter = tx * width_a + center_x_a;
float ymin = (float)((y_center - h ) / 2.);
float xmin = (float)((x_ceneter - w ) / 2.);
float ymax = (float)((y_center + h ) / 2.);
float xmax = (float)((x_ceneter + w ) / 2.);
In order to decode raw_outputs/box_encodings you also need anchors as the box_encodings are encoded with respect to anchors.
Following is my implementation of decoding raw_outputs/box_encodings:
private float[][][] decodeBoxEncodings(final float[][][] boxEncoding, final float[][] anchor, final int numBoxes) {
final float[][][] decodedBoxes = new float[1][numBoxes][4];
for (int i = 0; i < numBoxes; ++i) {
final double ycenter = boxEncoding[0][i][0] / y_scale * anchor[i][2] + anchor[i][0];
final double xcenter = boxEncoding[0][i][1] / x_scale * anchor[i][3] + anchor[i][1];
final double half_h = 0.5 * Math.exp((boxEncoding[0][i][2] / h_scale)) * anchor[i][2];
final double half_w = 0.5 * Math.exp((boxEncoding[0][i][3] / w_scale)) * anchor[i][3];
decodedBoxes[0][i][0] = (float)(ycenter - half_h); //ymin
decodedBoxes[0][i][1] = (float)(xcenter - half_w); //xmin
decodedBoxes[0][i][2] = (float)(ycenter + half_h); //ymax
decodedBoxes[0][i][3] = (float)(xcenter + half_w); //xmax
}
return decodedBoxes;
}
This decoding technique is from TFLite detection_postprocess operation.
Edit: scale values are:
float y_scale = 10.0f;
float x_scale = 10.0f;
float h_scale = 5.0f;
float w_scale = 5.0f;
https://actcast.hatenablog.com/entry/2021/08/06/085134
This worked for me.
My pb model - SSD Mobilenet V1 0.75 Depth Quantized (tflite_graph.pb)
Tf Version - 1.15
outputs - raw_outputs/box_encodings & raw_outputs/class_predictions
Start from the step 3 (Create Anchor) steps in the above mentioned blog (as the model ready was ready with me, I didn't do training part)
4th step is not required if model ready & load our model (they have
loaded nnoir_model, instead of that we can load our model)
corresponding google colabs : https://colab.research.google.com/github/Idein/tensorflow-object-detection-api-to-nnoir/blob/master/notebook/ssd_mobilenet_v1_coco_2018_01_28_to_nnoir.ipynb#scrollTo=51d0jACEslWu

Inputs and Outputs of the Geometry Shader

I was wondering if anyone would be so kind as to pin-point the problem with my program. I am certain the setback has something to do with the way in which data is passed through the GS. If, for instance, the geometry shader is taken out of the code (modifying the other two stages to accommodate for the change as well), I end up with a operational pipeline. And if I modify the data input of the GS to accept PS_INPUT instead of VS_DATA, the program does not crash, but outputs a blank blue screen. My intent here is to create a collection of squares on a two-dimensional plane, so blank blue screens are not exactly what I am going for.
Texture2D txDiffuse[26] : register(t0);
SamplerState samLinear : register(s0); //For Texturing
#define AWR_MAX_SHADE_LAY 1024
cbuffer ConstantBuffer : register(b0)
{
float4 Matrix_Array[30];
matrix Screen;
float GM;
float GA;
float GD;
float epsilon;
}
// Includes Layer Data
cbuffer CBLayer : register(b1)
{
float4 Array_Fill_Color[AWR_MAX_SHADE_LAY];
float4 Array_Line_Color[AWR_MAX_SHADE_LAY];
float Array_Width[AWR_MAX_SHADE_LAY];
float Array_Line_Pattern[AWR_MAX_SHADE_LAY];
float Array_Z[AWR_MAX_SHADE_LAY];
float Array_Thickness[AWR_MAX_SHADE_LAY];
}
//Input for Vertex Shader
struct VS_DATA
{
float4 Pos : POSITION;
int M2W_index : M2W_INDEX;
int Layer_index : LAYER_INDEX;
};
//Input for Pixel Shader
struct PS_INPUT{
float4 Pos : SV_POSITION;
float4 Color : COLOR;
int Layer_index : LAYER_INDEX;
};
//Vertex Shader
VS_DATA VS(VS_DATA input)// Vertex Shader
{
VS_DATA output = (VS_DATA)0;
//Model to World Transform
float xm = input.Pos.x, yw = input.Pos.y, zm = input.Pos.z, ww = input.Pos.w, xw, zw;
float4 transformation = Matrix_Array[input.M2W_index];
xw = ((xm)*transformation.y - (zm)*transformation.x) + transformation.z;
zw = ((xm)*transformation.x + (zm)*transformation.y) + transformation.w;
//set color
int valid_index = input.Layer_index;
output.Color = Array_Fill_Color[valid_index];
output.Color.a = 0.0;
//output.Vertex_index = input.Vertex_index;
//output.Next_Vertex_index = input.Next_Vertex_index;
//Snapping process
float sgn_x = (xw >= 0) ? 1.0 : -1.0;
float sgn_z = (zw >= 0) ? 1.0 : -1.0;
int floored_x = (int)((xw + (sgn_x*GA) + epsilon)*GD);
int floored_z = (int)((zw + (sgn_z*GA) + epsilon)*GD);
output.Pos.x = ((float)floored_x)*GM;
output.Pos.y = yw;
output.Pos.z = ((float)floored_z)*GM;
output.Pos.w = ww;
int another_valid_index = input.Layer_index;
output.Layer_index = another_valid_index;
// Transform to Screen Space
output.Pos = mul(output.Pos, Screen);
return output;
}
[maxvertexcount(6)]
void GS_Line(line VS_DATA points[2], inout TriangleStream<PS_INPUT> output)
{
float4 p0 = points[0].Pos;
float4 p1 = points[1].Pos;
float w0 = p0.w;
float w1 = p1.w;
p0.xyz /= p0.w;
p1.xyz /= p1.w;
float3 line01 = p1 - p0;
float3 dir = normalize(line01);
float3 ratio = float3(700.0, 0.0, 700.0);
ratio = normalize(ratio);
float3 unit_z = normalize(float3(0.0, -1.0, 0.0));
float3 normal = normalize(cross(unit_z, dir) * ratio);
float width = 0.01;
PS_INPUT v[4];
float3 dir_offset = dir * ratio * width;
float3 normal_scaled = normal * ratio * width;
float3 p0_ex = p0 - dir_offset;
float3 p1_ex = p1 + dir_offset;
v[0].Pos = float4(p0_ex - normal_scaled, 1) * w0;
v[0].Color = float4(1.0, 1.0, 1.0, 1.0);
v[0].Layer_index = 1;
v[1].Pos = float4(p0_ex + normal_scaled, 1) * w0;
v[1].Color = float4(1.0, 1.0, 1.0, 1.0);
v[1].Layer_index = 1;
v[2].Pos = float4(p1_ex + normal_scaled, 1) * w1;
v[2].Color = float4(1.0, 1.0, 1.0, 1.0);
v[2].Layer_index = 1;
v[3].Pos = float4(p1_ex - normal_scaled, 1) * w1;
v[3].Color = float4(1.0, 1.0, 1.0, 1.0);
v[3].Layer_index = 1;
output.Append(v[2]);
output.Append(v[1]);
output.Append(v[0]);
output.RestartStrip();
output.Append(v[3]);
output.Append(v[2]);
output.Append(v[0]);
output.RestartStrip();
}
//Pixel Shader
float4 PS(PS_INPUT input) : SV_Target{
float2 Tex = float2(input.Pos.x / (8.0), input.Pos.y / (8.0));
int the_index = input.Layer_index;
float4 tex0 = txDiffuse[25].Sample(samLinear, Tex);
if (tex0.r > 0.0)
tex0 = float4(1.0, 1.0, 1.0, 1.0);
else
tex0 = float4(0.0, 0.0, 0.0, 0.0);
if (tex0.r == 0.0)
discard;
tex0 *= input.Color;
return tex0;
}
If you compile your vertex shader as it is, you will have the following error :
(line 53) : invalid subscript 'Color'
output.Color = Array_Fill_Color[valid_index];
output is of type VS_DATA which does not contain color.
If you change your VS definition as :
PS_INPUT VS(VS_DATA input)// Vertex Shader
{
PS_INPUT output = (PS_INPUT)0;
//rest of the code here
Then your vs will compile, but then you will have a mismatched layout with GS (GS still expects a line of VS_DATA as input, and now you provide PS_INPUT to it)
This will not give you any error until you draw (and generally runtime will silently fail, you would have a mismatch message in case debug layer is on)
So you also need to modify your GS to accept PS_INPUT as input eg:
[maxvertexcount(6)]
void GS_Line(line PS_INPUT points[2], inout TriangleStream<PS_INPUT> output)

Problems limiting object rotation with Mathf.Clamp()

I am working on a game that rotates an object on the z axis. I need to limit the total rotation to 80 degrees. I tried the following code, but it doesn't work. minAngle = -40.0f and maxAngle = 40.0f
Vector3 pos = transform.position;
pos.z = Mathf.Clamp(pos.z, minAngle, maxAngle);
transform.position = pos;
The code you posted clamps the z position. What you want is to use transform.rotation
void ClampRotation(float minAngle, float maxAngle, float clampAroundAngle = 0)
{
//clampAroundAngle is the angle you want the clamp to originate from
//For example a value of 90, with a min=-45 and max=45, will let the angle go 45 degrees away from 90
//Adjust to make 0 be right side up
clampAroundAngle += 180;
//Get the angle of the z axis and rotate it up side down
float z = transform.rotation.eulerAngles.z - clampAroundAngle;
z = WrapAngle(z);
//Move range to [-180, 180]
z -= 180;
//Clamp to desired range
z = Mathf.Clamp(z, minAngle, maxAngle);
//Move range back to [0, 360]
z += 180;
//Set the angle back to the transform and rotate it back to right side up
transform.rotation = Quaternion.Euler(transform.rotation.eulerAngles.x, transform.rotation.eulerAngles.y, z + clampAroundAngle);
}
//Make sure angle is within 0,360 range
float WrapAngle(float angle)
{
//If its negative rotate until its positive
while (angle < 0)
angle += 360;
//If its to positive rotate until within range
return Mathf.Repeat(angle, 360);
}
Here's a static version of the nice solution by Imapler that, instead of changing the angle itself, it returns the campled angle, so it can be used with any axis.
public static float ClampAngle(
float currentValue,
float minAngle,
float maxAngle,
float clampAroundAngle = 0
) {
return Mathf.Clamp(
WrapAngle(currentValue - (clampAroundAngle + 180)) - 180,
minAngle,
maxAngle
) + 360 + clampAroundAngle;
}
public static float WrapAngle(float angle)
{
while (angle < 0) {
angle += 360;
}
return Mathf.Repeat(angle, 360);
}
Or if you don't expect to use the WrapAngle method, here's an all-in-one version:
public static float ClampAngle(
float currentValue,
float minAngle,
float maxAngle,
float clampAroundAngle = 0
) {
float angle = currentValue - (clampAroundAngle + 180);
while (angle < 0) {
angle += 360;
}
angle = Mathf.Repeat(angle, 360);
return Mathf.Clamp(
angle - 180,
minAngle,
maxAngle
) + 360 + clampAroundAngle;
}
So now you can do:
transform.localEulerAngles.x = YourMathf.ClampAngle(
transform.localEulerAngles.x,
minX,
maxX
);

Ray tracing object in the wrong position

I am writing a simple ray shader and I am trying to prodcue a dice with a cube and a number of spheres representing the dots. The spheres are correct, but the sides of the cube are on the x, y and z axes. The cube is centred around 0, 0, 0.
I have checked that the coordinate of the vertices are correct. I am assuming that my ray calculation is correct as the spheres are in the correct positions.
Here is the code for the ray calculation
Ray Image::RayThruPixel(float i, float j)
{
float alpha = m_tanFOVx * ((j - m_halfWidth) / m_halfWidth);
float beta = m_tanFOVy * ((m_halfHeight - i) / m_halfHeight);
vec3 *coordFrame = m_camera.CoordFrame();
vec3 p1 = (coordFrame[U_VEC] * alpha) + (coordFrame[V_VEC] * beta) - coordFrame[W_VEC];
return Ray(m_camera.Eye(), p1);
}
where m_tanFOVx is tan(FOVx / 2) and m_tanFOVy is tan(FOVy / 2) FOVx and FOVy are in radians.
To find the intersection of the ray and triangle my code is as follows:
bool Triangle::Intersection(Ray ray, float &fDistance)
{
static float epsilon = 0.000001;
bool bHit = false;
float fMinDist(10000000);
float divisor = glm::dot(ray.p1, normal);
// if divisor == 0 then the ray is parallel with the triangle
if(divisor > -epsilon && divisor < epsilon)
{
bHit = false;
}
else
{
float t = (glm::dot(v0, normal) - glm::dot(ray.p0, normal)) / divisor;
if(t > 0)
{
vec3 P = ray.p0 + (ray.p1 * t);
vec3 v2 = P - m_vertexA;
v0 = m_vertexB - m_vertexA;
v1 = m_vertexC - m_vertexA;
normal = glm::normalize(glm::cross(v0, v1));
d00 = glm::dot(v0, v0);
d01 = glm::dot(v0, v1);
d11 = glm::dot(v1, v1);
denom = d00 * d11 - d01 * d01;
float d20 = glm::dot(v2, v0);
float d21 = glm::dot(v2, v1);
float alpha = (d11 * d20 - d01 * d21) / denom;
float beta = (d00 * d21 - d01 * d20) / denom;
float gamma = 1.0 - alpha - beta;
vec3 testP = alpha * m_vertexA + beta * m_vertexB + gamma * m_vertexC;
if((alpha >= 0 ) &&
(beta >= 0) &&
(alpha + beta <= 1))
{
bHit = true;
fDistance = t;
}
}
}
return bHit;
}

Resources