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

Advanced Shader #280

Merged
merged 27 commits into from
Jul 19, 2024
Merged
Show file tree
Hide file tree
Changes from 26 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
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
58 changes: 58 additions & 0 deletions packages/custom-material/src/advanced-shader/eye/Eye.gs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
Shader "/eyes/Eye.gs" {
EditorProperties {
Header("Sclera") {
material_ScleraColor("Sclera Color", Color) = (1,1,1,1);
material_ScleraSize("Sclera Size", Range(0, 5, 0.01)) = 1.0;
material_Metal("Sclera Specular", Range(0,1,0.01) ) = 0.2;
material_Roughness("Sclera Roughness", Range( 0, 1, 0.01 ) ) = 0.2;
material_ScleraNormalStrength("Sclera NormalStrength", Range(0, 5, 0.01)) = 1;
material_ScleraTexture("Sclera Texture", Texture2D);
material_ScleraNormal("Sclera NormalTexture", Texture2D);
material_ScleraMask("Sclera Mask", Texture2D);
}
Header("Iris") {
material_IrisColor("Iris Color", Color) = (1,1,1,1);
material_PupilSize("Pupil Dilation", Vector2) =(0.3, 0.3);
material_Limbal("Limbal Ring Amount", Range(0, 1, 0.01)) = 0.5;
material_IrisSize("Iris Size", Range(0, 5, 0.01)) = 2;
material_Parallax("Parallax Layer", Range(0, 0.5, 0.01)) = 0.08;
material_IrisNormalStrength("Iris NormalStrength", Range(0, 5, 0.01)) = 1;

material_IrisTexture("Iris Texture", Texture2D);
material_IrisNormal("Iris NormalTexture", Texture2D);
}

Header("Common") {
material_AlphaCutoff( "AlphaCutoff", Range(0, 1, 0.01) ) = 0;
}
}

EditorMacros {
Header("Conditional Macors") {
MATERIAL_IS_TRANSPARENT("IS_TRANSPARENT");
MATERIAL_IS_ALPHA_CUTOFF("IS_ALPHA_CUTOFF");
MATERIAL_HAS_SCLERA_NORMAL("HAS_SCLERA_NORMAL");
MATERIAL_HAS_IRIS_NORMAL("HAS_IRIS_NORMAL");
MATERIAL_HAS_SCLERA_MASK("HAS_SCLERA_MASK");
MATERIAL_HAS_IRIS_TEXTURE("HAS_IRIS_TEXTURE");
MATERIAL_HAS_SCLERA_TEXTURE("HAS_SCLERA_TEXTURE");
}

}

SubShader "Default" {
UsePass "pbr/Default/ShadowCaster"

Pass "Forward Pass" {
Tags { pipelineStage = "Forward"}

#define IS_METALLIC_WORKFLOW

VertexShader = PBRVertex;
FragmentShader = PBRFragment;

#include "./EyeForwardPass.glsl"
}
}
}

122 changes: 122 additions & 0 deletions packages/custom-material/src/advanced-shader/eye/EyeForwardPass.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
#ifndef FORWARD_PASS_PBR_INCLUDED
#define FORWARD_PASS_PBR_INCLUDED

#include "Common.glsl"
#include "Fog.glsl"

#include "AttributesPBR.glsl"
#include "VaryingsPBR.glsl"
#include "LightDirectPBR.glsl"
#include "LightIndirectPBR.glsl"

#include "VertexPBR.glsl"
#include "FragmentPBR.glsl"

#include "./EyeFunction.glsl"

