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

fix: cannot assign readonly properties primitives #698

Open
wants to merge 71 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
71 commits
Select commit Hold shift + click to select a range
224ab06
feat: 474 vue chrome devtools plugin (#479)
alvarosabu Dec 22, 2023
e760525
chore: release v4.0.0-next.0
alvarosabu Dec 22, 2023
c8e79bc
Merge branch 'main' into v4
alvarosabu Jan 2, 2024
5fad3b8
feat: update to three `v160` and vue `v3.4` (#488)
alvarosabu Jan 2, 2024
a63eb90
fix(types): added `Object3DEventMap` to `Object3D` generics for point…
alvarosabu Jan 2, 2024
83f0e06
Merge branch 'main' into v4
alvarosabu Jan 17, 2024
f688c64
feat: 140 on demand rendering (#497)
alvarosabu Jan 23, 2024
8bbafde
feat: remove default camera warning (#499)
alvarosabu Jan 23, 2024
c4547f9
feat: 492 set tone mapping default to acesfilmictonemapping (#498)
alvarosabu Jan 23, 2024
08717ef
feat: 516 localstate for custom renderer node instances instead of us…
alvarosabu Feb 1, 2024
898a8b1
Merge branch 'main' into v4
alvarosabu Feb 2, 2024
52dad5c
fix: camera aspect
andretchen0 Feb 5, 2024
79d8a76
feat: 503 conditional rendering of primitives (#514)
alvarosabu Feb 21, 2024
1fa9385
Merge branch 'main' into v4
alvarosabu Mar 4, 2024
1a705b1
Merge branch 'v4' into fix/camera-aspect
alvarosabu Mar 6, 2024
97dcd7b
Merge pull request #538 from Tresjs/fix/camera-aspect
andretchen0 Mar 6, 2024
ddc229e
fix: `nodeOps` is now a function (#579)
alvarosabu Mar 8, 2024
963e662
chore: next package json version
alvarosabu Mar 18, 2024
47713bc
chore: release v4.0.0-next.1
alvarosabu Mar 18, 2024
8efe6e0
Merge branch 'main' into v4
alvarosabu Mar 20, 2024
cd0c3bc
fix: refactor nodeOps to return methods at the end of the function (#…
alvarosabu Mar 26, 2024
a6578a7
chore: internal playground organisation (#601)
alvarosabu Mar 27, 2024
5f0979e
chore: deps update
alvarosabu Mar 27, 2024
18375e4
chore: internal primitive model test playground
alvarosabu Mar 27, 2024
affd17c
chore: fix lint
alvarosabu Mar 27, 2024
bd1909c
chore: release v4.0.0-next.2
alvarosabu Mar 27, 2024
05069ab
chore: misc routes
alvarosabu Mar 31, 2024
906f2e1
fix: do not change pierced props case (#608)
andretchen0 Mar 31, 2024
fe37d7c
chore: lint fix
alvarosabu Mar 31, 2024
9d217d6
Merge branch 'main' into v4
alvarosabu Apr 9, 2024
4b7fb86
chore: problem with package version
alvarosabu Apr 9, 2024
1429905
chore: fix lint
alvarosabu Apr 9, 2024
b5aae35
Merge branch 'v4' of https://github.com/Tresjs/tres into v4
andretchen0 Apr 12, 2024
bc12f0b
chore: rebuild pnpm-lock
andretchen0 Apr 12, 2024
e3874f9
test(nodeOps): clean up tests
andretchen0 Apr 12, 2024
0cf1771
test(nodeOps): organize tests
andretchen0 Apr 12, 2024
576cf6c
test: add coverage plugin
andretchen0 Apr 12, 2024
fbcaa41
test: add coverage to package.json script
andretchen0 Apr 13, 2024
6fd8c08
test(nodeOps): improve test coverage
andretchen0 Apr 13, 2024
986543c
Merge branch 'main' into v4
alvarosabu Apr 16, 2024
1ad7b8b
Merge branch 'main' into v4
alvarosabu Apr 16, 2024
cdf6b6f
feat: devtools renderer improvements (#614)
alvarosabu Apr 18, 2024
b536ab1
feat(events)!: pointerevents manager and state (#529)
garrlker Apr 22, 2024
5f20467
Merge branch 'main' into v4
alvarosabu Apr 23, 2024
e98ca6d
feat: 499 better memory management (#606)
alvarosabu Apr 24, 2024
15ef7a2
Merge branch 'main' into v4
alvarosabu Apr 24, 2024
0badfb1
chore: fix lint
alvarosabu Apr 24, 2024
d4e1fe2
chore: type issues (#663)
alvarosabu Apr 25, 2024
17d3b29
chore: release v4.0.0-rc.0
alvarosabu Apr 25, 2024
a3d7bbe
chore: merge v4 updates
andretchen0 Apr 26, 2024
1121eb1
Merge pull request #637 from Tresjs/test/nodeOps-organize
andretchen0 Apr 26, 2024
ac152df
fix: make on* callbacks settable (#672)
andretchen0 May 3, 2024
1b2fa70
feat: 633 use loop (#673)
alvarosabu May 14, 2024
96960d1
chore(playground): adding missing import and removing the directives …
alvarosabu May 15, 2024
6be07a5
chore(playground): use new composable on animations
alvarosabu May 15, 2024
e5a2cef
fix(utils): reorder object disposal to avoid issue with Helper `dispo…
alvarosabu May 15, 2024
59b8139
Merge branch 'main' into v4
alvarosabu May 15, 2024
119a5ac
Merge branch 'v4' of github.com:Tresjs/tres into v4
alvarosabu May 15, 2024
22b0f26
chore: updated deps
alvarosabu May 15, 2024
f840614
chore: release v4.0.0-rc.1
alvarosabu May 15, 2024
0720d18
fix: manual rendering blank (#685)
alvarosabu May 18, 2024
a41f532
fix: 686 useloop callback state missing controls (#687)
alvarosabu May 22, 2024
ea71654
chore(deps): update deps 24-0-2024
alvarosabu May 24, 2024
dac4d7c
chore: release v4.0.0-rc.2
alvarosabu May 24, 2024
43c3229
fix: start loop if user calls useRenderLoop (#695)
andretchen0 May 27, 2024
2788afe
fix: manually iterate all writable properties and asign transform wit…
alvarosabu May 27, 2024
cc5cadb
chore: fix lint
alvarosabu May 27, 2024
37a9a1a
fix: alternative that doesnt break the tests
alvarosabu May 27, 2024
4afed5b
chore: lint fix
alvarosabu May 27, 2024
b6b53ca
Merge branch 'main' into fix/cannot-assign-readonly-properties-primit…
alvarosabu May 31, 2024
6506921
Merge branch 'main' into fix/cannot-assign-readonly-properties-primit…
alvarosabu May 31, 2024
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
343 changes: 343 additions & 0 deletions playground/src/pages/advanced/cientos/ContactShadowsLocal.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,343 @@
<!-- eslint-disable no-console -->
<script setup lang="ts">
// The author of the original code is @mrdoob https://twitter.com/mrdoob
// https://threejs.org/examples/?q=con#webgl_shadow_contact

// As well, basically the same implementation as in pmndrs drei but with Vue Composition API
// https://github.com/pmndrs/drei/blob/master/src/core/ContactShadows.tsx#L113

import type { TresColor } from '@tresjs/core'
import { useLoop, useTresContext } from '@tresjs/core'
import type {
ColorRepresentation,
Material,
Texture,
} from 'three'
import {
Color,
Mesh,
MeshDepthMaterial,
OrthographicCamera,
PlaneGeometry,
ShaderMaterial,
WebGLRenderTarget,
} from 'three'
import { computed, shallowRef, watchEffect } from 'vue'
import { HorizontalBlurShader, VerticalBlurShader } from 'three-stdlib'
import { useThrottleFn } from '@vueuse/core'

export interface ContactShadowsProps {
/**
*
* The opacity of the shadows.
*
* @default 1
* @type {number}
* @memberof ContactShadowsProps
*
*/
opacity?: number
/**
* The width of the shadows.
*
* @default 1
* @type {number}
* @memberof ContactShadowsProps
*
*/
width?: number
/**
* The height of the shadows.
*
* @default 1
* @type {number}
* @memberof ContactShadowsProps
*
*/
height?: number
/**
* The blur of the shadows.
*
* @default 1
* @type {number}
* @memberof ContactShadowsProps
*
*/
blur?: number
/**
* How far the OrthographicCamera should be to capture the shadows.
*
* @default 10
* @type {number}
* @memberof ContactShadowsProps
*
*/
far?: number
/**
* Whether the shadows should be smooth or not.
*
* @default true
* @type {boolean}
* @memberof ContactShadowsProps
*
*/
smooth?: boolean
/**
* The resolution of the shadows.
*
* @default 512
* @type {number}
* @memberof ContactShadowsProps
*
*/
resolution?: number
/**
* The number of frames to render the shadows.
*
* @default Infinity
* @type {number}
* @memberof ContactShadowsProps
*
*/
frames?: number
/**
* The scale of the shadows.
*
* @default 10
* @type {(number | [x: number, y: number])}
* @memberof ContactShadowsProps
*
*/
scale?: number | [x: number, y: number]
/**
* The color of the shadows.
*
* @default '#000000'
* @type {TresColor}
* @memberof ContactShadowsProps
*
*/
color?: TresColor
/**
* Whether the shadows should write to the depth buffer or not.
*
* @default false
* @type {boolean}
* @memberof ContactShadowsProps
*
*/
depthWrite?: boolean
/**
* Whether the OrthographicCamera helper should be visible or not.
*
* @default false
* @type {boolean}
* @memberof ContactShadowsProps
*
*/
helper?: boolean
}

const props = withDefaults(defineProps<ContactShadowsProps>(), {
scale: 10,
frames: Number.POSITIVE_INFINITY,
opacity: 1,
width: 1,
height: 1,
blur: 1,
far: 10,
resolution: 512,
smooth: true,
color: '#000000',
depthWrite: false,
helper: false,
})

const groupRef = shallowRef()
const shadowCamera = shallowRef<OrthographicCamera>()

/* defineExpose(groupRef) */

let renderTarget: WebGLRenderTarget, renderTargetBlur: WebGLRenderTarget
let planeGeometry: PlaneGeometry, blurPlane: Mesh
let depthMaterial: MeshDepthMaterial

const { renderer, scene } = useTresContext()

const cameraW = computed(() => props.width * (Array.isArray(props.scale) ? props.scale[0] : props.scale || 1))
const cameraH = computed(() => props.height * (Array.isArray(props.scale) ? props.scale[1] : props.scale || 1))

watchEffect(() => {
// the render target that will show the shadows in the plane texture and
// the target that we will use to blur the first render target
if (renderTarget) { renderTarget.dispose() }
if (renderTargetBlur) { renderTargetBlur.dispose() }
if (planeGeometry) { planeGeometry.dispose() }
if (blurPlane) { blurPlane.geometry.dispose() }
renderTarget = new WebGLRenderTarget(props.resolution, props.resolution)
renderTargetBlur = new WebGLRenderTarget(props.resolution, props.resolution)
renderTargetBlur.texture.generateMipmaps = renderTarget.texture.generateMipmaps = false

shadowCamera.value = new OrthographicCamera(
-cameraW.value / 2,
cameraW.value / 2,
cameraH.value / 2,
-cameraH.value / 2,
0,
props.far,
)

planeGeometry = new PlaneGeometry(cameraW.value, cameraH.value).rotateX(Math.PI / 2)
blurPlane = new Mesh(planeGeometry)
blurPlane.visible = false
console.log('shadowCamera', {
left: shadowCamera.value.left,
right: shadowCamera.value.right,
top: shadowCamera.value.top,
bottom: shadowCamera.value.bottom,
near: shadowCamera.value.near,
far: shadowCamera.value.far,
cameraH: cameraH.value,
cameraW: cameraW.value,
})
})

watchEffect(() => {
if (props.color) {
if (depthMaterial) { depthMaterial.dispose() }
depthMaterial = new MeshDepthMaterial()
depthMaterial.depthTest = depthMaterial.depthWrite = false

// Overwrite depthMaterial sahders
depthMaterial.onBeforeCompile = (shader) => {
shader.uniforms = {
...shader.uniforms,
ucolor: { value: props.color ? new Color(props.color as ColorRepresentation) : new Color() },
}
shader.fragmentShader = shader.fragmentShader.replace(
'void main() {', //
`uniform vec3 ucolor;
void main() {
`,
)
shader.fragmentShader = shader.fragmentShader.replace(
'vec4( vec3( 1.0 - fragCoordZ ), opacity );',
// Colorize the shadow, multiply by the falloff so that the center can remain darker
'vec4( ucolor * fragCoordZ * 2.0, ( 1.0 - fragCoordZ ) * 1.0 );',
)
}
}
})

// make a plane and make it face up

// Initialize the blur shaders
const horizontalBlurMaterial = new ShaderMaterial(HorizontalBlurShader)
const verticalBlurMaterial = new ShaderMaterial(VerticalBlurShader)
verticalBlurMaterial.depthTest = horizontalBlurMaterial.depthTest = false

// Blur the shadow

function blurShadows(blur: number) {
if (!renderer.value || !shadowCamera.value) { return }

blurPlane.visible = true
blurPlane.material = horizontalBlurMaterial
horizontalBlurMaterial.uniforms.tDiffuse.value = renderTarget.texture
horizontalBlurMaterial.uniforms.h.value = blur / 256

renderer.value.setRenderTarget(renderTargetBlur)
renderer.value.render(blurPlane, unref(shadowCamera.value))

blurPlane.material = verticalBlurMaterial
verticalBlurMaterial.uniforms.tDiffuse.value = renderTargetBlur.texture
verticalBlurMaterial.uniforms.v.value = blur / 256

renderer.value.setRenderTarget(renderTarget)
renderer.value.render(blurPlane, unref(shadowCamera.value))

blurPlane.visible = false
}

let count = 0
let initialBackground: Color | Texture | null
let initialOverrideMaterial: Material | null

const { onBeforeRender } = useLoop()
const log = useThrottleFn(state => console.log('updating sphere', state), 3000)

onBeforeRender(() => {
if (!shadowCamera.value || scene.value === undefined || renderer.value === undefined) { return }

log({
left: shadowCamera.value.left,
right: shadowCamera.value.right,
top: shadowCamera.value.top,
bottom: shadowCamera.value.bottom,
near: shadowCamera.value.near,
far: shadowCamera.value.far,
cameraH: cameraH.value,
cameraW: cameraW.value,
})

if (props.frames === Number.POSITIVE_INFINITY || count < props.frames) {
count++

// Save the initial background and override material
initialBackground = scene.value.background
initialOverrideMaterial = scene.value.overrideMaterial

// Render the shadows
groupRef.value.visible = false
scene.value.background = null
scene.value.overrideMaterial = depthMaterial

renderer.value.setRenderTarget(renderTarget)
renderer.value.render(scene.value, unref(shadowCamera.value))

// Blur the shadows
blurShadows(props.blur)

if (props.smooth) {
blurShadows(props.blur * 0.4)
}

// Restore the initial background and override material
renderer.value.setRenderTarget(null)

groupRef.value.visible = true
scene.value.background = initialBackground
scene.value.overrideMaterial = initialOverrideMaterial
}
}, -1)
</script>

<template>
<TresGroup
ref="groupRef"
v-bind="$attrs"
>
<TresMesh
:scale="[1, -1, 1]"
:geometry="planeGeometry"
>
<TresMeshBasicMaterial
:map="renderTarget.texture"
:opacity="opacity"
:depth-write="depthWrite"
:transparent="true"
/>
</TresMesh>
<primitive :object="blurPlane" />

<TresCameraHelper
v-if="shadowCamera && helper"
:args="[shadowCamera]"
/>
<TresOrthographicCamera
ref="shadowCamera"
:position="[0, 0, 0]"
:rotation="[Math.PI / 2, 0, 0]"
/>
</TresGroup>
</template>
Loading
Loading