Skip to content

Commit

Permalink
CubeCamera.svelte Fix ESLint no-explicit-any warnings (#170)
Browse files Browse the repository at this point in the history
Also: polish `let` / `const` usage and variable types / type assertions
  • Loading branch information
Vatroslav Vrbanic committed Oct 4, 2022
1 parent 5e61dcb commit b009162
Showing 1 changed file with 20 additions and 22 deletions.
42 changes: 20 additions & 22 deletions src/lib/components/CubeCamera.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ Renders a `CubeMap` which can be used with **non-PBR** materials having an `.env
MeshBasicMaterial,
MeshLambertMaterial
} from "three"
import type { RemoveLast } from "../types/types-extra"
import type { RemoveLast, MeshAssignableMaterial } from "../types/types-extra"
import type { default as MeshSvelthreeComponent } from "./Mesh.svelte"
import type { default as Object3DSvelthreeComponent } from "./Object3D.svelte"
Expand Down Expand Up @@ -99,7 +99,8 @@ Renders a `CubeMap` which can be used with **non-PBR** materials having an `.env
* ☝️ The `pos` shorthand attribute will override `bind_pos`! _Alternatively (standard)_:
* add the `CubeCamera` component as a child to either a `Mesh` or an `Object3D`/`Group` component,
* in this case `CubeCamera`'s position will be bound to it's parent / object (three) instance. */
export let bind_pos: MeshSvelthreeComponent<any> | Object3DSvelthreeComponent | Object3D = undefined
export let bind_pos: MeshSvelthreeComponent<MeshAssignableMaterial> | Object3DSvelthreeComponent | Object3D =
undefined
$: if (camera && $svelthreeStores[sti].renderer && bind_pos && !bind_pos_offset && !dynamic) update_cubecam()
/** Adjust `CubeCamera`'s position by setting an offset relative to the pivot of the object specified by `bind_pos`. */
Expand All @@ -110,7 +111,8 @@ Renders a `CubeMap` which can be used with **non-PBR** materials having an `.env
* Default: `CubeCamera`'s parent component's object (three) instance will be hidden.
* -> ☝️ If you add `CubeCamera` as a direct child of a `Scene` component without specifying some other object / objects to be hidden,
* the **root scene** will be hidden during the 'envMap'-texture rendering and your 'envMap' texture will be blank! */
export let hide: (MeshSvelthreeComponent<any> | Object3DSvelthreeComponent | Object3D)[] = undefined
export let hide: (MeshSvelthreeComponent<MeshAssignableMaterial> | Object3DSvelthreeComponent | Object3D)[] =
undefined
/** Binds the texture generated by the `CubeCamera` to some `Mesh`-component's `.material`(_currently non PBR + has `.envMap`_).
* This is the opposite of / alternative to binding the material's `.envMap` property to `[CubeCamera component reference].texture` */
Expand Down Expand Up @@ -375,24 +377,24 @@ Renders a `CubeMap` which can be used with **non-PBR** materials having an `.env
// checking if their material has an '.envMap' property and is not a PBR material and then apply the the cubemap texture to those objects.
// But for now, this is not possible.
let bound_pos: typeof bind_pos = bind_pos || our_parent
let to_hide: typeof hide | typeof bind_pos = hide || bound_pos
let renderer: WebGLRenderer = $svelthreeStores[sti].renderer
let active_scene: Scene = $svelthreeStores[sti].activeScene
const bound_pos: typeof bind_pos = bind_pos || our_parent
const to_hide: typeof hide | typeof bind_pos = hide || bound_pos
const renderer: WebGLRenderer = $svelthreeStores[sti].renderer
const active_scene: Scene = $svelthreeStores[sti].activeScene
if (pos === undefined) {
// the floor hack -> see https://jsfiddle.net/3mprbLc9/
if (is_floor) {
let active_cam: Camera = $svelthreeStores[sti].activeCamera
let target_pos: Vector3 = get_cubecam_target_position(active_cam)
const active_cam: Camera = $svelthreeStores[sti].activeCamera
const target_pos: Vector3 = get_cubecam_target_position(active_cam)
camera.position.copy(target_pos)
// IMPORTANT GOOD this does NOT triggers all 'camera' bound reactive statements as opposed to `camera.position.y *= -1`!
camera.position.setY(camera.position.y * -1)
} else {
let target_pos: Vector3 = get_cubecam_target_position(bound_pos)
const target_pos: Vector3 = get_cubecam_target_position(bound_pos)
if (bind_pos_offset) {
let corrected_target_pos: Vector3 = target_pos.clone().add(bind_pos_offset)
const corrected_target_pos: Vector3 = target_pos.clone().add(bind_pos_offset)
camera.position.copy(corrected_target_pos)
} else {
camera.position.copy(target_pos)
Expand All @@ -407,16 +409,16 @@ Renders a `CubeMap` which can be used with **non-PBR** materials having an `.env
camera_updated = true
if (!bind_tex) {
let op: Mesh = bound_pos as Mesh
let op_mat: MaterialWithEnvMap = op.material as MaterialWithEnvMap
const op = bound_pos as Mesh
const op_mat = op.material as MaterialWithEnvMap
if (op_mat && Object.prototype.hasOwnProperty.call(op_mat, "envMap")) {
op_mat.envMap = camera.renderTarget.texture
}
}
}
function change_visibility(to_change: typeof hide | typeof bind_pos, val: boolean) {
let toc: typeof hide = to_change as typeof hide
const toc = to_change as typeof hide
if (toc.length && toc.length > 0) {
for (let i = 0; i < toc.length; i++) {
set_visibility(toc[i], val)
Expand All @@ -428,12 +430,10 @@ Renders a `CubeMap` which can be used with **non-PBR** materials having an `.env
function set_visibility(obj: typeof hide | typeof bind_pos, val: boolean) {
if (obj["is_svelthree_component"]) {
let o: MeshSvelthreeComponent<any> | Object3DSvelthreeComponent = obj as
| MeshSvelthreeComponent<any>
| Object3DSvelthreeComponent
const o = obj as MeshSvelthreeComponent<MeshAssignableMaterial> | Object3DSvelthreeComponent
o.get_instance().visible = val
} else {
let o = obj as Object3D
const o = obj as Object3D
o.visible = val
}
}
Expand All @@ -442,12 +442,10 @@ Renders a `CubeMap` which can be used with **non-PBR** materials having an `.env
let wp: Vector3 = new Vector3()
if (typeof obj["getWorldPosition"] === "function") {
let o = obj as Object3D
const o = obj as Object3D
o.getWorldPosition(wp)
} else {
let o: MeshSvelthreeComponent<any> | Object3DSvelthreeComponent = obj as
| MeshSvelthreeComponent<any>
| Object3DSvelthreeComponent
const o = obj as MeshSvelthreeComponent<MeshAssignableMaterial> | Object3DSvelthreeComponent
o.get_instance().getWorldPosition(wp)
}
Expand Down

0 comments on commit b009162

Please sign in to comment.