URL
EMBED
Page 0:
Page 1: ShaderX5
2.7 Animating Vegetation Using GPU Programs
ohyecloudy@gmail.com
http://cafe.naver.com/shader.cafe
Page 2: Introduction
• 잔잔한 바람에 흔들리는 식물을 구현한다. • vertex animation을 사용. • static lightmap
유의 사항 : z-up right-hand 좌표계를 사용했다.
Page 3: 적용 가능한 식물
• • • • • • •
palms birches mushrooms grass weeds flowers scrub
Page 4: 구현 단계
• Animate Vegetation
– 일단 움직이게 한다.
개선
• Adding Chaos to Vertex Movement • Lightmap and Height Bias
Page 5: Vertex Shader
uniform float time; const float magnitude = 0.006; varying vec2 texCoords;
// varying : 모든 vertex마다 정의하고 pixel shader에서는 보간된 값을 갖는다.
void main() { float amplitude = magnitude * gl_Vertex.z;
vec4 wave = amplitude * vec4( sin(time), cos(time), 0, 0);
vec4 vert = gl_Vertex + wave; vert.w = 1.0;
}
gl_Position = gl_ModelViewProjectionMatrix * vert; texCoord = gl_MultiTexCoord0.xy;
Page 6: 문제점
• mesh group이 하나일 때는 그럴듯하다. • 복수의 mesh group일 때는 문제
– wave 변수인 time이 똑같다.
Page 7: 구현 단계
• Animate Vegetation • Adding Chaos to Vertex Movement
– 단조로운 움직임을 없앤다.
개선
• Lightmap and Height Bias
Page 8: Vertex Shader
vec4 wave = amplitude * vec4( sin(time), cos(time), 0, 0);
vec4 wave = amplitude * vec4( sin(time + gl_Vertex.x), cos(time + gl_Vertex.y), 0, 0); - Vertex 마다 다른 wave 값을 갖는다. - 움직임이 부드럽다.
- 이웃한 vertex끼리 차이가 작다.
Page 9: 문제점
• World 좌표로 계산한다. • amplitude 값이 높이에 비례한다.
• Z 좌표가 0인 object에 비해 50인 object 의 움직임이 너무 과하다.
Page 10: 구현 단계
• Animate Vegetation • Adding Chaos to Vertex Movement • Lightmap and Height Bias
– 라이트 적용 – Height bais를 사용한 amplitude 보정.
개선
Page 11: Vertex Shader
float amplitude = magnitude * (gl_Vertex.z);
float amplitude = magnitude * (gl_Vertex.z – gl_Color.a); Lightmap
R G
Vertex color
B
A Bias
Page 12: Fragment Shader(Pixel Shader)
uniform sampler2D baseMap; varying vec4 vertColor; varying vec2 texCoords; void main() { vec4 mapColor = texture2D(baseMap, texCoords); vec3 fragColor = mapColor.rgb * vertColor.rgb;
float trans = step(0.5, mapColor.a);
gl_FragColor = vec4(fragColor.rgb, trans);
}
• transparency artifact 제거
– 텍스쳐 필터링, 밉맵핑이 원인.
Page 13: