项目作者: marcofugaro

项目描述 :
🕺 Some cool ass iridescent effect
高级语言: JavaScript
项目地址: git://github.com/marcofugaro/webgl-iridescence-twerk.git
创建时间: 2019-10-15T12:50:31Z
项目社区:https://github.com/marcofugaro/webgl-iridescence-twerk

开源协议:MIT License

下载


webgl-iridescence-twerk

Some cool ass iridescent effect

Built with threejs-modern-app.

Highlights

  • The iridescence effects boils down to colors mapped to the fresnel effect. I explored three ways of doing this:
  1. Use the mod() function to map the whole HSL color wheel to the fresnel value. The full code is here.
  1. // circle the whole hue wheel,
  2. // the function looks like this /|/|/|/|/
  3. float f = mod(iridescence, 1.0);
  4. vec3 iridescentColor = hsl2rgb(f, 1.0, 0.5);
  1. Alternate between two colors with a ping-pong function. The full code is here.
  1. // alternate between two colors,
  2. // the function looks like this /\/\/\/\/
  3. float f = abs(1.0 - mod(iridescence, 2.0));
  4. vec3 iridescentColor = mix(firstColor, secondColor, f);
  1. Cycle between more than two colors using weights.
  1. float f = iridescence;
  2. // the step values
  3. float step1 = 0.0;
  4. float step2 = 0.33;
  5. float step3 = 0.66;
  6. float step4 = 1.0;
  7. // the step colors
  8. vec3 firstColor = vec3(...);
  9. vec3 secondColor = vec3(...);
  10. vec3 thirdColor = vec3(...);
  11. vec3 fourthColor = vec3(...); // this must be the same of the first color to prevent a harsh color change
  12. // this is a gradient step
  13. // first color is the front one, the last color is the perpendicular one
  14. vec3 iridescentColor = mix(firstColor, secondColor, smoothstep(step1, step2, f));
  15. iridescentColor = mix(iridescentColor, thirdColor, smoothstep(step2, step3, f));
  16. iridescentColor = mix(iridescentColor, fourthColor, smoothstep(step3, step4, f));

This is similar to the ColorRamp node in Blender.

  • The normals after the vertex shader displacement in the hills were calculated using this method. Relevant code is here.
  1. // http://lolengine.net/blog/2013/09/21/picking-orthogonal-vector-combing-coconuts
  2. vec3 orthogonal(vec3 v) {
  3. return normalize(abs(v.x) > abs(v.z) ? vec3(-v.y, v.x, 0.0)
  4. : vec3(0.0, -v.z, v.y));
  5. }
  6. // the function which defines the displacement
  7. float frequency = 0.4;
  8. float speed = 0.05;
  9. float displace(vec3 point) {
  10. // distance from center
  11. float distance = hypot(point.xz);
  12. float amplitude = pow(distance, 2.0) * 0.05;
  13. return (noise(vec3(point.xz * frequency, timeFixed * speed)) * 0.5 + 0.5) * amplitude;
  14. }
  15. // ...
  16. vec3 displacedPosition = position + normal * displace(position);
  17. float offset = 0.01;
  18. vec3 tangent = orthogonal(normal);
  19. vec3 bitangent = normalize(cross(normal, tangent));
  20. vec3 neighbour1 = position + tangent * offset;
  21. vec3 neighbour2 = position + bitangent * offset;
  22. vec3 displacedNeighbour1 = neighbour1 + normal * displace(neighbour1);
  23. vec3 displacedNeighbour2 = neighbour2 + normal * displace(neighbour2);
  24. // https://i.ya-webdesign.com/images/vector-normals-tangent-16.png
  25. vec3 displacedTangent = displacedNeighbour1 - displacedPosition;
  26. vec3 displacedBitangent = displacedNeighbour2 - displacedPosition;
  27. // https://upload.wikimedia.org/wikipedia/commons/d/d2/Right_hand_rule_cross_product.svg
  28. vec3 displacedNormal = normalize(cross(displacedTangent, displacedBitangent));
  • The model was downloaded from threedscans.com, I then t-posed it in Blender:
Before After

After that, I used Mixamo to attach the animation to the mesh.

Usage

Once you installed the dependencies running yarn, these are the available commands:

  • yarn start starts a server locally
  • yarn build builds the project for production, ready to be deployed from the build/ folder

All the build tools logic is in the package.json and webpack.config.js.

Read the full documentation for a more in-depth guide