2019-07-03 02:56:33 +08:00
|
|
|
import React, { ReactNode, Component } from "react";
|
2019-07-03 00:08:06 +08:00
|
|
|
import { SkinnedMesh } from "three";
|
|
|
|
import { get_model_renderer } from "../../rendering/ModelRenderer";
|
2019-07-01 01:55:30 +08:00
|
|
|
|
|
|
|
type Props = {
|
2019-07-03 00:08:06 +08:00
|
|
|
model?: SkinnedMesh;
|
|
|
|
};
|
2019-07-01 01:55:30 +08:00
|
|
|
|
2019-07-03 02:56:33 +08:00
|
|
|
export class RendererComponent extends Component<Props> {
|
2019-07-01 01:55:30 +08:00
|
|
|
private renderer = get_model_renderer();
|
|
|
|
|
2019-07-03 02:56:33 +08:00
|
|
|
render(): ReactNode {
|
2019-07-03 00:08:06 +08:00
|
|
|
return <div style={{ overflow: "hidden" }} ref={this.modifyDom} />;
|
2019-07-01 01:55:30 +08:00
|
|
|
}
|
|
|
|
|
2019-07-03 02:56:33 +08:00
|
|
|
componentDidMount(): void {
|
2019-07-03 00:08:06 +08:00
|
|
|
window.addEventListener("resize", this.onResize);
|
2019-07-01 01:55:30 +08:00
|
|
|
}
|
|
|
|
|
2019-07-03 02:56:33 +08:00
|
|
|
componentWillUnmount(): void {
|
2019-07-03 00:08:06 +08:00
|
|
|
window.removeEventListener("resize", this.onResize);
|
2019-07-01 01:55:30 +08:00
|
|
|
}
|
|
|
|
|
2019-07-03 02:56:33 +08:00
|
|
|
componentWillReceiveProps({ model }: Props): void {
|
2019-07-01 01:55:30 +08:00
|
|
|
this.renderer.set_model(model);
|
|
|
|
}
|
|
|
|
|
2019-07-03 02:56:33 +08:00
|
|
|
shouldComponentUpdate(): boolean {
|
2019-07-01 01:55:30 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
private modifyDom = (div: HTMLDivElement | null) => {
|
|
|
|
if (div) {
|
|
|
|
this.renderer.set_size(div.clientWidth, div.clientHeight);
|
|
|
|
div.appendChild(this.renderer.dom_element);
|
|
|
|
}
|
2019-07-03 00:08:06 +08:00
|
|
|
};
|
2019-07-01 01:55:30 +08:00
|
|
|
|
|
|
|
private onResize = () => {
|
2019-07-02 23:00:24 +08:00
|
|
|
const wrapper_div = this.renderer.dom_element.parentNode as HTMLDivElement;
|
|
|
|
this.renderer.set_size(wrapper_div.clientWidth, wrapper_div.clientHeight);
|
2019-07-03 00:08:06 +08:00
|
|
|
};
|
2019-07-01 01:55:30 +08:00
|
|
|
}
|