-
Notifications
You must be signed in to change notification settings - Fork 23
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
TypeScript and React Three Fiber support #22
Comments
I will migrate it to TS soon. Basically, I'm nearly finished, but there are some issues with rollup I can't figure out ;-; |
Sweet! TS stuff sounds great, and no worries with the React stuff. I'll put something up in the future when I get things working 😄 |
TS migration done in v3.0.0 |
Thanks mate! For the type defs you'll need to do some work https://github.com/StrandedKitty/three-csm/blob/master/package.json#L7 They're currently pointing to a file that doesn't exist. Pointing to CSM.d.ts gets it working. Looking at the generated types some are off:
I can contribute anything here if you'd like. |
@itsdouges regarding react, it wouldn't need changes from a library, it will work ootb. trying it here but getting shaders errors from csm, did you figure this out? https://codesandbox.io/s/exemple-basique-avec-tentative-csm-ver2-forked-5cywrd?file=/src/App.js ERROR: 0:1703: '[]' : array index out of range
ERROR: 0:1703: '[]' : array index out of range
ERROR: 0:1705: '[]' : array index out of range
ERROR: 0:1705: '[]' : array index out of range
1698: getDirectionalLightInfo( directionalLight, geometry, directLight );
1699:
1700: #if defined( USE_SHADOWMAP ) && ( 4 < 5 )
1701:
1702: directionalLightShadow = directionalLightShadows[ 4 ];
> 1703: if(linearDepth >= CSM_cascades[4].x && linearDepth < CSM_cascades[4].y) directLight.color *= all( bvec2( |
figured it out from another github issue about that |
I can link code if you want - I had to defer instantiation of the class similar to some three Controls. Which issue did you use? |
someone had the same issue above, you can't have lights that castShadow in your scene, that's now disallowed with csm. |
This is the code I used for CSM, your shader errors are probably from CSM instantiating a few times because it adds scene objects immediately... when instantiated. import { useFrame, useThree } from '@react-three/fiber';
import { useLayoutEffect, useMemo } from 'react';
import { OrthographicCamera, PerspectiveCamera, Vector3, Vector3Tuple } from 'three';
import CSM, { CSMParams } from 'three-csm';
interface CascadedShadowMapProps extends Omit<CSMParams, 'lightDirection' | 'camera' | 'parent'> {
fade?: boolean;
lightDirection?: Vector3Tuple;
}
class CSMProxy {
instance: CSM | undefined;
args: CSMParams;
constructor(args: CSMParams) {
this.args = args;
}
set fade(fade: boolean) {
if (this.instance) {
this.instance.fade = fade;
}
}
set camera(camera: PerspectiveCamera | OrthographicCamera) {
if (this.instance) {
this.instance.camera = camera;
}
}
set lightDirection(vector: Vector3 | Vector3Tuple) {
if (this.instance) {
this.instance.lightDirection = Array.isArray(vector)
? new Vector3().fromArray(vector).normalize()
: vector;
}
}
attach() {
this.instance = new CSM(this.args);
}
dispose() {
if (this.instance) {
this.instance.dispose();
}
}
}
export function CascadedShadowMap({
maxFar = 50,
shadowMapSize = 1024,
lightIntensity = 0.25,
cascades = 2,
fade,
lightDirection = [1, -1, 1],
shadowBias = 0.000001,
customSplitsCallback,
lightFar,
lightMargin,
lightNear,
mode,
}: CascadedShadowMapProps) {
const camera = useThree((three) => three.camera);
const parent = useThree((three) => three.scene);
const proxyInstance = useMemo(
() =>
new CSMProxy({
camera,
cascades,
customSplitsCallback,
lightDirection: new Vector3().fromArray(lightDirection).normalize(),
lightFar,
lightIntensity,
lightMargin,
lightNear,
maxFar,
mode,
parent,
shadowBias,
shadowMapSize,
}),
// These values will cause CSM to re-instantiate itself.
// This is an expensive operation and should be avoided.
// eslint-disable-next-line react-hooks/exhaustive-deps
[
// Values that can be updated during runtime are omitted from this deps check.
cascades,
customSplitsCallback,
fade,
lightFar,
lightIntensity,
lightMargin,
lightNear,
maxFar,
mode,
shadowBias,
shadowMapSize,
]
);
useFrame(() => {
if (proxyInstance && proxyInstance.instance) {
proxyInstance.instance.update();
}
});
useLayoutEffect(() => {
proxyInstance.attach();
return () => {
proxyInstance.dispose();
};
}, [proxyInstance]);
return (
<primitive object={proxyInstance} camera={camera} fade={fade} lightDirection={lightDirection} />
);
} |
Hello! Found your library through the threejs examples, it looks great! Currently I'm trying to integrate it into my point & click game and found some friction points:
Would you consider adding support for both? Types would either be the creation of a typedef file or converting to TS, and R3F would be following https://docs.pmnd.rs/react-three-fiber/tutorials/typescript#extending-threeelements. A few libraries come with support such as https://github.com/FarazzShaikh/THREE-CustomShaderMaterial
The text was updated successfully, but these errors were encountered: