#version  es
#define varying in
out highp vec4 pc_fragColor;
#define gl_FragColor pc_fragColor
#define texture2D texture
#define textureCube texture
#define texture2DProj textureProj
#define texture2DLodEXT textureLod
#define texture2DProjLodEXT textureProjLod
#define textureCubeLodEXT textureLod
#define texture2DGradEXT textureGrad
#define texture2DProjGradEXT textureProjGrad
#define textureCubeGradEXT textureGrad
#define GL2
precision highp float;
#ifdef GL2
precision highp sampler2DShadow;
#endif
varying vec3 vPositionW;
varying vec3 vNormalW;
uniform vec3 view_position;
uniform vec3 light_globalAmbient;
float square(float x) {
return x*x;
}
float saturate(float x) {
return clamp(x, 0.0, 1.0);
}
vec3 saturate(vec3 x) {
return clamp(x, vec3(0.0), vec3(1.0));
}
vec4 dReflection;
vec3 dAlbedo;
vec3 dNormalW;
vec3 dVertexNormalW;
vec3 dViewDirW;
vec3 dReflDirW;
vec3 dDiffuseLight;
vec3 dSpecularLight;
vec3 dSpecularity;
float dGlossiness;
float dAlpha;
void getNormal() {
dNormalW = normalize(dVertexNormalW);
}
vec3 gammaCorrectInput(vec3 color) {
return pow(color, vec3(2.2));
}
float gammaCorrectInput(float color) {
return pow(color, 2.2);
}
vec4 gammaCorrectInput(vec4 color) {
return vec4(pow(color.rgb, vec3(2.2)), color.a);
}
vec4 texture2DSRGB(sampler2D tex, vec2 uv) {
vec4 rgba = texture2D(tex, uv);
rgba.rgb = gammaCorrectInput(rgba.rgb);
return rgba;
}
vec4 texture2DSRGB(sampler2D tex, vec2 uv, float bias) {
vec4 rgba = texture2D(tex, uv, bias);
rgba.rgb = gammaCorrectInput(rgba.rgb);
return rgba;
}
vec4 textureCubeSRGB(samplerCube tex, vec3 uvw) {
vec4 rgba = textureCube(tex, uvw);
rgba.rgb = gammaCorrectInput(rgba.rgb);
return rgba;
}
vec3 gammaCorrectOutput(vec3 color) {
#ifdef HDR
return color;
#else
color += vec3(0.0000001);
return pow(color, vec3(0.45));
#endif
}
uniform float exposure;
// 线性toneMap整体颜色亮度调节
vec3 toneMap(vec3 color) {
return color * exposure;
}
vec3 addFog(vec3 color) {
return color;
}
// 这里RGBM解码是pc引擎专门修改过的 编码做了pow(0.5) 最大值调整为了8.0
vec3 decodeRGBM(vec4 rgbm) {
vec3 color = (8.0 * rgbm.a) * rgbm.rgb;
return color * color;
}
vec3 texture2DRGBM(sampler2D tex, vec2 uv) {
return decodeRGBM(texture2D(tex, uv));
}
vec3 textureCubeRGBM(samplerCube tex, vec3 uvw) {
return decodeRGBM(textureCube(tex, uvw));
}
vec3 fixSeams(vec3 vec, float mipmapIndex) {
float scale = 1.0 - exp2(mipmapIndex) / 128.0;
float M = max(max(abs(vec.x), abs(vec.y)), abs(vec.z));
if (abs(vec.x) != M) vec.x *= scale;
if (abs(vec.y) != M) vec.y *= scale;
if (abs(vec.z) != M) vec.z *= scale;
return vec;
}
vec3 fixSeams(vec3 vec) {
float scale = 1.0 - 1.0 / 128.0;
float M = max(max(abs(vec.x), abs(vec.y)), abs(vec.z));
if (abs(vec.x) != M) vec.x *= scale;
if (abs(vec.y) != M) vec.y *= scale;
if (abs(vec.z) != M) vec.z *= scale;
return vec;
}
vec3 fixSeamsStatic(vec3 vec, float invRecMipSize) {
float scale = invRecMipSize;
float M = max(max(abs(vec.x), abs(vec.y)), abs(vec.z));
if (abs(vec.x) != M) vec.x *= scale;
if (abs(vec.y) != M) vec.y *= scale;
if (abs(vec.z) != M) vec.z *= scale;
return vec;
}
vec3 cubeMapProject(vec3 dir) {
return dir;
}
vec3 processEnvironment(vec3 color) {
return color;
}
#undef MAPFLOAT #undef MAPCOLOR #undef MAPVERTEX #undef MAPTEXTURE
#ifdef MAPCOLOR
uniform vec3 material_diffuse;
#endif
#ifdef MAPTEXTURE
uniform sampler2D texture_diffuseMap;
#endif
// 获取基础色
void getAlbedo() {
dAlbedo = vec3(1.0);
#ifdef MAPCOLOR
dAlbedo *= material_diffuse.rgb;
#endif
#ifdef MAPTEXTURE
dAlbedo *= texture2DSRGB(texture_diffuseMap, UV).CH;
#endif
#ifdef MAPVERTEX
dAlbedo *= gammaCorrectInput(saturate(vVertexColor.VC));
#endif
}
#undef MAPFLOAT #undef MAPCOLOR
#define MAPCOLOR #undef MAPVERTEX #undef MAPTEXTURE
#ifdef MAPCOLOR
uniform vec3 material_emissive;
#endif
#ifdef MAPFLOAT
uniform float material_emissiveIntensity;
#endif
#ifdef MAPTEXTURE
uniform sampler2D texture_emissiveMap;
#endif
// 获取自发光颜色
vec3 getEmission() {
vec3 emission = vec3(1.0);
#ifdef MAPFLOAT
emission *= material_emissiveIntensity;
#endif
#ifdef MAPCOLOR
emission *= material_emissive;
#endif
#ifdef MAPTEXTURE
emission *= texture2DSAMPLE(texture_emissiveMap, UV).CH;
#endif
#ifdef MAPVERTEX
emission *= gammaCorrectInput(saturate(vVertexColor.VC));
#endif
return emission;
}
float antiAliasGlossiness(float power) {
return power;
}
#undef MAPFLOAT
#define MAPFLOAT #undef MAPCOLOR #undef MAPVERTEX #undef MAPTEXTURE
// 通过金属性计算各个颜色通道镜面反射强度系数,调整基础色颜色亮度
void processMetalness(float metalness) {
const float dielectricF0 = 0.04;
dSpecularity = mix(vec3(dielectricF0), dAlbedo, metalness);
dAlbedo *= 1.0 - metalness;
}
#ifdef MAPFLOAT
uniform float material_metalness;
#endif
#ifdef MAPTEXTURE
uniform sampler2D texture_metalnessMap;
#endif
// 获取镜面反射强度系数
void getSpecularity() {
float metalness = 1.0;
#ifdef MAPFLOAT
metalness *= material_metalness;
#endif
#ifdef MAPTEXTURE
metalness *= texture2D(texture_metalnessMap, UV).CH;
#endif
#ifdef MAPVERTEX
metalness *= saturate(vVertexColor.VC);
#endif
processMetalness(metalness);
}
#undef MAPFLOAT
#define MAPFLOAT #undef MAPCOLOR #undef MAPVERTEX #undef MAPTEXTURE
#ifdef MAPFLOAT
uniform float material_shininess;
#endif
#ifdef MAPTEXTURE
uniform sampler2D texture_glossMap;
#endif
// 计算光泽度
void getGlossiness() {
dGlossiness = 1.0;
#ifdef MAPFLOAT
dGlossiness *= material_shininess;
#endif
#ifdef MAPTEXTURE
dGlossiness *= texture2D(texture_glossMap, UV).CH;
#endif
#ifdef MAPVERTEX
dGlossiness *= saturate(vVertexColor.VC);
#endif
dGlossiness += 0.0000001;
}
// Schlick's approximation
uniform float material_fresnelFactor; // unused
// 计算菲尼尔系数并且调整镜面反射系数
void getFresnel() {
float fresnel = 1.0 - max(dot(dNormalW, dViewDirW), 0.0);
float fresnel2 = fresnel * fresnel;
fresnel *= fresnel2 * fresnel2;
fresnel *= dGlossiness * dGlossiness;
dSpecularity = dSpecularity + (1.0 - dSpecularity) * fresnel;
}
#ifndef PMREM4
#define PMREM4
uniform samplerCube texture_prefilteredCubeMap128;
#endif
uniform float material_reflectivity;
// 添加ibl全局光镜面反射颜色
void addReflection() {
float bias = saturate(1.0 - dGlossiness) * 5.0; // multiply by max mip level #ifdef ENABLE_ENVROT
vec3 fixedReflDir = fixSeams(cubeMapProject(environmentRotate(dReflDirW)), bias);
#else
vec3 fixedReflDir = fixSeams(cubeMapProject(dReflDirW), bias);
#endif
fixedReflDir.x *= -1.0;
vec3 refl = processEnvironment(decodeRGBM( textureCubeLodEXT(texture_prefilteredCubeMap128, fixedReflDir, bias) ).rgb);
dReflection += vec4(refl, material_reflectivity);
}
// 漫反射 * 基础色 * (1.0 - 镜面反射强度) + (镜面反射 + 环境反射) * 镜面反射强度
// 这里漫反射包含了直接光+ibl全局光
vec3 combineColor() {
return mix(dAlbedo * dDiffuseLight, dSpecularLight + dReflection.rgb * dReflection.a, dSpecularity);
}
#ifndef PMREM4
#define PMREM4
uniform samplerCube texture_prefilteredCubeMap128;
#endif // 获取ibl全局光漫反射颜色
void addAmbient() {
#ifdef ENABLE_ENVROT
vec3 fixedReflDir = fixSeamsStatic(environmentRotate(dNormalW), 1.0 - 1.0 / 4.0);
#else
vec3 fixedReflDir = fixSeamsStatic(dNormalW, 1.0 - 1.0 / 4.0);
#endif
fixedReflDir.x *= -1.0;
dDiffuseLight += processEnvironment(decodeRGBM( textureCubeLodEXT(texture_prefilteredCubeMap128, fixedReflDir, 5.0) ).rgb);
}
// 获取V向量
void getViewDir() {
dViewDirW = normalize(view_position - vPositionW);
}
// 获取R向量
void getReflDir() {
dReflDirW = normalize(-reflect(dViewDirW, dNormalW));
}
void main(void) {
// 漫反射颜色
dDiffuseLight = vec3();
// 镜面反射颜色
dSpecularLight = vec3();
// ibl全局环境反射
dReflection = vec4();
// 镜面反射强度系数
dSpecularity = vec3();
// 顶点世界空间法线
dVertexNormalW = vNormalW;
dAlpha = 1.0;
getViewDir();// 计算 view - pos
getNormal();// 计算世界空间法线
getReflDir();// 计算反射方向
getAlbedo();// 计算基础色,一般走diffuseMap、顶点颜色、材质diffuse颜色直接取出
getSpecularity();// 计算镜面反射强度系数, 镜面反射强度通过金属性系数来计算,金属性越强反射强度越大漫反射越弱,金属性越弱反射强度越小漫反射颜色越强,从而漫反射和镜面反射遵循能量守恒
getGlossiness();// 计算光泽度(相当于粗糙度的相反叫法) 通过外部给材质设定系数或者走光泽度贴图获取
getFresnel(); // 计算菲尼尔效果
addAmbient(); // 计算环境光颜色(间接光漫反射颜色)
addReflection(); // 计算环境光反射(间接光镜面高光颜色)
gl_FragColor.rgb = combineColor(); // 组合漫反射、基础色、镜面反射输出颜色
gl_FragColor.rgb += getEmission(); // 累加自发光颜色
gl_FragColor.rgb = addFog(gl_FragColor.rgb); // 雾化效果计算
#ifndef HDR
gl_FragColor.rgb = toneMap(gl_FragColor.rgb); // tonemap计算
gl_FragColor.rgb = gammaCorrectOutput(gl_FragColor.rgb);// gamma矫正回到gama颜色空间
#endif
gl_FragColor.a = 1.0;
}

