LECTURE 11 Procedural Generation Procedural Generation WHITE NOISE


















































- Slides: 50

LECTURE 11 Procedural Generation

Procedural Generation WHITE NOISE

What is noise? • Randomness • e. g. From 0 to 14 take a random number between 0 and 1 • By itself, it is jagged and not useful

White Noise // returns a pseudorandom noise value for a given seed #include <random> float Terrain: : noise(float seed) { std: : default_random_engine e(seed); std: : uniform_real_distribution<float> dis(0. 0 f, 1. 0 f); return dis(e); }

Procedural Generation VALUE NOISE

Value Noise • Smooth white noise by taking an average of neighbors • Turns white noise into something useful

Value Noise // returns a weighted average of the 9 points around the Vec 2 i v float Terrain: : value. Noise(glm: : ivec 2 vec) { // four corners, each multiplied by 1/16 float corners = ( noise(vec. x-1, vec. y-1) + noise(vec. x+1, vec. y-1) + noise(vec. x-1, vec. y+1) + noise(vec. x+1, vec. y+1) ) / 16 // four sides, each multiplied by 1/8 float sides = ( noise(vec. x-1, vec. y) + noise(vec. x+1, vec. y) + noise(vec. x, vec. y-1) + noise(vec. x, vec. y+1) ) / 8 // center, multiplied by 1/4 float center = noise(vec. x, vec. y) / 4 return center + sides + corners }

Procedural Generation INTERPOLATION

Interpolation • Most interpolation functions take three arguments. • a and b, the value to interpolate between. • t, a value between 0 and 1. – When t is 0, function returns a – When t is 1, function returns b

Interpolation • Option 1: linear interpolation • For values a and b and interpolation parameter t: • f = a * (1 - t) + b * t

Interpolation • Option 2: cosine interpolation • t’ = (1 - cos(t * pi)) / 2 • f = a * (1 - t’) + b * t’ • Slower, but much smoother

Interpolation • Option 3: cubic interpolation • t’ = 3 t 2 - 2 t 3 • f = a * (1 - t’) + b * t’ • Similar to cosine

Interpolation • Option 4: Perlin interpolation • t’ = 6 t 5 - 15 t 4 + 10 t 3 • f = a * (1 - t’) + b * t’ • Slightly slower than cubic • Super smooth

Fractional Coordinates • What if our x and y aren’t integers? • Just find the values along the vertices of the unit square and interpolate x 0, y 0 x 1, y 0 x, y x 0, y 1 x 1, y 1

Fractional Coordinates // returns the noise interpolated from the four nearest vertices float Terrain: : interpolated. Noise(glm: : vec 2 vec){ glm: : ivec 2 top. Left = glm: : ivec 2((int) vec. x, (int) vec. y); glm: : ivec 2 top. Right = glm: : ivec 2((int) vec. x + 1, (int) vec. y); glm: : ivec 2 bot. Left = glm: : ivec 2((int) vec. x, (int) vec. y + 1); glm: : ivec 2 bot. Right = glm: : ivec 2(int) vec. x + 1, (int) vec. y + 1); float dx = vec. x – ((int) vec. x); float dy = vec. y – ((int) vec. y); float top. Noise = interpolate(value. Noise(top. Left), value. Noise(top. Right), dx); float bot. Noise = interpolate(value. Noise(bot. Left), value. Noise(bot. Right), dx); return interpolate(top. Noise, bot. Noise, dy); }

Procedural Generation PERLIN NOISE

Perlin Noise Named for its creator, this guy, Ken Perlin. It’s a great way to make smooth, natural noise which can be used to create terrain, cloud patterns, wood grain, and more! But you’ll probably use it for terrain…

Recall: Value Noise • Smooth white noise by taking an average of neighbors • Turns white noise into something useful

Perlin Noise • Assign each vertex a pseudorandom gradient glm: : vec 2 gradient(glm: : ivec 2 vec) { float theta = noise(vec) * 2 * M_PI; return glm: : vec 2(cos(theta), sin(theta)); }

Perlin Noise • The noise value of each vertex is the dot product of its gradient and the vertex to the target point

Perlin Noise • Interpolate between the noise values of the four vertices (just like for value noise)

Procedural Generation PERLIN NOISE VS VALUE NOISE

Perlin Noise vs Value Noise • Value noise is easier • Perlin noise has fewer plateaus

Procedural Generation ADDING NOISE

Adding Noise Functions Freq. 1 2 4 8 Amp. 1 1/ 1/ 1/ Noise + 2 + 4 + result 8 =

A Good Noise Function • What does our noise function need? – Given an (x, y) pair and a seed, returns the same value between 0 and 1 every time • C++ PRNGs only take a single seed as an argument

A Good Noise Function • Hash the glm: : ivec 2 – Returns a single value that is unique to each pair – Will return the same value every time • Use this number to generate your seed