Varyings PBRVertex(Attributes attributes) {
Varyings varyings;

// Don't need tilling offest
varyings.uv = attributes.TEXCOORD_0;

#ifdef RENDERER_HAS_UV1
varyings.uv1 = attributes.TEXCOORD_1;
#endif

#ifdef RENDERER_ENABLE_VERTEXCOLOR
varyings.vertexColor = attributes.COLOR_0;
#endif


VertexInputs vertexInputs = getVertexInputs(attributes);

// positionWS
varyings.positionWS = vertexInputs.positionWS;

// positionVS
#if SCENE_FOG_MODE != 0
varyings.positionVS = vertexInputs.positionVS;
#endif

// normalWS、tangentWS、bitangentWS
#ifdef RENDERER_HAS_NORMAL
varyings.normalWS = vertexInputs.normalWS;
#ifdef RENDERER_HAS_TANGENT
varyings.tangentWS = vertexInputs.tangentWS;
varyings.bitangentWS = vertexInputs.bitangentWS;
#endif
#endif

// ShadowCoord
#if defined(NEED_CALCULATE_SHADOWS) && (SCENE_SHADOW_CASCADED_COUNT == 1)
varyings.shadowCoord = getShadowCoord(vertexInputs.positionWS);
#endif

gl_Position = renderer_MVPMat * vertexInputs.positionOS;

return varyings;
}


void PBRFragment(Varyings varyings) {
BRDFData brdfData;


// Get aoUV
vec2 aoUV = varyings.uv;
#if defined(MATERIAL_HAS_OCCLUSION_TEXTURE) && defined(RENDERER_HAS_UV1)
if(material_OcclusionTextureCoord == 1.0){
aoUV = varyings.uv1;
}
#endif

SurfaceData surfaceData = getSurfaceData(varyings, aoUV, gl_FrontFacing);

#ifdef RENDERER_HAS_TANGENT
mat3 tbn = mat3(surfaceData.tangent, surfaceData.bitangent, surfaceData.normal);
#endif

// Modify surfaceData by eye algorithm
surfaceData.albedoColor = calculateEyeColor(varyings.uv, tbn);
surfaceData.normal =calculateEyeNormal(varyings.uv, tbn, gl_FrontFacing);
surfaceData.f0 = 0.04;

// Can modify surfaceData here
initBRDFData(surfaceData, brdfData);

vec4 color = vec4(0, 0, 0, surfaceData.opacity);

// Get shadow attenuation
float shadowAttenuation = 1.0;
#if defined(SCENE_DIRECT_LIGHT_COUNT) && defined(NEED_CALCULATE_SHADOWS)
#if SCENE_SHADOW_CASCADED_COUNT == 1
vec3 shadowCoord = varyings.shadowCoord;
#else
vec3 shadowCoord = getShadowCoord(varyings.positionWS);
#endif
shadowAttenuation *= sampleShadowMap(varyings.positionWS, shadowCoord);
#endif

// Evaluate direct lighting
evaluateDirectRadiance(varyings, surfaceData, brdfData, shadowAttenuation, color.rgb);

// IBL
evaluateIBL(varyings, surfaceData, brdfData, color.rgb);

// Emissive
color.rgb += surfaceData.emissiveColor;


#if SCENE_FOG_MODE != 0
color = fog(color, varyings.positionVS);
#endif

#ifndef ENGINE_IS_COLORSPACE_GAMMA
color = linearToGamma(color);
#endif

gl_FragColor = color;
}

#endif
133 changes: 133 additions & 0 deletions packages/custom-material/src/advanced-shader/eye/EyeFunction.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
float material_ScleraSize;
float material_IrisSize;
vec2 material_PupilSize;
float material_Limbal;
float material_Parallax;
float material_ScleraNormalStrength;
float material_IrisNormalStrength;
vec4 material_ScleraColor;
vec4 material_IrisColor;

#ifdef MATERIAL_HAS_SCLERA_NORMAL
sampler2D material_ScleraNormal;
#endif

#ifdef MATERIAL_HAS_SCLERA_TEXTURE
sampler2D material_ScleraTexture;
#endif

#ifdef MATERIAL_HAS_SCLERA_MASK
sampler2D material_ScleraMask;
#endif

#ifdef MATERIAL_HAS_IRIS_TEXTURE
sampler2D material_IrisTexture;
#endif

#ifdef MATERIAL_HAS_IRIS_NORMAL
sampler2D material_IrisNormal;
#endif

vec2 parallaxOffset( float heighttex, float height, vec3 viewDir )
{
float heightTex = heighttex * height- height/2.0;
vec3 s = viewDir;
s.z -= 0.42;
s.y -= s.y;
return heightTex * (s.xy / s.z);
}