PlayCanvas PBR材质shader代码分析(pixel shader)的更多相关文章

  1. PlayCanvas PBR材质shader代码分析(vertex shader)

    顶点shader主要对顶点坐标变换,将顶点坐标从local->world->view->clip 空间变换 local空间:模型物体坐标系 world空间:世界空间坐标系 view空 ...

  2. 【OpenGL】Shader实例分析(七)- 雪花飘落效果

    转发请保持地址:http://blog.csdn.net/stalendp/article/details/40624603 研究了一个雪花飘落效果.感觉挺不错的.分享给大家,效果例如以下: 代码例如 ...

  3. 【OpenGL】Shader实例分析(九)- AngryBots中的主角受伤特效

    转发请保持地址:http://blog.csdn.net/stalendp/article/details/40859441 AngryBots是Unity官方的一个非常棒的样例.非常有研究价值. 曾 ...

  4. 片元着色器(Fragment Shader)被称为像素着色器(Pixel Shader),但

    片元着色器(Fragment Shader)被称为像素着色器(Pixel Shader),但片元着色器是一个更合适的名字, 因为此时的片元并不是一个真正意义上的像素.

  5. [工作积累] D3D10+ 中 Pixel Shader 的input semantic和参数顺序

    由于semantic的使用,我们有理由相信 vertex shader的output 和 pixel shader的input是按照semantic来匹配的,而跟传入顺序无关.印象dx9时代是这样. ...

  6. 今天写shader流光效果,shader代码少了个括号,unity shader compiler卡死且不提示原因

    今天写shader流光效果,shader代码少了个括号,unity shader compiler卡死且不提示原因 好在找到了原因,shader 代码如下,原理是提高经过的颜色亮度 void surf ...

  7. UE制作PBR材质攻略Part 1 - 色彩知识

    目录 一.前言 二.色彩知识 2.1 色彩理论 2.1.1 成像原理 2.1.2 色彩模型和色彩空间 2.1.3 色彩属性 2.1.4 直方图 2.1.5 色调曲线 2.1.6 线性空间与Gamma空 ...

  8. 《图说VR入门》——googleVR入门代码分析

    本文章由cartzhang编写,转载请注明出处. 所有权利保留. 文章链接:http://blog.csdn.net/cartzhang/article/details/53013843 作者:car ...

  9. Unity Shader 00 - 梳理 Unity Shader 的基本结构

    0x00 写在前面 之前一直在阅读 The Book of Shaders 一书,为什么会开始写 Unity Shader 呢?一方面,因为该书目前尚未完结,写下此文时已阅读到该书的最新章节:另一方面 ...