References • What follows is a lot of pseudocode that contains concepts that we haven’t discussed – Persistence, octaves, etc. • Use this website as a reference for value noise: – https: //web. archive. org/web/20160310084426/http: //freespace. vir gin. net/hugo. elias/models/m_perlin. htm – Also covers an even smoother version of cubic interpolation • Use this website as a reference for Perlin noise: – http: //flafla 2. github. io/2014/08/09/perlinnoise. html – We stole our Perlin noise pictures from them

Procedural Generation QUESTIONS?

LECTURE 10 Advanced Graphics

Advanced Graphics PARTICLES

Particles • What is a particle? – A particle is a tiny entity that is used in massive quantities to create a visually pleasing effect – Used to model effects like fire, liquids, hair, etc. – Conceptually old—first paper published in

Particles • What makes particles look good? • Fade out or get smaller linearly over their lifespan – Once the particle is completely gone or transparent, it can be removed from the world • Adding some kind of randomness to how they move – Starting position, velocity, acceleration, color, size, shape

Particles • Particles are great • But they are very slow if not done correctly • Things that make them slow: – It’s a lot of information to tick – It’s a lot of information to draw – There are way too many of them to consider doing collision detection against

Particles • What shape should my particles be? • Sphere? – Too many vertices • Triangle – Use a texture like this one – Rotate the triangle to always face the camera – This texture has a black background and no alpha information • Use graphics>enable. Blend. Test(Graphics: : BLEND_FUNC: : A DD) • This says take all of the background, and add the color of this particle on top of it • Particles that are denser will appear brighter

Particle Optimizations • Reduce the amount of information in your particles – Vector 3 position, Vector 3 velocity – maybe some noise values to make them scatter – Less information to tick • Don’t make your particles Game. Objects – Keep them in a separate list so that you can tick and draw them all at once – Binding the particle texture once and then drawing all your particles without unbinding and rebinding the texture is a HUGE improvement • Don’t collide them with each other – If you must have your particles collide, have them collide only with entities or terrain, not with each other – This means they also don’t need a shape, so they take up less space • Tick them in your draw loop – Only iterate over them once • Do them on the GPU (see CS 1230 lab)

Particles QUESTIONS?

LECTURE 11 Rigged Animation

What you’ll need in your engine • Skeleton or Rig class – Define the pose of a character model • Skinning – Deform character model based on position of all joints • Animations – Define keyframes poses – Interpolate pose between key frame poses – (Probably) Parse animation file type (e. g. . fbx) to create sequence of keyframe poses

Skeleton • Skeleton represented as a tree • Each joint is a node, may or may not have children • Store transformation at each joint relative to parent joint – Global transformation of a joint = global transformation of parent * local transformation of joint relative to parent • Like CS 123 Sceneview

Skinning • Consists of binding a skin (mesh) on top of a skeleton so that the mesh deform realistically when bones move • Useful for many animations

Skinning • Given a set of vertices representing your mesh, and a set of joints or bones: 1. Determine how much each vertex is affected by each bone 2. Calculate how the changing bone positions affect the vertices they influence • The first item is something you’ll do while creating content, the last is your engine feature

Skinning: Vertex Weights • Each vertex will have bone/joint ‘weights’ – Should sum to 1 – Each weight determines how much bone affects it • Each vertex can have up to a set number (usually around 3 -4) bones that affect it • Assigning weights will be done by hand in a program like Maya

Skinning: Vertex Transformation • Need to define “rest pose” of the mesh – Where the mesh vertices are when the skeleton is in standard T-pose – Extract the transformation of each vertex relative to associated bones • Next step: when pose changes deform vertices by interpolating transformation relative associated joints

Skinning: Vertex Transformation • Example: Linear Interpolation – Say we have joints 1 and 2 in the mesh – Let v be a vertex with joint weights 0. 2 and 0. 8 for joints 1 and 2 – Let v 1 be the position of vertex v in the coordinate system of joint 1 in the initial mesh pose – Let v 2 be the position of vertex v in the coordinate system of joint 2 in the initial mesh pose – Let T 1 be the transformation of joint 1, T 2 be the transformation of joint 2 – v = 0. 2 * T 1 * v 1 + 0. 8 * T 2 * v 2 • http: //scribblethink. org/Work/PSD/SSDtutorial. pdf • Other types of interpolation as well – Dual quaternion is the most popular

Skinning: Vertex Transformation • Probably want to perform this transformation in a vertex shader – This way – GPU operates in parallel on all vertices – Need some way of passing skeleton, and vertex weights to the GPU

Animations • Keyframes – At time t 0, joint j 3 rotation is x 0 – At time t 1, joint j 3 rotation is x 1 – Interpolate between these • Combination of tools – Ex. Joints animate with a function while character is moving, then interpolate back to resting position

Animations • Abstraction: build small animation pieces (ex. ‘swing. Arms’, ‘bounce. Tail’) and combine them for more complex behaviors • Programs like Maya have these features and many more – look for inspiration to make something complex

Animations • Loading animations from files – Use assimp – It can load all of the file types into common data structure – https: //github. com/assimp

Content Creation • Rigged animation takes good amount work to set up • But if you’re planning on creating content also expect to spend a lot time on making animations that look nice