mirror of
https://gitee.com/dgflash/oops-framework.git
synced 2026-06-09 19:22:11 +08:00
初次提交
This commit is contained in:
35
assets/script/core/common/texture/RtToModel.ts
Normal file
35
assets/script/core/common/texture/RtToModel.ts
Normal file
@@ -0,0 +1,35 @@
|
||||
import { Camera, Component, gfx, MeshRenderer, RenderTexture, view, _decorator } 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();
|
||||
}
|
||||
}
|
||||
9
assets/script/core/common/texture/RtToModel.ts.meta
Normal file
9
assets/script/core/common/texture/RtToModel.ts.meta
Normal file
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"ver": "4.0.22",
|
||||
"importer": "typescript",
|
||||
"imported": true,
|
||||
"uuid": "ed72a01b-9451-4614-828a-9a41072a27ea",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {}
|
||||
}
|
||||
105
assets/script/core/common/texture/RtToSprite.ts
Normal file
105
assets/script/core/common/texture/RtToSprite.ts
Normal file
@@ -0,0 +1,105 @@
|
||||
import { Camera, Component, EventTouch, gfx, Node, RenderTexture, Sprite, SpriteFrame, SystemEventType, UITransform, Vec3, _decorator } from 'cc';
|
||||
const { ccclass, property } = _decorator;
|
||||
|
||||
/** 三维模型显示到二维精灵上 */
|
||||
@ccclass('RtToSprite')
|
||||
export class RtToSprite extends Component {
|
||||
@property({
|
||||
type: Camera,
|
||||
tooltip: "渲染模型的三维摄像机"
|
||||
})
|
||||
camera: Camera | null = null;
|
||||
|
||||
@property({
|
||||
type: Sprite,
|
||||
tooltip: "显示模型的二维精灵组件"
|
||||
})
|
||||
sprite: Sprite | null = null;
|
||||
|
||||
@property({
|
||||
tooltip: "是否触摸控制旋转"
|
||||
})
|
||||
rotation: boolean = false;
|
||||
|
||||
@property({
|
||||
type: Node,
|
||||
tooltip: "三维模型",
|
||||
visible: function () {
|
||||
//@ts-ignore
|
||||
return this.rotation === true;
|
||||
},
|
||||
})
|
||||
model: Node | null = null;
|
||||
|
||||
private rt: RenderTexture = new RenderTexture();
|
||||
private touched = false; // 是否触摸节点
|
||||
|
||||
start() {
|
||||
let size = this.sprite!.getComponent(UITransform)!;
|
||||
this.refreshRenderTexture(size.width, size.height);
|
||||
|
||||
if (this.rotation) {
|
||||
this.sprite!.node.on(SystemEventType.TOUCH_START, this.onTouchStart, this);
|
||||
this.sprite!.node.on(SystemEventType.TOUCH_MOVE, this.onTouchMove, this);
|
||||
this.sprite!.node.on(SystemEventType.TOUCH_END, this.onTouchEnd, this);
|
||||
this.sprite!.node.on(SystemEventType.TOUCH_CANCEL, this.onTouchEnd, this);
|
||||
}
|
||||
}
|
||||
|
||||
protected onTouchStart(event: EventTouch) {
|
||||
this.touched = true;
|
||||
}
|
||||
|
||||
protected onTouchMove(event: EventTouch) {
|
||||
if (this.touched) {
|
||||
let eulerAngles: Vec3 = this.model!.eulerAngles;
|
||||
let deltaX = event.touch!.getDelta().x!;
|
||||
eulerAngles.y += -deltaX;
|
||||
this.model!.eulerAngles = eulerAngles;
|
||||
}
|
||||
}
|
||||
|
||||
protected onTouchEnd(event: EventTouch) {
|
||||
this.touched = false;
|
||||
}
|
||||
|
||||
/** 刷新纹理内容 */
|
||||
refreshRenderTexture(w: number, h: number): void {
|
||||
const colorAttachment = new gfx.ColorAttachment();
|
||||
const depthStencilAttachment = new gfx.DepthStencilAttachment();
|
||||
const pi = new gfx.RenderPassInfo([colorAttachment], depthStencilAttachment, []);
|
||||
|
||||
this.rt.reset({
|
||||
width: w,
|
||||
height: h,
|
||||
passInfo: pi
|
||||
});
|
||||
|
||||
let spriteframe: SpriteFrame = this.sprite!.spriteFrame!;
|
||||
let sp: SpriteFrame = new SpriteFrame();
|
||||
sp.reset({
|
||||
originalSize: spriteframe.originalSize,
|
||||
rect: spriteframe.rect,
|
||||
offset: spriteframe.offset,
|
||||
isRotate: spriteframe.rotated,
|
||||
borderTop: spriteframe.insetTop,
|
||||
borderLeft: spriteframe.insetLeft,
|
||||
borderBottom: spriteframe.insetBottom,
|
||||
borderRight: spriteframe.insetRight,
|
||||
});
|
||||
|
||||
this.camera!.targetTexture = this.rt;
|
||||
sp.texture = this.rt;
|
||||
this.sprite!.spriteFrame = sp;
|
||||
}
|
||||
|
||||
onDestroy() {
|
||||
if (this.rotation) {
|
||||
this.sprite!.node.off(SystemEventType.TOUCH_START, this.onTouchStart, this);
|
||||
this.sprite!.node.off(SystemEventType.TOUCH_MOVE, this.onTouchMove, this);
|
||||
this.sprite!.node.off(SystemEventType.TOUCH_END, this.onTouchEnd, this);
|
||||
this.sprite!.node.off(SystemEventType.TOUCH_CANCEL, this.onTouchEnd, this);
|
||||
}
|
||||
this.rt.destroy();
|
||||
}
|
||||
}
|
||||
9
assets/script/core/common/texture/RtToSprite.ts.meta
Normal file
9
assets/script/core/common/texture/RtToSprite.ts.meta
Normal file
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"ver": "4.0.22",
|
||||
"importer": "typescript",
|
||||
"imported": true,
|
||||
"uuid": "a266c942-aa61-45ac-9dcd-7ad398427989",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {}
|
||||
}
|
||||
Reference in New Issue
Block a user