Skip to content

fix: nodeOps is now a function #579

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
97 changes: 0 additions & 97 deletions playground/src/components/MultipleCanvas.vue

This file was deleted.

81 changes: 77 additions & 4 deletions playground/src/pages/multiple.vue
Original file line number Diff line number Diff line change
@@ -1,7 +1,80 @@
<script setup lang="ts"></script>
<script setup lang="ts">
import { TresCanvas, useRenderLoop } from '@tresjs/core'
import { reactive, shallowRef, ref } from 'vue'
import { BasicShadowMap, SRGBColorSpace, NoToneMapping } from 'three'
import { OrbitControls } from '@tresjs/cientos'

const state = reactive({
clearColor: '#82DBC5',
shadows: true,
alpha: false,
shadowMapType: BasicShadowMap,
outputColorSpace: SRGBColorSpace,
toneMapping: NoToneMapping,
})

const { onLoop } = useRenderLoop()

const boxRef = shallowRef(null)
const showBox = ref(true)

/* onLoop(({ elapsed }) => {
if (boxRef.value) {
boxRef.value.rotation.y = elapsed
boxRef.value.rotation.z = elapsed
}
}) */

setInterval(() => {
showBox.value = !showBox.value
}, 3000)
</script>

<template>
<Suspense>
<MultipleCanvas />
</Suspense>
<div class="grid grid-cols-2">
<div class="aspect-video">
<TresCanvas clear-color="#fff">
<TresPerspectiveCamera
:position="[5, 5, 5]"
:look-at="[0, 0, 0]"
/>

