Skip to content

Commit

Permalink
fix: Fix turning shadows on and off for Materials and MeshRenderers (O…
Browse files Browse the repository at this point in the history
…rillusion#343)

Fix turning shadows on and off for Materials and MeshRenderers
  • Loading branch information
hellmor authored Dec 6, 2023
1 parent b02c85a commit 6858cc0
Show file tree
Hide file tree
Showing 17 changed files with 218 additions and 122 deletions.
13 changes: 4 additions & 9 deletions packages/effect/grass/shader/GrassBaseShader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,10 @@ export let GrassBaseShader = /*wgsl*/ `
@location(4) vWorldPos: vec4<f32>,
@location(5) vWorldNormal: vec3<f32>,
@location(6) vColor: vec4<f32>,
#if USE_SHADOWMAPING
@location(7) vShadowPos: vec4<f32>,
#endif
#if USE_TANGENT
@location(8) TANGENT: vec4<f32>,
#endif
@location(7) vShadowPos: vec4<f32>,
#if USE_TANGENT
@location(8) TANGENT: vec4<f32>,
#endif
@builtin(front_facing) face: bool,
@builtin(position) fragCoord : vec4<f32>
};
Expand Down
4 changes: 1 addition & 3 deletions packages/effect/grass/shader/GrassVertexAttributeShader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,7 @@ export let GrassVertexAttributeShader: string = /*wgsl*/ `
@location(5) varying_WPos: vec4<f32>,
@location(6) varying_WNormal: vec3<f32>,
@location(7) varying_Color: vec4<f32>,
#if USE_SHADOWMAPING
@location(8) varying_ShadowPos: vec4<f32>,
#endif
@location(8) varying_ShadowPos: vec4<f32>,
@builtin(position) member: vec4<f32>
};
Expand Down
100 changes: 100 additions & 0 deletions samples/lights/Sample_ShadowToggle.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
import { GUIHelp } from "@orillusion/debug/GUIHelp";
import { Scene3D, HoverCameraController, Engine3D, AtmosphericComponent, Object3D, Camera3D, Vector3, View3D, DirectLight, KelvinUtil, LitMaterial, MeshRenderer, BoxGeometry, CameraUtil, SphereGeometry, Color, Object3DUtil, BlendMode } from "@orillusion/core";
import { GUIUtil } from "@samples/utils/GUIUtil";

//sample of toggle shadow
class Sample_ShadowToggle {
scene: Scene3D;
async run() {
Engine3D.setting.shadow.enable = true;
Engine3D.setting.shadow.autoUpdate = true;
Engine3D.setting.shadow.shadowSize = 1024;
await Engine3D.init({});

GUIHelp.init();

this.scene = new Scene3D();
let sky = this.scene.addComponent(AtmosphericComponent);

// init camera3D
let mainCamera = CameraUtil.createCamera3D(null, this.scene);
// mainCamera.enableCSM = true;
mainCamera.perspective(60, Engine3D.aspect, 1, 5000.0);
//set camera data
mainCamera.object3D.z = -15;
mainCamera.object3D.addComponent(HoverCameraController).setCamera(-15, -35, 200);

sky.relativeTransform = this.initLight();
this.initScene();

let view = new View3D();
view.scene = this.scene;
view.camera = mainCamera;

Engine3D.startRenderView(view);
}

// create direction light
private initLight() {
let lightObj3D = new Object3D();
lightObj3D.rotationX = 46;
lightObj3D.rotationY = 62;
lightObj3D.rotationZ = 0;
let sunLight = lightObj3D.addComponent(DirectLight);
sunLight.intensity = 15;
sunLight.lightColor = KelvinUtil.color_temperature_to_rgb(6553);
sunLight.castShadow = true;

this.scene.addChild(lightObj3D);
GUIUtil.renderDirLight(sunLight, false);
return sunLight.transform;
}

initScene() {
{
let geometry = new SphereGeometry(20, 20, 20);
let material = new LitMaterial();
material.name = 'Box Material';
let obj = new Object3D();
obj.y = 20;
let mr = obj.addComponent(MeshRenderer);
mr.geometry = geometry;
mr.material = material;

this.scene.addChild(obj);
GUIHelp.addFolder('MeshRenderer - Sphere');
GUIHelp.add(mr, 'castShadow');
GUIHelp.open();
GUIHelp.endFolder();
GUIUtil.renderLitMaterial(material, true);
}

//add wall
{
let geometry = new BoxGeometry(40, 10, 80);
let material = new LitMaterial();
let obj = new Object3D();
obj.x = 40;
obj.y = 5;
let mr = obj.addComponent(MeshRenderer);
mr.geometry = geometry;
mr.material = material;
this.scene.addChild(obj);
}

{
let material = new LitMaterial();
material.name = 'Floor Material';

material.baseMap = Engine3D.res.grayTexture;
let floor = new Object3D();
let mr = floor.addComponent(MeshRenderer);
mr.geometry = new BoxGeometry(10000, 1, 10000);
mr.material = material;
this.scene.addChild(floor);
GUIUtil.renderLitMaterial(material, true);
}
}
}

