Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor: split pbr shader #514

Merged
merged 12 commits into from
Oct 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/core/src/lighting/AmbientLight.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ export class AmbientLight {

if (value) {
shaderData.setTexture(AmbientLight._specularTextureProperty, value);
shaderData.setFloat(AmbientLight._mipLevelProperty, this._specularReflection.mipmapCount);
shaderData.setFloat(AmbientLight._mipLevelProperty, this._specularReflection.mipmapCount - 1);
shaderData.enableMacro(AmbientLight._specularMacro);
} else {
shaderData.disableMacro(AmbientLight._specularMacro);
Expand Down
5 changes: 3 additions & 2 deletions packages/core/src/material/PBRBaseMaterial.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,9 +151,10 @@ export abstract class PBRBaseMaterial extends BaseMaterial {
/**
* Create a pbr base material instance.
* @param engine - Engine to which the material belongs
* @param shader - Shader used by the material
*/
protected constructor(engine: Engine) {
super(engine, Shader.find("pbr"));
protected constructor(engine: Engine, shader: Shader) {
super(engine, shader);

const shaderData = this.shaderData;

Expand Down
3 changes: 1 addition & 2 deletions packages/core/src/material/PBRMaterial.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,7 @@ export class PBRMaterial extends PBRBaseMaterial {
* @param engine - Engine to which the material belongs
*/
constructor(engine: Engine) {
super(engine);
this.shaderData.enableMacro("IS_METALLIC_WORKFLOW");
super(engine, Shader.find("pbr"));
this.shaderData.setFloat(PBRMaterial._metallicProp, 1.0);
this.shaderData.setFloat(PBRMaterial._roughnessProp, 1.0);
}
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/material/PBRSpecularMaterial.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { PBRBaseMaterial } from "./PBRBaseMaterial";
*/
export class PBRSpecularMaterial extends PBRBaseMaterial {
private static _specularColorProp = Shader.getPropertyByName("u_specularColor");
private static _glossinessProp = Shader.getPropertyByName("u_glossinessFactor");
private static _glossinessProp = Shader.getPropertyByName("u_glossiness");
private static _specularGlossinessTextureProp = Shader.getPropertyByName("u_specularGlossinessSampler");

/**
Expand Down Expand Up @@ -59,7 +59,7 @@ export class PBRSpecularMaterial extends PBRBaseMaterial {
* @param engine - Engine to which the material belongs
*/
constructor(engine: Engine) {
super(engine);
super(engine, Shader.find("pbr-specular"));

this.shaderData.setColor(PBRSpecularMaterial._specularColorProp, new Color(1, 1, 1, 1));
this.shaderData.setFloat(PBRSpecularMaterial._glossinessProp, 1.0);
Expand Down
4 changes: 0 additions & 4 deletions packages/core/src/shader/Shader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,13 +162,9 @@ export class Shader {
#ifdef GL_FRAGMENT_PRECISION_HIGH
precision highp float;
precision highp int;
#define O3_VERTEX_PRECISION highp
#define O3_FRAGMENT_PRECISION highp
#else
precision mediump float;
precision mediump int;
#define O3_VERTEX_PRECISION mediump
#define O3_FRAGMENT_PRECISION mediump
#endif
`;

Expand Down
2 changes: 2 additions & 0 deletions packages/core/src/shader/ShaderPool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import blinnPhongVs from "../shaderlib/extra/blinn-phong.vs.glsl";
import particleFs from "../shaderlib/extra/particle.fs.glsl";
import particleVs from "../shaderlib/extra/particle.vs.glsl";
import pbrFs from "../shaderlib/extra/pbr.fs.glsl";
import pbrSpecularFs from "../shaderlib/extra/pbr-specular.fs.glsl";
import pbrVs from "../shaderlib/extra/pbr.vs.glsl";
import shadowMapFs from "../shaderlib/extra/shadow-map.fs.glsl";
import shadowMapVs from "../shaderlib/extra/shadow-map.vs.glsl";
Expand All @@ -27,6 +28,7 @@ export class ShaderPool {
static init(): void {
Shader.create("blinn-phong", blinnPhongVs, blinnPhongFs);
Shader.create("pbr", pbrVs, pbrFs);
Shader.create("pbr-specular", pbrVs, pbrSpecularFs);
Shader.create("unlit", unlitVs, unlitFs);
Shader.create("shadow-map", shadowMapVs, shadowMapFs);
Shader.create("shadow", shadowMapVs, shadowFs);
Expand Down
8 changes: 2 additions & 6 deletions packages/core/src/shaderlib/ShaderLib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,7 @@ import worldpos_vert from "./worldpos_vert.glsl";
import shadow_vert from "./shadow_vert.glsl";
import fog_vert from "./fog_vert.glsl";

import direct_light_frag from "./direct_light_frag.glsl";
import point_light_frag from "./point_light_frag.glsl";
import spot_light_frag from "./spot_light_frag.glsl";
import light_frag_define from "./light_frag_define.glsl";
import mobile_material_frag from "./mobile_material_frag.glsl";
import fog_frag from "./fog_frag.glsl";

Expand Down Expand Up @@ -83,9 +81,7 @@ export const ShaderLib = {
shadow_vert,
fog_vert,

direct_light_frag,
point_light_frag,
spot_light_frag,
light_frag_define,
mobile_material_frag,
fog_frag,

Expand Down
2 changes: 2 additions & 0 deletions packages/core/src/shaderlib/common.glsl
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#define PI 3.14159265359
#define RECIPROCAL_PI 0.31830988618
#define EPSILON 1e-6
#define LOG2 1.442695

#define saturate( a ) clamp( a, 0.0, 1.0 )
Expand Down
16 changes: 8 additions & 8 deletions packages/core/src/shaderlib/common_frag.glsl
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
uniform O3_VERTEX_PRECISION mat4 u_localMat;
uniform O3_VERTEX_PRECISION mat4 u_modelMat;
uniform O3_VERTEX_PRECISION mat4 u_viewMat;
uniform O3_VERTEX_PRECISION mat4 u_projMat;
uniform O3_VERTEX_PRECISION mat4 u_MVMat;
uniform O3_VERTEX_PRECISION mat4 u_MVPMat;
uniform O3_VERTEX_PRECISION mat4 u_normalMat;
uniform O3_VERTEX_PRECISION vec3 u_cameraPos;
uniform mat4 u_localMat;
uniform mat4 u_modelMat;
uniform mat4 u_viewMat;
uniform mat4 u_projMat;
uniform mat4 u_MVMat;
uniform mat4 u_MVPMat;
uniform mat4 u_normalMat;
uniform vec3 u_cameraPos;
10 changes: 0 additions & 10 deletions packages/core/src/shaderlib/direct_light_frag.glsl

This file was deleted.

5 changes: 1 addition & 4 deletions packages/core/src/shaderlib/extra/blinn-phong.fs.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@
#include <color_share>
#include <worldpos_share>

#include <pbr_envmap_light_frag_define>
#include <direct_light_frag>
#include <point_light_frag>
#include <spot_light_frag>
#include <light_frag_define>
#include <mobile_material_frag>

#include <fog_share>
Expand Down
21 changes: 21 additions & 0 deletions packages/core/src/shaderlib/extra/pbr-specular.fs.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#include <common>
#include <common_frag>

#include <fog_share>

#include <uv_share>
#include <normal_share>
#include <color_share>
#include <worldpos_share>

#include <light_frag_define>


#include <pbr_frag_define>
#include <pbr_helper>

void main() {
#include <pbr_frag>
#include <gamma_frag>
#include <fog_frag>
}
33 changes: 5 additions & 28 deletions packages/core/src/shaderlib/extra/pbr.fs.glsl
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#define IS_METALLIC_WORKFLOW
#include <common>
#include <common_frag>
#include <pbr_common_frag_define>
#include <pbr_util_frag_define>

#include <fog_share>

Expand All @@ -10,36 +9,14 @@
#include <color_share>
#include <worldpos_share>

// light
#include <direct_light_frag>
#include <point_light_frag>
#include <spot_light_frag>
#include <pbr_envmap_light_frag_define>
#include <light_frag_define>

// prop & texture
#include <pbr_base_frag_define>
#include <pbr_texture_frag_define>

// runtime context
#include <pbr_runtime_frag_define>

#include <normal_get>

// todo: BxDF
#include <pbr_brdf_cook_torrance_frag_define>


// direct + indirect
#include <pbr_direct_irradiance_frag_define>
#include <pbr_ibl_diffuse_frag_define>
#include <pbr_ibl_specular_frag_define>
#include <pbr_frag_define>
#include <pbr_helper>

void main() {
#include <pbr_begin_frag>
#include <pbr_direct_irradiance_frag>
#include <pbr_ibl_diffuse_frag>
#include <pbr_ibl_specular_frag>
#include <pbr_end_frag>
#include <pbr_frag>
#include <gamma_frag>
#include <fog_frag>
}
8 changes: 4 additions & 4 deletions packages/core/src/shaderlib/fog_share.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@

varying vec3 v_fogDepth;

uniform O3_VERTEX_PRECISION vec3 u_fogColor;
uniform vec3 u_fogColor;

#ifdef O3_FOG_EXP2

uniform O3_VERTEX_PRECISION float u_fogDensity;
uniform float u_fogDensity;

#else

uniform O3_VERTEX_PRECISION float u_fogNear;
uniform O3_VERTEX_PRECISION float u_fogFar;
uniform float u_fogNear;
uniform float u_fogFar;

#endif

Expand Down
69 changes: 69 additions & 0 deletions packages/core/src/shaderlib/light_frag_define.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// directional light
#ifdef O3_DIRECT_LIGHT_COUNT

struct DirectLight {
vec3 color;
vec3 direction;
};

uniform vec3 u_directLightColor[O3_DIRECT_LIGHT_COUNT];
uniform vec3 u_directLightDirection[O3_DIRECT_LIGHT_COUNT];

#endif


// point light
#ifdef O3_POINT_LIGHT_COUNT

struct PointLight {
vec3 color;
vec3 position;
float distance;
};

uniform vec3 u_pointLightColor[ O3_POINT_LIGHT_COUNT ];
uniform vec3 u_pointLightPosition[ O3_POINT_LIGHT_COUNT ];
uniform float u_pointLightDistance[ O3_POINT_LIGHT_COUNT ];

#endif


// spot light
#ifdef O3_SPOT_LIGHT_COUNT

struct SpotLight {
vec3 color;
vec3 position;
vec3 direction;
float distance;
float angleCos;
float penumbraCos;
};

uniform vec3 u_spotLightColor[ O3_SPOT_LIGHT_COUNT ];
uniform vec3 u_spotLightPosition[ O3_SPOT_LIGHT_COUNT ];
uniform vec3 u_spotLightDirection[ O3_SPOT_LIGHT_COUNT ];
uniform float u_spotLightDistance[ O3_SPOT_LIGHT_COUNT ];
uniform float u_spotLightAngleCos[ O3_SPOT_LIGHT_COUNT ];
uniform float u_spotLightPenumbraCos[ O3_SPOT_LIGHT_COUNT ];

#endif

// ambient light
struct EnvMapLight {
vec3 diffuse;
float mipMapLevel;
float diffuseIntensity;
float specularIntensity;
};


uniform EnvMapLight u_envMapLight;

#ifdef O3_USE_SH
uniform vec3 u_env_sh[9];
#endif

#ifdef O3_USE_SPECULAR_ENV
uniform samplerCube u_env_specularSampler;
#endif
13 changes: 0 additions & 13 deletions packages/core/src/shaderlib/pbr/base_frag_define.glsl

This file was deleted.

66 changes: 0 additions & 66 deletions packages/core/src/shaderlib/pbr/begin_frag.glsl

This file was deleted.

Loading