vec3 calculateEyeColor(Varyings varyings, mat3 tbn)
{
// Sclera UV
vec2 scleraUV = (varyings.uv * material_ScleraSize)-((material_ScleraSize-1.0)/2.0);

// Sclera Texture
#ifdef MATERIAL_HAS_SCLERA_TEXTURE
vec4 scleraColor = texture2D(material_ScleraTexture, scleraUV);
scleraColor *= material_ScleraColor;
#ifndef ENGINE_IS_COLORSPACE_GAMMA
scleraColor = gammaToLinear(scleraColor);
#endif
#else
vec4 scleraColor = vec4(1.0);
#endif

// Iris Size For Mask
vec2 irisSizeUV = (varyings.uv * material_IrisSize) - ((material_IrisSize-1.0)/2.0);
float irisSize = material_IrisSize * 0.6;

// Pupil Size
vec2 pupilSize = mix(vec2(mix(0.5,0.2,irisSize/5.0)),vec2(mix(1.2,0.75,irisSize/5.0)),material_PupilSize.xy);

// Parallax UV
vec2 parallaxUV = mix((varyings.uv * 0.75)-((0.75-1.0)/2.0) ,(varyings.uv * pupilSize)-((pupilSize-1.0)/2.0),varyings.uv );

// Get Mask
float heighttexture = 0.0;
#ifdef MATERIAL_HAS_SCLERA_MASK
vec3 irisMaskTex = (texture2D(material_ScleraMask, irisSizeUV)).rgb;
float uvmask = 1.0 - (texture2D(material_ScleraMask, varyings.uv )).b;
heighttexture = 1.0 - (texture2D(material_ScleraMask, parallaxUV)).b;
#else
vec3 irisMaskTex = vec3(1.0);
float uvmask = 1.0;
heighttexture = 1.0;
#endif

// Transform ViewdirWS To ViewdirTS
vec3 vDir = normalize(camera_Position - varyings.positionWS);
vec3 viewDirInTBN = tbn * vDir;

vec2 offset = parallaxOffset(heighttexture, material_Parallax, viewDirInTBN);

// Iris UV And Pupil UV
vec2 irisUV = (varyings.uv * irisSize) - ((irisSize-1.0)/2.0);
vec2 pupilUV = irisUV * ((-1.0 + (uvmask * pupilSize)))-( 0.5 *(uvmask * pupilSize));

// Parallax Color
vec4 parallax = vec4(0.0);
#ifdef MATERIAL_HAS_IRIS_TEXTURE
parallax = texture2D(material_IrisTexture, pupilUV - offset);
#ifndef ENGINE_IS_COLORSPACE_GAMMA
parallax = gammaToLinear(parallax);
#endif
parallax.rgb *= material_IrisColor.rgb;
#endif

vec4 baseColor = mix(scleraColor,parallax,irisMaskTex.r);

// Limbus
vec4 limbalstrength = (0.0 - (material_Limbal * 10.0 )) * baseColor;
float limbalRadius = saturate(irisMaskTex.g * (1.0 - irisMaskTex.r));
baseColor = mix(baseColor,limbalstrength,limbalRadius);
return baseColor.rgb;
}

