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

Assets #1530

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft

Assets #1530

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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4143,7 +4143,7 @@ Sets up a global cubemap, which affects the default `scene.environment`, and opt
/>
```

The simplest way to use it is to provide a preset. 👉 Note: `preset` property is not meant to be used in production environments and may fail as it relies on CDNs.
> **Warning** `preset` will use a base64 packed DWAB-EXR compressed HDRI assets similar to https://github.com/pmndrs/assets. Each preset is highly optimized and only weighs around ~150 KB in size, but incurs a small runtime unpack cost.

```jsx
<Environment preset="city" />
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
"dependencies": {
"@babel/runtime": "^7.11.2",
"@mediapipe/tasks-vision": "0.10.2-rc2",
"@pmndrs/assets": "^1.6.0",
"@react-spring/three": "~9.6.1",
"@use-gesture/react": "^10.2.24",
"camera-controls": "^2.4.2",
Expand Down
1 change: 1 addition & 0 deletions src/assets/hdri/apartment.exr.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions src/assets/hdri/bridge.exr.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions src/assets/hdri/city.exr.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions src/assets/hdri/dawn.exr.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions src/assets/hdri/esplanade.exr.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions src/assets/hdri/forest.exr.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions src/assets/hdri/hall.exr.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions src/assets/hdri/lab.exr.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions src/assets/hdri/lobby.exr.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions src/assets/hdri/night.exr.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions src/assets/hdri/park.exr.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions src/assets/hdri/sky.exr.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions src/assets/hdri/studio.exr.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions src/assets/hdri/sunrise.exr.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions src/assets/hdri/sunset.exr.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions src/assets/hdri/venice.exr.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions src/assets/hdri/warehouse.exr.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions src/assets/hdri/workshop.exr.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions src/assets/textures/cloud.webp.js

Large diffs are not rendered by default.

30 changes: 22 additions & 8 deletions src/core/Cloud.tsx
Original file line number Diff line number Diff line change
@@ -1,39 +1,53 @@
import * as React from 'react'
import { Group, Texture } from 'three'
import { useFrame } from '@react-three/fiber'
import { suspend } from 'suspend-react'
import { Billboard } from './Billboard'
import { Plane } from './shapes'
import { useTexture } from './useTexture'

const CLOUD_URL = 'https://rawcdn.githack.com/pmndrs/drei-assets/9225a9f1fbd449d9411125c2f419b843d0308c9f/cloud.png'
export type CloudProps = {
opacity?: number
speed?: number
width?: number
depth?: number
segments?: number
texture?: string
color?: string
depthTest?: boolean
} & JSX.IntrinsicElements['group']

export function Cloud({
opacity = 0.5,
speed = 0.4,
speed = 0.2,
width = 10,
depth = 1.5,
segments = 20,
texture = CLOUD_URL,
texture,
color = '#ffffff',
depthTest = true,
...props
}) {
}: CloudProps) {
const group = React.useRef<Group>()
const cloudTexture = useTexture(texture) as Texture
const cloudUri = suspend(async () => {
return texture ? { default: texture } : import('../assets/textures/cloud.webp.js')
}, ['r3f_drei_cloud', texture]).default

const cloudTexture = useTexture(cloudUri) as Texture
const clouds = React.useMemo(
() =>
[...new Array(segments)].map((_, index) => ({
x: width / 2 - Math.random() * width,
y: width / 2 - Math.random() * width,
scale: 0.4 + Math.sin(((index + 1) / segments) * Math.PI) * ((0.2 + Math.random()) * 10),
density: Math.max(0.2, Math.random()),
rotation: Math.max(0.002, 0.005 * Math.random()) * speed,
rotation: Math.max(0.1, 0.2 * Math.random()) * speed,
})),
[width, segments, speed]
)
useFrame((state) =>
useFrame((state, delta) =>
group.current?.children.forEach((cloud, index) => {
cloud.children[0].rotation.z += clouds[index].rotation
cloud.children[0].rotation.z += clouds[index].rotation * delta
cloud.children[0].scale.setScalar(
clouds[index].scale + (((1 + Math.sin(state.clock.getElapsedTime() / 10)) / 2) * index) / 10
)
Expand Down
5 changes: 2 additions & 3 deletions src/core/useEnvironment.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import {
TextureEncoding,
} from 'three'
import { RGBELoader, EXRLoader } from 'three-stdlib'
import { suspend } from 'suspend-react'
import { presetsObj, PresetsType } from '../helpers/environment-assets'

const CUBEMAP_ROOT = 'https://raw.githack.com/pmndrs/drei-assets/456060a26bbeb8fdf79326f224b6d99b8bcce736/hdri/'
const isArray = (arr: any): arr is string[] => Array.isArray(arr)

export type EnvironmentLoaderProps = {
Expand All @@ -35,8 +35,7 @@ export function useEnvironment({

if (preset) {
if (!(preset in presetsObj)) throw new Error('Preset must be one of: ' + Object.keys(presetsObj).join(', '))
files = presetsObj[preset]
path = CUBEMAP_ROOT
files = suspend(() => import(`../assets/hdri/${presetsObj[preset]}.exr.js`), ['r3f_drei_env', preset]).default
}

// Everything else
Expand Down
28 changes: 18 additions & 10 deletions src/helpers/environment-assets.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,22 @@
export const presetsObj = {
apartment: 'lebombo_1k.hdr',
city: 'potsdamer_platz_1k.hdr',
dawn: 'kiara_1_dawn_1k.hdr',
forest: 'forest_slope_1k.hdr',
lobby: 'st_fagans_interior_1k.hdr',
night: 'dikhololo_night_1k.hdr',
park: 'rooitou_park_1k.hdr',
studio: 'studio_small_03_1k.hdr',
sunset: 'venice_sunset_1k.hdr',
warehouse: 'empty_warehouse_01_1k.hdr',
apartment: 'apartment',
bridge: 'bridge',
city: 'city',
dawn: 'dawn',
esplanade: 'esplanade',
forest: 'forest',
hall: 'hall',
lab: 'lab',
lobby: 'lobby',
night: 'night',
park: 'park',
sky: 'sky',
studio: 'studio',
sunrise: 'sunrise',
sunset: 'sunset',
venice: 'venice',
warehouse: 'warehouse',
workshop: 'workshop',
}

export type PresetsType = keyof typeof presetsObj
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2199,6 +2199,11 @@
"@nodelib/fs.scandir" "2.1.5"
fastq "^1.6.0"

"@pmndrs/assets@^1.6.0":
version "1.6.0"
resolved "https://registry.yarnpkg.com/@pmndrs/assets/-/assets-1.6.0.tgz#d5df66d107d64db98331b45a93483f5ecb980561"
integrity sha512-fOFrjV6FNYIr29iEhF3JrdTAAo3v98TatmYF3efC1MNiMeN0Sx2BGvGnwgu55s6E9gBxR6yXcm/mkk7gRDT9Bg==

"@react-spring/animated@~9.6.1":
version "9.6.1"
resolved "https://registry.yarnpkg.com/@react-spring/animated/-/animated-9.6.1.tgz#ccc626d847cbe346f5f8815d0928183c647eb425"
Expand Down
Loading