Skip to content

Commit

Permalink
feat(core): provide inject worked again
Browse files Browse the repository at this point in the history
  • Loading branch information
alvarosabu committed Mar 12, 2023
1 parent da84165 commit 2390ec1
Show file tree
Hide file tree
Showing 13 changed files with 50 additions and 36 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
"@changesets/cli": "^2.25.2",
"@stackblitz/sdk": "^1.8.1",
"@tresjs/cientos": "workspace:^1.8.0",
"@tresjs/core": "workspace:^1.8.1",
"@tresjs/core": "workspace:^2.0.0",
"@typescript-eslint/eslint-plugin": "^5.42.0",
"@typescript-eslint/parser": "^5.42.0",
"conventional-changelog-cli": "^2.2.2",
Expand Down
4 changes: 2 additions & 2 deletions packages/cientos/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
"changelog": "conventional-changelog -p angular -i CHANGELOG.md -s"
},
"peerDependencies": {
"@tresjs/core": "workspace:^1.8.1",
"@tresjs/core": "workspace:^2.0.0",
"three": "latest",
"vue": "^3.2.47"
},
Expand All @@ -55,7 +55,7 @@
"vite-plugin-dts": "2.0.0-beta.1"
},
"dependencies": {
"@tresjs/core": "workspace:^1.8.1",
"@tresjs/core": "workspace:^2.0.0",
"three-stdlib": "^2.21.8"
}
}
1 change: 0 additions & 1 deletion packages/cientos/src/core/OrbitControls.vue
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,5 @@ watch(controls, value => {
v-if="state.camera && state.renderer"
ref="controls"
:args="[unref(state.camera) || camera, state.renderer?.domElement || domElement]"
:enabling-dampling="enableDamping"
/>
</template>
12 changes: 9 additions & 3 deletions packages/cientos/src/core/useCientos.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useTres } from '@tresjs/core'
import { inject } from 'vue'
import { useTres, extend } from '@tresjs/core'
import { inject, watch } from 'vue'

/**
* Allows to use and extend the state of the core package.
Expand All @@ -8,13 +8,19 @@ import { inject } from 'vue'
* @return {*}
*/
export function useCientos() {
const { state, setState } = inject('useTres', useTres())
const extend =
inject<(objects: any) => void>('extend') ||
(() => {
console.warn('No extend function provided')
})

const { state, setState } = inject('useTres', useTres())
watch(
() => state,
() => {
console.log('cientos state changed', state)
},
)
return {
state,
setState,
Expand Down
2 changes: 1 addition & 1 deletion packages/tres/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@tresjs/core",
"description": "Declarative ThreeJS using Vue Components",
"version": "1.8.1",
"version": "2.0.0",
"type": "module",
"author": "Alvaro Saburido <[email protected]> (https://github.com/alvarosabu/)",
"files": [
Expand Down
14 changes: 8 additions & 6 deletions packages/tres/src/App.vue
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<script setup lang="ts">
import { ref, watchEffect } from 'vue'
import { OrbitControls } from 'three/addons/controls/OrbitControls.js'
import { provide, ref, watchEffect } from 'vue'
import { OrbitControls } from '../../cientos/src/'
import TresCanvas from './components/TresCanvas'
import { useRenderLoop, useTres } from '/@/composables'
import { extend } from './core/catalogue'
import AkuAku from './demos/AkuAku.vue'
// import TheEvents from '/@/components/TheEvents.vue'
const gl = {
antialias: true,
Expand All @@ -14,8 +14,6 @@ const gl = {
const { state } = useTres()
extend({ OrbitControls })
const { onLoop } = useRenderLoop()
watchEffect(() => {
Expand Down Expand Up @@ -48,8 +46,9 @@ function enter(e) {

<template>
<TresCanvas v-bind="gl">
<OrbitControls />
<TresPerspectiveCamera :args="[75, 1, 0.1, 1000]" :position="[0, 2, 7]"></TresPerspectiveCamera>
<TresOrbitControls v-if="state.camera" :args="[state.camera, state.renderer.domElement]" />
<!-- <TresOrbitControls v-if="state.camera" :args="[state.camera, state.renderer.domElement]" /> -->
<TresAmbientLight :color="0xffffff" :intensity="0.75" />
<TresDirectionalLight :color="0xffffff" :intensity="2" :position="[-2, 2, 0]" cast-shadow />
<TresMesh :position="[0, 3, 0]" @click="click" @pointer-enter="enter" cast-shadow>
Expand All @@ -60,6 +59,9 @@ function enter(e) {
<TresPlaneGeometry :args="[12, 12, 100, 100]"></TresPlaneGeometry>
<TresMeshToonMaterial color="gray"></TresMeshToonMaterial>
</TresMesh>
<Suspense>
<AkuAku />
</Suspense>
<TresGridHelper :args="[4, 4]"></TresGridHelper>
</TresCanvas>
</template>
Expand Down
21 changes: 11 additions & 10 deletions packages/tres/src/components/TresCanvas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ import { defineComponent, h, PropType, ref, watch } from 'vue'
import * as THREE from 'three'
import { ShadowMapType, TextureEncoding, ToneMapping } from 'three'
import { createTres } from '/@/core/renderer'
import { useCamera, useRenderer, useRenderLoop, useRaycaster } from '/@/composables'
import { useCamera, useRenderer, useRenderLoop, useRaycaster, useTres } from '/@/composables'
import { TresObject } from '../types'
import { extend } from '../core/catalogue'

export const TresCanvas = defineComponent({
name: 'TresCanvas',
Expand Down Expand Up @@ -33,13 +34,12 @@ export const TresCanvas = defineComponent({
setup(props, { slots, expose }) {
const container = ref<HTMLElement>()
const canvas = ref<HTMLCanvasElement>()
const scene = new THREE.Scene()

watch(canvas, () => {
const { renderer } = useRenderer(canvas, container, props)
const { activeCamera } = useCamera()

const scene = new THREE.Scene()

const { onLoop } = useRenderLoop()

const { raycaster, pointer } = useRaycaster()
Expand All @@ -50,14 +50,15 @@ export const TresCanvas = defineComponent({
raycaster.value.setFromCamera(pointer.value, activeCamera.value)
renderer.value?.render(scene, activeCamera.value)
})
})

const app = createTres(slots)
app.mount(scene as unknown as TresObject)

console.log({ scene, renderer })
expose({
scene,
})
const app = createTres(slots)
app.provide('awiwi', 'uwu')
app.provide('useTres', useTres())
app.provide('extend', extend)
app.mount(scene as unknown as TresObject)
expose({
scene,
})

return () => {
Expand Down
9 changes: 7 additions & 2 deletions packages/tres/src/core/catalogue.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
export const catalogue: Record<string, any> = {}
export const extend = (objects: any) => void Object.assign(catalogue, objects)
import { MathUtils } from 'three'
import { Ref, ref } from 'vue'
import { TresCatalogue } from '../types'

export const catalogue: Ref<TresCatalogue> = ref({ uuid: MathUtils.generateUUID() })

export const extend = (objects: any) => void Object.assign(catalogue.value, objects)

export default { catalogue, extend }
4 changes: 2 additions & 2 deletions packages/tres/src/core/nodeOps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ export const nodeOps: RendererOptions<TresObject, TresObject> = {
if (props === null) props = {}

if (props?.args) {
instance = new catalogue[tag.replace('Tres', '')](...props.args)
instance = new catalogue.value[tag.replace('Tres', '')](...props.args)
} else {
instance = new catalogue[tag.replace('Tres', '')]()
instance = new catalogue.value[tag.replace('Tres', '')]()
}

if (instance.isCamera) {
Expand Down
9 changes: 5 additions & 4 deletions packages/tres/src/demos/AkuAku.vue
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<script setup lang="ts">
import { useTweakPane, useGLTF } from '@tresjs/cientos'
import { ref, watch } from 'vue'
import { inject, ref, watch, watchEffect } from 'vue'
useTweakPane()
Expand All @@ -18,10 +18,11 @@ const akuAkuRef = ref(null)
watch(akuAkuRef, value => {
console.log('akuAkuRef', value)
})
const awiwi = inject('awiwi')
console.log('awiwi', awiwi)
</script>

<template>
<Suspense>
<TresMesh ref="akuAkuRef" v-bind="model" />
</Suspense>
<TresMesh ref="akuAkuRef" v-bind="model" />
</template>
3 changes: 2 additions & 1 deletion packages/tres/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { App } from 'vue'
import { App, ref } from 'vue'
import { TresCanvas } from '/@/components/TresCanvas'
export * from '/@/composables'
export * from '/@/core/catalogue'
export * from './keys'
export * from './types'

Expand Down
1 change: 0 additions & 1 deletion packages/tres/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ export default defineConfig({
resolve: {
alias: {
'/@': resolve(__dirname, './src'),
vue: 'vue/dist/vue.esm-bundler.js',
},
dedupe: ['@tresjs/cientos'],
},
Expand Down
4 changes: 2 additions & 2 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 2390ec1

Please sign in to comment.