vec3 calculateEyeNormal(Varyings varyings, mat3 tbn, bool isFrontFacing)
{
// Sclera UV
vec2 scleraUV = (varyings.uv * material_ScleraSize)-((material_ScleraSize-1.0)/2.0);
// Iris UV
vec2 irisSizeUV = (varyings.uv * material_IrisSize) - ((material_IrisSize-1.0)/2.0);

#ifdef MATERIAL_HAS_SCLERA_MASK
vec3 irisMaskTex = (texture2D(material_ScleraMask, irisSizeUV)).rgb;
#else
vec3 irisMaskTex = vec3(1.0);
#endif

// Normal
#ifdef MATERIAL_HAS_SCLERA_NORMAL
vec3 scleraNormal = getNormalByNormalTexture(tbn, material_ScleraNormal, material_ScleraNormalStrength, scleraUV, isFrontFacing);
#else
vec3 scleraNormal = tbn[2];
#endif

#ifdef MATERIAL_HAS_IRIS_NORMAL
vec3 irisNormal = getNormalByNormalTexture(tbn, material_IrisNormal, material_IrisNormalStrength, irisSizeUV, isFrontFacing);
#else
vec3 irisNormal = tbn[2];
#endif
return mix(scleraNormal,irisNormal,irisMaskTex.r);
}
78 changes: 78 additions & 0 deletions packages/custom-material/src/advanced-shader/hair/Hair.gs
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
Shader "hair/hair.gs" {
EditorProperties {
Header("Base"){
material_IOR("IOR", Range(0, 5, 0.01)) = 1.5;
material_BaseColor("BaseColor", Color) = (1, 1, 1, 1);
material_BaseTexture("BaseTexture", Texture2D);
}

Header("Metal Roughness") {
material_Metal( "Metal", Range(0,1,0.01) ) = 1;
material_Roughness( "Roughness", Range( 0, 1, 0.01 ) ) = 1;
material_RoughnessMetallicTexture("RoughnessMetallicTexture", Texture2D);
}

Header("HairAnisotropy") {
material_HairFirstWidth("HairFirstWidth", Range(0, 15, 0.01)) = 15;
material_HairSecondWidth("HairSecondWidth", Range(0, 15, 0.01)) = 15;
material_HairFirstStrength("HairsFirstrength", Range(0, 5, 0.01)) = 1;
material_HairSecondStrength("HairsSecondstrength", Range(0, 5, 0.01)) = 0.5;
material_HairFirstOffset("HairFirstOffest", Range(-1, 5, 0.01)) = 0.2;
material_HairSecondOffset("HairSecondOffest", Range(-1, 5, 0.01)) = 0.2;
material_HairFirstColor("HairFirstColor", Color) = (1,1,1,1);
material_HairSecondColor("HairSecondColor", Color) = (1,1,1,1);
material_HairAnisotropyTexture("HairAnisotropyTexture", Texture2D);
}

Header("Normal") {
material_NormalTexture("NormalTexture", Texture2D);
material_NormalIntensity("NormalIntensity", Range(0, 5, 0.01)) = 1;
}

Header("Emissive") {
material_EmissiveColor("EmissiveColor", Color ) = (0, 0, 0, 1);
material_EmissiveTexture("EmissiveTexture", Texture2D);
}

Header("Occlusion") {
material_OcclusionTexture("OcclusionTexture", Texture2D);
material_OcclusionIntensity("OcclusionIntensity", Range(0, 5, 0.01)) = 1;
material_OcclusionTextureCoord("OcclusionTextureCoord", Float) = 0;
}

Header("Common") {
material_AlphaCutoff( "AlphaCutoff", Range(0, 1, 0.01) ) = 0;
material_TilingOffset("TilingOffset", Vector4) = (1, 1, 0, 0);
}
}

EditorMacros {
Header("Conditional Macors") {
MATERIAL_HAS_BASETEXTURE("HAS_BASETEXTURE");
MATERIAL_HAS_ROUGHNESS_METALLIC_TEXTURE("HAS_ROUGHNESS_METALLIC_TEXTURE");
MATERIAL_HAS_HAIRANISOTROPY_TEXTURE("HAIRANISOTROPY");
MATERIAL_HAS_NORMALTEXTURE("HAS_NORMALTEXTURE");
MATERIAL_HAS_EMISSIVETEXTURE("HAS_EMISSIVETEXTURE");
MATERIAL_HAS_OCCLUSION_TEXTURE("HAS_OCCLUSION_TEXTURE");
MATERIAL_IS_TRANSPARENT("IS_TRANSPARENT");
MATERIAL_IS_ALPHA_CUTOFF("IS_ALPHA_CUTOFF");
}
}

SubShader "Default" {

UsePass "pbr/Default/ShadowCaster"

Pass "Forward Pass" {
Tags { pipelineStage = "Forward"}

#define IS_METALLIC_WORKFLOW

VertexShader = PBRVertex;
FragmentShader = PBRFragment;

#include "./HairForwardPass.glsl"

}
}
}
Loading
Loading