Skip to content
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

feat(environment): enhance environment and background props #590

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
24 changes: 14 additions & 10 deletions docs/guide/staging/environment.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,16 +88,6 @@ You can incorporate `Lightformer` into the environment just like a slot.
</template>
```

### Props for Lightformer

Lightformer inherits from mesh, and its extension parameters include:
| Prop | Description | Default |
| :----------- | :------------------------------------------------------------------- | -------------------------------------------------------------------------------- |
| `from` | 'circle' , 'ring' , 'rect' , any:other Mesh.The type of Lightformer | `rect` |
| `intensity` | number : the intensity of the light. | 1 |
| `color` | the color of the light. | `0xffffff` |
| `args` | the arguments of the Geometry | When using other geometries, set the corresponding arguments. |

## Props

| Prop | Description | Default |
Expand All @@ -112,3 +102,17 @@ Lightformer inherits from mesh, and its extension parameters include:
| `near` | The near of the CubeCamera. | 1 |
| `far` | The far of the CubeCamera. | 1000 |
| `frames` | The frames of the cubeCamera.update. | Infinity |
| `backgroundIntensity` | Intensity of the background. | 1 |
| `environmentIntensity` | Intensity of the environment. | 1 |
| `backgroundRotation` | Rotation of the background. | [0, 0, 0] |
| `environmentRotation` | Rotation of the environment. | [0, 0, 0] |

### Props for Lightformer

Lightformer inherits from mesh, and its extension parameters include:
| Prop | Description | Default |
| :----------- | :------------------------------------------------------------------- | -------------------------------------------------------------------------------- |
| `from` | 'circle' , 'ring' , 'rect' , any:other Mesh.The type of Lightformer | `rect` |
| `intensity` | number : the intensity of the light. | 1 |
| `color` | the color of the light. | `0xffffff` |
| `args` | the arguments of the Geometry | When using other geometries, set the corresponding arguments. |
Expand Down
4 changes: 4 additions & 0 deletions docs/guide/staging/use-environment.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,7 @@ const texture = await useEnvironment({
| **background** | `boolean` | `false` | If `true` the texture will be used as the scene background. |
| **blur** | `number` | `0` | Blur factor between 0 and 1. (only works with three 0.146 and up) |
| **preset** | `string` | `undefined` | Preset environment map. |
| **backgroundIntensity** | `number` | `1` | Intensity of the background. |
| **environmentIntensity** | `number` | `1` | Intensity of the environment. |
| **backgroundRotation** | `VectorFlexibleParams` | `[0, 0, 0]` | Rotation of the background. |
| **environmentRotation** | `VectorFlexibleParams` | `[0, 0, 0]` | Rotation of the environment. |
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Environment, Lightformer, OrbitControls, Sphere, useProgress } from '@t
import { TresCanvas } from '@tresjs/core'
import Lightformers from './Lightformers.vue'
import { TresLeches, useControls } from '@tresjs/leches'
import { BasicShadowMap, NoToneMapping, SRGBColorSpace } from 'three'
import { BasicShadowMap, NoToneMapping, SRGBColorSpace, Vector3 } from 'three'
import '@tresjs/leches/styles'

/* const environmentFiles = ['/px.jpg', '/nx.jpg', '/py.jpg', '/ny.jpg', '/pz.jpg', '/nz.jpg'] */
Expand All @@ -18,7 +18,7 @@ const gl = {
toneMapping: NoToneMapping,
}

const { background, blur, preset } = useControls({
const { background, blur, preset, backgroundIntensity, environmentIntensity, backgroundRotation, environmentRotation } = useControls({
background: true,
blur: {
value: 0,
Expand All @@ -43,6 +43,24 @@ const { background, blur, preset } = useControls({
],
value: 'sunset',
},
backgroundIntensity: {
value: 1,
min: 0,
max: 1,
step: 0.1,
},
environmentIntensity: {
value: 1,
min: 0,
max: 1,
step: 0.1,
},
backgroundRotation: {
value: new Vector3(0, 0, 0),
},
environmentRotation: {
value: new Vector3(0, 0, 0),
},
})

const environmentRef = ref(null)
Expand Down Expand Up @@ -91,6 +109,10 @@ const { progress, hasFinishLoading } = await useProgress()
:blur="blur.value"
:preset="preset.value"
:frames="Infinity"
:background-intensity="backgroundIntensity.value"
:environment-intensity="environmentIntensity.value"
:background-rotation="[backgroundRotation.value.x, backgroundRotation.value.y, backgroundRotation.value.z]"
:environment-rotation="[environmentRotation.value.x, environmentRotation.value.y, environmentRotation.value.z]"
>
<Lightformer
:intensity="0.75"
Expand Down
2 changes: 2 additions & 0 deletions src/core/staging/useEnvironment/component.vue
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ const props = withDefaults(defineProps<EnvironmentOptions>(), {
near: 1,
far: 1000,
frames: Number.POSITIVE_INFINITY,
backgroundIntensity: 1,
environmentIntensity: 1,
})

const texture: Ref<Texture | CubeTexture | null> = ref(null)
Expand Down
30 changes: 30 additions & 0 deletions src/core/staging/useEnvironment/const.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import type { VectorFlexibleParams } from '@tresjs/core'

export interface EnvironmentOptions {
/**
* If true, the environment will be set as the scene's background.
Expand Down Expand Up @@ -61,6 +63,34 @@ export interface EnvironmentOptions {
* @default Infinity
*/
frames?: number
/**
* The intensity of the background.
*
* @type {number}
* @default 1
*/
backgroundIntensity?: number
/**
* The rotation of the background.
*
* @type {VectorFlexibleParams}
* @default [0, 0, 0]
*/
backgroundRotation?: VectorFlexibleParams
/**
* The intensity of the environment.
*
* @type {number}
* @default 1
*/
environmentIntensity?: number
/**
* The rotation of the environment.
*
* @type {VectorFlexibleParams}
* @default [0, 0, 0]
*/
environmentRotation?: VectorFlexibleParams
}

export const environmentPresets = {
Expand Down
73 changes: 72 additions & 1 deletion src/core/staging/useEnvironment/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import {
CubeReflectionMapping,
CubeTextureLoader,
EquirectangularReflectionMapping,
Euler,
Vector3,
} from 'three'
import { RGBELoader } from 'three-stdlib'
import { computed, ref, toRefs, unref, watch } from 'vue'
Expand All @@ -28,7 +30,11 @@ const PRESET_ROOT = 'https://raw.githubusercontent.com/Tresjs/assets/main/textur
* background = false,
* path = undefined,
* preset = undefined,
* colorSpace = undefined,
* colorSpace = 'srgb',
* backgroundIntensity = 1,
* environmentIntensity = 1,
* backgroundRotation = [0, 0, 0],
* environmentRotation = [0, 0, 0],
* @param {Ref<WebGLCubeRenderTarget | null>} fbo - The framebuffer object
* @return {Promise<Ref<Texture | CubeTexture | null>>} The loaded texture
*/
Expand All @@ -44,6 +50,10 @@ export async function useEnvironment(
files = ref([]),
path = ref(''),
background,
backgroundIntensity = ref(1),
environmentIntensity = ref(1),
backgroundRotation = ref([0, 0, 0]),
environmentRotation = ref([0, 0, 0]),
Copy link
Contributor

@andretchen0 andretchen0 Jan 25, 2025

Choose a reason for hiding this comment

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

In the playground, changing environmentRotation doesn't seem to have an effect.

I was expecting that changing environmentRotation would rotate the material map similar to this THREE demo, when backgroundRotationX and syncMaterial are checked (though not in sync with the background rotation).

} = toRefs(options)

watch(options, () => {
Expand Down Expand Up @@ -105,6 +115,67 @@ export async function useEnvironment(
immediate: true,
})

watch(() => backgroundIntensity?.value, (value) => {
if (scene.value && value) {
Copy link
Contributor

Choose a reason for hiding this comment

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

0 will make this predicate false.

scene.value.backgroundIntensity = value
}
}, {
immediate: true,
})

watch(() => environmentIntensity?.value, (value) => {
Copy link
Contributor

Choose a reason for hiding this comment

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

0 will make this predicate false.

if (scene.value && value) {
scene.value.environmentIntensity = value
}
}, {
immediate: true,
})

watch(() => backgroundRotation?.value, (value) => {
if (scene.value && value) {
Copy link
Contributor

Choose a reason for hiding this comment

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

value can be a number. 0 is a valid value, but will cause this predicate to be false.

// TODO: would be nice to abstract this to a function on @tresjs/core
if (value instanceof Euler) {
scene.value.backgroundRotation = value
}
else if (Array.isArray(value)) {
scene.value.backgroundRotation = new Euler(value[0], value[1], value[2])
}
else if (typeof value === 'number') {
scene.value.backgroundRotation = new Euler(value, value, value)
}
else if (value instanceof Vector3) {
scene.value.backgroundRotation = new Euler(value.x, value.y, value.z)
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)

This branch could be removed. The next predicate will match Vector3 and set the value accordingly.

}
else if (typeof value === 'object' && 'x' in value && 'y' in value && 'z' in value) {
scene.value.backgroundRotation = new Euler(value.x, value.y, value.z)
}
}
}, {
immediate: true,
})

watch(() => environmentRotation?.value, (value) => {
if (scene.value && value) {
Copy link
Contributor

Choose a reason for hiding this comment

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

value can be a number. 0 is a valid value, but will cause this predicate to be false.

if (value instanceof Euler) {
scene.value.environmentRotation = value
}
else if (Array.isArray(value)) {
scene.value.environmentRotation = new Euler(value[0], value[1], value[2])
}
else if (typeof value === 'number') {
scene.value.environmentRotation = new Euler(value, value, value)
}
else if (value instanceof Vector3) {
scene.value.environmentRotation = new Euler(value.x, value.y, value.z)
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)

This branch could be removed. The next predicate will match Vector3 and set the value accordingly.

}
else if (typeof value === 'object' && 'x' in value && 'y' in value && 'z' in value) {
scene.value.environmentRotation = new Euler(value.x, value.y, value.z)
}
}
}, {
immediate: true,
})

watch(() => preset?.value, async (value) => {
if (value && value in environmentPresets) {
const _path = PRESET_ROOT
Expand Down
Loading