forked from Orillusion/orillusion
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(outline): add outlineManager (Orillusion#97)
add outline manager to support control of outline effects The outline effect cannot display properly and has been fixed.
- Loading branch information
Showing
3 changed files
with
143 additions
and
85 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import { Object3D, Color, MeshRenderer, outlinePostData } from "@orillusion/core"; | ||
|
||
/** | ||
* manager of outline effect | ||
* @group IO | ||
*/ | ||
export class OutlinePostManager { | ||
|
||
private _tempIndexArray: number[] = []; | ||
|
||
/** | ||
* config outline manager. | ||
* Specify specific 3D objects to use the specified color for display outline | ||
* @param objectList A set of 3D objects | ||
* @param color Specified color for outline | ||
*/ | ||
public setOutline(objectList: Object3D[], color?: Color) { | ||
this.setOutlineList([objectList], color ? [color] : null); | ||
} | ||
|
||
/** | ||
* config outline manager. | ||
* The first set of objects uses the first color to display outline, and so on | ||
* @param groupList A group of 3D objects set | ||
* @param colorList Specified color list for outline | ||
*/ | ||
public setOutlineList(groupList: Object3D[][], colorList?: Color[]) { | ||
groupList ||= []; | ||
let defaultColor = outlinePostData.defaultColor; | ||
let maxGroup = outlinePostData.SlotCount; | ||
for (let i = 0; i < maxGroup; i++) { | ||
this._tempIndexArray.length = 0; | ||
let group = groupList[i]; | ||
let color = (colorList ? colorList[i] : null) || defaultColor; | ||
if (group) { | ||
for (const item of group) { | ||
this.getEntityIdList(item, this._tempIndexArray); | ||
} | ||
} | ||
outlinePostData.fillDataAt(i, this._tempIndexArray, color); | ||
} | ||
} | ||
|
||
/** | ||
* clear outline effect | ||
*/ | ||
public clearOutline(): this { | ||
outlinePostData.clear(); | ||
return this; | ||
} | ||
|
||
private _rendererList: MeshRenderer[] = []; | ||
|
||
private getEntityIdList(item: Object3D, target: number[]): void { | ||
this._rendererList.length = 0; | ||
let renderers = item.getComponents(MeshRenderer, this._rendererList); | ||
for (const render of renderers) { | ||
target.push(render.object3D.transform._worldMatrix.index); | ||
} | ||
} | ||
|
||
|
||
} | ||
|
||
/** | ||
* @internal | ||
*/ | ||
export let outlinePostManager: OutlinePostManager = new OutlinePostManager(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import { Engine3D } from "../Engine3D"; | ||
import { Color } from "../math/Color"; | ||
|
||
export class OutlinePostSlot { | ||
public indexList: Float32Array; | ||
public color: Color; | ||
public count: number; | ||
} | ||
|
||
export class OutlinePostData { | ||
//Supports up to 8 sets of colors | ||
public readonly SlotCount: number = 8; | ||
public readonly MaxEntities: number = 16; | ||
public readonly defaultColor: Color = new Color(0.2, 1, 1, 1); | ||
private readonly slots: OutlinePostSlot[] = []; | ||
|
||
private dataDirty: boolean = true; | ||
|
||
constructor() { | ||
let groupCount = Engine3D.setting.render.postProcessing.outline.groupCount; | ||
this.SlotCount = Math.max(1, Math.min(groupCount, this.SlotCount)); | ||
for (let i = 0; i < this.SlotCount; i++) { | ||
let slot: OutlinePostSlot = (this.slots[i] = new OutlinePostSlot()); | ||
slot.indexList = new Float32Array(this.MaxEntities); | ||
slot.color = this.defaultColor.clone(); | ||
slot.count = 0; | ||
} | ||
} | ||
|
||
public clear(): void { | ||
for (let i = 0; i < this.SlotCount; i++) { | ||
this.clearAt(i); | ||
} | ||
} | ||
|
||
public clearAt(slotIndex: number): this { | ||
this.dataDirty = true; | ||
let slot: OutlinePostSlot = this.slots[slotIndex]; | ||
slot.color.copyForm(this.defaultColor); | ||
slot.indexList.fill(-1); | ||
slot.count = 0; | ||
return this; | ||
} | ||
|
||
public fillDataAt(slot: number, indexList: number[], color: Color): this { | ||
this.dataDirty = true; | ||
let data = this.slots[slot]; | ||
if (data) { | ||
data.indexList.fill(-1); | ||
for (let i = 0, c = indexList.length; i < c; i++) { | ||
data.indexList[i] = indexList[i]; | ||
} | ||
data.count = indexList.length; | ||
data.color.copyForm(color); | ||
} | ||
return this; | ||
} | ||
|
||
public fetchData(target: { dirty: boolean; slots: OutlinePostSlot[] }): this { | ||
target.dirty = this.dataDirty; | ||
target.slots = this.slots; | ||
this.dataDirty = false; | ||
return this; | ||
} | ||
} | ||
|
||
export let outlinePostData: OutlinePostData = new OutlinePostData(); |