Class: Program
着色器程序对象封装,主要功能如下:
- 创建Program管线,编译顶点着色器和片段着色器源码;
- 管理
Attribute
属性 - 管理
Uniform
属性 - 渲染状态的切换
示例代码:
function render(props) { const drawModelVertex = ` attribute vec2 uv; attribute vec3 position; uniform mat4 modelViewMatrix; uniform mat4 projectionMatrix; varying vec2 vUv; void main() { vUv = uv; gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0); // gl_PointSize only applicable for gl.POINTS draw mode gl_PointSize = 5.0; } `; const drawModelFragment = ` precision highp float; uniform float uTime; varying vec2 vUv; void main() { gl_FragColor.rgb = 0.5 + 0.3 * sin(vUv.yxx + uTime) + vec3(0.2, 0.0, 0.1); gl_FragColor.a = 1.0; } `; const refDom = useRef(null); const init = () => { const canvas = refDom.current; canvas.width = canvas.clientWidth; canvas.height = canvas.clientHeight; const renderer = new Renderer(canvas, { alpha: true, }); const fov = 15; const nearZ = 0.1; const farZ = 100; const camera = new PerspectiveCamera(fov, canvas.width / canvas.height, nearZ, farZ); camera.position.z = 15; function resize(target) { const { width, height } = target.getBoundingClientRect(); renderer.setSize(width, height); camera.aspect = width / height; } const scene = new Scene(); const geometry = new Geometry(renderer, { position: { size: 3, data: new Float32Array([ -0.5, 0.5, 0, -0.5, -0.5, 0, 0.5, 0.5, 0, 0.5, -0.5, 0 ]) }, uv: { size: 2, data: new Float32Array([0, 1, 1, 1, 0, 0, 1, 0]) }, index: { data: new Uint16Array([0, 1, 2, 1, 3, 2]) }, }); const program = new Program(renderer, { vertexShader: drawModelVertex, fragmentShader: drawModelFragment, uniforms: { uTime: { value: 0 }, }, }); const points = new Mesh(renderer, { mode: renderer.gl.POINTS, geometry, program }); points.setParent(scene); points.position.set(-1, 1, 0); const lineStrip = new Mesh(renderer, { mode: renderer.gl.LINES, geometry, program }); lineStrip.setParent(scene); lineStrip.position.set(1, 1, 0); const lineLoop = new Mesh(renderer, { mode: renderer.gl.LINE_LOOP, geometry, program }); lineLoop.setParent(scene); lineLoop.position.set(-1, -1, 0); const triangles = new Mesh(renderer, { mode: renderer.gl.TRIANGLES, geometry, program }); triangles.setParent(scene); triangles.position.set(1, -1, 0); const raf = new Raf((t) => { program.setUniform('uTime', t); renderer.render({ scene, camera }); }); return { canvas, resize, } } useEffect(() => { const { canvas, resize } = init(); observe(canvas, resize); return () => { unobserve(canvas, resize); }; }, []); return ( <div className="live-wrap"> <canvas className="scene-canvas" ref={refDom}></canvas> </div> ); }
Hierarchy
-
↳
Program
Table of contents
Constructors
Properties
Accessors
Methods
- use
- setStates
- applyState
- setUniform
- bind
- unbind
- createHandle
- deleteHandle
- destroy
- swapHandle
- restoreHandle
- delete
- removeStats
- toString
Constructors
constructor
• new Program(renderer
, options?
): Program
Parameters
Name | Type |
---|---|
renderer | any |
options | Partial <ProgramOptions > |
Returns
Overrides
Defined in
Properties
renderer
• renderer: Renderer
Inherited from
Defined in
attributeOrder
• attributeOrder: string
Defined in
uniforms
• uniforms: any
Defined in
id
• id: string
Inherited from
Defined in
name
• name: WithUndef
<string
>
Inherited from
Defined in
userData
• userData: any
Inherited from
Defined in
byteLength
• byteLength: number
Inherited from
Defined in
options
• options: Partial
<ProgramOptions
>
Inherited from
Defined in
Accessors
gl
• get
gl(): WebGLRenderingContext
| WebGL2RenderingContext
获取 webgl
实例
Returns
WebGLRenderingContext
| WebGL2RenderingContext
Inherited from
Resource.gl
Defined in
rendererState
• get
rendererState(): State
获取渲染状态
Returns
Inherited from
Resource.rendererState
Defined in
uniformLocations
• get
uniformLocations(): Map
<any
, any
>
Returns
Map
<any
, any
>
Defined in
attributeLocations
• get
attributeLocations(): Map
<any
, any
>
Returns
Map
<any
, any
>
Defined in
vertexShader
• get
vertexShader(): VertexShader
获取 VertexShader
对象
Returns
VertexShader
Defined in
fragmentShader
• get
fragmentShader(): FragmentShader
获取 FragmentShader
对象
Returns
FragmentShader
Defined in
handle
• get
handle(): any
Returns
any
Inherited from
Resource.handle
Defined in
Methods
use
▸ use(): void
Returns
void
Defined in
setStates
▸ setStates(states
, merge?
): void
设置 Program 的 render state
Parameters
Name | Type | Default value | Description |
---|---|---|---|
states | Partial <ProgramRenderState > | undefined | |
merge | boolean | true | 是否使用合并模式,如果为 false 则直接替换 |
Returns
void
Defined in
applyState
▸ applyState(): void
Returns
void
Defined in
setUniform
▸ setUniform(key
, value
): void
设置对应的 Uniform 值
Parameters
Name | Type |
---|---|
key | any |
value | any |
Returns
void
Defined in
bind
▸ bind(): void
使用此 Program
Returns
void
Overrides
Defined in
unbind
▸ unbind(): void
取消使用此 Program
Returns
void
Overrides
Defined in
createHandle
▸ createHandle(): null
| WebGLProgram
Returns
null
| WebGLProgram
Overrides
Defined in
deleteHandle
▸ deleteHandle(): void
Returns
void
Overrides
Defined in
destroy
▸ destroy(): void
销毁此Program
Returns
void
Overrides
Defined in
swapHandle
▸ swapHandle(handle
): void
交换为新的 handle
(一般我们会用在虚拟 webgl 环境中)
Parameters
Name | Type |
---|---|
handle | any |
Returns
void
Inherited from
Defined in
restoreHandle
▸ restoreHandle(): void
恢复为原有 handle
Returns
void
Inherited from
Defined in
delete
▸ delete(«destructured»?
): Program
Parameters
Name | Type | Default value |
---|---|---|
«destructured» | Object | {} |
› deleteChildren | undefined | boolean | false |
Returns
Inherited from
Defined in
removeStats
▸ removeStats(): void
Returns
void
Inherited from
Defined in
toString
▸ toString(): string
Returns
string