diff --git a/engine/rend/shaders/lit_mesh.frag.hlsl b/engine/rend/shaders/lit_mesh.frag.hlsl index bb17255..aa4df15 100644 --- a/engine/rend/shaders/lit_mesh.frag.hlsl +++ b/engine/rend/shaders/lit_mesh.frag.hlsl @@ -13,7 +13,7 @@ float3 BlinnPhong(float3 normal, float3 fragPos, float3 lightPos, float3 lightCo { float dist = length(lightPos - fragPos); // attenuation - float maxDist = 5; + float maxDist = 15; float cutoff1 = 2; float cutoff2 = 4; float cutoffFactor = 0.5; @@ -31,6 +31,11 @@ float3 BlinnPhong(float3 normal, float3 fragPos, float3 lightPos, float3 lightCo { attenuation = 0; } + + if (attenuation > 1.0) + { + attenuation = 1.0; + } if(length(normal) < 0.1) { return fragPos * lightColor * (1 / (dist * dist)) * attenuation; @@ -76,7 +81,7 @@ float4 main( float3 lightPos = lightPosition.xyz; //+ float3(0, 20, 0); float3 lightColor = float3(0.8, 0.8, 0.5); - float3 ambient = lerp(float3(0.01, 0.01, 0.003), float3(0.004, 0.004, 0.06), dot(Normal, float3(0.0,1.0,0.0)) ) * 0.1; + float3 ambient = lerp(float3(0.01, 0.01, 0.003), float3(0.004, 0.004, 0.06), dot(Normal, float3(0.0,1.0,0.0)) ) * 0.001; float3 col = s.xyz; diff --git a/engine/rend/src/camera/CameraComponent.zig b/engine/rend/src/camera/CameraComponent.zig index 00a5a92..bc44f11 100644 --- a/engine/rend/src/camera/CameraComponent.zig +++ b/engine/rend/src/camera/CameraComponent.zig @@ -33,6 +33,10 @@ pub fn initECS(self: *@This(), handle: core.SetHandle) void { self.entity = core.Entity.fromHandle(handle); } +pub fn forward(self: *@This()) core.Vectorf { + return core.Vectorf.fromZm(mul(self.worldTransform, core.Vectorf.Forward.toZm())); +} + pub fn updateProjections(self: *@This()) void { self.projection = zm.perspectiveFovLh(core.radians(self.fov), 16.0 / 9.0, 0.1, 200000); // self.projection[1][1] *= -1; @@ -60,15 +64,18 @@ pub fn resolve(self: *@This()) void { position.x = -position.x; { var base = core.zm.identity(); - base = mul(core.zm.rotationY(-self.yaw), base); + base = mul(core.zm.rotationY(self.yaw), base); base = mul(core.zm.rotationX(self.pitch), base); base = mul(core.zm.rotationZ(self.roll), base); - const pr2 = core.scene.SceneObjectPosRot{ - .position = position, + var posRot: core.scene.SceneObjectPosRot = .{ + .rotation = scene.getRotation(), + .position = scene.getPosition(), }; + posRot.rotation.quat[0] *= 1; + posRot.rotation.quat[1] *= -1; - self.worldTransform = mul(base, pr2.toTransform()); + self.worldTransform = mul(posRot.toTransform(), base); } // calculate viewProjections diff --git a/engine/rend/src/sgpu/renderer.zig b/engine/rend/src/sgpu/renderer.zig index 34eba6a..871b3d7 100644 --- a/engine/rend/src/sgpu/renderer.zig +++ b/engine/rend/src/sgpu/renderer.zig @@ -254,6 +254,11 @@ pub const Renderer = struct { self.meshPipe = self.device.createGPUGraphicsPipeline(&pci); } + pub fn preTick(self: *@This(), dt: f64) !void { + _ = self; + _ = dt; + } + pub fn createBuffers(self: *@This()) !void { // ssbo buffer @@ -384,10 +389,6 @@ pub const Renderer = struct { // self.device.unmapGPUTransferBuffer(self.colorBufferTransfer); - if (self.activeCamera) |camera| { - camera.resolve(); - } - const copyPass = cmd.beginGPUCopyPass(); // copyPass.uploadToGPUBuffer(&.{ .transfer_buffer = self.colorBufferTransfer, .offset = 0 }, &.{ // .buffer = self.colorBuffer, @@ -414,6 +415,10 @@ pub const Renderer = struct { pub fn tick(self: *@This(), dt: f64) void { self.totalTime += dt; + if (self.activeCamera) |camera| { + camera.resolve(); + } + const cmd = self.device.acquireGPUCommandBuffer(); self.frameUploads(); try self.uploadUniforms(cmd); diff --git a/projects/content/_shaders/dxil/lit_mesh.frag.dxil b/projects/content/_shaders/dxil/lit_mesh.frag.dxil index eecd278..96f27bf 100644 Binary files a/projects/content/_shaders/dxil/lit_mesh.frag.dxil and b/projects/content/_shaders/dxil/lit_mesh.frag.dxil differ diff --git a/projects/content/_shaders/msl/lit_mesh.frag.msl b/projects/content/_shaders/msl/lit_mesh.frag.msl index 3587446..5b1b5bd 100644 --- a/projects/content/_shaders/msl/lit_mesh.frag.msl +++ b/projects/content/_shaders/msl/lit_mesh.frag.msl @@ -25,48 +25,49 @@ struct main0_in fragment main0_out main0(main0_in in [[stage_in]], constant type_Uniforms& Uniforms [[buffer(0)]], texture2d Texture [[texture(0)]], sampler Sampler [[sampler(0)]]) { main0_out out = {}; - float4 _58 = Texture.sample(Sampler, in.in_var_TEXCOORD0); - float _59 = _58.w; - if (_59 < 0.00999999977648258209228515625) + float4 _59 = Texture.sample(Sampler, in.in_var_TEXCOORD0); + float _60 = _59.w; + if (_60 < 0.00999999977648258209228515625) { discard_fragment(); } - float3 _115; + float3 _118; do { - float3 _73 = Uniforms.lightPosition.xyz - in.in_var_TEXCOORD1; - float _74 = length(_73); - float _75 = _74 * _74; - float _76 = 1.0 / _75; - float _81; - if (_74 > 2.0) + float3 _74 = Uniforms.lightPosition.xyz - in.in_var_TEXCOORD1; + float _75 = length(_74); + float _76 = _75 * _75; + float _77 = 1.0 / _76; + float _82; + if (_75 > 2.0) { - _81 = 0.5 / _75; + _82 = 0.5 / _76; } else { - _81 = _76; + _82 = _77; } - float _86; - if (_74 > 4.0) + float _87; + if (_75 > 4.0) { - _86 = _81 * 0.5; + _87 = _82 * 0.5; } else { - _86 = _81; + _87 = _82; } - float _88 = (_74 > 5.0) ? 0.0 : _86; + float _89 = (_75 > 15.0) ? 0.0 : _87; + float _91 = (_89 > 1.0) ? 1.0 : _89; if (length(in.in_var_TEXCOORD2) < 0.100000001490116119384765625) { - _115 = ((in.in_var_TEXCOORD1 * float3(0.800000011920928955078125, 0.800000011920928955078125, 0.5)) * _76) * _88; + _118 = ((in.in_var_TEXCOORD1 * float3(0.800000011920928955078125, 0.800000011920928955078125, 0.5)) * _77) * _91; break; } - float3 _96 = fast::normalize(_73); - _115 = ((float3(0.800000011920928955078125, 0.800000011920928955078125, 0.5) * precise::max(dot(_96, in.in_var_TEXCOORD2), 0.0)) * _88) + (((float3(0.800000011920928955078125, 0.800000011920928955078125, 0.5) * powr(precise::max(dot(in.in_var_TEXCOORD2, fast::normalize(_96 + fast::normalize(in.in_var_TEXCOORD1 - Uniforms.viewPos.xyz))), 0.0), 30.0)) * 2.0) * _88); + float3 _99 = fast::normalize(_74); + _118 = ((float3(0.800000011920928955078125, 0.800000011920928955078125, 0.5) * precise::max(dot(_99, in.in_var_TEXCOORD2), 0.0)) * _91) + (((float3(0.800000011920928955078125, 0.800000011920928955078125, 0.5) * powr(precise::max(dot(in.in_var_TEXCOORD2, fast::normalize(_99 + fast::normalize(in.in_var_TEXCOORD1 - Uniforms.viewPos.xyz))), 0.0), 30.0)) * 2.0) * _91); break; } while(false); - out.out_var_SV_Target0 = float4(powr(_58.xyz * ((mix(float3(0.00999999977648258209228515625, 0.00999999977648258209228515625, 0.0030000000260770320892333984375), float3(0.0040000001899898052215576171875, 0.0040000001899898052215576171875, 0.0599999986588954925537109375), float3(in.in_var_TEXCOORD2.y)) * 0.100000001490116119384765625) + _115), float3(0.4545454680919647216796875)), _59); + out.out_var_SV_Target0 = float4(powr(_59.xyz * ((mix(float3(0.00999999977648258209228515625, 0.00999999977648258209228515625, 0.0030000000260770320892333984375), float3(0.0040000001899898052215576171875, 0.0040000001899898052215576171875, 0.0599999986588954925537109375), float3(in.in_var_TEXCOORD2.y)) * 0.001000000047497451305389404296875) + _118), float3(0.4545454680919647216796875)), _60); return out; } diff --git a/projects/content/_shaders/spv/lit_mesh.frag.spv b/projects/content/_shaders/spv/lit_mesh.frag.spv index ed95d7b..d93bfcc 100644 Binary files a/projects/content/_shaders/spv/lit_mesh.frag.spv and b/projects/content/_shaders/spv/lit_mesh.frag.spv differ diff --git a/projects/sampleGame/main.zig b/projects/sampleGame/main.zig index f31f764..5eedb7a 100644 --- a/projects/sampleGame/main.zig +++ b/projects/sampleGame/main.zig @@ -15,6 +15,8 @@ rotateAxisPitch: f32 = 0.0, sensitivity: f32 = 5.0, verticalMove: f32 = 0.0, +centerDist: f32 = 2.0, +centerDistMove: f32 = 2.0, yaw: f32 = 0.0, pitch: f32 = 0.0, @@ -142,7 +144,7 @@ pub fn prepare(self: *@This()) !void { i.addKey(.f, 1.0); i.addKey(.v, -1.0); - _ = i.data.addListener(self, cameraRotatePitch); + _ = i.data.addListener(self, distanceMove); i.activate(); } @@ -215,9 +217,9 @@ fn slowDown(ctx: ?*anyopaque, action: core.ActionEvent) void { } } -fn cameraRotatePitch(ctx: ?*anyopaque, axis: f32) void { +fn distanceMove(ctx: ?*anyopaque, axis: f32) void { const self: *@This() = @ptrCast(@alignCast(ctx)); - self.rotateAxisPitch = axis; + self.centerDistMove = axis; } fn cameraRotate(ctx: ?*anyopaque, axis: f32) void { @@ -264,19 +266,22 @@ pub fn tick(self: *@This(), dt: f64) void { self.yaw += core.radians(mouseMovement.x * self.sensitivity); self.pitch -= core.radians(mouseMovement.y * self.sensitivity); - self.cameraComponent.pitch = std.math.clamp(core.radians(self.pitch), core.radians(-90.0), core.radians(90.0)); + self.pitch = std.math.clamp(self.pitch, -90.0, 90.0); + self.cameraComponent.pitch = core.radians(self.pitch); posRot.rotation = core.Rotation.eulerY(core.radians(self.yaw)); const movement = posRot.rotation.rotateVector(vector.fmul(self.cameraSpeed * fdt)); posRot.position = posRot.position.add(movement); - const forward = posRot.rotation.forward(); - const debugCenter = posRot.position.add(forward.fmul(2)); + self.cameraComponent.resolve(); + self.centerDist += self.centerDistMove * fdt; + const forward = self.cameraComponent.forward(); //posRot.rotation.forward(); + const debugCenter = posRot.position.add(forward.fmul(self.centerDist)); core.debugSphere(debugCenter, 0.3, .{}); core.debugSphere(debugCenter, 0.1, .{ .color = .{ .x = 1.0 } }); - rend.context().lightPosition = posRot.position.add(.{ .y = -0.01 }); + rend.context().lightPosition = debugCenter; } var show: bool = true;