Skip to content

Commit

Permalink
feat(cientos): environment preset support
Browse files Browse the repository at this point in the history
  • Loading branch information
alvarosabu committed Feb 16, 2023
1 parent 4058f58 commit ff8c86c
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 10 deletions.
6 changes: 1 addition & 5 deletions packages/cientos/src/core/useEnvironment/component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { EnvironmentOptions, EnvironmentPresetsType } from './const'
import { CubeTexture, Texture, TextureEncoding } from 'three'
import { defineComponent, PropType } from 'vue'

import { useCientos } from '../useCientos'
import { useEnvironment } from '.'

export const Environment = defineComponent({
Expand All @@ -21,10 +20,7 @@ export const Environment = defineComponent({
required: true,
},
encoding: Object as PropType<TextureEncoding>,
path: {
type: String,
default: '/',
},
path: String,
preset: Object as PropType<EnvironmentPresetsType>,
},
async setup(props, { expose }) {
Expand Down
12 changes: 10 additions & 2 deletions packages/cientos/src/core/useEnvironment/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
} from 'three'
import { RGBELoader } from 'three-stdlib'
import { useCientos } from '../useCientos'
import { EnvironmentOptions } from './const'
import { EnvironmentOptions, environmentPresets } from './const'

export async function useEnvironment({
files = ['/px.png', '/nx.png', '/py.png', '/ny.png', '/pz.png', '/nz.png'],
Expand All @@ -21,12 +21,20 @@ export async function useEnvironment({
encoding = undefined,
}: Partial<EnvironmentOptions>): Promise<Texture | CubeTexture> {
const { state } = useCientos()

if (preset) {
if (!(preset in environmentPresets))
throw new Error('Preset must be one of: ' + Object.keys(environmentPresets).join(', '))
files = environmentPresets[preset]
path = 'https://raw.githubusercontent.com/Tresjs/assets/main/textures/hdr/'
}

const isCubeMap = Array.isArray(files)

const loader = isCubeMap ? CubeTextureLoader : RGBELoader

const result = await useLoader(loader, isCubeMap ? [files] : files, (loader: any) => {
/* if (path) loader.setPath(path) */
if (path) loader.setPath(path)
if (encoding) loader.encoding = encoding
})

Expand Down
14 changes: 11 additions & 3 deletions packages/tres/src/components/TheEnvironment.vue
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,16 @@ const sphereRef = ref()
const { pane } = useTweakPane()
const environmentFiles = [
/* const environmentFiles = [
'https://raw.githubusercontent.com/Tresjs/assets/main/textures/environmentMap/px.jpg',
'https://raw.githubusercontent.com/Tresjs/assets/main/textures/environmentMap/nx.jpg',
'https://raw.githubusercontent.com/Tresjs/assets/main/textures/environmentMap/py.jpg',
'https://raw.githubusercontent.com/Tresjs/assets/main/textures/environmentMap/ny.jpg',
'https://raw.githubusercontent.com/Tresjs/assets/main/textures/environmentMap/pz.jpg',
'https://raw.githubusercontent.com/Tresjs/assets/main/textures/environmentMap/nz.jpg',
]
] */
const environmentFiles = ['/px.jpg', '/nx.jpg', '/py.jpg', '/ny.jpg', '/pz.jpg', '/nz.jpg']
let envMap = null
Expand Down Expand Up @@ -109,7 +111,13 @@ pane
<TresPerspectiveCamera :position="[8, 8, 8]" :fov="45" :near="0.1" :far="1000" :look-at="[-8, 3, -3]" />
<OrbitControls make-default />
<TresScene>
<Environment ref="environmentTexture" background :files="environmentFiles" />
<!-- <Environment
ref="environmentTexture"
background
:files="environmentFiles"
:path="'https://raw.githubusercontent.com/Tresjs/assets/main/textures/environmentMap'"
/> -->
<Environment ref="environmentTexture" background preset="sunset" />
<TresAmbientLight :intensity="0.5" />

<TresMesh ref="sphereRef" :position="[0, 4, 0]" cast-shadow>
Expand Down

0 comments on commit ff8c86c

Please sign in to comment.