fixed the rest of my mouse and camera attachment issues

This commit is contained in:
Peter Li 2025-04-25 00:09:14 -07:00
parent c66dbaaab6
commit 126b4f978d
7 changed files with 61 additions and 38 deletions

View File

@ -13,7 +13,7 @@ float3 BlinnPhong(float3 normal, float3 fragPos, float3 lightPos, float3 lightCo
{ {
float dist = length(lightPos - fragPos); float dist = length(lightPos - fragPos);
// attenuation // attenuation
float maxDist = 5; float maxDist = 15;
float cutoff1 = 2; float cutoff1 = 2;
float cutoff2 = 4; float cutoff2 = 4;
float cutoffFactor = 0.5; float cutoffFactor = 0.5;
@ -31,6 +31,11 @@ float3 BlinnPhong(float3 normal, float3 fragPos, float3 lightPos, float3 lightCo
{ {
attenuation = 0; attenuation = 0;
} }
if (attenuation > 1.0)
{
attenuation = 1.0;
}
if(length(normal) < 0.1) if(length(normal) < 0.1)
{ {
return fragPos * lightColor * (1 / (dist * dist)) * attenuation; return fragPos * lightColor * (1 / (dist * dist)) * attenuation;
@ -76,7 +81,7 @@ float4 main(
float3 lightPos = lightPosition.xyz; //+ float3(0, 20, 0); float3 lightPos = lightPosition.xyz; //+ float3(0, 20, 0);
float3 lightColor = float3(0.8, 0.8, 0.5); 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; float3 col = s.xyz;

View File

@ -33,6 +33,10 @@ pub fn initECS(self: *@This(), handle: core.SetHandle) void {
self.entity = core.Entity.fromHandle(handle); 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 { pub fn updateProjections(self: *@This()) void {
self.projection = zm.perspectiveFovLh(core.radians(self.fov), 16.0 / 9.0, 0.1, 200000); self.projection = zm.perspectiveFovLh(core.radians(self.fov), 16.0 / 9.0, 0.1, 200000);
// self.projection[1][1] *= -1; // self.projection[1][1] *= -1;
@ -60,15 +64,18 @@ pub fn resolve(self: *@This()) void {
position.x = -position.x; position.x = -position.x;
{ {
var base = core.zm.identity(); 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.rotationX(self.pitch), base);
base = mul(core.zm.rotationZ(self.roll), base); base = mul(core.zm.rotationZ(self.roll), base);
const pr2 = core.scene.SceneObjectPosRot{ var posRot: core.scene.SceneObjectPosRot = .{
.position = position, .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 // calculate viewProjections

View File

@ -254,6 +254,11 @@ pub const Renderer = struct {
self.meshPipe = self.device.createGPUGraphicsPipeline(&pci); self.meshPipe = self.device.createGPUGraphicsPipeline(&pci);
} }
pub fn preTick(self: *@This(), dt: f64) !void {
_ = self;
_ = dt;
}
pub fn createBuffers(self: *@This()) !void { pub fn createBuffers(self: *@This()) !void {
// ssbo buffer // ssbo buffer
@ -384,10 +389,6 @@ pub const Renderer = struct {
// self.device.unmapGPUTransferBuffer(self.colorBufferTransfer); // self.device.unmapGPUTransferBuffer(self.colorBufferTransfer);
if (self.activeCamera) |camera| {
camera.resolve();
}
const copyPass = cmd.beginGPUCopyPass(); const copyPass = cmd.beginGPUCopyPass();
// copyPass.uploadToGPUBuffer(&.{ .transfer_buffer = self.colorBufferTransfer, .offset = 0 }, &.{ // copyPass.uploadToGPUBuffer(&.{ .transfer_buffer = self.colorBufferTransfer, .offset = 0 }, &.{
// .buffer = self.colorBuffer, // .buffer = self.colorBuffer,
@ -414,6 +415,10 @@ pub const Renderer = struct {
pub fn tick(self: *@This(), dt: f64) void { pub fn tick(self: *@This(), dt: f64) void {
self.totalTime += dt; self.totalTime += dt;
if (self.activeCamera) |camera| {
camera.resolve();
}
const cmd = self.device.acquireGPUCommandBuffer(); const cmd = self.device.acquireGPUCommandBuffer();
self.frameUploads(); self.frameUploads();
try self.uploadUniforms(cmd); try self.uploadUniforms(cmd);

View File

@ -25,48 +25,49 @@ struct main0_in
fragment main0_out main0(main0_in in [[stage_in]], constant type_Uniforms& Uniforms [[buffer(0)]], texture2d<float> Texture [[texture(0)]], sampler Sampler [[sampler(0)]]) fragment main0_out main0(main0_in in [[stage_in]], constant type_Uniforms& Uniforms [[buffer(0)]], texture2d<float> Texture [[texture(0)]], sampler Sampler [[sampler(0)]])
{ {
main0_out out = {}; main0_out out = {};
float4 _58 = Texture.sample(Sampler, in.in_var_TEXCOORD0); float4 _59 = Texture.sample(Sampler, in.in_var_TEXCOORD0);
float _59 = _58.w; float _60 = _59.w;
if (_59 < 0.00999999977648258209228515625) if (_60 < 0.00999999977648258209228515625)
{ {
discard_fragment(); discard_fragment();
} }
float3 _115; float3 _118;
do do
{ {
float3 _73 = Uniforms.lightPosition.xyz - in.in_var_TEXCOORD1; float3 _74 = Uniforms.lightPosition.xyz - in.in_var_TEXCOORD1;
float _74 = length(_73); float _75 = length(_74);
float _75 = _74 * _74; float _76 = _75 * _75;
float _76 = 1.0 / _75; float _77 = 1.0 / _76;
float _81; float _82;
if (_74 > 2.0) if (_75 > 2.0)
{ {
_81 = 0.5 / _75; _82 = 0.5 / _76;
} }
else else
{ {
_81 = _76; _82 = _77;
} }
float _86; float _87;
if (_74 > 4.0) if (_75 > 4.0)
{ {
_86 = _81 * 0.5; _87 = _82 * 0.5;
} }
else 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) 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; break;
} }
float3 _96 = fast::normalize(_73); float3 _99 = fast::normalize(_74);
_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); _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; break;
} while(false); } 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; return out;
} }

View File

@ -15,6 +15,8 @@ rotateAxisPitch: f32 = 0.0,
sensitivity: f32 = 5.0, sensitivity: f32 = 5.0,
verticalMove: f32 = 0.0, verticalMove: f32 = 0.0,
centerDist: f32 = 2.0,
centerDistMove: f32 = 2.0,
yaw: f32 = 0.0, yaw: f32 = 0.0,
pitch: f32 = 0.0, pitch: f32 = 0.0,
@ -142,7 +144,7 @@ pub fn prepare(self: *@This()) !void {
i.addKey(.f, 1.0); i.addKey(.f, 1.0);
i.addKey(.v, -1.0); i.addKey(.v, -1.0);
_ = i.data.addListener(self, cameraRotatePitch); _ = i.data.addListener(self, distanceMove);
i.activate(); 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)); const self: *@This() = @ptrCast(@alignCast(ctx));
self.rotateAxisPitch = axis; self.centerDistMove = axis;
} }
fn cameraRotate(ctx: ?*anyopaque, axis: f32) void { 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.yaw += core.radians(mouseMovement.x * self.sensitivity);
self.pitch -= core.radians(mouseMovement.y * 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)); posRot.rotation = core.Rotation.eulerY(core.radians(self.yaw));
const movement = posRot.rotation.rotateVector(vector.fmul(self.cameraSpeed * fdt)); const movement = posRot.rotation.rotateVector(vector.fmul(self.cameraSpeed * fdt));
posRot.position = posRot.position.add(movement); posRot.position = posRot.position.add(movement);
const forward = posRot.rotation.forward(); self.cameraComponent.resolve();
const debugCenter = posRot.position.add(forward.fmul(2)); 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.3, .{});
core.debugSphere(debugCenter, 0.1, .{ .color = .{ .x = 1.0 } }); 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; var show: bool = true;