From ce935a04e6890b54585a1b273d40a0cc95332f33 Mon Sep 17 00:00:00 2001 From: RandomityGuy <31925790+RandomityGuy@users.noreply.github.com> Date: Sun, 6 Jun 2021 10:49:08 +0530 Subject: [PATCH] camera basic stuff --- src/CameraController.hx | 80 +++++++++++++++++++++++++++++++++++++---- src/Marble.hx | 15 +++++--- src/MarbleWorld.hx | 1 + src/Sky.hx | 13 +++++-- 4 files changed, 94 insertions(+), 15 deletions(-) diff --git a/src/CameraController.hx b/src/CameraController.hx index acc19d4e..5f2f9927 100644 --- a/src/CameraController.hx +++ b/src/CameraController.hx @@ -1,3 +1,13 @@ +package src; + +import sdl.Cursor; +import sdl.Sdl; +import hxd.Window; +import hxd.Event; +import src.MarbleWorld; +import h3d.scene.Object; +import src.Marble; +import h3d.Camera; import h3d.Vector; import hxsl.Types.Matrix; import h3d.scene.Scene; @@ -7,8 +17,9 @@ enum CameraMode { FixedOrbit; } -class CameraController { - var scene:Scene; +class CameraController extends Object { + var marble:Marble; + var level:MarbleWorld; var view:Matrix; var projection:Matrix; @@ -27,17 +38,72 @@ class CameraController { var camZoomSpeed:Float; - public var CameraDistance = 5; + public var CameraDistance = 2.5; public var CameraMinDistance = 1; public var CameraMaxDistance = 25; public var CameraPitch:Float; public var CameraYaw:Float; - public function new(scene:Scene) { - this.scene = scene; + public var phi:Float; + public var theta:Float; + + var screenHeight:Int; + var screenWidth:Int; + + public function new(marble:Marble) { + super(); + this.marble = marble; } - function createLookAt() { - this.view = Matrix.lookAtX(LookAtPoint.sub(Position), Up); + public function init(level:MarbleWorld) { + this.level = level; + Window.getInstance().addEventTarget(onEvent); + // level.scene.addEventListener(onEvent); + // Sdl.setRelativeMouseMode(true); + this.screenHeight = Sdl.getScreenHeight(); + this.screenWidth = Sdl.getScreenWidth(); + level.scene.camera.fovY = 60; + Cursor.show(false); + } + + function onEvent(e:Event) { + switch (e.kind) { + case EMove: + orbit(e.relX, e.relY); + Sdl.warpMouseGlobal(cast this.screenWidth / 2, cast this.screenHeight / 2); + default: + } + trace(e); + } + + function orbit(mouseX:Float, mouseY:Float) { + var window = Window.getInstance(); + var deltaposX = (window.width / 2) - mouseX; + var deltaposY = (window.height / 2) - mouseY; + var rotX = deltaposX * 0.001 * CameraSensitivity * Math.PI * 2; + var rotY = deltaposY * 0.001 * CameraSensitivity * Math.PI * 2; + CameraYaw -= rotX; + CameraPitch += rotY; + // CameraYaw = Math.PI / 2; + // CameraPitch = Math.PI / 4; + + if (CameraPitch > Math.PI) + CameraPitch = 3.141; + if (CameraPitch < 0) + CameraPitch = 0.001; + } + + public function update(dt:Float) { + var camera = level.scene.camera; + + var x = CameraDistance * Math.sin(CameraPitch) * Math.cos(CameraYaw); + var y = CameraDistance * Math.sin(CameraPitch) * Math.sin(CameraYaw); + var z = CameraDistance * Math.cos(CameraPitch); + + var targetpos = this.marble.getAbsPos().getPosition(); + this.x = targetpos.x + x; + this.y = targetpos.y + y; + this.z = targetpos.z + z; + camera.follow = {pos: this, target: this.marble}; } } diff --git a/src/Marble.hx b/src/Marble.hx index b650a713..1a454f12 100644 --- a/src/Marble.hx +++ b/src/Marble.hx @@ -17,11 +17,11 @@ import collision.CollisionWorld; import h3d.col.ObjectCollider; import h3d.col.Collider.GroupCollider; import h3d.Vector; -import h3d.scene.CameraController; import h3d.mat.Material; import h3d.scene.CustomObject; import h3d.prim.Sphere; import h3d.scene.Object; +import src.CameraController; class Move { public var d:Vector; @@ -33,6 +33,7 @@ class Move { class Marble extends Object { public var camera:CameraController; + public var cameraObject:Object; public var controllable:Bool = false; public var collider:SphereCollisionEntity; @@ -84,7 +85,7 @@ class Marble extends Object { this.velocity = new Vector(); this.omega = new Vector(); - this.camera = new CameraController(20); + this.camera = new CameraController(cast this); this.collider = new SphereCollisionEntity(cast this); } @@ -102,9 +103,9 @@ class Marble extends Object { function getMarbleAxis() { var cammat = Matrix.I(); var xrot = new Matrix(); - xrot.initRotationX(this.camera.phi); + xrot.initRotationX(this.camera.CameraPitch); var zrot = new Matrix(); - zrot.initRotationZ(this.camera.theta); + zrot.initRotationZ(this.camera.CameraYaw); cammat.multiply(xrot, zrot); var updir = gravityDir.multiply(-1); var motiondir = new Vector(cammat._21, cammat._22, cammat._23); @@ -604,6 +605,10 @@ class Marble extends Object { advancePhysics(currentTime, dt, move, collisionWorld, pathedInteriors); - this.camera.target.load(this.getAbsPos().getPosition().toPoint()); + if (this.controllable) { + this.camera.update(dt); + } + + // this.camera.target.load(this.getAbsPos().getPosition().toPoint()); } } diff --git a/src/MarbleWorld.hx b/src/MarbleWorld.hx index c52250f6..2a7bb3b8 100644 --- a/src/MarbleWorld.hx +++ b/src/MarbleWorld.hx @@ -75,6 +75,7 @@ class MarbleWorld { this.marbles.push(marble); marble.level = cast this; if (marble.controllable) { + marble.camera.init(cast this); this.scene.addChild(marble.camera); // Ugly hack sky.follow = marble; diff --git a/src/Sky.hx b/src/Sky.hx index 567d5c9d..2a755179 100644 --- a/src/Sky.hx +++ b/src/Sky.hx @@ -1,5 +1,6 @@ package src; +import h3d.scene.pbr.Environment; import src.Util; import src.MarbleWorld; import hxd.BitmapData; @@ -19,13 +20,19 @@ class Sky extends Object { public function init(level:MarbleWorld) { var texture = createSkyboxCubeTextured(this.dmlPath); - var sky = new h3d.prim.Sphere(300, 128, 128); + var sky = new h3d.prim.Sphere(1, 128, 128); sky.addNormals(); + sky.addUVs(); var skyMesh = new h3d.scene.Mesh(sky, this); skyMesh.material.mainPass.culling = Front; - skyMesh.material.mainPass.addShader(new h3d.shader.CubeMap(texture)); + skyMesh.material.mainPass.setPassName("overlay"); + skyMesh.scale(200); + var env = new Environment(texture); + env.compute(); + var renderer = cast(level.scene.renderer, h3d.scene.pbr.Renderer); + // renderer.env = env; + skyMesh.material.mainPass.addShader(new h3d.shader.pbr.CubeLod(texture)); skyMesh.material.shadows = false; - skyMesh.culled = false; } function createSkyboxCubeTextured(dmlPath:String) {