Skip to content

Commit

Permalink
feat(app): 329 Add cast-shadow/receive-shadow to 3D model
Browse files Browse the repository at this point in the history
  • Loading branch information
JaimeTorrealba committed Jan 11, 2024
1 parent 8fac5fa commit 471e28a
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 25 deletions.
2 changes: 2 additions & 0 deletions docs/guide/loaders/fbx-model.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,5 @@ The `FBXModel` component is a wrapper around [`useFBX`](./use-fbx.md) composable
| Prop | Description | Default |
| :----- | :---------------------- | ----------- |
| `path` | Path to the model file. | `undefined` |
| `castShadow` | Apply `cast-shadow` to all meshes inside your model. | `false` |
| `receiveShadow` | Apply `receive-shadow` to all meshes inside your model. | `false` |
6 changes: 4 additions & 2 deletions docs/guide/loaders/gltf-model.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ The `GLTFModel` component is a wrapper around [`useGLTF`](./use-gltf.md) composa

You can access the model reference by passing a `ref` to the `model` prop and then using to get the object.

```ts
```vue
<script setup lang="ts">
import { OrbitControls, GLTFModel } from '@tresjs/cientos'
const modelRef = shallowRef<THREE.Object3D>()
watch(modelRef, model => {
watch(modelRef, (model) => {
// Do something with the model
model.position.set(0, 0, 0)
})
Expand All @@ -34,3 +34,5 @@ watch(modelRef, model => {
| `path` | Path to the model file. | `undefined` |
| `draco` | Enable [Draco compression](https://threejs.org/docs/index.html?q=drac#examples/en/loaders/DRACOLoader) for the model. | `false` |
| `decoderPath` | Path to a local Draco decoder. | `undefined` |
| `castShadow` | Apply `cast-shadow` to all meshes inside your model. | `false` |
| `receiveShadow` | Apply `receive-shadow` to all meshes inside your model. | `false` |
26 changes: 9 additions & 17 deletions playground/src/components/ModelsDemo.vue
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<script setup lang="ts">
import { ref } from 'vue'
import { TresCanvas } from '@tresjs/core'
import { useGLTF, OrbitControls, GLTFModel, useFBX, FBXModel } from '@tresjs/cientos'
import { useGLTF, GLTFModel, useFBX, FBXModel } from '@tresjs/cientos'
import { NoToneMapping } from 'three'
const modelPath = 'https://raw.githubusercontent.com/Tresjs/assets/main/models/gltf/aku-aku/AkuAku.gltf'
Expand All @@ -24,44 +23,37 @@ const gl = {
</script>

<template>
<TresCanvas
v-bind="gl"
>
<TresPerspectiveCamera :position="[0, 2, 10]" />
<TresGridHelper :args="[10, 10]" />
<OrbitControls
:keys="{}"
:auto-rotate="true"
/>
<TresGroup>
<primitive
:object="model"
:position="[-3, -2, 0]"
:position="[-3, 0, 0]"
/>
<Suspense>
<GLTFModel
ref="akuAkuRef"
:path="modelPath"
draco
:position="[0, -2, 0]"
:rotation-x="0.5"
name="Aku_aku"
cast-shadow
/>
</Suspense>
<!-- FBX MODELS -->
<primitive
:object="modelFbx"
:position="[6, 0, 0]"
:position="[6, 0, 2]"
:scale="[0.01, 0.01, 0.01]"
/>
<Suspense>
<FBXModel
ref="jeepRef"
:path="modelPathFbx"
:scale="0.01"
:position="[0, -4, 0]"
:position="[-6, -1, 2]"
name="jeep_model"
cast-shadow
/>
</Suspense>
<TresAmbientLight />
<TresDirectionalLight />
</TresCanvas>
</TresGroup>
</template>
20 changes: 16 additions & 4 deletions playground/src/pages/loaders/UseGLTFDemo.vue
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<script setup lang="ts">
import { TresCanvas } from '@tresjs/core'
import { BasicShadowMap, SRGBColorSpace, NoToneMapping } from 'three'
import { OrbitControls } from '@tresjs/cientos'
import { CameraControls } from '@tresjs/cientos'
import ModelsDemo from '../../components/ModelsDemo.vue'
const gl = {
clearColor: '#82DBC5',
Expand All @@ -17,10 +17,22 @@ const gl = {
<template>
<TresCanvas v-bind="gl">
<TresPerspectiveCamera :position="[3, 3, 3]" />
<OrbitControls />
<CameraControls />
<Suspense>
<AkuAku />
<ModelsDemo />
</Suspense>
<TresMesh
:rotate-x="Math.PI * -0.5"
:position-y="-2"
receive-shadow
>
<TresPlaneGeometry :args="[40, 40]" />
<TresMeshStandardMaterial :color="0xf7f7f7" />
</TresMesh>
<TresAmbientLight :intensity="1" />
<TresDirectionalLight
:intensity="1"
cast-shadow
/>
</TresCanvas>
</template>
38 changes: 36 additions & 2 deletions src/core/loaders/useFBX/component.vue
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,36 @@ export interface FBXModelProps {
* @required
*/
path: string
/**
*
* Whether to use cast-shadow to the model.
*
* @type {boolean}
* @default false
* @memberof FBXModelProps
*
**/
castShadow?: boolean
/**
*
* Whether to use receive-shadow to the model.
*
* @type {boolean}
* @default false
* @memberof FBXModelProps
*
**/
receiveShadow?: boolean
}
const props = defineProps<{
const props = withDefaults(defineProps<{
path: string
}>()
castShadow?: boolean
receiveShadow?: boolean
}>(), {
castShadow: false,
receiveShadow: false,
})
const modelRef = ref()
Expand All @@ -24,6 +49,15 @@ defineExpose({
})
const model = await useFBX(props.path as string)
if ( props.castShadow || props.receiveShadow ) {
model.traverse((child) => {
if (child.isMesh) {
child.castShadow = props.castShadow
child.receiveShadow = props.receiveShadow
}
})
}
</script>

<template>
Expand Down
32 changes: 32 additions & 0 deletions src/core/loaders/useGLTF/component.vue
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,26 @@ export interface GLTFModelProps {
*
**/
draco?: boolean
/**
*
* Whether to use cast-shadow to the model.
*
* @type {boolean}
* @default false
* @memberof GLTFModelProps
*
**/
castShadow?: boolean
/**
*
* Whether to use receive-shadow to the model.
*
* @type {boolean}
* @default false
* @memberof GLTFModelProps
*
**/
receiveShadow?: boolean
/**
*
* The path to the Draco decoder.
Expand All @@ -40,9 +60,13 @@ const props = withDefaults(
path: string
draco?: boolean
decoderPath?: string
castShadow?: boolean
receiveShadow?: boolean
}>(),
{
draco: false,
castShadow: false,
receiveShadow: false,
decoderPath: 'https://www.gstatic.com/draco/versioned/decoders/1.4.1/',
},
)
Expand All @@ -56,6 +80,14 @@ const { scene: model } = await useGLTF(props.path as string, {
draco: props.draco,
decoderPath: props.decoderPath,
})
if ( props.castShadow || props.receiveShadow ) {
model.traverse((child) => {
if (child.isMesh) {
child.castShadow = props.castShadow
child.receiveShadow = props.receiveShadow
}
})
}
</script>

<template>
Expand Down

0 comments on commit 471e28a

Please sign in to comment.