【发布时间】:2020-01-21 03:34:32
【问题描述】:
我正在尝试启动由 shaderfx 开发人员 tdmaav 创建的酷炫WebGL sea shader。后来着色器适应Construct 2 引擎。对于大多数用户来说,Construct 2 实现正常显示,如下所示:
如果我使用 Construct 2 启动效果,海色和反射显示不正确:
我以为是Nvidea显卡的问题,后来我尝试使用radeon rx560显卡的效果,但得到了同样的结果。 C2 效果创建者试图为我修改效果,但没有帮助。 有谁知道是什么导致了颜色问题?还是显卡问题还是别的什么?也许是反射造成了麻烦。
Construct 2 的着色器代码
// "Seascape" by Alexander Alekseev aka TDM - 2014
// License Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.
/// Gigatron the magnificient Gls attempt !
/// For Scirra Construct2
//
#ifdef GL_ES
precision mediump float;
#endif
uniform mediump sampler2D samplerFront;
varying vec2 vTex;
uniform mediump float seconds;
uniform mediump float date;
uniform mediump float pixelWidth;
uniform mediump float pixelHeight;
vec2 iResolution = vec2( 1./pixelWidth, 1./pixelHeight);
uniform mediump float scale_x;
uniform mediump float scale_y;
uniform float xx,yy,mspeed;
// colors sea - base
uniform float sbred,sbgreen,sbblue;
// colors sea water color
uniform float swred,swgreen,swblue;
const int NUM_STEPS = 6;// 8
const float PI = 3.1415;
const float EPSILON = 1e-3;
float EPSILON_NRM = 0.1 / iResolution.x;
// sea
const int ITER_GEOMETRY = 3;
const int ITER_FRAGMENT = 5;
uniform float SEA_HEIGHT;//0.6
uniform float SEA_CHOPPY;//4.0
uniform float SEA_SPEED;//2.0
uniform float SEA_FREQ;//0.16
//const vec3 SEA_BASE = vec3(0.1,0.19,0.22); // removed
//const vec3 SEA_WATER_COLOR = vec3(0.8,0.9,0.6); // removed replaced by uniform vars !! gtr
vec3 SEA_BASE = vec3(sbred,sbgreen,sbblue);
vec3 SEA_WATER_COLOR = vec3(swred,swgreen,swblue);
float SEA_TIME = seconds * SEA_SPEED;
mat2 octave_m = mat2(1.6,1.2,-1.2,1.6);
const vec4 cHashA4 = vec4 (0., 1., 57., 58.);
const vec3 cHashA3 = vec3 (1., 57., 113.);
const float cHashM = 43758.54;
vec2 Hashv2f (float p)
{
return fract (sin (p + cHashA4.xy) * cHashM);
}
vec4 Hashv4f (float p)
{
return fract (sin (p + cHashA4) * cHashM);
}
vec4 Hashv4v3 (vec3 p)
{
const vec3 cHashVA3 = vec3 (37.1, 61.7, 12.4);
const vec3 e = vec3 (1., 0., 0.);
return fract (sin (vec4 (dot (p + e.yyy, cHashVA3), dot (p + e.xyy, cHashVA3),
dot (p + e.yxy, cHashVA3), dot (p + e.xxy, cHashVA3))) * cHashM);
}
float Noiseff (float p)
{
vec2 t;
float ip, fp;
ip = floor (p);
fp = fract (p);
fp = fp * fp * (3. - 2. * fp);
t = Hashv2f (ip);
return mix (t.x, t.y, fp);
}
float Noisefv2 (vec2 p)
{
vec4 t;
vec2 ip, fp;
ip = floor (p);
fp = fract (p);
fp = fp * fp * (3. - 2. * fp);
t = Hashv4f (dot (ip, cHashA3.xy));
return mix (mix (t.x, t.y, fp.x), mix (t.z, t.w, fp.x), fp.y);
}
float Noisefv3a (vec3 p)
{
vec4 t1, t2;
vec3 ip, fp;
ip = floor (p);
fp = fract (p);
fp = fp * fp * (3. - 2. * fp);
t1 = Hashv4v3 (ip);
t2 = Hashv4v3 (ip + vec3 (0., 0., 1.));
return mix (mix (mix (t1.x, t1.y, fp.x), mix (t1.z, t1.w, fp.x), fp.y),
mix (mix (t2.x, t2.y, fp.x), mix (t2.z, t2.w, fp.x), fp.y), fp.z);
}
float Fbm1 (float p)
{
float f, a;
f = 0.;
a = 1.;
for (int i = 0; i < 5; i ++) {
f += a * Noiseff (p);
a *= 0.5;
p *= 2.;
}
return f;
}
float Fbm2 (vec2 p)
{
float f, a;
f = 0.;
a = 1.;
for (int i = 0; i < 5; i ++) {
f += a * Noisefv2 (p);
a *= 0.5;
p *= 2.;
}
return f;
}
float Fbm3 (vec3 p)
{
const mat3 mr = mat3 (0., 0.8, 0.6, -0.8, 0.36, -0.48, -0.6, -0.48, 0.64);
float f, a;
f = 0.;
a = 1.;
for (int i = 0; i < 6; i ++) {
f += a * Noisefv3a (p);
a *= 0.5;
p *= 4. * mr;
}
return f;
}
float Fbmn (vec3 p, vec3 n)
{
vec3 s;
float a;
s = vec3 (0.);
a = 1.;
for (int i = 0; i < 5; i ++) {
s += a * vec3 (Noisefv2 (p.yz), Noisefv2 (p.zx), Noisefv2 (p.xy));
a *= 0.5;
p *= 2.;
}
return dot (s, abs (n));
}
vec3 qHit, sunDir, cloudDisp, waterDisp;
float tCur;
int idObj;
const float dstFar = 100.;
vec3 SkyBg (vec3 rd)
{
return mix (vec3 (0.2, 0.2, 0.9), vec3 (0.45, 0.45, 0.6),
1. - max (rd.y, 0.));
}
// math
mat3 fromEuler(vec3 ang) {
vec2 a1 = vec2(sin(ang.x),cos(ang.x));
vec2 a2 = vec2(sin(ang.y),cos(ang.y));
vec2 a3 = vec2(sin(ang.z),cos(ang.z));
mat3 m;
m[0] = vec3(a1.y*a3.y+a1.x*a2.x*a3.x,a1.y*a2.x*a3.x+a3.y*a1.x,-a2.y*a3.x);
m[1] = vec3(-a2.y*a1.x,a1.y*a2.y,a2.x);
m[2] = vec3(a3.y*a1.x*a2.x+a1.y*a3.x,a1.x*a3.x-a1.y*a3.y*a2.x,a2.y*a3.y);
return m;
}
float hash( vec2 p ) {
float h = dot(p,vec2(127.1,311.7));
return fract(sin(h)*43758.5453123);
}
float noise( in vec2 p ) {
vec2 i = floor( p );
vec2 f = fract( p );
vec2 u = f*f*(3.0-2.0*f);
return -1.0+2.0*mix( mix( hash( i + vec2(0.0,0.0) ),
hash( i + vec2(1.0,0.0) ), u.x),
mix( hash( i + vec2(0.0,1.0) ),
hash( i + vec2(1.0,1.0) ), u.x), u.y);
}
// lighting
float diffuse(vec3 n,vec3 l,float p) {
return pow(dot(n,l) * 0.4 + 0.6,p);
}
float specular(vec3 n,vec3 l,vec3 e,float s) {
float nrm = (s + 8.0) / (3.1415 * 8.0);
return pow(max(dot(reflect(e,n),l),0.0),s) * nrm;
}
// sky
vec3 getSkyColor(vec3 e) {
e.y = max(e.y,0.0);
vec3 ret;
ret.x = pow(1.0-e.y,2.0);
ret.y = 1.0-e.y;
ret.z = 0.6+(1.0-e.y)*0.4;
return ret;
}
// sea
float sea_octave(vec2 uv, float choppy) {
uv += noise(uv);
vec2 wv = 1.0-abs(sin(uv));
vec2 swv = abs(cos(uv));
wv = mix(wv,swv,wv);
return pow(1.0-pow(wv.x * wv.y,0.65),choppy);
}
float map(vec3 p) {
float freq = SEA_FREQ;//0.16
float amp = SEA_HEIGHT;
float choppy = SEA_CHOPPY;
vec2 uv = p.xz; uv.x *= 0.75;
float d, h = 0.0;
for(int i = 0; i < ITER_GEOMETRY; i++) {
d = sea_octave((uv+SEA_TIME)*freq,choppy);
d += sea_octave((uv-SEA_TIME)*freq,choppy);
h += d * amp;
uv *= octave_m; freq *= 1.9; amp *= 0.22;
choppy = mix(choppy,1.0,0.2);
}
return p.y - h;
}
float map_detailed(vec3 p) {
float freq = SEA_FREQ;
float amp = SEA_HEIGHT;
float choppy = SEA_CHOPPY;
vec2 uv = p.xz; uv.x *= 0.75;
float d, h = 0.0;
for(int i = 0; i < ITER_FRAGMENT; i++) {
d = sea_octave((uv+SEA_TIME)*freq,choppy);
d += sea_octave((uv-SEA_TIME)*freq,choppy);
h += d * amp;
uv *= octave_m; freq *= 1.9; amp *= 0.22;
choppy = mix(choppy,1.0,0.2);
}
return p.y - h;
}
vec3 getSeaColor(vec3 p, vec3 n, vec3 l, vec3 eye, vec3 dist) {
float fresnel = 1.0 - max(dot(n,-eye),0.0);
fresnel = pow(fresnel,3.0) * 0.65;
vec3 reflected = getSkyColor(reflect(eye,n));
vec3 refracted = SEA_BASE + diffuse(n,l,80.0) * SEA_WATER_COLOR * 0.12;
vec3 color = mix(refracted,reflected,fresnel);
float atten = max(1.0 - dot(dist,dist) * 0.001, 0.0);
color += SEA_WATER_COLOR * (p.y - SEA_HEIGHT) * 0.18 * atten;
color += vec3(specular(n,l,eye,60.0));
return color;
}
// tracing
vec3 getNormal(vec3 p, float eps) {
vec3 n;
n.y = map_detailed(p);
n.x = map_detailed(vec3(p.x+eps,p.y,p.z)) - n.y;
n.z = map_detailed(vec3(p.x,p.y,p.z+eps)) - n.y;
n.y = eps;
return normalize(n);
}
float heightMapTracing(vec3 ori, vec3 dir, out vec3 p) {
float tm = 0.0;
float tx = 1000.0;
float hx = map(ori + dir * tx);
if(hx > 0.0) return tx;
float hm = map(ori + dir * tm);
float tmid = 0.0;
for(int i = 0; i < NUM_STEPS; i++) {
tmid = mix(tm,tx, hm/(hm-hx));
p = ori + dir * tmid;
float hmid = map(p);
if(hmid < 0.0) {
tx = tmid;
hx = hmid;
} else {
tm = tmid;
hm = hmid;
}
}
return tmid;
}
#define AA 1
// main
void main() {
vec2 uv = vTex;
uv = uv * 2.0 - 1.0;
uv.y =1.-uv.y;
uv.x *= iResolution.x / iResolution.y;
float time = seconds * 0.3 /*+ iMouse.x*0.01*/;
// AntiAlias
// for( int mm=0; mm<AA; mm++ )
// for( int nn=0; nn<AA; nn++ )
// ray
vec3 ang = vec3(0.,yy ,xx);
vec3 ori = vec3(mspeed,3.5,5.0);
vec3 dir = normalize(vec3(uv.xy,2.0)); //dir.z += length(uv) * 0.15*((-.1*iMouse.y/iResolution.y));
dir = normalize(dir) * fromEuler(ang);
// tracing
vec3 p;
heightMapTracing(ori,dir,p);
vec3 dist = p - ori;
vec3 n = getNormal(p, dot(dist,dist) * EPSILON_NRM);
vec3 light = normalize(vec3(0.0,1.0,0.8));
// color // sky color not yet added !!!
vec4 color = mix(
vec4(SkyBg(dir),0.),
vec4(0.,getSeaColor(p,n,light,dir,dist)),
pow(smoothstep(0.0,-0.05,dir.y),0.3));
//color *=SkyHrzCol (ori, dir);
// post
gl_FragColor = vec4(pow(color,vec4(0.75)));
gl_FragColor.w = 1.0;
}
【问题讨论】: