项目需要使用LayaBox做一个给3D物体动态描边的一个功能,前前后后踩了一些坑算是把这个东西给做出来了把我改好的代码贴上,分享给大家。
(1)想要使用描边你的引擎版本必须 >=2.9.0
(2)你必须下载 Laya官方的BlurShader库才能使用这个功能,下载地址:https://github.com/layabox/layaair-demo/tree/master/h5/3d/ts/LayaAir3D_PostProcess/BlurShader
下载完之后把BlurShader放到自己项目的代码目录里使用
以下是我使用 CommandBuffer_Outline 的类,使用的时候调用 AddCommandBuffet_Outline(要描边的物体),不用的时候记得调用RemoveCommandBuffer_Outline移除掉
import InsManager from "../manager/InsManager";
import {
BlurEffect, BlurMaterial } from "../shader/BlurShader/BlurEffect";
export class CommandBuffer_Outline {
private commandBuffer: Laya.CommandBuffer;
private cameraEventFlag: Laya.CameraEventFlags = Laya.CameraEventFlags.BeforeImageEffect;
private camera: Laya.Camera;
private renders:Laya.BaseRender[] = [];
private materials:Laya.Material[] = [];
private viewPort: Laya.Viewport = null;
public static instance: CommandBuffer_Outline ;
private isUseOuline: boolean = true; // 控制是否使用描边
public static GetInstance(): CommandBuffer_Outline{
if(this.instance == null) this.instance = new CommandBuffer_Outline();
return this.instance;
}
public init(): void {
//使用之前必须先初始化
if(this.camera == null){
BlurEffect.init();
this.camera = InsManager.GetInstance()._SceneManager.player.playerCamera; //传入你照射物体的相机
this.viewPort = this.camera.viewport;
//Laya.Shader3D.debugMode = true;
}
}
private renderTexture: Laya.RenderTexture = null;
private subRendertexture: Laya.RenderTexture = null;
private downRenderTexture: Laya.RenderTexture = null;
private blurTexture: Laya.RenderTexture = null;
private buf: Laya.CommandBuffer = null;
public createDrawMeshCommandBuffer(camera: