Skip to content

Commit

Permalink
暴露灯光修改方法
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaxiangfeng committed Aug 8, 2023
1 parent 3677baa commit 4fe0e28
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 7 deletions.
14 changes: 14 additions & 0 deletions src/FBX/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,30 @@ function FBX(
render();
return renderer.current?.domElement.toDataURL('image/png', 1);
},
setLight: (type: 'directionalLight' | 'ambientLight', color?: string, intensity?: number) => {
const light: any = scene.current?.getObjectByName(type);
if (light) {
if (color !== undefined) {
light.color = new THREE.Color(color);
}
if (intensity !== undefined) {
light.intensity = intensity;
}
render();
}
},
}));

useEffect(() => {
const ambientLight = new THREE.AmbientLight(conf.ambientLightColor, conf.ambientLightIntensity);
ambientLight.name = 'ambientLight';
scene.current?.add(ambientLight);

const directionalLight = new THREE.DirectionalLight(
conf.directionalLightColor,
conf.directionalIntensity,
);
directionalLight.name = 'directionalLight';
directionalLight.position.set(1, 1, 0).normalize();

scene.current?.add(directionalLight);
Expand Down
44 changes: 38 additions & 6 deletions src/GLTF/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,44 @@ nav:
Demo:

```tsx
import React from 'react';
import React, { useRef } from 'react';
import Model from 'react-3dmodelx';

export default () => (
<div style={{ maxWidth: 800, width: '100%', height: 400, margin: 'auto' }}>
<Model.GLTF src="./chair1.gltf" />
</div>
);
export default () => {
const modelRef = useRef(null);
const setLight = (color) => {
modelRef.current.setLight('ambientLight', color, 2);
};
return (
<div style={{ maxWidth: 800, width: '100%', height: 400, margin: 'auto' }}>
<div>
<span
onClick={() => {
setLight('red');
}}
style={{ background: 'red', cursor: 'pointer' }}
>
</span>
<span
onClick={() => {
setLight('blue');
}}
style={{ background: 'blue', cursor: 'pointer' }}
>
</span>
<span
onClick={() => {
setLight('green');
}}
style={{ background: 'green', cursor: 'pointer' }}
>
绿
</span>
</div>
<Model.GLTF ref={(object) => (modelRef.current = object)} src="./chair1.gltf" />
</div>
);
};
```
16 changes: 15 additions & 1 deletion src/GLTF/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,30 @@ function GLTF(
render();
return renderer.current?.domElement.toDataURL('image/png', 1);
},
setLight: (type: 'directionalLight' | 'ambientLight', color?: string, intensity?: number) => {
const light: any = scene.current?.getObjectByName(type);
if (light) {
if (color !== undefined) {
light.color = new THREE.Color(color);
}
if (intensity !== undefined) {
light.intensity = intensity;
}
render();
}
},
}));

useEffect(() => {
const ambientLight = new THREE.AmbientLight(conf.ambientLightColor, conf.ambientLightIntensity);
ambientLight.name = 'ambientLight';
scene.current?.add(ambientLight);

console.log(scene.current);
const directionalLight = new THREE.DirectionalLight(
conf.directionalLightColor,
conf.directionalIntensity,
);
directionalLight.name = 'directionalLight';
directionalLight.position.set(1, 1, 0).normalize();

scene.current?.add(directionalLight);
Expand Down
14 changes: 14 additions & 0 deletions src/Group/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,30 @@ function Group(
render();
return renderer.current?.domElement.toDataURL('image/png', 1);
},
setLight: (type: 'directionalLight' | 'ambientLight', color?: string, intensity?: number) => {
const light: any = scene.current?.getObjectByName(type);
if (light) {
if (color !== undefined) {
light.color = new THREE.Color(color);
}
if (intensity !== undefined) {
light.intensity = intensity;
}
render();
}
},
}));

useEffect(() => {
const ambientLight = new THREE.AmbientLight(conf.ambientLightColor, conf.ambientLightIntensity);
ambientLight.name = 'ambientLight';
scene.current?.add(ambientLight);

const directionalLight = new THREE.DirectionalLight(
conf.directionalLightColor,
conf.directionalIntensity,
);
directionalLight.name = 'directionalLight';
directionalLight.position.set(1, 1, 0).normalize();

scene.current?.add(directionalLight);
Expand Down

0 comments on commit 4fe0e28

Please sign in to comment.