Advertisement
Guest User

Untitled

a guest
Mar 25th, 2017
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.11 KB | None | 0 0
  1. <!DOCTYPE>
  2. <html>
  3. <head>
  4. <title>Computer Graphcis - Vertex and Fragment Shader - APT GG Task 1 - Coloring a 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. * This function is executed once your webpage has been loaded
  21. */
  22. function onLoad() {
  23. var canvasContainer = document.getElementById('myCanvasContainer'); // We get the element to render our 3D into
  24. var width = 800;
  25. var height = 500;
  26.  
  27. vertexShader = document.getElementById('vertexShader').textContent; // We get our shader codes
  28. fragmentShader = document.getElementById('fragmentShader').textContent;
  29.  
  30. renderer = new THREE.WebGLRenderer(); // This is Three.js renderer
  31. renderer.setSize(width, height);
  32. canvasContainer.appendChild(renderer.domElement); // Adds a rendering viewport into our page
  33.  
  34. scene = new THREE.Scene(); // Three.js has a scene, where our 3D objects are in
  35.  
  36. //We will use a perspective camera, with FOV 80 degrees
  37. camera = new THREE.PerspectiveCamera(80, width / height, 1, 1000);
  38. camera.position.set(viewerPosition.x, viewerPosition.y, viewerPosition.z);
  39. camera.up = new THREE.Vector3(0, 1, 0);
  40. camera.lookAt(new THREE.Vector3(0, 0, 0)); // Camera is looking at the center of the world
  41. scene.add(camera);
  42.  
  43.  
  44. sphere = createSphere();
  45. scene.add(sphere);
  46.  
  47. draw();
  48.  
  49. window.addEventListener('keydown', function(event) { // When you press Space, you see the wireframe
  50. if (event.keyCode == 32) { //Space
  51. sphere.material.wireframe = !sphere.material.wireframe;
  52. }
  53. }, false);
  54. }
  55.  
  56. /**
  57. * This is our main rendering loop.
  58. */
  59.  
  60. var ScaleF = 0;
  61.  
  62. function draw() {
  63. requestAnimationFrame(draw);
  64. renderer.render(scene, camera);
  65. sphere.rotateX(0.01);
  66. }
  67.  
  68. /**
  69. * Every 3D mesh object consists of a geometry and a material.
  70. * Here we create a sphere and use a custom material (with our own shaders)
  71. */
  72. function createSphere() {
  73. var geometry = new THREE.SphereGeometry(1, 32, 32);
  74. var material = createShaderMaterial();
  75. var sphereMesh = new THREE.Mesh(geometry, material);
  76.  
  77. return sphereMesh;
  78. }
  79.  
  80. /**
  81. * This creates a material, which uses our own shaders
  82. */
  83. function createShaderMaterial() {
  84.  
  85. return new THREE.ShaderMaterial({
  86. uniforms: {
  87. lightPosition: {
  88. value: lightPosition
  89. }
  90. },
  91. vertexShader: vertexShader,
  92. fragmentShader: fragmentShader
  93. });
  94. }
  95.  
  96. </script>
  97.  
  98. <!--
  99. Below are the shader codes for the vertex and fragment shaders.
  100.  
  101. -- Task --
  102. 1) Make 2 varying vec3 variables: interpolatedPosition and interpolatedNormal (feel free to name them as you like)
  103. You need to define them in both the vertex and fragment shaders
  104. 2) In the vertex shader assign:
  105. 2.1) The position transformed into view space to interpolatedPosition (note that modelViewMatrix is mat4)
  106. 2.2) The normal (Three.js sends it to you) transformed into view space (use normalMatrix from Three.js) to interpolatedNormal
  107. 3) In the fragment shader:
  108. 3.1) Define the uniform vec3 lightPosition
  109. 3.2) Create a vec3 n, set it to the normalized interpolatedNormal
  110. 3.3) Create a vec3 l, calculate and set it to the normalized vector towards the light source
  111. 3.4) Use n and l to find the Lambertian reflectance coefficient
  112. 3.5) Take into account some color for the material
  113. 3.6) Take into account some ambient light
  114. -->
  115. <script id="vertexShader" type="x-shader/x-vertex">
  116.  
  117. varying vec3 interpolatedPosition;
  118. varying vec3 interpolatedNormal;
  119. void main() {
  120. interpolatedNormal = normalMatrix * normal;
  121. interpolatedPosition = (modelViewMatrix * vec4(position, 1.0)).xyz;
  122. gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0);
  123. }
  124. </script>
  125. <script id="fragmentShader" type="x-shader/x-fragment">
  126. varying vec3 interpolatedNormal;
  127. varying vec3 interpolatedPosition;
  128. uniform vec3 lightPosition;
  129. void main() {
  130. vec3 n = normalize(interpolatedNormal);
  131. vec3 l = normalize(lightPosition - interpolatedPosition);
  132.  
  133. vec3 baseColor = vec3(0.5, 0.5, 0.0);
  134.  
  135. vec3 color = baseColor * 0.2;
  136.  
  137. gl_FragColor = vec4(color, 1.0);
  138. }
  139. </script>
  140. </head>
  141. <body onload="onLoad()">
  142. <div id="myCanvasContainer"></div>
  143. </body>
  144. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement