Skip to content

Commit

Permalink
glassSetup
Browse files Browse the repository at this point in the history
  • Loading branch information
neuroprod committed Nov 18, 2023
1 parent d2df397 commit 134681a
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 15 deletions.
6 changes: 2 additions & 4 deletions frontend/src/CanvasRenderPass.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,19 +55,17 @@ export default class CanvasRenderPass extends RenderPass {

this.blitTest = new Blit(renderer, 'blit', this.blitMaterial)

this.passSelect.push(new SelectItem("Glass", {texture: "GlassPass", type: 0}));

this.passSelect.push(new SelectItem("Post", {texture: "PostPass", type: 0}));

this.passSelect.push(new SelectItem("SSR", {texture: "ReflectionPass", type: 0}));

this.passSelect.push(new SelectItem("Glass", {texture: "GlassPass", type: 0}));
this.passSelect.push(new SelectItem("SSOA", {texture: "OAPass", type: 0}));
this.passSelect.push(new SelectItem("OABlur", {texture: "OABlurPass", type: 0}));
this.passSelect.push(new SelectItem("Light", {texture: "LightPass", type: 0}));
this.passSelect.push(new SelectItem("GColor", {texture: "GColor", type: 0}));
this.passSelect.push(new SelectItem("GMRE", {texture: "GMRA", type: 0}));
this.passSelect.push(new SelectItem("GNormal", {texture: "GNormal", type: 0}));


this.passSelect.push(new SelectItem("GDepth", {texture: "GDepth", type: 0}));

let value = this.passSelect[0].value;
Expand Down
3 changes: 3 additions & 0 deletions frontend/src/Main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,9 @@ export default class Main {

}
for (let m of this.glFTLoader.modelsGlass) {

m.material.uniforms.setTexture("gDepth",this.renderer.texturesByLabel["GDepth"])
m.material.uniforms.setTexture("background",this.renderer.texturesByLabel["LightPass"])
this.glassPass.modelRenderer.addModel(m)

}
Expand Down
51 changes: 40 additions & 11 deletions frontend/src/shaders/GlassShader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import DefaultTextures from "../lib/textures/DefaultTextures";
import {ShaderType} from "../lib/core/ShaderTypes";
import Camera from "../lib/Camera";
import ModelTransform from "../lib/model/ModelTransform";
import {getWorldFromUVDepth} from "./ShaderChunks";

export default class GlassShader extends Shader{

Expand All @@ -18,10 +19,12 @@ export default class GlassShader extends Shader{

}
this.addUniform("scale",1);
this.addTexture("colorTexture",DefaultTextures.getWhite(this.renderer))
this.addTexture("mraTexture",DefaultTextures.getWhite(this.renderer))
this.addTexture("normalTexture",DefaultTextures.getNormal(this.renderer))
this.addSampler("mySampler")
this.addTexture("gDepth",DefaultTextures.getWhite(this.renderer),"unfilterable-float");
this.addTexture("background",DefaultTextures.getWhite(this.renderer),"unfilterable-float");
this.addTexture("colorTexture",DefaultTextures.getWhite(this.renderer));
this.addTexture("mraTexture",DefaultTextures.getWhite(this.renderer));
this.addTexture("normalTexture",DefaultTextures.getNormal(this.renderer));
this.addSampler("mySampler");

this.needsTransform =true;
this.needsCamera=true;
Expand All @@ -37,6 +40,7 @@ struct VertexOutput
@location(2) tangent : vec3f,
@location(3) biTangent : vec3f,
@location(4) world : vec3f,
@location(5) projPos : vec4f,
@builtin(position) position : vec4f
}
Expand All @@ -45,33 +49,58 @@ struct VertexOutput
${Camera.getShaderText(0)}
${ModelTransform.getShaderText(1)}
${this.getShaderUniforms(2)}
${getWorldFromUVDepth()}
@vertex
fn mainVertex( ${this.getShaderAttributes()} ) -> VertexOutput
{
var output : VertexOutput;
output.position =camera.viewProjectionMatrix*model.modelMatrix *vec4( aPos,1.0);
output.uv0 =aUV0;
output.world =(model.modelMatrix *vec4( aPos,1.0)).xyz;
output.projPos =output.position;
output.world =(model.modelMatrix *vec4( aPos,1.0)).xyz;
output.normal =model.normalMatrix *aNormal;
output.tangent =model.normalMatrix*aTangent.xyz;
output.biTangent =model.normalMatrix* (normalize(cross( normalize(aTangent.xyz), normalize(aNormal)))*aTangent.w);
output.biTangent =model.normalMatrix* (normalize(cross( normalize(aTangent.xyz), normalize(aNormal)))*aTangent.w);
return output;
}
@fragment
fn mainFragment(@location(0) uv0: vec2f,@location(1) normal: vec3f,@location(2) tangent: vec3f,@location(3) biTangent: vec3f,@location(4) world: vec3f) -> @location(0) vec4f
fn mainFragment(@location(0) uv0: vec2f,@location(1) normal: vec3f,@location(2) tangent: vec3f,@location(3) biTangent: vec3f,@location(4) world: vec3f, @location(5) projPos : vec4f) -> @location(0) vec4f
{
let textureSize =vec2<f32>( textureDimensions(gDepth));
var uvScreen = (projPos.xy/projPos.w)*0.5+0.5;
uvScreen.y =1.0-uvScreen.y;
let albedo = textureSample(colorTexture, mySampler, uv0);
//output.normal =vec4(normalize(normal)*0.5+0.5,1.0);
let uvScreenI = vec2<i32>(floor(uvScreen *textureSize));
let worldS =getWorldFromUVDepth(uvScreen ,textureLoad(gDepth, uvScreenI ,0).x);
let albedo = textureSample(colorTexture, mySampler, uv0).xyz;
var normalText = textureSample(normalTexture, mySampler, uv0).xyz* 2. - 1.;
let N = mat3x3f(normalize(tangent),normalize(biTangent),normalize(normal))*normalize(normalText);
let mra =textureSample(mraTexture, mySampler, uv0) ;
return vec4(N,0.5);
let V = -normalize(camera.worldPosition.xyz - world);
let dir =refract(V,-N,0.95);
let dist =distance(worldS,world);
let sPos =world+dir*0.02*dist;
let pPos =camera.viewProjectionMatrix*vec4(sPos,1.0);
let uvRef = (pPos.xy/pPos.w)*0.5+0.5;
let uvRefI = vec2<i32>(floor(vec2(uvRef.x,1.0-uvRef.y )*textureSize));
var refColor = textureLoad(background, uvRefI ,0).xyz;
refColor+=albedo*0.1;
return vec4(refColor,1.0);
}
///////////////////////////////////////////////////////////
Expand Down

0 comments on commit 134681a

Please sign in to comment.