Glsl 我需要帮助将此2D天空着色器转换为3D
我在github上找到了这个着色器函数,并设法在GameMaker Studio 2(我当前选择的编程套件)中实现了它。但是,这是一种2D效果,不考虑摄影机上方向向量,也不考虑fov。还有什么可以添加到这个中的吗?对于着色器,我只是中等水平的技能,所以我不确定应该采取什么样的方法,或者在这一点上是否值得,或者是否应该从另一个例子开始Glsl 我需要帮助将此2D天空着色器转换为3D,glsl,shader,fragment-shader,game-maker-studio-2,Glsl,Shader,Fragment Shader,Game Maker Studio 2,我在github上找到了这个着色器函数,并设法在GameMaker Studio 2(我当前选择的编程套件)中实现了它。但是,这是一种2D效果,不考虑摄影机上方向向量,也不考虑fov。还有什么可以添加到这个中的吗?对于着色器,我只是中等水平的技能,所以我不确定应该采取什么样的方法,或者在这一点上是否值得,或者是否应该从另一个例子开始 uniform vec3 u_sunPosition; varying vec2 v_vTexcoord; varying vec4 v_vColour; vary
uniform vec3 u_sunPosition;
varying vec2 v_vTexcoord;
varying vec4 v_vColour;
varying vec3 v_vPosition;
#define PI 3.141592
#define iSteps 16
#define jSteps 8
vec2 rsi(vec3 r0, vec3 rd, float sr) {
// ray-sphere intersection that assumes
// the sphere is centered at the origin.
// No intersection when result.x > result.y
float a = dot(rd, rd);
float b = 2.0 * dot(rd, r0);
float c = dot(r0, r0) - (sr * sr);
float d = (b*b) - 4.0*a*c;
if (d < 0.0) return vec2(1e5,-1e5);
return vec2(
(-b - sqrt(d))/(2.0*a),
(-b + sqrt(d))/(2.0*a)
);
}
vec3 atmosphere(vec3 r, vec3 r0, vec3 pSun, float iSun, float rPlanet, float rAtmos, vec3 kRlh, float kMie, float shRlh, float shMie, float g) {
// Normalize the sun and view directions.
pSun = normalize(pSun);
r = normalize(r);
// Calculate the step size of the primary ray.
vec2 p = rsi(r0, r, rAtmos);
if (p.x > p.y) return vec3(0,0,0);
p.y = min(p.y, rsi(r0, r, rPlanet).x);
float iStepSize = (p.y - p.x) / float(iSteps);
// Initialize the primary ray time.
float iTime = 0.0;
// Initialize accumulators for Rayleigh and Mie scattering.
vec3 totalRlh = vec3(0,0,0);
vec3 totalMie = vec3(0,0,0);
// Initialize optical depth accumulators for the primary ray.
float iOdRlh = 0.0;
float iOdMie = 0.0;
// Calculate the Rayleigh and Mie phases.
float mu = dot(r, pSun);
float mumu = mu * mu;
float gg = g * g;
float pRlh = 3.0 / (16.0 * PI) * (1.0 + mumu);
float pp = 1.0 + gg - 2.0 * mu * g;
float pMie = 3.0 / (8.0 * PI) * ((1.0 - gg) * (mumu + 1.0)) / (sign(pp)*pow(abs(pp), 1.5) * (2.0 + gg));
// Sample the primary ray.
for (int i = 0; i < iSteps; i++) {
// Calculate the primary ray sample position.
vec3 iPos = r0 + r * (iTime + iStepSize * 0.5);
// Calculate the height of the sample.
float iHeight = length(iPos) - rPlanet;
// Calculate the optical depth of the Rayleigh and Mie scattering for this step.
float odStepRlh = exp(-iHeight / shRlh) * iStepSize;
float odStepMie = exp(-iHeight / shMie) * iStepSize;
// Accumulate optical depth.
iOdRlh += odStepRlh;
iOdMie += odStepMie;
// Calculate the step size of the secondary ray.
float jStepSize = rsi(iPos, pSun, rAtmos).y / float(jSteps);
// Initialize the secondary ray time.
float jTime = 0.0;
// Initialize optical depth accumulators for the secondary ray.
float jOdRlh = 0.0;
float jOdMie = 0.0;
// Sample the secondary ray.
for (int j = 0; j < jSteps; j++) {
// Calculate the secondary ray sample position.
vec3 jPos = iPos + pSun * (jTime + jStepSize * 0.5);
// Calculate the height of the sample.
float jHeight = length(jPos) - rPlanet;
// Accumulate the optical depth.
jOdRlh += exp(-jHeight / shRlh) * jStepSize;
jOdMie += exp(-jHeight / shMie) * jStepSize;
// Increment the secondary ray time.
jTime += jStepSize;
}
// Calculate attenuation.
vec3 attn = exp(-(kMie * (iOdMie + jOdMie) + kRlh * (iOdRlh + jOdRlh)));
// Accumulate scattering.
totalRlh += odStepRlh * attn;
totalMie += odStepMie * attn;
// Increment the primary ray time.
iTime += iStepSize;
}
// Calculate and return the final color.
return iSun * (pRlh * kRlh * totalRlh + pMie * kMie * totalMie);
}
vec3 ACESFilm( vec3 x )
{
float tA = 2.51;
float tB = 0.03;
float tC = 2.43;
float tD = 0.59;
float tE = 0.14;
return clamp((x*(tA*x+tB))/(x*(tC*x+tD)+tE),0.0,1.0);
}
void main() {
vec3 color = atmosphere(
normalize( v_vPosition ), // normalized ray direction
vec3(0,6372e3,0), // ray origin
u_sunPosition, // position of the sun
22.0, // intensity of the sun
6371e3, // radius of the planet in meters
6471e3, // radius of the atmosphere in meters
vec3(5.5e-6, 13.0e-6, 22.4e-6), // Rayleigh scattering coefficient
21e-6, // Mie scattering coefficient
8e3, // Rayleigh scale height
1.2e3, // Mie scale height
0.758 // Mie preferred scattering direction
);
// Apply exposure.
color = ACESFilm( color );
gl_FragColor = vec4(color, 1.0);
}
统一的vec3u\u位置;
可变矢量2 v_vTexcoord;
可变的vec4 v_v颜色;
可变的vec3 v_位置;
#定义PI 3.141592
#定义iSteps 16
#定义jstep8
vec2 rsi(vec3 r0、vec3 rd、浮动sr){
//光线与球体相交,假定
//球体以原点为中心。
//当result.x>result.y时没有交点
浮点a=点(rd,rd);
浮点数b=2.0*点(rd,r0);
浮点数c=点(r0,r0)-(sr*sr);
浮动d=(b*b)-4.0*a*c;
如果(d<0.0)返回vec2(1e5,-1e5);
返回向量2(
(-b-sqrt(d))/(2.0*a),
(-b+sqrt(d))/(2.0*a)
);
}
vec3大气(vec3 r、vec3 r0、vec3 pSun、浮子iSun、浮子RPLAN、浮子rAtmos、vec3 kRlh、浮子kMie、浮子shRlh、浮子shMie、浮子g){
//规范化日光和视图方向。
pSun=正常化(pSun);
r=标准化(r);
//计算主光线的步长。
vec2p=rsi(r0,r,rAtmos);
如果(p.x>p.y)返回vec3(0,0,0);
p、 y=最小值(p.y,rsi(r0,r,rPlanet).x);
浮动利率iStepSize=(p.y-p.x)/浮动利率(iSteps);
//初始化主光线时间。
浮动iTime=0.0;
//初始化瑞利散射和米氏散射的累加器。
vec3-totalRlh=vec3(0,0,0);
vec3-totalMie=vec3(0,0,0);
//初始化主光线的光学深度累加器。
浮动iOdRlh=0.0;
浮动iOdMie=0.0;
//计算瑞利相位和米氏相位。
浮点μ=点(r,pSun);
浮点数mumu=mu*mu;
浮点数gg=g*g;
浮动pRlh=3.0/(16.0*PI)*(1.0+mumu);
浮动pp=1.0+gg-2.0*mu*g;
浮动pMie=3.0/(8.0*PI)*((1.0-gg)*(mumumu+1.0))/(符号(pp)*功率(abs(pp),1.5)*(2.0+gg));
//对主光线进行采样。
for(int i=0;i
但是,这是一种2D效果,不考虑摄影机上方向向量,也不考虑fov
如果要在3D中绘制天空,则必须在规格化设备空间的后平面上绘制。标准化的设备空间是一个立方体,其左下角接近(-1,-1,-1),右上角接近(1,1,1)。后平面为四边形,具有:
左下角:-1,-1,1
右下角:1,-1,1
右上角:-1,-1,1
左上角:-1,-1,1
渲染此四边形。注意,顶点坐标不必通过任何矩阵进行变换,因为它们是标准化的设备空间坐标。但是您必须变换用于天空的光线(传递到大气的方向)。
这条光线必须是世界的一个方向