Pas2JS_WebGL_OBJ.html 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. <html>
  2. <head>
  3. <meta charset="utf-8"/>
  4. <script type="application/javascript" src="../Pas2JS_WebGL_OBJ.js"></script>
  5. </head>
  6. <body>
  7. <script type="application/glsl" id="vertex.glsl">
  8. attribute vec3 in_position;
  9. attribute vec2 in_texCoord;
  10. attribute vec3 in_normal;
  11. uniform mat4 projTransform;
  12. uniform mat4 viewTransform;
  13. uniform mat4 modelTransform;
  14. uniform mat4 inverseViewTransform;
  15. uniform vec3 lightPosition;
  16. varying vec3 toLight;
  17. varying vec3 surfaceNormal;
  18. varying vec3 toCamera;
  19. void main() {
  20. vec4 worldPosition = modelTransform * vec4(in_position, 1);
  21. gl_Position = projTransform * viewTransform * worldPosition;
  22. surfaceNormal = (modelTransform * vec4(in_normal, 0)).xyz;
  23. toLight = lightPosition - worldPosition.xyz;
  24. toCamera = (inverseViewTransform * vec4(0, 0, 0, 1)).xyz - worldPosition.xyz;
  25. }
  26. </script>
  27. <script type="application/glsl" id="fragment.glsl">
  28. precision mediump float;
  29. uniform vec3 lightColor;
  30. uniform float shineDamper;
  31. uniform float reflectivity;
  32. varying vec3 toLight;
  33. varying vec3 surfaceNormal;
  34. varying vec3 toCamera;
  35. void main() {
  36. float brightness = dot(normalize(toLight), normalize(surfaceNormal));
  37. brightness = max(brightness, 0.0);
  38. vec3 diffuse = lightColor * brightness;
  39. vec3 lightDirection = -normalize(toLight);
  40. vec3 reflectedDirection = reflect(lightDirection, normalize(surfaceNormal));
  41. float specular = dot(reflectedDirection, normalize(toCamera));
  42. specular = max(specular, 0.0);
  43. float damper = pow(specular, shineDamper);
  44. vec3 specularColor = damper * reflectivity * lightColor;
  45. gl_FragColor = vec4(diffuse, 1) * vec4(0.3, 0.8, 0.2, 1) + vec4(specularColor, 1);
  46. }
  47. </script>
  48. <script type="application/javascript">
  49. rtl.run();
  50. </script>
  51. </body>
  52. </html>