Files
oops-plugin-framework/assets/libs/render-texture/RtToModel.ts
2024-11-12 18:30:10 +08:00

36 lines
1013 B
TypeScript

import { _decorator, Camera, Component, gfx, MeshRenderer, RenderTexture, view } from 'cc';
const { ccclass, property } = _decorator;
/** 三维摄像机内容显示到模型上 */
@ccclass('RtToModel')
export class RtToModel extends Component {
@property(Camera)
camara: Camera = null!;
@property(MeshRenderer)
model: MeshRenderer = null!;
private rt: RenderTexture = new RenderTexture();
start() {
const size = view.getVisibleSize();
const colorAttachment = new gfx.ColorAttachment();
const depthStencilAttachment = new gfx.DepthStencilAttachment();
const pi = new gfx.RenderPassInfo([colorAttachment], depthStencilAttachment);
this.rt.reset({
width: size.width,
height: size.height,
passInfo: pi
});
this.camara.targetTexture = this.rt;
const mat = this.model.material!;
mat.setProperty('mainTexture', this.rt);
}
onDestroy() {
this.rt.destroy();
}
}