vanilladocs
vanillapunch.com
API / core/graphic/shaderprogram

core/graphic/shaderprogram

셰이더 프로그램.

Shader program.

ShaderProgramextends Object

짝Counterpart: vanilla-engine ShaderProgram

셰이더 프로그램.

  • 버텍스/프래그먼트 셰이더의 컴파일과 링크, 로케이션 캐시를 담당.

Shader program.

  • Handles compiling and linking vertex/fragment shaders and the location cache.
예제Example셰이더를 바꿔 끼워 흑백으로 그리기Swap in a shader to draw in grayscale
import { Engine, EngineConfiguration, Scene, Vector2, Rect, ShaderProgram } from "./build/vanilla.js";

const VERTEX_SHADER_SOURCE = `#version 300 es
in vec2 vertexPosition;
in vec2 vertexTextureCoordinate;
uniform mat3 projectionMatrix;
uniform mat3 modelMatrix;
out vec2 fragmentTextureCoordinate;
void main() {
	vec3 transformedPosition = projectionMatrix * (modelMatrix * vec3(vertexPosition, 1.0));
	gl_Position = vec4(transformedPosition.xy, 0.0, 1.0);
	fragmentTextureCoordinate = vertexTextureCoordinate;
}
`;
const GRAY_FRAGMENT_SHADER_SOURCE = `#version 300 es
precision highp float;
in vec2 fragmentTextureCoordinate;
uniform sampler2D mainTexture;
uniform vec4 mainColor;
uniform float globalAlpha;
out vec4 outputColor;
void main() {
	vec4 textureColor = texture(mainTexture, fragmentTextureCoordinate);
	vec3 color = textureColor.rgb * mainColor.rgb;
	float gray = dot(color, vec3(0.299, 0.587, 0.114));
	float alpha = textureColor.a * mainColor.a * globalAlpha;
	outputColor = vec4(vec3(gray) * mainColor.a * globalAlpha, alpha);
}
`;

class MainScene extends Scene {
	initialize(engine) {
		const graphic = engine.getGraphic();
		const webGL2RenderingContext = graphic.getWebGL2RenderingContext();
		const defaultShaderProgram = graphic.getShaderProgram();
		const attributeLocationTable = {
			vertexPosition: defaultShaderProgram.getAttributeLocation("vertexPosition"),
			vertexTextureCoordinate: defaultShaderProgram.getAttributeLocation("vertexTextureCoordinate"),
		};
		this.grayShaderProgram = new ShaderProgram(webGL2RenderingContext, VERTEX_SHADER_SOURCE, GRAY_FRAGMENT_SHADER_SOURCE, attributeLocationTable);
	}

	draw(graphic) {
		super.draw(graphic);
		graphic.setFillColor("#ff6600");
		graphic.drawRect(Rect.create(60, 120, 200, 120));
		graphic.setShaderProgramOverride(this.grayShaderProgram);
		graphic.drawRect(Rect.create(380, 120, 200, 120));
		graphic.setShaderProgramOverride(null);
	}
}

const engineConfiguration = new EngineConfiguration();
engineConfiguration.referenceResolutionSize = Vector2.create(640, 360);
const engine = new Engine(engineConfiguration);
engine.run(new MainScene());
생성자constructorconstructor(webGL2RenderingContext, vertexShaderSource, fragmentShaderSource, attributeLocationTable = null, isDeferred = false)#0.3.0 부터since 0.3.0소스source

생성.

  • 컴파일이나 링크에 실패하면 그때까지 지은 셰이더와 프로그램을 지우고 오류를 던진다.

Constructor.

  • If compiling or linking fails, it deletes the shaders and program created so far and throws an error.
webGL2RenderingContextWebGL2RenderingContext
WebGL2 렌더링 컨텍스트.WebGL2 rendering context.
vertexShaderSourcestring
버텍스 셰이더 GLSL 소스.Vertex shader GLSL source.
fragmentShaderSourcestring
프래그먼트 셰이더 GLSL 소스.Fragment shader GLSL source.
attributeLocationTableobject | null
링크 전에 고정할 어트리뷰트 위치. ({ 이름: 위치 }, 다른 프로그램의 버텍스 어레이를 함께 쓸 때)Attribute locations fixed before linking. ({ name: location }, when sharing vertex arrays with another program)
isDeferredboolean
true 면 컴파일과 링크를 맡기기만 하고 결과는 확인하지 않는다. (기본 false, finishBuild 를 불러야 쓸 수 있다)If true, only hands off compiling and linking without checking the result. (Default false; call finishBuild before use)

