Skip to content

Commit

Permalink
fix theatre export, canvas
Browse files Browse the repository at this point in the history
  • Loading branch information
clementroche committed Jan 8, 2025
1 parent a066437 commit f124163
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 29 deletions.
6 changes: 4 additions & 2 deletions app/(pages)/(components)/wrapper/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,24 @@ import cn from 'clsx'
import type { LenisOptions } from 'lenis'
import { usePathname } from 'next/navigation'
import { useEffect } from 'react'
import { Canvas } from '~/libs/webgl/components/canvas'
import type { themeNames } from '~/styles/config.mjs'
import { Footer } from '../footer'
import { Lenis } from '../lenis'
import { Navigation } from '../navigation'
import s from './page-config.module.css'

interface WrapperProps extends React.HTMLAttributes<HTMLDivElement> {
theme?: (typeof themeNames)[number]
lenis?: LenisOptions
webgl?: boolean | object
}

export function Wrapper({
children,
theme = 'dark',
className,
lenis,
webgl,
...props
}: WrapperProps) {
const pathname = usePathname()
Expand All @@ -31,7 +33,7 @@ export function Wrapper({

return (
<>
<Navigation />
{webgl && <Canvas root {...(typeof webgl === 'object' && webgl)} />}
<main className={cn(s.main, className)} {...props}>
{children}
<script>
Expand Down
3 changes: 2 additions & 1 deletion biome.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@
"useValidAnchor": "warn"
},
"style": {
"noNonNullAssertion": "off"
"noNonNullAssertion": "off",
"noUnusedTemplateLiteral": "off"
}
}
},
Expand Down
9 changes: 5 additions & 4 deletions libs/theatre/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import {
useContext,
useEffect,
useImperativeHandle,
useLayoutEffect,
useMemo,
useRef,
useState,
Expand All @@ -31,14 +30,16 @@ export function TheatreProjectProvider({

useEffect(() => {
if (project) {
console.log(`theatre project ${id} loaded`)
isLoadingRef.current = false
window.THEATRE_PROJECT_ID = project.address.projectId
console.log(`Theatre: project ${id} loaded`)
}
}, [project, id])

useLayoutEffect(() => {
useEffect(() => {
if (config) {
if (!isLoadingRef.current) {
console.log(`theatre project ${id} loading...`)
console.log(`Theatre: project ${id} loading...`)
isLoadingRef.current = true
fetch(config)
.then((response) => response.json())
Expand Down
29 changes: 15 additions & 14 deletions libs/theatre/studio/index.tsx
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
import '@theatre/core'
'use client'

import studio from '@theatre/studio'
import { useEffect } from 'react'
import s from './studio.module.css'

let initialized = false

export function Studio() {
useEffect(() => {
if (initialized) return

studio.initialize()
function initializeStudio() {
// @ts-ignore
studio.initialize().then(() => {
studio.ui.restore()
console.log('Theatre: Studio initialized')
})
}

initialized = true
}, [])
initializeStudio()

export function Studio() {
useEffect(() => {
studio.ui.restore()
initializeStudio()

return () => {
studio.ui.hide()
Expand All @@ -27,9 +28,9 @@ export function Studio() {
<button
type="button"
onClick={() => {
const project = studio.getStudioProject()
const id = project.address.projectId
const json = studio.createContentOfSaveFile(id)
const id = window.THEATRE_PROJECT_ID

const json = studio.createContentOfSaveFile(window.THEATRE_PROJECT_ID)

const file = new File([JSON.stringify(json)], 'config.json', {
type: 'application/json',
Expand Down
9 changes: 6 additions & 3 deletions libs/webgl/components/canvas/webgl.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
'use client'

import { OrthographicCamera } from '@react-three/drei'
import { Canvas } from '@react-three/fiber'
import { SheetProvider } from '~/libs/theatre'
Expand All @@ -10,13 +12,14 @@ import s from './webgl.module.css'

type WebGLCanvasProps = React.HTMLAttributes<HTMLDivElement> & {
render?: boolean
postprocessing?: boolean
}

export function WebGLCanvas({
render = true,
postprocessing = true,
postprocessing = false,
...props
}) {
}: WebGLCanvasProps) {
const { WebGLTunnel, DOMTunnel } = useCanvas()

return (
Expand Down Expand Up @@ -50,7 +53,7 @@ export function WebGLCanvas({
/>
<RAF render={render} />
<FlowmapProvider>
<PostProcessing />
{postprocessing && <PostProcessing />}
<WebGLTunnel.Out />
</FlowmapProvider>
<Preload />
Expand Down
2 changes: 2 additions & 0 deletions libs/webgl/components/postprocessing/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import { useEffect, useMemo } from 'react'
import { HalfFloatType } from 'three'

export function PostProcessing() {
console.log('WebGL: PostProcessing enabled')

const gl = useThree((state) => state.gl)
const viewport = useThree((state) => state.viewport)
const camera = useThree((state) => state.camera)
Expand Down
12 changes: 7 additions & 5 deletions libs/webgl/components/preload/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@ export function Preload() {
async () => {
if (active) return

console.log('Preloading...')
console.log('WebGL: Preloading...')

console.time('WebGL: Preload took:')

const invisible: THREE.Object3D[] = []
scene.traverse((object: any) => {
scene.traverse((object: THREE.Object3D) => {
if (object.visible === false) {
invisible.push(object)
object.visible = true
Expand All @@ -28,16 +30,16 @@ export function Preload() {
await gl.compileAsync(scene, camera)
const cubeRenderTarget = new WebGLCubeRenderTarget(128)
const cubeCamera = new CubeCamera(0.01, 100000, cubeRenderTarget)
cubeCamera.update(gl as any, scene as any)
cubeCamera.update(gl as THREE.WebGLRenderer, scene as THREE.Scene)
cubeRenderTarget.dispose()

for (const object of invisible) {
object.visible = false
}

console.timeEnd('Preload')
console.timeEnd('WebGL: Preload took:')
},
500,
1000,
[active, gl, camera, scene]
)

Expand Down
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"compilerOptions": {
"target": "ES2017",
"lib": ["dom", "dom.iterable", "esnext"],
"checkJs": true,
"allowJs": true,
"skipLibCheck": true,
"strict": true,
Expand Down

1 comment on commit f124163

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚡️ Lighthouse report for the changes in this commit:

🟠 Performance: 88
🟠 Accessibility: 89
🟢 Best practices: 100
🟠 SEO: 63

Lighthouse ran on https://satus-6vy7nfyzt-darkroom-engineering.vercel.app/

Please sign in to comment.