Skip to content

Commit

Permalink
1.7.1 - Initial Plugin System (#63)
Browse files Browse the repository at this point in the history
* checking in working state hey pixel interface

* working text block generationg

* fix the fallbacks

* pro

* adds pro mirror block

* add basic edit component

* adds transform controls to edit component

* add editor plugin provider

* add plugin system

* checking in working plugin system but broken render

* dont lose the nights work

* working editor state mirror block

* standardize the id object

* check in nights work

* working context bridge

* build commands

* adds build script

* build directory

* fixes example mirror block. working state.

* add empty build directories

* add empty build directories

* add ignore and keeps for build folders so script can build on a fresh pull

* remove old file

* adds ignore for the pro folder

* remove pro

* remove pro folder

* set block object

* remove pro cruft

* remove more pro cruft

* remove messy comments

* adjusts readme

* tested up to

* fixes static method
  • Loading branch information
antpb authored Oct 19, 2023
1 parent 0178a33 commit 907f8d6
Show file tree
Hide file tree
Showing 22 changed files with 515 additions and 124 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,9 @@ vendor/
vendor
wordpress
build
pro/
.phpunit.result.cache
plugin-build/pro/three-object-viewer/*
!plugin-build/pro/three-object-viewer/.gitkeep
plugin-build/free/three-object-viewer/*
!plugin-build/free/three-object-viewer/.gitkeep
2 changes: 1 addition & 1 deletion admin/three-object-viewer-settings/init.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ function () {
echo '<div id="three-object-viewer-settings"></div>';
}
);
});
}, 9);

function three_encrypt($value = ""){
if( empty( $value ) ) {
Expand Down
47 changes: 25 additions & 22 deletions blocks/environment/Edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,10 @@ import {
import { Icon, moveTo, more, rotateLeft, resizeCornerNE } from "@wordpress/icons";
import * as THREE from "three";
import defaultEnvironment from "../../inc/assets/default_grid.glb";

import ThreeObjectEdit from "./components/ThreeObjectEdit";
import { EditorPluginProvider, useEditorPlugins, EditorPluginContext } from './components/EditorPluginProvider'; // Import the PluginProvider

export default function Edit({ attributes, setAttributes, isSelected }) {

const ALLOWED_BLOCKS = allowed_blocks;
const [focusPosition, setFocusPosition] = useState(new THREE.Vector3());
const [focusPoint, setFocus] = useState(new THREE.Vector3());
Expand Down Expand Up @@ -299,26 +298,30 @@ export default function Edit({ attributes, setAttributes, isSelected }) {
/>
</div>
{mainModel && (
<ThreeObjectEdit
url={mainModel}
hdr={attributes.hdr}
deviceTarget={attributes.deviceTarget}
backgroundColor={attributes.bg_color}
zoom={attributes.zoom}
scale={attributes.scale}
hasZoom={attributes.hasZoom}
hasTip={attributes.hasTip}
positionX={attributes.positionX}
positionY={attributes.positionY}
animations={attributes.animations}
rotationY={attributes.rotationY}
setFocusPosition={setFocusPosition}
setFocus={setFocus}
changeFocusPoint={changeFocusPoint}
focusPosition={focusPosition}
focusPoint={focusPoint}
selected={isSelected}
/>
<>
<EditorPluginProvider>
<ThreeObjectEdit
url={mainModel}
hdr={attributes.hdr}
deviceTarget={attributes.deviceTarget}
backgroundColor={attributes.bg_color}
zoom={attributes.zoom}
scale={attributes.scale}
hasZoom={attributes.hasZoom}
hasTip={attributes.hasTip}
positionX={attributes.positionX}
positionY={attributes.positionY}
animations={attributes.animations}
rotationY={attributes.rotationY}
setFocusPosition={setFocusPosition}
setFocus={setFocus}
changeFocusPoint={changeFocusPoint}
focusPosition={focusPosition}
focusPoint={focusPoint}
selected={isSelected}
/>
</EditorPluginProvider>
</>
)}
</>
</div>
Expand Down
44 changes: 44 additions & 0 deletions blocks/environment/components/ContextBridgeComponent.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import React, { useState, useEffect } from "react";
import { useFrontPlugins, FrontPluginContext } from './FrontPluginProvider';
import { useContextBridge } from "@react-three/drei";

//import contextBridgef
// add function for context
export function ContextBridgeComponent(props) {
const { plugins } = useFrontPlugins(); // From your own context
const [registeredThreeovBlocks, setRegisteredThreeovBlocks] = useState([]);
const ContextBridge = useContextBridge(FrontPluginContext);

useEffect(() => {
if (plugins.length > 0) {
plugins.forEach((plugin) => {
// add the plugin to the registered blocks
setRegisteredThreeovBlocks((registeredThreeovBlocks) => [
...registeredThreeovBlocks,
plugin,
]);
});
}
}, [plugins]);

return (
<ContextBridge>
{
registeredThreeovBlocks.length > 0 && registeredThreeovBlocks.map((blockElement, index) => {
const BlockComponent = blockElement.type;
return (
<group
key={index}
position={[0, 0, 0]}
rotation={[0, 0, 0]}
scale={[1, 1, 1]}
>
<BlockComponent key={index} {...blockElement.props} />
</group>
)
})
}
</ContextBridge>
)

}
31 changes: 31 additions & 0 deletions blocks/environment/components/EditorPluginProvider.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import React, { useState, useContext, useEffect, useCallback } from "react";

export const EditorPluginContext = React.createContext();

export function EditorPluginProvider({ children }) {

const [plugins, setPlugins] = useState([]);

const registerEditorPlugin = useCallback((plugin) => {
setPlugins(prevPlugins => [...prevPlugins, plugin]);
}, []);

useEffect(() => {
// Expose the registerPlugin method globally
window.registerEditorPlugin = registerEditorPlugin;
window.dispatchEvent(new Event('registerEditorPluginReady'));

return () => {
// Cleanup
window.registerEditorPlugin = null;
};
}, [registerEditorPlugin]);

return (
<EditorPluginContext.Provider value={{ plugins, registerEditorPlugin }}>
{children}
</EditorPluginContext.Provider>
);
}

export const useEditorPlugins = () => useContext(EditorPluginContext);
25 changes: 9 additions & 16 deletions blocks/environment/components/EnvironmentFront.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import * as THREE from "three";
import { Fog } from 'three/src/scenes/Fog'
// import { Reflector } from 'three/examples/jsm/objects/Reflector';
import React, { Suspense, useRef, useState, useEffect, useMemo } from "react";
import { useLoader, useThree, useFrame, Canvas } from "@react-three/fiber";
import { GLTFLoader } from "three/examples/jsm/loaders/GLTFLoader";
Expand All @@ -14,7 +13,8 @@ import axios from "axios";
import ReactNipple from 'react-nipple';
import ScrollableFeed from 'react-scrollable-feed'
import { Resizable } from "re-resizable";
import { Environment } from "@react-three/drei";
import { Environment, useContextBridge } from "@react-three/drei";
import { FrontPluginProvider, FrontPluginContext } from './FrontPluginProvider'; // Import the PluginProvider

import {
useAnimations,
Expand Down Expand Up @@ -44,6 +44,7 @@ import { Portal } from "./core/front/Portal";
import { ThreeSky } from "./core/front/ThreeSky";
import { TextObject } from "./core/front/TextObject";
import { useKeyboardControls } from "./Controls";
import { ContextBridgeComponent } from "./ContextBridgeComponent";

function isMobile() {
return /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent);
Expand Down Expand Up @@ -509,17 +510,6 @@ function SavedObject(props) {
}
});

// Mirror logic.
// const mirror = new Reflector(
// new THREE.PlaneGeometry(10, 10),
// {
// color: new THREE.Color(0x7f7f7f),
// textureWidth: window.innerWidth * window.devicePixelRatio,
// textureHeight: window.innerHeight * window.devicePixelRatio
// }
// )
// gltf.scene.add(mirror);

meshesToAdd.forEach((mesh) => {
meshesScene.attach(mesh);
});
Expand Down Expand Up @@ -670,19 +660,19 @@ export default function EnvironmentFront(props) {
const [messageObject, setMessageObject] = useState({"tone": "happy", "message": "hello!"});
const [objectsInRoom, setObjectsInRoom] = useState([]);
const [url, setURL] = useState(props.threeUrl ? props.threeUrl : (threeObjectPlugin + defaultEnvironment));

if (loaded === true) {
// find the element that contains the text "WEBXR NOT AVAILABLE" and hide it
// set an element const that selects the body of the document
const elements = document.body.getElementsByTagName('*');
const webXRNotAvail = Array.from(elements).find((el) => el.textContent === 'WEBXR NOT AVAILABLE');
if (webXRNotAvail) {
webXRNotAvail.style.display = "none";
}

if (props.deviceTarget === "vr") {
return (
<>
<VRCanvas
resize={{ scroll: false }}
camera={{
fov: 70,
zoom: 1,
Expand All @@ -702,6 +692,7 @@ export default function EnvironmentFront(props) {
zIndex: 1
}}
>
<FrontPluginProvider>
{ isVRCompatible() && <XRButton mode={'VR' | 'inline'}/>}
{/* <Perf className="stats" /> */}
{/* <fog attach="fog" color="hotpink" near={100} far={20} /> */}
Expand All @@ -715,6 +706,7 @@ export default function EnvironmentFront(props) {
background
/>
}
<ContextBridgeComponent/>
<Physics
// debug
>
Expand Down Expand Up @@ -1913,6 +1905,7 @@ export default function EnvironmentFront(props) {
{/* <OrbitControls
enableZoom={ true }
/> */}
</FrontPluginProvider>
</VRCanvas>
{Object.values(
props.npcsToAdd
Expand Down
33 changes: 33 additions & 0 deletions blocks/environment/components/FrontPluginProvider.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import React, { createContext, useContext, useState, useEffect, useCallback } from "react";
import { useThree } from '@react-three/fiber';

export const FrontPluginContext = React.createContext();

export function FrontPluginProvider({ children }) {

const [plugins, setPlugins] = useState([]);
const { scene, camera } = useThree();

const registerFrontPlugin = useCallback((plugin) => {
setPlugins(prevPlugins => [...prevPlugins, plugin]);
}, []);

useEffect(() => {
// Expose the registerPlugin method globally
window.registerFrontPlugin = registerFrontPlugin;
window.dispatchEvent(new Event('registerFrontPluginReady'));

return () => {
// Cleanup
window.registerFrontPlugin = null;
};
}, [registerFrontPlugin]);

return (
<FrontPluginContext.Provider value={{ plugins, registerFrontPlugin, scene, camera }}>
{children}
</FrontPluginContext.Provider>
);
}

export const useFrontPlugins = () => useContext(FrontPluginContext);
2 changes: 1 addition & 1 deletion blocks/environment/components/Player.js
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ export default function Player(props) {
setTimeout(() => {
fetchProfile()
.then((response) => {
// You can handle the response here if needed
// handle the response here if needed
})
.catch((err) => {
// Handle the error here if needed
Expand Down
Loading

0 comments on commit 907f8d6

Please sign in to comment.