짝Counterpart: vanilla-engine ShaderProgram.new

정적staticstatic isParallelCompileSupported(webGL2RenderingContext)#0.4.0 부터since 0.4.0소스source

병렬 셰이더 컴파일 확장이 있는지. (KHR_parallel_shader_compile, 있으면 컴파일과 링크를 맡기고 주 스레드를 막지 않고 끝을 물을 수 있다)

Whether the parallel shader compile extension exists. (KHR_parallel_shader_compile; if present, compiling and linking can be handed off and completion polled without blocking the main thread)

webGL2RenderingContextWebGL2RenderingContext
WebGL2 렌더링 컨텍스트.WebGL2 rendering context.

반환Returnsboolean 있으면 true.True if present.

짝Counterpart: vanilla-engine ShaderProgram.is_parallel_compile_supported

메서드methodisBuildComplete()#0.4.0 부터since 0.4.0소스source

맡긴 짓기가 끝났는지. (맡기지 않았거나 이미 끝냈으면 true, 확장이 없으면 true, 물을 뿐 주 스레드를 막지 않는다)

Whether the handed-off build is done. (True if nothing was handed off or it is already finished, or if the extension is missing; it only asks and never blocks the main thread)

반환Returnsboolean 끝났으면 true. (true 여도 finishBuild 로 결과를 확인해야 쓴다)True if done. (Even when true, call finishBuild to check the result before use)

짝Counterpart: vanilla-engine ShaderProgram.is_build_complete

메서드methodfinishBuild()#0.4.0 부터since 0.4.0소스source

맡긴 짓기 끝내기. (컴파일과 링크 결과를 확인한다, 안 끝났으면 끝날 때까지 기다린다, 맡기지 않았거나 이미 끝냈으면 아무것도 하지 않는다)

  • 실패하면 셰이더와 프로그램을 지우고 오류를 던진다. (생성자와 같은 글)

Finishes the handed-off build. (Checks the compile and link results and waits until done if not; does nothing if nothing was handed off or it is already finished)

  • On failure, deletes the shaders and program and throws. (Same message as the constructor)

짝Counterpart: vanilla-engine ShaderProgram.finish_build

메서드methodcreateShader(shaderType, shaderSource)#0.3.0 부터since 0.3.0소스source

셰이더 객체 생성.

Creates a shader object.

shaderTypeGLenum
셰이더 갈래. (VERTEX_SHADER 또는 FRAGMENT_SHADER)Shader type. (VERTEX_SHADER or FRAGMENT_SHADER)
shaderSourcestring
GLSL 소스.GLSL source.

반환ReturnsWebGLShader 컴파일한 셰이더. (컴파일에 실패하면 오류를 던진다)The compiled shader. (throws an error if compiling fails)

메서드methoduseProgram()#0.4.0 부터since 0.4.0소스source

프로그램 사용 시작. (컨텍스트의 상태 캐시가 이미 이 프로그램을 쓴다고 기억하면 다시 걸지 않는다)

Starts using the program. (Does not set it again when the context's state cache remembers that this program is already in use)

짝Counterpart: vanilla-engine ShaderProgram.use_program

메서드methodgetAttributeLocation(attributeName)#0.3.0 부터since 0.3.0소스source

어트리뷰트 로케이션 반환. (캐시)

Returns the attribute location. (cached)

attributeNamestring
어트리뷰트 이름.Attribute name.

반환ReturnsGLint 어트리뷰트 위치. (없으면 -1)Attribute location. (-1 if none)

짝Counterpart: vanilla-engine ShaderProgram.get_attribute_location

메서드methodgetUniformLocation(uniformName)#0.3.0 부터since 0.3.0소스source

유니폼 로케이션 반환. (캐시)

Returns the uniform location. (cached)

uniformNamestring
유니폼 이름.Uniform name.

반환ReturnsWebGLUniformLocation 유니폼 위치. (없으면 null)Uniform location. (null if none)

짝Counterpart: vanilla-engine ShaderProgram.get_uniform_location

메서드methodgetWebGL2RenderingContext()#0.3.0 부터since 0.3.0소스source

렌더링 컨텍스트 반환.

Returns the rendering context.

반환ReturnsWebGL2RenderingContext 렌더링 컨텍스트.Rendering context.

메서드methodgetProgram()#0.3.0 부터since 0.3.0소스source

프로그램 객체 반환.

Returns the program object.

반환ReturnsWebGLProgram 링크한 프로그램 객체.The linked program object.

짝Counterpart: vanilla-engine ShaderProgram.get_program