Advertisement
Guest User

Untitled

a guest
Mar 25th, 2017
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.17 KB | None | 0 0
  1. <!DOCTYPE>
  2. <html>
  3. <head>
  4. <title>Computer Graphcis - Vertex and Fragment Shader - APT GG Task 3 - Wobbly Sphere</title>
  5. <script type="text/javascript" src="three.r84.js"></script>
  6. <script type="text/javascript">
  7. /**
  8. * This part is executed on the CPU: in your browser!
  9. * There are a lot of Three.js specific things here, ask me if you feel confused.
  10. */
  11. var renderer, scene, camera; // First some global variables
  12. var sphere; // We will have one object, a sphere
  13.  
  14. var vertexShader, fragmentShader; // These variables will hold the shader code, which will be sent to the GPU
  15.  
  16. var lightPosition = new THREE.Vector3(2.0, 2.0, 0.0); // Light source position is currently in the view space (relative to the camera) for simplicity
  17. var viewerPosition = new THREE.Vector3(0.0, 0.0, 3.0); // Viewer position in our right-handed world
  18.  
  19. /**
  20. * Helper function for getting time.
  21. * There are many ways to get the time...
  22. */
  23. function millis() {
  24.  
  25. return performance.now();
  26. }
  27.  
  28. /**
  29. * This function is executed once your webpage has been loaded
  30. */
  31. function onLoad() {
  32. var canvasContainer = document.getElementById('myCanvasContainer'); // We get the element to render our 3D into
  33. var width = 1920;
  34. var height = 1080;
  35.  
  36. vertexShader = document.getElementById('vertexShader').textContent; // We get our shader codes
  37. fragmentShader = document.getElementById('fragmentShader').textContent;
  38.  
  39. renderer = new THREE.WebGLRenderer(); // This is Three.js renderer
  40. renderer.setSize(width, height);
  41. canvasContainer.appendChild(renderer.domElement); // Adds a rendering viewport into our page
  42.  
  43. scene = new THREE.Scene(); // Three.js has a scene, where our 3D objects are in
  44.  
  45. //We will use a perspective camera, with FOV 80 degrees
  46. camera = new THREE.PerspectiveCamera(80, width / height, 1, 1000);
  47. camera.position.set(viewerPosition.x, viewerPosition.y, viewerPosition.z);
  48. camera.up = new THREE.Vector3(0, 1, 0);
  49. camera.lookAt(new THREE.Vector3(0, 0, 0)); // Camera is looking at the center of the world
  50. scene.add(camera);
  51.  
  52.  
  53. sphere = createSphere();
  54. scene.add(sphere);
  55.  
  56. draw();
  57.  
  58. window.addEventListener('keydown', function(event) { // When you press Space, you see the wireframe
  59. if (event.keyCode == 32) { //Space
  60. sphere.material.wireframe = !sphere.material.wireframe;
  61. }
  62. }, false);
  63. }
  64.  
  65. /**
  66. * This is our main rendering loop.
  67. */
  68. function draw() {
  69. requestAnimationFrame(draw);
  70. sphere.material.uniforms.time.value = millis()/1000;
  71. sphere.material.uniforms.distortionWeight.value = Math.pow(Math.sin(millis()/100), 10.0);
  72. /**
  73. * -- Task --
  74. * Update the uniform values for the sphere:
  75. * - sphere.material.uniforms.time.value - make it milliseconds divided 1000, so it will be in seconds
  76. * - sphere.material.uniforms.distortionWeight.value - make it a sine wave to the 4th power, argument can be milliseconds divided by 10000
  77. * in JavaScript there are functions Math.pow and Math.sin
  78. */
  79.  
  80. renderer.render(scene, camera);
  81. }
  82.  
  83. /**
  84. * Every 3D mesh object consists of a geometry and a material.
  85. * Here we create a sphere and use a custom material (with our own shaders)
  86. */
  87. function createSphere() {
  88. var geometry = new THREE.SphereGeometry(1, 32, 32);
  89. var material = createShaderMaterial();
  90. var sphereMesh = new THREE.Mesh(geometry, material);
  91.  
  92. return sphereMesh;
  93. }
  94.  
  95. /**
  96. * This creates a material, which uses our own shaders
  97. */
  98. function createShaderMaterial() {
  99.  
  100. return new THREE.ShaderMaterial({
  101. uniforms: {
  102. lightPosition: {
  103. value: lightPosition
  104. },
  105. time: { // Here are some new uniforms, update their values in the rendering loop
  106. value: 0
  107. },
  108. distortionWeight: {
  109. value: 0
  110. }
  111. },
  112. vertexShader: vertexShader,
  113. fragmentShader: fragmentShader
  114. });
  115. }
  116.  
  117. </script>
  118.  
  119. <!--
  120. Below are the shader codes for the vertex and fragment shaders.
  121. -->
  122. <script id="vertexShader" type="x-shader/x-vertex">
  123. /**
  124. * -- Task --
  125. * Receive the time and distortionWeight uniforms
  126. */
  127. varying vec3 interpolatedPosition;
  128. varying vec3 interpolatedNormal;
  129. uniform float time;
  130. uniform float distortionWeight;
  131.  
  132.  
  133. void main() {
  134. interpolatedPosition = (modelViewMatrix * vec4(position, 1.0)).xyz;
  135. interpolatedNormal = normalMatrix * normal;
  136.  
  137. /**
  138. * -- Task --
  139. * Create a new variable vec3 distortion, which holds the distortion amount in all axes
  140. * One way is: Have each coordinate oscillate via sine waves, where:
  141. * - The argument has a different multiple of time summed with
  142. * - a positional coordinate with different multiples
  143. * - Entire wave is divided by 10 (otherwise the distortion is too big)
  144. */
  145.  
  146. vec3 distortion = vec3(
  147. sin(time + position.x * distortionWeight * 999.0) + 0.0,
  148. sin(time + position.y * distortionWeight * 999.0) + 0.0,
  149. sin(time + position.z * distortionWeight * 999.0) + 0.0);
  150.  
  151. /**
  152. * -- Task --
  153. * Find a vec3 distortedPosition, which has the weighted distortion added to the position
  154. * Use that new distortedPosition in the gl_Position calculation below
  155. */
  156.  
  157.  
  158. gl_Position = projectionMatrix * modelViewMatrix * vec4(position + distortion, 1.0);
  159. }
  160. </script>
  161. <script id="fragmentShader" type="x-shader/x-fragment">
  162. varying vec3 interpolatedNormal;
  163. varying vec3 interpolatedPosition;
  164. uniform vec3 lightPosition;
  165.  
  166. void main() {
  167. vec3 n = normalize(interpolatedNormal);
  168. vec3 l = normalize(lightPosition - interpolatedPosition);
  169.  
  170. vec3 baseColor = vec3(1.0, 0.0, 0.0);
  171. vec3 color = baseColor * (vec3(0.2) + max(0.0, dot(n, l)));
  172. // You can also use the discretized colors here or anything else you like
  173.  
  174. gl_FragColor = vec4(color, 1.0);
  175. }
  176. </script>
  177. </head>
  178. <body onload="onLoad()">
  179. <div id="myCanvasContainer"></div>
  180. </body>
  181. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement