-
-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(Outline): add component, demo, docs (#532)
* squash * chore: lint * feat(Outline): add component, demo, docs * chore(shaderMaterial): destructure import * docs(Outline): update playground demo * chore: lint --------- Co-authored-by: alvarosabu <[email protected]>
- Loading branch information
1 parent
ad916a2
commit 5bae4de
Showing
10 changed files
with
286 additions
and
15 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<script setup lang="ts"> | ||
import { TresCanvas } from '@tresjs/core' | ||
import { OrbitControls, Outline } from '@tresjs/cientos' | ||
</script> | ||
|
||
<template> | ||
<TresCanvas clear-color="#4f4f4f"> | ||
<TresPerspectiveCamera /> | ||
<OrbitControls /> | ||
<TresAmbientLight :intensity="3.14" /> | ||
<TresPointLight :intensity="50" :position="[2, 2, 0]" /> | ||
<TresMesh :position-x="-0.75"> | ||
<TresBoxGeometry /> | ||
<TresMeshPhongMaterial /> | ||
<Outline :thickness="7.5" color="#82dbc5" /> | ||
</TresMesh> | ||
<TresMesh :position-x="0.75"> | ||
<TresSphereGeometry :args="[0.5]" /> | ||
<TresMeshPhongMaterial /> | ||
<Outline :thickness="7.5" color="#fbb03b" /> | ||
</TresMesh> | ||
</TresCanvas> | ||
</template> |
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,22 @@ | ||
# Outline | ||
|
||
`<Outline />` creates an inverted-hull outline using its parent's geometry. Supported parents are `<TresMesh>` and `<TresSkinnedMesh>`. | ||
|
||
<DocsDemo> | ||
<OutlineDemo /> | ||
</DocsDemo> | ||
|
||
## Usage | ||
|
||
<<< @/.vitepress/theme/components/OutlineDemo.vue | ||
|
||
## Props | ||
|
||
| Props | Description | Default | | ||
|--------------|--------------------------------------------------------------------| ------- | | ||
| color | Outline color | `'black'` | | ||
| screenspace | Whether line thickness is independent of zoom | `false` | | ||
| opacity | Outline opacity | `1` | | ||
| transparent | Outline transparency | `false` | | ||
| thickness | Outline thickness | `0.05` | | ||
| angle | Geometry crease angle (`0` is no crease). See [BufferGeometryUtils.toCreasedNormals](https://threejs.org/docs/#examples/en/utils/BufferGeometryUtils.toCreasedNormals) | `Math.PI` | |
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,31 @@ | ||
<script setup lang="ts"> | ||
import { TresCanvas } from '@tresjs/core' | ||
import { OrbitControls, Outline } from '@tresjs/cientos' | ||
const thickness = shallowRef(1) | ||
const color = shallowRef('black') | ||
let elapsed = 0 | ||
let intervalId: ReturnType<typeof setInterval> | ||
onMounted(() => { | ||
intervalId = setInterval(() => { | ||
elapsed += 0.01 * 1000 / 30 | ||
thickness.value = (1 + Math.sin(elapsed)) * 5 | ||
color.value = Math.cos(elapsed) > 0 ? 'blue' : 'orange' | ||
}, 1000 / 30) | ||
}) | ||
onUnmounted(() => clearInterval(intervalId)) | ||
</script> | ||
|
||
<template> | ||
<TresCanvas> | ||
<TresPerspectiveCamera :position="[0, 0, 10]" /> | ||
<OrbitControls /> | ||
<TresMesh> | ||
<TresTorusKnotGeometry /> | ||
<TresMeshBasicMaterial /> | ||
<Outline :thickness="thickness" :color="color" /> | ||
</TresMesh> | ||
</TresCanvas> | ||
</template> |
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,59 @@ | ||
import { Color, Vector2 } from 'three' | ||
import { shaderMaterial } from '../../../utils/shaderMaterial' | ||
|
||
// NOTE: Source | ||
// https://github.com/pmndrs/drei/blob/master/src/core/Outlines.tsx | ||
|
||
const OutlineMaterialImpl = shaderMaterial( | ||
{ | ||
screenspace: false, | ||
color: new Color('black'), | ||
opacity: 1, | ||
thickness: 0.05, | ||
size: new Vector2(1, 1), | ||
}, | ||
`#include <common> | ||
#include <morphtarget_pars_vertex> | ||
#include <skinning_pars_vertex> | ||
uniform float thickness; | ||
uniform bool screenspace; | ||
uniform vec2 size; | ||
void main() { | ||
#if defined (USE_SKINNING) | ||
#include <beginnormal_vertex> | ||
#include <morphnormal_vertex> | ||
#include <skinbase_vertex> | ||
#include <skinnormal_vertex> | ||
#include <defaultnormal_vertex> | ||
#endif | ||
#include <begin_vertex> | ||
#include <morphtarget_vertex> | ||
#include <skinning_vertex> | ||
#include <project_vertex> | ||
vec4 tNormal = vec4(normal, 0.0); | ||
vec4 tPosition = vec4(transformed, 1.0); | ||
#ifdef USE_INSTANCING | ||
tNormal = instanceMatrix * tNormal; | ||
tPosition = instanceMatrix * tPosition; | ||
#endif | ||
if (screenspace) { | ||
vec3 newPosition = tPosition.xyz + tNormal.xyz * thickness; | ||
gl_Position = projectionMatrix * modelViewMatrix * vec4(newPosition, 1.0); | ||
} else { | ||
vec4 clipPosition = projectionMatrix * modelViewMatrix * tPosition; | ||
vec4 clipNormal = projectionMatrix * modelViewMatrix * tNormal; | ||
vec2 offset = normalize(clipNormal.xy) * thickness / size * clipPosition.w * 2.0; | ||
clipPosition.xy += offset; | ||
gl_Position = clipPosition; | ||
} | ||
}`, | ||
`uniform vec3 color; | ||
uniform float opacity; | ||
void main(){ | ||
gl_FragColor = vec4(color, opacity); | ||
#include <tonemapping_fragment> | ||
#include <colorspace_fragment> | ||
}`, | ||
) | ||
|
||
export default OutlineMaterialImpl |
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,126 @@ | ||
<script setup lang="ts"> | ||
import type { TresColor } from '@tresjs/core' | ||
import { normalizeColor, useTres } from '@tresjs/core' | ||
import type { BufferGeometry, Group } from 'three' | ||
import { BackSide, InstancedMesh, Mesh, SkinnedMesh, Vector2 } from 'three' | ||
import { onMounted, onUnmounted, shallowRef, watch } from 'vue' | ||
import OutlineMaterialImpl from './OutlineMaterialImpl' | ||
import { toCreasedNormals } from 'three-stdlib' | ||
// NOTE: Source | ||
// https://github.com/pmndrs/drei/blob/master/src/core/Outlines.tsx | ||
interface OutlineProps { | ||
/** Outline color, default: black */ | ||
color?: TresColor | ||
/** Line thickness is independent of zoom, default: false */ | ||
screenspace?: boolean | ||
/** Outline opacity, default: 1 */ | ||
opacity?: number | ||
/** Outline transparency, default: false */ | ||
transparent?: boolean | ||
/** Outline thickness, default 0.05 */ | ||
thickness?: number | ||
/** Geometry crease angle (-1 === no crease), default: Math.PI, See [BufferGeometryUtils.toCreasedNormals](https://threejs.org/docs/#examples/en/utils/BufferGeometryUtils.toCreasedNormals) */ | ||
angle?: number | ||
toneMapped?: boolean | ||
polygonOffset?: boolean | ||
polygonOffsetFactor?: number | ||
renderOrder?: number | ||
} | ||
const props = withDefaults(defineProps<OutlineProps>(), { | ||
color: 'black', | ||
opacity: 1, | ||
transparent: false, | ||
screenspace: false, | ||
toneMapped: true, | ||
polygonOffset: false, | ||
polygonOffsetFactor: 0, | ||
renderOrder: 0, | ||
thickness: 0.05, | ||
angle: Math.PI, | ||
}) | ||
const groupRef = shallowRef() | ||
defineExpose({ instance: groupRef }) | ||
const material = new OutlineMaterialImpl({ ...props }) | ||
const contextSize = new Vector2(1, 1) | ||
let oldAngle = 0 | ||
let oldGeometry: BufferGeometry | null = null | ||
function updateMesh(group: Group) { | ||
const parent = group.parent as Mesh & SkinnedMesh & InstancedMesh | ||
if (!parent || !parent.geometry) { return } | ||
if (oldAngle !== props.angle || oldGeometry !== parent.geometry) { | ||
oldAngle = props.angle | ||
oldGeometry = parent.geometry | ||
// NOTE: Remove old mesh | ||
let mesh = group.children?.[0] as any | ||
if (mesh) { | ||
if (props.angle) { mesh.geometry.dispose() } | ||
group.remove(mesh) | ||
} | ||
if (parent.skeleton) { | ||
mesh = new SkinnedMesh() | ||
mesh.material = material | ||
mesh.bind(parent.skeleton, parent.bindMatrix) | ||
group.add(mesh) | ||
} | ||
else if (parent.isInstancedMesh) { | ||
mesh = new InstancedMesh(parent.geometry, material, parent.count) | ||
mesh.instanceMatrix = parent.instanceMatrix | ||
group.add(mesh) | ||
} | ||
else { | ||
mesh = new Mesh() | ||
mesh.material = material | ||
group.add(mesh) | ||
} | ||
mesh.geometry = props.angle ? toCreasedNormals(parent.geometry, props.angle) : parent.geometry | ||
} | ||
} | ||
function updateMaterial() { | ||
material.side = BackSide | ||
material.transparent = props.transparent | ||
material.thickness = props.thickness | ||
material.color = normalizeColor(props.color) | ||
material.opacity = props.opacity | ||
material.size = contextSize | ||
material.screenspace = props.screenspace | ||
material.toneMapped = props.toneMapped | ||
material.polygonOffset = props.polygonOffset | ||
material.polygonOffsetFactor = props.polygonOffsetFactor | ||
} | ||
const sizes = useTres().sizes | ||
watch(() => [sizes.width.value, sizes.height.value], ([w, h]) => { | ||
contextSize.set(w, h) | ||
}) | ||
watch(() => [props.angle], () => { | ||
if (groupRef.value) { updateMesh(groupRef.value) } | ||
}) | ||
watch(() => [props.transparent, props.thickness, props.color, props.opacity, contextSize, props.screenspace, props.toneMapped, props.polygonOffset, props.polygonOffsetFactor], () => updateMaterial(), { immediate: true }, | ||
) | ||
onMounted(() => updateMesh(groupRef.value)) | ||
onUnmounted(() => { | ||
const mesh = groupRef.value?.children[0] as Mesh | ||
if (mesh) { | ||
mesh.geometry.dispose() | ||
material.dispose() | ||
mesh.removeFromParent() | ||
} | ||
}) | ||
</script> | ||
|
||
<template> | ||
<TresGroup ref="groupRef" /> | ||
</template> |
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