<TresAmbientLight
:intensity="0.5"
color="red"
/>
<TresMesh
v-if="showBox"
ref="boxRef"
:position="[0, 2, 0]"
>
<TresBoxGeometry :args="[1, 1, 1]" />
<TresMeshNormalMaterial />
</TresMesh>
<TresDirectionalLight
:position="[0, 2, 4]"
:intensity="1"
cast-shadow
/>
<TresAxesHelper />
<TresGridHelper :args="[10, 10, 0x444444, 'teal']" />
</TresCanvas>
</div>
<div class="aspect-video">
<TresCanvas clear-color="#000">
<TresPerspectiveCamera
:position="[5, 5, 5]"
:look-at="[0, 0, 0]"
/>
<TresMesh>
<TresSphereGeometry :args="[1, 32, 32]" />
<TresMeshNormalMaterial />
</TresMesh>
<TresAmbientLight
:intensity="0.5"
color="red"
/>
</TresCanvas>
</div>
</div>
</template>
11 changes: 8 additions & 3 deletions src/components/TresCanvas.vue
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import type {
ShadowMapType,
ToneMapping,
} from 'three'
import type { Ref,
App } from 'vue'
import * as THREE from 'three'
import type { Ref, App } from 'vue'
import {
computed,
onMounted,
Expand All @@ -20,6 +20,7 @@ import {
defineComponent,
h,
getCurrentInstance,
createRenderer,
} from 'vue'
import pkg from '../../package.json'
import {
Expand All @@ -29,7 +30,7 @@ import {
type TresContext,
} from '../composables'
import { extend } from '../core/catalogue'
import { render } from '../core/renderer'
import { nodeOps } from '../core/nodeOps'

import type { RendererPresetsType } from '../composables/useRenderer/const'
import type { TresCamera, TresObject } from '../types/'
Expand Down Expand Up @@ -87,6 +88,7 @@ const slots = defineSlots<{
}>()

const instance = getCurrentInstance()?.appContext.app
extend(THREE)
Copy link
Contributor

@andretchen0 andretchen0 Mar 7, 2024

Choose a reason for hiding this comment

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

(Non-blocking)

I think this is a requirement of nodeOps, right? (Like, if we created a totally different Vue renderer for something besides THREE, we wouldn't need this at all.)

So, can it be close to line 111 or else inside of nodeOps?

Copy link
Member Author

Choose a reason for hiding this comment

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

If we created a totally different Vue renderer for something besides THREE, we wouldn't need this at all.

Do we? If we would create a custom renderer for, dunno, Babylon, it should exist in a completely different package.


const createInternalComponent = (context: TresContext) =>
defineComponent({
Expand All @@ -105,6 +107,9 @@ const createInternalComponent = (context: TresContext) =>

const mountCustomRenderer = (context: TresContext) => {
const InternalComponent = createInternalComponent(context)

const { render } = createRenderer(nodeOps())

render(h(InternalComponent), scene.value as unknown as TresObject)
}

Expand Down
8 changes: 4 additions & 4 deletions src/core/nodeOps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ function noop(fn: string): any {
fn
}

let scene: TresScene | null = null
const { logError } = useLogger()

const supportedPointerEvents = [
Expand All @@ -33,8 +32,9 @@ export function invalidateInstance(instance: TresObject) {

}

export const nodeOps: RendererOptions<TresObject, TresObject | null> = {
createElement(tag, _isSVG, _anchor, props): TresObject | null {
export const nodeOps: () => RendererOptions<TresObject, TresObject | null> = () => {
Copy link
Contributor

Choose a reason for hiding this comment

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

(non-blocking)

Maybe use a verb now, since this is a function? Rename to getNodeOps?

let scene: TresScene | null = null
return { createElement(tag, _isSVG, _anchor, props): TresObject | null {
if (!props) props = {}

if (!props.args) {
Expand Down Expand Up @@ -303,5 +303,5 @@ export const nodeOps: RendererOptions<TresObject, TresObject | null> = {
setScopeId: () => noop('setScopeId'),
cloneNode: () => noop('cloneNode'),

insertStaticContent: () => noop('insertStaticContent'),
insertStaticContent: () => noop('insertStaticContent') }
}
26 changes: 13 additions & 13 deletions src/core/nodeOpts.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ describe('nodeOps', () => {
const props = { args: [] }

// Test
const instance = nodeOps.createElement(tag, false, null, props)
const instance = nodeOps().createElement(tag, false, null, props)

// Assert
expect(instance.isObject3D).toBeTruthy()
Expand All @@ -28,7 +28,7 @@ describe('nodeOps', () => {
const props = { args: [10, 3, 16, 100] }

// Test
const instance = nodeOps.createElement(tag, false, null, props)
const instance = nodeOps().createElement(tag, false, null, props)

// Assert
expect(instance.parameters.radius).toBe(10)
Expand All @@ -43,7 +43,7 @@ describe('nodeOps', () => {
const props = { args: [75, 2, 0.1, 5] }

// Test
const instance = nodeOps.createElement(tag, false, null, props)
const instance = nodeOps().createElement(tag, false, null, props)

// Assert
expect(instance.isCamera).toBeTruthy()
Expand All @@ -60,7 +60,7 @@ describe('nodeOps', () => {
consoleWarnSpy.mockImplementation(() => { })

// Test
const instance = nodeOps.createElement(tag, false, null, props)
const instance = nodeOps().createElement(tag, false, null, props)

// Assert
expect(instance.isCamera).toBeTruthy()
Expand All @@ -74,7 +74,7 @@ describe('nodeOps', () => {
const props = { args: [] }

// Test
const instance = nodeOps.createElement(tag, false, null, props)
const instance = nodeOps().createElement(tag, false, null, props)

// Assert
expect(instance.isMaterial).toBeTruthy()
Expand All @@ -87,7 +87,7 @@ describe('nodeOps', () => {
const props = { args: [] }

// Test
const instance = nodeOps.createElement(tag, false, null, props)
const instance = nodeOps().createElement(tag, false, null, props)

// Assert
expect(instance.isBufferGeometry).toBeTruthy()
Expand Down Expand Up @@ -115,7 +115,7 @@ describe('nodeOps', () => {
}

// Test
nodeOps.insert(child, parent, null)
nodeOps().insert(child, parent, null)

// Assert
expect(parent.children.includes(child)).toBeTruthy()
Expand All @@ -130,10 +130,10 @@ describe('nodeOps', () => {
child.__vnode = {
type: 'TresMesh',
}
nodeOps.insert(child, parent)
nodeOps().insert(child, parent)

// Test
nodeOps.remove(child)
nodeOps().remove(child)

// Assert
expect(!parent.children.includes(child)).toBeTruthy()
Expand All @@ -151,7 +151,7 @@ describe('nodeOps', () => {
const nextValue = false

// Test
nodeOps.patchProp(node, prop, null, nextValue)
nodeOps().patchProp(node, prop, null, nextValue)

// Assert
expect(node.visible === nextValue)
Expand All @@ -169,7 +169,7 @@ describe('nodeOps', () => {
const nextValue = 5

// Test
nodeOps.patchProp(node, prop, null, nextValue)
nodeOps().patchProp(node, prop, null, nextValue)

// Assert
expect(node.position.x === nextValue)
Expand All @@ -187,7 +187,7 @@ describe('nodeOps', () => {
const nextValue = true

// Test
nodeOps.patchProp(node, prop, null, nextValue)
nodeOps().patchProp(node, prop, null, nextValue)

// Assert
expect(node.castShadow === nextValue)
Expand All @@ -201,7 +201,7 @@ describe('nodeOps', () => {
child.parent = parent

// Test
const parentNode = nodeOps.parentNode(child)
const parentNode = nodeOps().parentNode(child)

// Assert
expect(parentNode === parent)
Expand Down
12 changes: 0 additions & 12 deletions src/core/renderer.ts

This file was deleted.