Skip to content

Commit

Permalink
feat: implemention & refactor of threex
Browse files Browse the repository at this point in the history
### Description

- `threex`'s structure improvement | From @jeromeetienne

Co-Authored-By: Jerome Etienne <[email protected]>
  • Loading branch information
Neosoulink and jeromeetienne committed Jan 6, 2024
1 parent dc72475 commit 662cf71
Show file tree
Hide file tree
Showing 11 changed files with 466 additions and 9 deletions.
Binary file modified src/assets/models/scene_3/model.glb
Binary file not shown.
2 changes: 1 addition & 1 deletion src/experiences/home/navigation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -463,7 +463,7 @@ export class Navigation extends ExperienceBasedBlueprint {
this._config.smallestSide;
}

if (!this.timeline.isActive()) {
if (this.timeline.isActive()) {
// Apply limits
if (this._view.spherical.limits.enabled)
this._view.spherical.value.radius = Math.min(
Expand Down
3 changes: 3 additions & 0 deletions src/experiences/home/world/manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,9 @@ export class WorldManager extends ExperienceBasedBlueprint {
});
});
},
onComplete: () => {
CURRENT_SCENE.intro();
},
});
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/experiences/home/world/scene-1.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -392,12 +392,12 @@ export class Scene1Component extends SceneComponentBlueprint {
*/
public togglePcOpening(state?: 0 | 1) {
if (!this._model || !this.modelScene || !this.pcTopArticulation) return;
const isOpening =
const isOpen =
this.pcTopArticulation.rotation.z ===
this._initialPcTopBone?.rotation.z || state === 1;

return this.timeline.to(this.pcTopArticulation.rotation, {
z: isOpening
z: isOpen
? this.pcTopArticulation.rotation.z + 2.1
: this._initialPcTopBone?.rotation.z ?? 0,
duration: Config.GSAP_ANIMATION_DURATION,
Expand Down
90 changes: 84 additions & 6 deletions src/experiences/home/world/scene-3.component.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,15 @@
import { Object3D, type Object3DEventMap, MeshBasicMaterial } from "three";
import {
Object3D,
type Object3DEventMap,
MeshBasicMaterial,
PerspectiveCamera,
} from "three";
import gsap from "gsap";
import THREEx from "~/lib/threex";

import type { HtmlMixerContext, HtmlMixerPlane } from "~/lib/threex/html-mixer";
// CONFIG
import { Config } from "~/config";

// BLUEPRINTS
import { SceneComponentBlueprint } from "~/blueprints/experiences/scene-component.blueprint";
Expand All @@ -7,8 +18,12 @@ import { SceneComponentBlueprint } from "~/blueprints/experiences/scene-componen
import type { Materials } from "~/common/experiences/experience-world.model";

export class Scene3Component extends SceneComponentBlueprint {
private _initialPcTopBone?: Object3D<Object3DEventMap>;
private _appRenderer = this._experience.app.renderer;
private _initialPcTopArticulation?: Object3D<Object3DEventMap>;
private _mixerContext?: HtmlMixerContext;
private _mixerPlane?: HtmlMixerPlane;

public readonly timeline = gsap.timeline();
public readonly navigationLimits = {
spherical: {
radius: { min: 4, max: 8 },
Expand All @@ -26,7 +41,7 @@ export class Scene3Component extends SceneComponentBlueprint {
},
};

public pcTopBone?: Object3D<Object3DEventMap>;
public pcTopArticulation?: Object3D<Object3DEventMap>;

constructor() {
try {
Expand All @@ -52,11 +67,11 @@ export class Scene3Component extends SceneComponentBlueprint {
}

private _setPcTopBone(item: Object3D<Object3DEventMap>) {
if (!(item instanceof Object3D) || item.name !== "pc_top_articulation")
if (!(item instanceof Object3D) || item.name !== "pc_top_articulation_2")
return;

this._initialPcTopBone = item.clone();
this.pcTopBone = item;
this._initialPcTopArticulation = item.clone();
this.pcTopArticulation = item;
}

protected _getAvailableMaterials() {
Expand Down Expand Up @@ -84,4 +99,67 @@ export class Scene3Component extends SceneComponentBlueprint {

return AVAILABLE_MATERIALS;
}

/**
* Toggle the state of the pc between open and close
*
* @param state Force the state of the pc (0 = "close" & 1 = "open")
* @returns
*/
public togglePcOpening(state?: 0 | 1) {
if (!this._model || !this.modelScene || !this.pcTopArticulation) return;
const isOpen =
this.pcTopArticulation.rotation.z ===
this._initialPcTopArticulation?.rotation.z || state === 1;

return this.timeline.to(this.pcTopArticulation.rotation, {
z: isOpen
? this.pcTopArticulation.rotation.z + 1.7
: this._initialPcTopArticulation?.rotation.z ?? 0,
duration: Config.GSAP_ANIMATION_DURATION,
onUpdate: () => {},
onComplete: () => {},
});
}

public construct(): void {
super.construct();

//
this._mixerContext = new THREEx.htmlMixer.HtmlMixerContext(
this._appRenderer.instance,
this._appCamera.instance as PerspectiveCamera
);
const rendererCss = this._mixerContext.rendererCss;
rendererCss.setSize(window.innerWidth, window.innerHeight);

//
const domElement = document.createElement("iframe");
domElement.src = "http://threejs.org/";
domElement.style.border = "none";
this._mixerPlane = new THREEx.htmlMixer.HtmlMixerPlane(
this._mixerContext,
domElement
);
this._mixerPlane.object3d.position.y += 3;
this._mixerPlane.object3d.scale.multiplyScalar(2);

//
const css3dElement = rendererCss.domElement;
css3dElement.style.position = "absolute";
css3dElement.style.top = "0px";

document.querySelector("#css")?.appendChild(css3dElement);
this._experience.app.scene?.add(this._mixerPlane.object3d);

this._experience.renderer?.addBeforeRenderUpdateCallBack("TT", () => {
this._mixerContext?.update();
});
}

public intro(): void {
this.togglePcOpening();
}

public update() {}
}
20 changes: 20 additions & 0 deletions src/lib/threex/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
The MIT License (MIT)

Copyright (c) 2013 Jerome Etienne

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
91 changes: 91 additions & 0 deletions src/lib/threex/html-mixer-helper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
import { HtmlMixerPlane } from "./html-mixer";
import { HtmlMultipleMixerPlane } from "./html-multiple-mixer";

export const isIOS = () => {
var iosQuirkPresent = function () {
var audio = new Audio();

audio.volume = 0.5;
return audio.volume === 1; // volume cannot be changed from "1" on iOS 12 and below
};

var isIOS = /iPad|iPhone|iPod/.test(navigator.userAgent);
var isAppleDevice = navigator.userAgent.includes("Macintosh");
var isTouchScreen = navigator.maxTouchPoints >= 1; // true for iOS 13 (and hopefully beyond)

return isIOS || (isAppleDevice && (isTouchScreen || iosQuirkPresent()));
};

/**
* create domElement for a iframe to insert in a THREEx.HtmlmixedPlane
*
* @param {String} url the url for the iframe
*/
export const createIframeDomElement = (
url: string
): HTMLDivElement | HTMLIFrameElement => {
// create the iframe element
let domElement = document.createElement("iframe");
domElement.src = url;
domElement.style.border = "none";

// TODO:: Ensure the IOS (and other mobile devices) compatibility
// IOS workaround for iframe
if (isIOS()) {
// - see the following post for explanation on this workaround
// - http://dev.magnolia-cms.com/blog/2012/05/strategies-for-the-iframe-on-the-ipad-problem/
domElement.style.width = "100%";
domElement.style.height = "100%";

let container = document.createElement("div");

container.appendChild(domElement);
container.style.overflow = "scroll";

return container;
}

return domElement;
};

/**
* Set the iframe.src in a mixerPlane.
* - Useful as it handle IOS specificities
*/
export const setIframeSrc = (
mixerPlane: HtmlMultipleMixerPlane | HtmlMixerPlane,
url: string
) => {
// handle THREEx.HtmlMultipleMixer.Plane
if (mixerPlane instanceof HtmlMultipleMixerPlane) {
mixerPlane.planes.forEach(function (plane) {
setIframeSrc(plane, url);
});
return;
}

// Sanity check
console.assert(mixerPlane instanceof HtmlMixerPlane);
// Get the domElement
let domElement = mixerPlane.domElement;
// Handle IOS special case
if (isIOS() && mixerPlane.domElement.firstChild instanceof HTMLElement)
domElement = mixerPlane.domElement.firstChild;

// sanity check
console.assert(domElement instanceof HTMLIFrameElement);
if (domElement instanceof HTMLIFrameElement)
// actually set the iframe.src
domElement.src = url;
};

/**
* Create domElement for a image to insert in a THREEx.HtmlmixedPlane
*
* @param {String} url the url for the iframe
*/
export const createImageDomElement = (url: string) => {
let domElement = document.createElement("img");
domElement.src = url;
return domElement;
};
Loading

0 comments on commit 662cf71

Please sign in to comment.