-
-
Notifications
You must be signed in to change notification settings - Fork 108
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(core): re-structure and tres custom renderer base
- Loading branch information
1 parent
c0b86ea
commit aad0953
Showing
33 changed files
with
371 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
import { defineComponent, h, onUnmounted, PropType, ref, watchEffect } from 'vue' | ||
/* eslint-disable vue/one-component-per-file */ | ||
import * as THREE from 'three' | ||
import { ShadowMapType, TextureEncoding, ToneMapping } from 'three' | ||
/* import { OrbitControls } from '@tresjs/cientos' */ | ||
import { extend, createTres } from '/@/core/renderer' | ||
|
||
export const TresCanvas = defineComponent({ | ||
name: 'TresCanvas', | ||
props: { | ||
shadows: Boolean, | ||
shadowMapType: Number as PropType<ShadowMapType>, | ||
physicallyCorrectLights: { | ||
type: Boolean, | ||
default: false, | ||
validator: (value: boolean) => { | ||
if (value) { | ||
console.warn('physicallyCorrectLights is deprecated. Use useLegacyLights instead.') | ||
} | ||
return true | ||
}, | ||
}, | ||
useLegacyLights: Boolean, | ||
outputEncoding: Number as PropType<TextureEncoding>, | ||
toneMapping: Number as PropType<ToneMapping>, | ||
toneMappingExposure: Number, | ||
context: Object as PropType<WebGLRenderingContext>, | ||
powerPreference: String as PropType<'high-performance' | 'low-power' | 'default'>, | ||
preserveDrawingBuffer: Boolean, | ||
clearColor: String, | ||
windowSize: { type: Boolean, default: false }, | ||
}, | ||
setup(props, { slots, attrs }) { | ||
const container = ref<HTMLElement>() | ||
const canvas = ref<HTMLCanvasElement>() | ||
|
||
watchEffect(() => { | ||
const renderer = new THREE.WebGLRenderer({ | ||
canvas: canvas.value, | ||
antialias: true, | ||
alpha: true, | ||
powerPreference: 'high-performance', | ||
}) | ||
renderer.outputEncoding = THREE.sRGBEncoding | ||
renderer.toneMapping = THREE.ACESFilmicToneMapping | ||
renderer.setSize(window.innerWidth, window.innerHeight) | ||
|
||
const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight) | ||
camera.position.set(0, 2, 7) | ||
|
||
/* const controls = new OrbitControls(camera, renderer.domElement) | ||
controls.enableDamping = true */ | ||
|
||
const scene = new THREE.Scene() | ||
|
||
window.addEventListener('resize', () => { | ||
renderer.setSize(window.innerWidth, window.innerHeight) | ||
camera.aspect = window.innerWidth / window.innerHeight | ||
camera.updateProjectionMatrix() | ||
}) | ||
|
||
renderer.setAnimationLoop(() => { | ||
/* controls.update() */ | ||
renderer.render(scene, camera) | ||
}) | ||
|
||
const internal = slots?.default() || [] | ||
|
||
const internalComponent = defineComponent({ | ||
name: 'Wrapper', | ||
setup() { | ||
return () => internal | ||
}, | ||
}) | ||
|
||
const app = createTres(internalComponent) | ||
app.mount(scene) | ||
|
||
console.log(app) | ||
|
||
onUnmounted(() => { | ||
app.unmount() | ||
}) | ||
}) | ||
|
||
return () => { | ||
return h( | ||
h( | ||
'div', | ||
{ | ||
ref: container, | ||
style: { | ||
position: 'relative', | ||
width: '100%', | ||
height: '100%', | ||
|
||
pointerEvents: 'auto', | ||
touchAction: 'none', | ||
...(attrs.style as Record<string, unknown>), | ||
}, | ||
}, | ||
[ | ||
h( | ||
'div', | ||
{ | ||
style: { | ||
width: '100%', | ||
height: '100%', | ||
}, | ||
}, | ||
[ | ||
h('canvas', { | ||
ref: canvas, | ||
style: { | ||
display: 'block', | ||
width: '100%', | ||
height: '100%', | ||
position: props.windowSize ? 'fixed' : 'absolute', | ||
top: 0, | ||
left: 0, | ||
}, | ||
}), | ||
], | ||
), | ||
], | ||
), | ||
) | ||
} | ||
}, | ||
}) | ||
|
||
export default TresCanvas |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,10 @@ | ||
export * from './useLogger' | ||
export * from './useCamera' | ||
export * from './useCatalogue' | ||
export * from './useInstanceCreator' | ||
export * from './useRenderLoop/' | ||
export * from './useRenderer/' | ||
export * from './useScene/' | ||
export * from './useLoader' | ||
export * from './useTexture' | ||
export * from './useTres' | ||
export * from './useRaycaster' |
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion
2
packages/tres/src/core/useCatalogue/index.ts → ...res/src/composables/useCatalogue/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
2 changes: 1 addition & 1 deletion
2
packages/tres/src/core/useLoader/index.ts → ...s/tres/src/composables/useLoader/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
packages/tres/src/core/useScene/component.ts → ...res/src/composables/useScene/component.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export const catalogue = {} | ||
export const extend = objects => void Object.assign(catalogue, objects) | ||
|
||
export default { catalogue, extend } |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
import { useCamera } from '@tresjs/core' | ||
import { RendererOptions } from 'vue' | ||
import { catalogue } from './catalogue' | ||
|
||
export const nodeOps: RendererOptions<Node, Element> = { | ||
createElement(type, _isSVG, _isCustomizedBuiltIn, props) { | ||
if (type === 'template') return null | ||
let instance | ||
|
||
if (props === null) { | ||
props = {} | ||
} | ||
|
||
if (props.arg) { | ||
instance = new catalogue[type.replace('Tres', '')](...props.args) | ||
} else { | ||
instance = new catalogue[type.replace('Tres', '')]() | ||
} | ||
|
||
if (instance.isCamera) { | ||
const { pushCamera } = useCamera() | ||
pushCamera(instance) | ||
} | ||
|
||
if (props.attach === undefined) { | ||
if (instance.isMaterial) instance.attach = 'material' | ||
else if (instance.isBufferGeometry) instance.attach = 'geometry' | ||
} | ||
|
||
console.log({ | ||
type, | ||
instance, | ||
threeObj: catalogue[type.replace('Tres', '')], | ||
}) | ||
|
||
return instance | ||
}, | ||
insert(child, parent, beforeChild) { | ||
if (parent?.isObject3D && child?.isObject3D) { | ||
const index = beforeChild ? parent.children.indexOf(beforeChild) : 0 | ||
child.parent = parent | ||
parent.children.splice(index, 0, child) | ||
child.dispatchEvent({ type: 'added' }) | ||
} else if (typeof child?.attach === 'string') { | ||
child.__previousAttach = child[parent.attach] | ||
parent[child.attach] = child | ||
} | ||
}, | ||
remove(node) { | ||
const parent = node.parent | ||
if (parent) { | ||
if (parent.isObject3D && node.isObject3D) { | ||
parent.remove(node) | ||
} else if (typeof child.attach === 'string') { | ||
parent[child.attach] = child.__previousAttach | ||
delete child.__previousAttach | ||
node.parent = null | ||
} | ||
} | ||
|
||
node.dispose?.() | ||
node.traverse?.(node => node.dispose?.()) | ||
}, | ||
patchProp(node, prop, prevValue, nextValue) { | ||
let root = node | ||
let key = prop | ||
let target = root[key] | ||
|
||
// Traverse pierced props (e.g. foo-bar=value => foo.bar = value) | ||
if (key.includes('-')) { | ||
const chain = key.split('-') | ||
target = chain.reduce((acc, key) => acc[key], root) | ||
key = chain.pop() | ||
|
||
if (!target?.set) root = chain.reduce((acc, key) => acc[key], root) | ||
} | ||
|
||
let value = nextValue | ||
try { | ||
const num = parseFloat(value) | ||
value = isNaN(num) ? JSON.parse(value) : num | ||
} catch (_) {} | ||
|
||
// Set prop, prefer atomic methods if applicable | ||
if (!target?.set) root[key] = value | ||
else if (target.constructor === value.constructor) target.copy(value) | ||
else if (Array.isArray(value)) target.set(...value) | ||
else if (!target.isColor && target.setScalar) target.setScalar(value) | ||
else target.set(value) | ||
}, | ||
createText(text) {}, | ||
createComment(text) {}, | ||
setText(node, text) {}, | ||
setElementText(node, text) {}, | ||
parentNode(node) { | ||
return node?.parent || null | ||
}, | ||
nextSibling(node) { | ||
if (node?.parent?.children) { | ||
const index = node.parent.children.indexOf(node) | ||
if (index !== -1) return node.parent.children[index + 1] | ||
} | ||
return null | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import * as THREE from 'three' | ||
|
||
import { createRenderer } from 'vue' | ||
import { extend } from './catalogue' | ||
import { nodeOps } from './nodeOps' | ||
|
||
export const { createApp: createTres } = createRenderer(nodeOps) | ||
|
||
extend(THREE) | ||
|
||
export default { createTres, extend } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './useLogger' |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,8 @@ | ||
import { createApp } from 'vue' | ||
import App from './App.vue' | ||
import plugin from '.' | ||
|
||
import './style.css' | ||
|
||
export const app = createApp(App) | ||
|
||
app.use(plugin) | ||
app.mount('#app') |
Oops, something went wrong.