Skip to content

Latest commit

 

History

History
149 lines (121 loc) · 8.36 KB

aryan_koundal_gsoc_2023.md

File metadata and controls

149 lines (121 loc) · 8.36 KB

Improving p5.js WebGL/3d functionality

By Aryan Koundal(@AryanKoundal)

Mentored by : Dave Pagurek(@davepagurek ), Tanvi Kumar(@TanviKumar )

Overview

In p5.js, there are two render modes: P2D (default renderer) and WebGL. WEBGL enables the user to draw in 3D. There are many ways to implement lighting. Currently, p5js has implemented 8. To add lighting to a 3D object, one can use these functionalities. But there is another technique to light objects, not by direct light, but using the surrounding environment as a single light source, which we call Image-Based lighting. This project aims to add lighting to a 3D object, using the surrounding environment as a single light source, which is generally called Image-Based lighting. In simple words, one can very quickly drop in an image from real life to use as surrounding lights, rather than continuously tweaking the colors and positions of ambient, point, etc lights.

  1. Diffused IBL
    1. Create scene's irradiance.
    2. Calculation of Scene’s irradiance for any direction.
      1. Sample the scene’s radiance from all possible directions, within the hemisphere, for each fragment.
      2. Take a finite number of samples of the scene's irradiance and pre-compute them.
      3. Create a pre-computed irradiance map.
    3. Sample the pre-computed irradiance map to retrieve diffuse irradiance.
  2. Specular IBL
    1. We need to preconvolute it.
    2. Use split sum approximation to divide the specular part into 2 further parts
      1. First one is a prefiltered environment map.
        1. Finalize Pre filter convolution using low discrepancy hammersley sequence and sample generation defined.
        2. Capturing prefilter mipmap levels, storing the results in prefiltered environment cubemap
      2. Second one is BDRF
        1. pre -calculate the BRDF’s response given input as roughness and angle between n and wi. Precalculations done using BRDF’S geometry function and Fresnel-Schlicks approximation.
        2. Stored in 2D lookup texture known as BRDF integration map.
    3. Combine the prefiltered environment map and BRDF integration map
  3. Combine the diffused and specular parts

Current State of The Project

The project is completed and here are some screenshots and videos demonstrating the work.

Image of Example 1

example 1

Video of Example 1

example.1.mp4

Image oF Example 2

example 2

Video of Example 2

example.2.mp4

Pull Request

Work Done

src/webgl/light.js

  1. This includes the function imageLight() which provides the whole functionality.
  2. 2 Examples for the imageLight() are also included in this files.

src/webgl/p5.RendererGL.js

  1. This includes 2 new maps, which are diffusedTextures and specularTextures for storing the p5 graphics.
  2. Then the function getDiffusedTexture() which gets called from _setImageLightUniforms. It's function is to create a blurry image from the input non blurry img, if it doesn't already exist.
  3. Also the function getSpecularTexture() which also gets called from _setImageLightUniforms. It's function is too create a texture from the input non blurry image, if it doesn't already exist
  4. Then the function _setImageLightUniforms(). It creates the textures and sets those textures in the shader being processed.

src/webgl/p5.Texture.js

  1. This includes the MipmapTexture class.

src/webgl/shaders/imageLight.vert

  1. It is a vertex shader only for the image Light feature.

src/webgl/shaders/imageLightDiffused.frag

  1. It is the Fragment shader for the calculations of the Diffused Part of Image Light.

src/webgl/shaders/imageLightSpecular.frag

  1. It is the Fragment shader for the calculations of the Specular Part of Image Light.
  2. It has the functions HammersleyNoBitOps() , VanDerCorput()and ImportanceSampleGGX() which are referenced from "https://learnopengl.com/PBR/IBL/Specular-IBL"

src/webgl/shaders/lighting.glsl

  1. This includes calculateImageDiffuse() and calculateImageSpecular() which actually calculates the output textures. These are calculated only when imageLight() is called.

List of All shaders modified to improve webGL compatibility to newer versions

  1. src/webgl/shaders/basic.frag
  2. src/webgl/shaders/immediate.vert
  3. src/webgl/shaders/light.vert
  4. src/webgl/shaders/light_texture.frag
  5. src/webgl/shaders/line.frag
  6. src/webgl/shaders/line.vert
  7. src/webgl/shaders/normal.frag
  8. src/webgl/shaders/normal.vert
  9. src/webgl/shaders/phong.frag
  10. src/webgl/shaders/phong.vert
  11. src/webgl/shaders/point.frag
  12. src/webgl/shaders/point.vert
  13. src/webgl/shaders/vertexColor.frag
  14. src/webgl/shaders/vertexColor.vert

Sketches I made, which might be useful

  1. Example 1 in imagelight https://editor.p5js.org/aryan_koundal/sketches/OEsyk6uZI
  2. Example 2 in imagelight https://editor.p5js.org/aryan_koundal/sketches/XjalPP7s4
  3. Final Prototype Diffused + Roughness (Variable Roughness) https://editor.p5js.org/aryan_koundal/sketches/4V790dB9Z
  4. Diffuse light final prototype https://editor.p5js.org/aryan_koundal/sketches/q_Zg37OB2
  5. Mipmap Specular roughness Prototype (visible variation) https://editor.p5js.org/aryan_koundal/sketches/Bi2BN7zjK
  6. Specular prefilterconvolution with chessboard https://editor.p5js.org/aryan_koundal/sketches/qaIhxRZHI
  7. Prefilterconvolution cubemaps prototypes 8 levels https://editor.p5js.org/aryan_koundal/sketches/YJTSFKhqt
  8. Specular prefilterconvolution with image https://editor.p5js.org/aryan_koundal/sketches/8divJgdQxO
  9. Specular mipmaps prototype with colors https://editor.p5js.org/aryan_koundal/sketches/3V9iDH8ax
  10. Sphere with point Lights https://editor.p5js.org/aryan_koundal/sketches/9NdeCtfdW
  11. Diffused texture Shader Prototype https://editor.p5js.org/aryan_koundal/sketches/DcFDcOFUn
  12. Resolution Pixelwise https://editor.p5js.org/aryan_koundal/sketches/5RfDIy2I9
  13. Cube reflecting mountain spheremap https://editor.p5js.org/aryan_koundal/sketches/2QS5-Fy0V
  14. Cube reflecting sky spheremap https://editor.p5js.org/aryan_koundal/sketches/O1NwI4ufp

Further Improvement

While working on this project, I realised that there is scope for improvement. Like increasing efficiency by using cubemaps instead of environment maps for Diffused Lighting. Also using framebuffers would improve the efficiency and reduce the time taken to render the lights.

Conclusion

My GSoC experience was genuinely transformative, leading to significant personal and professional development within the open-source domain. I successfully overcame initial obstacles related to setting up the project and navigating the codebase, steadily advancing through my contributions. Throughout this summer, I actively participated in the p5.js open-source community, surpassing my initial expectations for my first major open-source venture and further igniting my enthusiasm. I want to express my heartfelt appreciation to my mentors, Dave Pagurek(@davepagurek ), and Tanvi Kumar(@TanviKumar ), for their invaluable guidance in navigating coding challenges. Effective teamwork, collaboration, and communication proved essential in this open-source journey. I eagerly anticipate expanding my contributions and honing my skills. This summer has instilled a sense of purpose in me, and I'm profoundly grateful for the mentorship and knowledge gained.