-
-
Notifications
You must be signed in to change notification settings - Fork 44
Creating a Shader Material
Shader materials define how the color output is calculated when a ray hits a triangle. Each child material must override the GetRGB function which returns an RGBColor given the intersection coordinate in respect to the origin, the normal of the triangle that was hit, and the UVW barycentric coordinate of the triangle that was hit.
The simple material returns a single RGB color no matter the input given from the Camera calculation.
#pragma once
#include "Material.h"
#include "..\Math\Vector2D.h"
//Converts gif to RGB XY pixel matrix
class SimpleMaterial : public Material {
private:
RGBColor rgb;
RGBColor baseRGB;
public:
SimpleMaterial(RGBColor rgb){
this->rgb = rgb;
this->baseRGB = rgb;
}
void HueShift(float hueDeg){
rgb = baseRGB.HueShift(hueDeg);
}
RGBColor GetRGB(Vector3D position, Vector3D normal, Vector3D uvw) override{
return rgb;
}
};
The depth material allows visualization of the depth of an object. The constructor takes 3 parameters, the axis in which the depth is visualized, the amplitude of the depth, and an offset from the origin position. The selected axis is mapped to the amplitude with the origin offset and then passed to the gradient material to determine the RGBColor that is returned.
#pragma once
#include "Material.h"
#include "..\Math\Vector2D.h"
#include "GradientMaterial.h"
//Converts gif to RGB XY pixel matrix
class DepthMaterial : public Material {
public:
enum Axis{
X,
Y,
Z
};
private:
RGBColor spectrum[4] = {RGBColor(0, 255, 0), RGBColor(255, 0, 0), RGBColor(0, 255, 0), RGBColor(0, 0, 255)};
GradientMaterial gNoiseMat = GradientMaterial(4, spectrum, 2.0f, false);
Axis axis;
float depth = 0.0f;
float zOffset = 0.0f;
public:
DepthMaterial(Axis axis, float depth, float zOffset){
this->axis = axis;
this->depth = depth;
this->zOffset = zOffset;
}
RGBColor GetRGB(Vector3D position, Vector3D normal, Vector3D uvw) override{
float axisValue = 0.0f;
switch(axis){
case X:
axisValue = position.X;
break;
case Y:
axisValue = position.Y;
break;
case Z:
axisValue = position.Z;
break;
default:
break;
}
float pos = Mathematics::Map(axisValue, -depth / 2.0f + zOffset, depth / 2.0f + zOffset, 0.0f, 1.0f);
return gNoiseMat.GetRGB(Vector3D(pos, 0, 0), Vector3D(), Vector3D());
}
};
Any recommendations on additional information to add can be requested in the discussions tab. If you have additional questions, you can @ me in my Discord server or on direct message me on Twitter.