随机推荐

  1. jeecg中自定义按钮时遇到的问题

  2. ELK学习实验003:Elasticsearch 集群安装

    前面已经介绍了Elasticsearch这个工具,并对单机安装做了简单介绍,现在使用三台机器做一个elasticsearch集群 一 环境准备 1.1 机器准备 1.2 同步时间 [root@node ...

  3. 015 Ceph的集群管理_1

    一.理解Cluster Map cluster map由monitor维护,用于跟踪ceph集群状态 当client启动时,会连接monitor获取cluster map副本,发现所有其他组件的位置, ...

  4. 星星泡饭-R1SE

    作词 : 吴孤儿 时光不用斟酌 再流淌 摩天轮慢慢地旋转 约定 留下搅拌的星光 赵磊: 媲美哪颗星星的孤寂 是我们 脏不了的心 勇敢 游戏 品尝着很饿的梦境 我的梦想只是梦想 哪怕回音只是气球碰撞 会 ...

  5. Python3 pip换源

    pip安装源 介绍 """ 1.采用国内源,加速下载模块的速度 2.常用pip源: -- 豆瓣:https://pypi.douban.com/simple -- 阿里: ...

  6. [02]java数据类型和运算符等知识

    00 Java中的注释 为了方便程序的阅读,Java语言允许程序员在程序中写上一些说明性的文字,用来提高程序的可读性,这些文字性的说明就称为注释.注释不会出现在字节码文件中,即Java编译器编译时会跳 ...

  7. 使用sqlmap中的tamper脚本绕过waf

    使用sqlmap中tamper脚本绕过waf 脚本名:0x2char.py 作用:用UTF-8全角对应字符替换撇号字符 作用:用等价的CONCAT(CHAR(),...)对应替换每个(MySQL)0x ...

  8. 1026 程序运行时间 (15 分)C语言

    题目描述 要获得一个C语言程序的运行时间,常用的方法是调用头文件time.h,其中提供了clock()函数,可以捕捉从程序开始运行到clock()被调用时所耗费的时间.这个时间单位是clock tic ...

  9. Spark学习笔记(三)—— Standalone模式

    上篇笔记记录了Local模式的一些内容,但是实际的应用中很少有使用Local模式的,只是为了我们方便学习和测试.真实的生产环境中,Standalone模式更加合适一点. 1.基础概述 Standalo ...

  10. schedule of 2016-11-7~2016-11-10(Monday~Thursday)——1st semester of 2nd Grade

    most important things to do 1.joint phd preparations 2.journal paper to write 3.solid fundamental kn ...