-
Notifications
You must be signed in to change notification settings - Fork 46
/
standard-pipeline.ts
146 lines (131 loc) · 4.59 KB
/
standard-pipeline.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import { ObjectRenderer, Renderer } from "@pixi/core"
import { MaterialRenderPass } from "./material-render-pass"
import { Mesh3D } from "../mesh/mesh"
import { ShadowRenderPass } from "../shadow/shadow-render-pass"
import { CompositeSprite } from "../sprite/composite-sprite"
import { CompositeSpriteOptions } from "../sprite/composite-sprite-options"
import { Model } from "../model"
import { ShadowCastingLight } from "../shadow/shadow-casting-light"
import { RenderPass } from "./render-pass"
import { StandardMaterial } from "../material/standard/standard-material"
import { MaterialRenderSortType } from "../material/material-render-sort-type"
import { Compatibility } from "../compatibility/compatibility"
import { SpriteBatchRenderer } from "../sprite/sprite-batch-renderer"
import { ProjectionSprite } from "../sprite/projection-sprite"
/**
* The standard pipeline renders meshes using the set render passes. It's
* created and used by default.
*/
export class StandardPipeline extends ObjectRenderer {
protected _spriteRenderer: SpriteBatchRenderer
protected _meshes: Mesh3D[] = []
protected _sprites: ProjectionSprite[] = []
/** The pass used for rendering materials. */
materialPass = new MaterialRenderPass(this.renderer, "material")
/** The pass used for rendering shadows. */
shadowPass = new ShadowRenderPass(this.renderer, "shadow")
/** The array of render passes. Each mesh will be rendered with these passes (if it has been enabled on that mesh). */
renderPasses: RenderPass[] = [
this.shadowPass, this.materialPass,
]
/**
* Creates a new standard pipeline using the specified renderer.
* @param renderer The renderer to use.
*/
constructor(public renderer: Renderer) {
super(renderer)
renderer.on("prerender", () => {
for (let pass of this.renderPasses) {
if (pass.clear) { pass.clear() }
}
})
this._spriteRenderer = new SpriteBatchRenderer(renderer)
}
/**
* Adds an object to be rendered.
* @param object The object to render.
*/
render(object: Mesh3D | ProjectionSprite) {
if (object.isSprite) {
this._sprites.push(<ProjectionSprite>object)
} else {
this._meshes.push(<Mesh3D>object)
}
}
/**
* Renders the added meshes using the specified render passes.
*/
flush() {
this.sort()
for (let pass of this.renderPasses) {
pass.render(this._meshes.filter(mesh => mesh.isRenderPassEnabled(pass.name)))
}
this._meshes = []
if (this._sprites.length > 0) {
this._spriteRenderer.start()
for (let sprite of this._sprites) {
// @ts-ignore
this._spriteRenderer.render(sprite)
}
this._spriteRenderer.stop()
this._sprites = []
}
}
/**
* Sorts the meshes by rendering order.
*/
sort() {
this._meshes.sort((a, b) => {
if (!a.material || !b.material) {
return 0
}
if (a.material.renderSortType !== b.material.renderSortType) {
return a.material.renderSortType === MaterialRenderSortType.transparent ? 1 : -1
}
if (a.renderSortOrder === b.renderSortOrder) {
return 0
}
return a.renderSortOrder < b.renderSortOrder ? -1 : 1
})
this._sprites.sort((a, b) => {
if (a.zIndex !== b.zIndex) {
return a.zIndex - b.zIndex;
}
return b.distanceFromCamera - a.distanceFromCamera;
})
}
/**
* Enables shadows for the specified object. Adds the shadow render pass to
* the specified object and enables the standard material to use the casting
* light.
* @param object The mesh or model to enable shadows for.
* @param light The shadow casting light to associate with the
* object when using the standard material.
*/
enableShadows(object: Mesh3D | Model, light?: ShadowCastingLight) {
let meshes = object instanceof Model ? object.meshes : [object]
for (let mesh of meshes) {
if (light && mesh.material instanceof StandardMaterial) {
mesh.material.shadowCastingLight = light
}
mesh.enableRenderPass(this.shadowPass.name)
}
if (light) {
this.shadowPass.addShadowCastingLight(light)
}
}
/**
* Disables shadows for the specified object.
* @param object The mesh or model to disable shadows for.
*/
disableShadows(object: Mesh3D | Model) {
let meshes = object instanceof Model ? object.meshes : [object]
for (let mesh of meshes) {
if (mesh.material instanceof StandardMaterial) {
mesh.material.shadowCastingLight = undefined
}
mesh.disableRenderPass(this.shadowPass.name)
}
}
}
Compatibility.installRendererPlugin("pipeline", StandardPipeline)