new Sample_ShadowToggle().run();
7 changes: 6 additions & 1 deletion samples/utils/GUIUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -428,7 +428,7 @@ export class GUIUtil {
GUIHelp.endFolder();
}

static renderLitMaterial(mat: LitMaterial) {
static renderLitMaterial(mat: LitMaterial, open?: boolean) {
GUIHelp.addFolder(mat.name);
GUIHelp.addColor(mat, 'baseColor').onChange((v) => {
let color = mat.baseColor;
Expand Down Expand Up @@ -463,6 +463,11 @@ export class GUIUtil {
mat.metallic = v;
});

GUIHelp.add(mat, 'castShadow');
GUIHelp.add(mat, 'acceptShadow');

open && GUIHelp.open();

GUIHelp.endFolder();
}

Expand Down
57 changes: 28 additions & 29 deletions src/assets/shader/compute/DDGILighting_CSShader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,24 +45,23 @@ const PointLightType = 1;
const DirectLightType = 2;
const SpotLightType = 3;
@group(0) @binding(1) var outputBuffer : texture_storage_2d<rgba16float, write>;
@group(0) @binding(2) var prefilterMapSampler: sampler;
@group(0) @binding(3) var prefilterMap: texture_cube<f32>;
@group(0) @binding(auto) var outputBuffer : texture_storage_2d<rgba16float, write>;
@group(0) @binding(auto) var prefilterMapSampler: sampler;
@group(0) @binding(auto) var prefilterMap: texture_cube<f32>;
@group(1) @binding(0) var positionMapSampler : sampler;
@group(1) @binding(1) var positionMap : texture_2d<f32>;
@group(1) @binding(auto) var positionMapSampler : sampler;
@group(1) @binding(auto) var positionMap : texture_2d<f32>;
@group(1) @binding(2) var normalMapSampler : sampler;
@group(1) @binding(3) var normalMap : texture_2d<f32>;
@group(1) @binding(auto) var normalMapSampler : sampler;
@group(1) @binding(auto) var normalMap : texture_2d<f32>;
@group(1) @binding(4) var colorMapSampler : sampler;
@group(1) @binding(5) var colorMap : texture_2d<f32>;
@group(1) @binding(auto) var colorMapSampler : sampler;
@group(1) @binding(auto) var colorMap : texture_2d<f32>;
@group(1) @binding(6) var shadowMapSampler : sampler_comparison;
@group(1) @binding(7) var shadowMap : texture_depth_2d_array;
@group(1) @binding(8) var pointShadowMapSampler: sampler;
@group(1) @binding(9) var pointShadowMap: texture_depth_cube_array ;
@group(1) @binding(auto) var shadowMapSampler : sampler_comparison;
@group(1) @binding(auto) var shadowMap : texture_depth_2d_array;
@group(1) @binding(auto) var pointShadowMapSampler: sampler;
@group(1) @binding(auto) var pointShadowMap: texture_depth_cube_array ;
@group(2) @binding(0)
var<storage,read> lightBuffer: array<LightData>;
Expand Down Expand Up @@ -104,24 +103,24 @@ fn directShadowMaping(P:vec3<f32>, N:vec3<f32>, shadowBias: f32) {
let enableCSM:bool = globalUniform.enableCSM > 0.5;
var light = lightBuffer[0];
var visibility = 1.0;
var shadowIndex = i32(light.castShadow);
if (shadowIndex >= 0 ) {
var shadowMatrix:mat4x4<f32>;
if(enableCSM && csmCount > 1){
for(var csm:i32 = 0; csm < csmCount; csm ++){
var csmShadowBias = globalUniform.csmShadowBias[csm];
shadowMatrix = globalUniform.csmMatrix[csm];
let csmShadowResult = directShadowMapingIndex(light, shadowMatrix, P, N, csm, csmShadowBias);
if(csmShadowResult.y < 0.5){
visibility = csmShadowResult.x;
break;
var shadowIndex = i32(light.castShadow);
if (shadowIndex >= 0 ) {
var shadowMatrix:mat4x4<f32>;
if(enableCSM && csmCount > 1){
for(var csm:i32 = 0; csm < csmCount; csm ++){
var csmShadowBias = globalUniform.csmShadowBias[csm];
shadowMatrix = globalUniform.csmMatrix[csm];
let csmShadowResult = directShadowMapingIndex(light, shadowMatrix, P, N, csm, csmShadowBias);
if(csmShadowResult.y < 0.5){
visibility = csmShadowResult.x;
break;
}
}
}else{
shadowMatrix = globalUniform.shadowMatrix[shadowIndex];
visibility = directShadowMapingIndex(light, shadowMatrix, P, N, shadowIndex, shadowBias).x;
}
}else{
shadowMatrix = globalUniform.shadowMatrix[shadowIndex];
visibility = directShadowMapingIndex(light, shadowMatrix, P, N, shadowIndex, shadowBias).x;
}
}
shadowStrut.directShadowVisibility = visibility;
}
Expand Down
6 changes: 1 addition & 5 deletions src/assets/shader/core/struct/FragmentVarying.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,7 @@ export let FragmentVarying: string = /*wgsl*/ `
@location(5) vWorldPos: vec4<f32>,
@location(6) vWorldNormal: vec3<f32>,
@location(7) vColor: vec4<f32>,
#if USE_SHADOWMAPING
@location(8) vShadowPos: vec4<f32>,
#endif
@location(8) vShadowPos: vec4<f32>,
#if USE_TANGENT
@location(9) TANGENT: vec4<f32>,
#endif
Expand Down
4 changes: 1 addition & 3 deletions src/assets/shader/core/struct/VertexAttributeIndexShader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,7 @@ export let VertexAttributeIndexShader: string = /*wgsl*/ `
@location(5) varying_WPos: vec4<f32>,
@location(6) varying_WNormal: vec3<f32>,
@location(7) varying_Color: vec4<f32>,
#if USE_SHADOWMAPING
@location(8) varying_ShadowPos: vec4<f32>,
#endif
@location(8) varying_ShadowPos: vec4<f32>,
@builtin(position) member: vec4<f32>
};
Expand Down
7 changes: 2 additions & 5 deletions src/assets/shader/core/struct/VertexAttributes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,14 +89,11 @@ export let VertexAttributes: string = /*wgsl*/ `
@location(5) varying_WPos: vec4<f32>,
@location(6) varying_WNormal: vec3<f32>,
@location(7) varying_Color: vec4<f32>,
#if USE_SHADOWMAPING
@location(8) varying_ShadowPos: vec4<f32>,
#endif
@location(8) varying_ShadowPos: vec4<f32>,
#if USE_TANGENT
@location(9) varying_Tangent: vec4<f32>,
#endif
@builtin(position) member: vec4<f32>
};
Expand Down
4 changes: 1 addition & 3 deletions src/assets/shader/materials/ColorLitShader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@ export let ColorLitShader: string = /*wgsl*/`
ORI_ShadingInput.Normal = ORI_VertexVarying.vWorldNormal.rgb ;
#if USE_SHADOWMAPING
useShadow();
#endif
useShadow();
BxDFShading();
}
Expand Down
4 changes: 1 addition & 3 deletions src/assets/shader/materials/Hair_shader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,7 @@ export let Hair_shader_op: string = /*wgsl*/ `
}
#endif
#if USE_SHADOWMAPING
useShadow();
#endif
useShadow();
ORI_ShadingInput.Specular = 1.0 ;
Expand Down
4 changes: 1 addition & 3 deletions src/assets/shader/materials/PBRLItShader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,7 @@ export let PBRLItShader: string = /*wgsl*/ `
}
#endif
#if USE_SHADOWMAPING
useShadow();
#endif
useShadow();
var roughnessChannel:f32 = 1.0 ;
#if USE_ROUGHNESS_A
Expand Down
4 changes: 1 addition & 3 deletions src/assets/shader/materials/PBRLitSSSShader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,7 @@ export let PBRLitSSSShader: string = /*wgsl*/ `
}
#endif
#if USE_SHADOWMAPING
useShadow();
#endif
useShadow();
// maskTex =vec4f( gammaToLiner(maskTex.rgb), maskTex.a );
Expand Down
Loading

0 comments on commit 6858cc0

Please sign in to comment.