Skip to content

Commit

Permalink
Merge branch 'dev/1.3' of github.com:galacean/runtime-toolkit into de…
Browse files Browse the repository at this point in the history
…v/1.3
  • Loading branch information
JujieX committed Jul 23, 2024
2 parents ab26a6e + f48e86a commit 6f3abcf
Show file tree
Hide file tree
Showing 12 changed files with 69 additions and 42 deletions.
2 changes: 1 addition & 1 deletion packages/custom-material/src/advanced-shader/eye/Eye.gs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Shader "/eyes/Eye.gs" {
Shader "DigitalHuman/Eye.gs" {
EditorProperties {
Header("Sclera") {
material_ScleraColor("Sclera Color", Color) = (1,1,1,1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,12 @@ void PBRFragment(Varyings varyings) {

#ifdef RENDERER_HAS_TANGENT
mat3 tbn = mat3(surfaceData.tangent, surfaceData.bitangent, surfaceData.normal);
#else
mat3 tbn = getTBNByDerivatives(aoUV, normal, varyings.positionWS, gl_FrontFacing);
#endif

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

Expand Down Expand Up @@ -108,7 +110,6 @@ void PBRFragment(Varyings varyings) {
// Emissive
color.rgb += surfaceData.emissiveColor;


#if SCENE_FOG_MODE != 0
color = fog(color, varyings.positionVS);
#endif
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,11 @@ 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;
s.z += 0.42;
return heightTex * (s.xy / s.z);
}

vec3 calculateEyeColor(Varyings varyings, mat3 tbn)
vec3 calculateEyeColor(Varyings varyings, mat3 tbn, SurfaceData surfaceData)
{
// Sclera UV
vec2 scleraUV = (varyings.uv * material_ScleraSize)-((material_ScleraSize-1.0)/2.0);
Expand Down Expand Up @@ -68,15 +67,15 @@ vec3 calculateEyeColor(Varyings varyings, mat3 tbn)
#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;
heighttexture = (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 vDir = surfaceData.viewDir;
vec3 viewDirInTBN = tbn * vDir;

vec2 offset = parallaxOffset(heighttexture, material_Parallax, viewDirInTBN);
Expand Down
2 changes: 1 addition & 1 deletion packages/custom-material/src/advanced-shader/hair/Hair.gs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Shader "hair/hair.gs" {
Shader "DigitalHuman/Hair.gs" {
EditorProperties {
Header("Base"){
material_IOR("IOR", Range(0, 5, 0.01)) = 1.5;
Expand Down
2 changes: 1 addition & 1 deletion packages/custom-material/src/advanced-shader/sss/SSS.gs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Shader "sss/sss.gs" {
Shader "DigitalHuman/SSS.gs" {
EditorProperties {
Header("Base"){
material_IOR("IOR", Range(0, 5, 0.01)) = 1.5;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Shader "/thin/Iridescene.gs" {
Shader "Advanced/Thin.gs" {
EditorProperties {
Header("Base"){
material_BaseColor("BaseColor", Color) = (1, 1, 1, 1);
Expand Down
18 changes: 14 additions & 4 deletions packages/custom-material/src/grid/GridMaterial.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,17 +121,23 @@ Shader.create(
"grid",
`
#include <common>
#include <common_vert>
attribute vec4 POSITION_FLIP;
uniform mat4 camera_ViewInvMat;
uniform mat4 camera_ProjMat;
uniform vec4 camera_ProjectionParams;
varying vec3 nearPoint;
varying vec3 farPoint;
vec3 UnprojectPoint(float x, float y, float z, mat4 viewInvMat, mat4 projInvMat) {
vec4 unprojectedPoint = viewInvMat * projInvMat * vec4(x, y, z, 1.0);
return unprojectedPoint.xyz / unprojectedPoint.w;
}
void main() {
float tol = 0.0001;
mat4 viewInvMat = camera_ViewInvMat;
Expand All @@ -140,9 +146,13 @@ void main() {
}
mat4 projInvMat = INVERSE_MAT(camera_ProjMat);
nearPoint = UnprojectPoint(POSITION.x, POSITION.y, -1.0, viewInvMat, projInvMat);// unprojecting on the near plane
farPoint = UnprojectPoint(POSITION.x, POSITION.y, 1.0, viewInvMat, projInvMat);// unprojecting on the far plane
gl_Position = vec4(POSITION, 1.0);// using directly the clipped coordinates
bool flipY = camera_ProjectionParams.x < 0.0;
float x = flipY? POSITION_FLIP.z : POSITION_FLIP.x;
float y = flipY? POSITION_FLIP.w : POSITION_FLIP.y;
nearPoint = UnprojectPoint(x, y, -1.0, viewInvMat, projInvMat);// unprojecting on the near plane
farPoint = UnprojectPoint(x, y, 1.0, viewInvMat, projInvMat);// unprojecting on the far plane
gl_Position = vec4(x, y, 0.0, 1.0);// using directly the clipped coordinates
}`,

`
Expand Down
40 changes: 21 additions & 19 deletions packages/custom-material/src/grid/GridMesh.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
import { ContentRestorer, Engine, ModelMesh, Vector3 } from "@galacean/engine";
import {
Buffer,
BufferBindFlag,
BufferUsage,
ContentRestorer,
Engine,
MeshTopology,
ModelMesh,
VertexElement,
VertexElementFormat
} from "@galacean/engine";

export class GridMesh {
static createGridPlane(engine: Engine): ModelMesh {
Expand All @@ -9,26 +19,18 @@ export class GridMesh {
}

static _updateGridData(mesh: ModelMesh) {
const positions = new Array<Vector3>(6);
positions[0] = new Vector3(1, 1, 0);
positions[1] = new Vector3(-1, -1, 0);
positions[2] = new Vector3(-1, 1, 0);
positions[3] = new Vector3(-1, -1, 0);
positions[4] = new Vector3(1, 1, 0);
positions[5] = new Vector3(1, -1, 0);
// No-FlipY: POSITION_FLIP.xy, FlipY: POSITION_FLIP.zw
// prettier-ignore
const vertices = new Float32Array([
-1, -1, 1, -1, // left-bottom
1, -1, -1, -1, // right-bottom
-1, 1, 1, 1, // left-top
1, 1, -1, 1]); // right-top

const indices = new Uint8Array(6);
indices[0] = 2;
indices[1] = 1;
indices[2] = 0;
indices[3] = 5;
indices[4] = 4;
indices[5] = 3;
mesh.setVertexElements([new VertexElement("POSITION_FLIP", 0, VertexElementFormat.Vector4, 0)]);
mesh.setVertexBufferBinding(new Buffer(mesh.engine, BufferBindFlag.VertexBuffer, vertices, BufferUsage.Static), 16);
mesh.addSubMesh(0, 4, MeshTopology.TriangleStrip);

mesh.setPositions(positions);
mesh.setIndices(indices);
mesh.uploadData(true);
mesh.addSubMesh(0, 6);
const { bounds } = mesh;
bounds.min.set(-Number.MAX_VALUE, -Number.MAX_VALUE, -Number.MAX_VALUE);
bounds.max.set(Number.MAX_VALUE, Number.MAX_VALUE, Number.MAX_VALUE);
Expand Down
18 changes: 18 additions & 0 deletions packages/framebuffer-picker/src/FramebufferPicker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
Camera,
dependentComponents,
DependentMode,
DepthTextureMode,
Logger,
Renderer,
RenderTarget,
Expand Down Expand Up @@ -130,6 +131,11 @@ export class FramebufferPicker extends Script {
this._checkFrameBufferSize();

const camera = this._camera;
const originalPostProcessEnabled = camera.enablePostProcess;
const originalHDR = camera.enableHDR;
const originalDepthMode = camera.depthTextureMode;
const originalOpaqueTextureEnabled = camera.opaqueTextureEnabled;

this._updateRenderersPickColor(camera.scene);
// Prepare render target and shader
const lastRenderTarget = camera.renderTarget;
Expand All @@ -138,12 +144,24 @@ export class FramebufferPicker extends Script {
camera.setReplacementShader(pickShader);
camera.aspectRatio = lastRatio;

// Reset internal RT and useless pass
camera.enablePostProcess = false;
camera.enableHDR = false;
camera.depthTextureMode = DepthTextureMode.None;
camera.opaqueTextureEnabled = false;

camera.render();

// Revert render target and shader
camera.resetReplacementShader();
camera.renderTarget = lastRenderTarget;
camera.resetAspectRatio();

// Revert internal RT and original pass
camera.enablePostProcess = originalPostProcessEnabled;
camera.enableHDR = originalHDR;
camera.depthTextureMode = originalDepthMode;
camera.opaqueTextureEnabled = originalOpaqueTextureEnabled;
}

private _readPixelFromRenderTarget(x: number, y: number, xEnd?: number, yEnd?: number): Uint8Array {
Expand Down
10 changes: 2 additions & 8 deletions packages/shaderlab/src/shaders/Normal.glsl
Original file line number Diff line number Diff line change
@@ -1,13 +1,6 @@
#ifndef NORMAL_INCLUDED
#define NORMAL_INCLUDED

// gl_FrontFacing has random value on Adreno GPUs
// the Adreno bug is only when gl_FrontFacing is inside a function
// https://bugs.chromium.org/p/chromium/issues/detail?id=1154842
vec3 getNormal(vec3 normal, bool isFrontFacing){
normal *= float( isFrontFacing ) * 2.0 - 1.0;
return normal;
}

vec3 getNormalByNormalTexture(mat3 tbn, sampler2D normalTexture, float normalIntensity, vec2 uv, bool isFrontFacing){
vec3 normal = (texture2D(normalTexture, uv)).rgb;
Expand All @@ -32,7 +25,8 @@ mat3 getTBNByDerivatives(vec2 uv, vec3 normal, vec3 position, bool isFrontFacing
vec3 tangent = dp2perp * duv1.x + dp1perp * duv2.x;
vec3 bitangent = dp2perp * duv1.y + dp1perp * duv2.y;
// construct a scale-invariant frame
float invmax = inversesqrt(max(dot(tangent, tangent), dot(bitangent, bitangent)));
float denom = max( dot(tangent, tangent), dot(bitangent, bitangent) );
float invmax = (denom == 0.0) ? 0.0 : camera_ProjectionParams.x / sqrt( denom );
return mat3(tangent * invmax, bitangent * invmax, normal);
#else
return mat3(vec3(0.0), vec3(0.0), normal);
Expand Down
1 change: 1 addition & 0 deletions packages/shaderlab/src/shaders/Transform.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@ mat4 renderer_NormalMat;

vec3 camera_Position;
vec3 camera_Forward;
vec4 camera_ProjectionParams;

#endif
2 changes: 2 additions & 0 deletions packages/shaderlab/src/shaders/shadingPBR/FragmentPBR.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -168,10 +168,12 @@ SurfaceData getSurfaceData(Varyings v, vec2 aoUV, bool isFrontFacing){
vec3 pos_dx = dFdx(v.positionWS);
vec3 pos_dy = dFdy(v.positionWS);
vec3 normal = normalize( cross(pos_dx, pos_dy) );
normal *= camera_ProjectionParams.x;
#else
vec3 normal = vec3(0, 0, 1);
#endif

normal *= float( isFrontFacing ) * 2.0 - 1.0;
surfaceData.normal = normal;

// Tangent
Expand Down

0 comments on commit 6f3abcf

Please sign in to comment.