Tresjs / tres

Declarative ThreeJS using Vue Components
https://tresjs.org
MIT License
2.28k stars 108 forks source link

LocalClippingEnabled #684

Open hthaigler opened 6 months ago

hthaigler commented 6 months ago

Describe the bug

I am trying to clip materials using local clipping planes, but no matter what I do, I am not able to access the localClippingEnabled attribute on the instance of the WebGLRenderer.

I think the most simple solution would be:

<TresCanvas ref="canvas" :shadows="true"  :local-clipping-enabled="true">
</TresCanvas>

... but this is not working.

I've tried using:

const { renderer } = useTresContext()
renderer.value.localClippingEnabled = true

but I get this error:

Uncaught TypeError: Cannot read properties of undefined (reading 'x')

Reproduction

https://stackblitz.com/edit/tresjs-basic-eaxcaf?file=src%2Fcomponents%2FTheExperience.vue

Steps to reproduce

No response

System Info

No response

Used Package Manager

npm

Code of Conduct

JaimeTorrealba commented 6 months ago

Hi I got this.

You can enable the localClippingEnabled by using the template ref (like in my example) or by using useTresContext

But I believe you got another problem related to the bound of clippingPlane null to the material.

<script setup lang="ts">
import { reactive, ref, shallowRef, watch } from 'vue';
import { BasicShadowMap, SRGBColorSpace, NoToneMapping } from 'three';
import { TresCanvas, vLog } from '@tresjs/core';
import { OrbitControls, ContactShadows } from '@tresjs/cientos';

const gl = {
  clearColor: '#82DBC5',
  shadows: true,
  alpha: false,
  shadowMapType: BasicShadowMap,
  outputColorSpace: SRGBColorSpace,
  toneMapping: NoToneMapping,
};

const clippingPlane = ref(null);

const ctxRef = shallowRef();

watch(ctxRef, (ctx) => {
  const { renderer } = ctx.context;
  renderer.value.localClippingEnabled = true;
  console.log(renderer.value.localClippingEnabled);
});
</script>

<template>
  <TresCanvas v-bind="gl" ref="ctxRef">
    <TresPerspectiveCamera :position="[9, 9, 9]" />
    <OrbitControls />
    <TresMesh>
      <TresBoxGeometry :args="[1, 1, 1]" />
      <TresMeshNormalMaterial /> // If I leave the null bound here throw error
    </TresMesh>
    <TresMesh :position="[0, 0.25, 0]" :rotation="[-Math.PI / 2, 0, 0]">
      <TresPlaneGeometry ref="clippingPlane" :args="[2, 2]" />
      <TresMeshBasicMaterial color="#555" :side="DoubleSide" />
    </TresMesh>
    <ContactShadows />
    <TresDirectionalLight :position="[0, 2, 4]" :intensity="1.2" cast-shadow />
  </TresCanvas>
</template>