diff --git a/README.md b/README.md index 2aa17bf..161f5d8 100644 --- a/README.md +++ b/README.md @@ -14,4 +14,5 @@ As of lesson 1, there is no `webpack.config.js`, just run webpack since ## Progress - [x] Lesson 1 -- [ ] Lesson 2 \ No newline at end of file +- [x] Lesson 2 +- [ ] Lesson 3 \ No newline at end of file diff --git a/dist/main.js b/dist/main.js index fb310fb..e08d1ba 100644 --- a/dist/main.js +++ b/dist/main.js @@ -1 +1,206 @@ -!function(t){var e={};function s(i){if(e[i])return e[i].exports;var n=e[i]={i:i,l:!1,exports:{}};return t[i].call(n.exports,n,n.exports,s),n.l=!0,n.exports}s.m=t,s.c=e,s.d=function(t,e,i){s.o(t,e)||Object.defineProperty(t,e,{configurable:!1,enumerable:!0,get:i})},s.r=function(t){Object.defineProperty(t,"__esModule",{value:!0})},s.n=function(t){var e=t&&t.__esModule?function(){return t.default}:function(){return t};return s.d(e,"a",e),e},s.o=function(t,e){return Object.prototype.hasOwnProperty.call(t,e)},s.p="",s(s.s=0)}([function(t,e,s){"use strict";s.r(e);var i=class{constructor(t,e){this.w=t,this.h=e,this.canvas=this._createCanvas()}_createCanvas(){const t=document.createElement("canvas");t.setAttribute("width",this.w),t.setAttribute("height",this.h);const e=t.getContext("2d"),s=e.getImageData(0,0,this.w,this.h);return{canvas:t,context:e,imageData:s,pixels:s.data}}putPixel(t,e,s){const i=4*(e*this.w+t);this.canvas.pixels[i]=0|s.r,this.canvas.pixels[i+1]=0|s.g,this.canvas.pixels[i+2]=0|s.b,this.canvas.pixels[i+3]=255}renderInto(t){this.canvas.context.putImageData(this.canvas.imageData,0,0),t.appendChild(this.canvas.canvas)}};var n=class{constructor(t,e,s,i){this.x1=t,this.x2=e,this.x3=s,this.x4=i}};var r=class{constructor(t,e){this.origin=t,this.direction=e}};var a=class{constructor(t,e,s){this.r=t,this.g=e,this.b=s}asVector3(){return new h(this.r,this.g,this.b)}rescale(t=0,e=255,s=0,i=255,n=0,r=255){let a=e-t,o=i-s,h=r-n;return this.r=parseInt((this.r-t)/a*255),this.g=parseInt((this.g-s)/o*255),this.b=parseInt((this.b-n)/h*255),this}};class o{constructor(t,e,s){this.x=t,this.y=e,this.z=s}_plus(t){return new o(this.x+t.x,this.y+t.y,this.z+t.z)}_minus(t){return new o(this.x-t.x,this.y-t.y,this.z-t.z)}_scale(t){return new o(t*this.x,t*this.y,t*this.z)}_linearInterpolation(t,e){return this._scale(1-e)._plus(t._scale(e))}asColor(){return new a(this.x,this.y,this.z)}}var h=o;class c{constructor(t,e,s,i){this.camera=t,this.imagePlane=e,this.H=i,this.W=s}static createDefaultView(){const t=new n(new h(-1,.75,0),new h(1,.75,0),new h(-1,-.75,0),new h(1,-.75,0)),e=new h(0,0,-1);return new c(e,t,256,192)}_bilinearInterpolation(t,e){let s=t/this.W,i=(this.H-e-1)/this.H,n=this.imagePlane.x1._linearInterpolation(this.imagePlane.x2,s),a=this.imagePlane.x3._linearInterpolation(this.imagePlane.x4,s)._linearInterpolation(n,i);return{point:a,ray:new r(a,a._minus(this.camera))}}colorSnapshot(){const t=new i(this.W,this.H);for(let e=0;e, ray: }\n */\n _bilinearInterpolation(x, y) {\n \n let a = x / this.W;\n let b = (this.H - y - 1) / this.H;\n let top = this.imagePlane.x1._linearInterpolation(this.imagePlane.x2,\n a);\n let bottom = this.imagePlane.x3._linearInterpolation(this.imagePlane.x4,\n a);\n let point = bottom._linearInterpolation(top, b);\n \n return {\n point: point,\n ray: new _data_structures_ray__WEBPACK_IMPORTED_MODULE_2__[\"default\"](point, point._minus(this.camera))\n };\n }\n \n // assignment1\n colorSnapshot() {\n \n const image = new _image__WEBPACK_IMPORTED_MODULE_0__[\"default\"](this.W, this.H);\n \n let minX = 0.5;\n let maxX = 0.5;\n let minY = 0.5;\n let maxY = 0.5;\n \n for (let y = 0; y < this.H; y++) {\n for (let x = 0; x < this.W; x++) {\n let pointray = this._bilinearInterpolation(x, y);\n image.putPixel(x, y, pointray.ray.direction.asColor().rescale(\n -1, 1, -0.75, 0.75));\n }\n }\n \n console.log(minX, maxX, minY, maxY);\n \n return image;\n }\n \n viewScene(scene) {\n \n const image = new _image__WEBPACK_IMPORTED_MODULE_0__[\"default\"](this.W, this.H);\n \n for (let y = 0; y < this.H; y++) {\n for (let x = 0; x < this.W; x++) {\n \n let pointray = this._bilinearInterpolation(x, y);\n let color = scene.backgroundColor;\n let objects = scene.getObjects();\n let bestT = null;\n let bestO = null;\n \n for (let i = 0; i < objects.length; ++i) {\n \n let o = objects[i];\n let t = o.intersection(pointray.ray);\n \n if (t !== null) {\n \n // console.log(t);\n \n if (bestT === null) {\n bestT = t;\n bestO = o;\n }\n else {\n if (t < bestT) {\n bestT = t;\n bestO = o;\n }\n }\n }\n }\n \n if (bestO !== null) {\n // console.log(bestO.color);\n color = bestO.color.normalized();\n }\n image.putPixel(x, y, color);\n }\n }\n \n return image;\n }\n};\n\n/* harmony default export */ __webpack_exports__[\"default\"] = (View);\n\n//# sourceURL=webpack:///./src/view/index.js?"); + +/***/ }) + +/******/ }); \ No newline at end of file diff --git a/node_modules/.cache/uglifyjs-webpack-plugin/content-v2/sha512/82/09/447ca7e4a3bb5c20c50f9c5a4c90f75f7db7389dd49a26ee18f0269ac7689d64ce3567c4c230e21a04855f3a2d7dc023b983f9a5752c61ca38c89ac56650 b/node_modules/.cache/uglifyjs-webpack-plugin/content-v2/sha512/82/09/447ca7e4a3bb5c20c50f9c5a4c90f75f7db7389dd49a26ee18f0269ac7689d64ce3567c4c230e21a04855f3a2d7dc023b983f9a5752c61ca38c89ac56650 new file mode 100644 index 0000000..5007d49 --- /dev/null +++ b/node_modules/.cache/uglifyjs-webpack-plugin/content-v2/sha512/82/09/447ca7e4a3bb5c20c50f9c5a4c90f75f7db7389dd49a26ee18f0269ac7689d64ce3567c4c230e21a04855f3a2d7dc023b983f9a5752c61ca38c89ac56650 @@ -0,0 +1 @@ +{"code":"!function(t){var e={};function s(n){if(e[n])return e[n].exports;var i=e[n]={i:n,l:!1,exports:{}};return t[n].call(i.exports,i,i.exports,s),i.l=!0,i.exports}s.m=t,s.c=e,s.d=function(t,e,n){s.o(t,e)||Object.defineProperty(t,e,{configurable:!1,enumerable:!0,get:n})},s.r=function(t){Object.defineProperty(t,\"__esModule\",{value:!0})},s.n=function(t){var e=t&&t.__esModule?function(){return t.default}:function(){return t};return s.d(e,\"a\",e),e},s.o=function(t,e){return Object.prototype.hasOwnProperty.call(t,e)},s.p=\"\",s(s.s=0)}([function(t,e,s){\"use strict\";s.r(e);class n{constructor(t,e,s){this.x=t,this.y=e,this.z=s}_plus(t){return new n(this.x+t.x,this.y+t.y,this.z+t.z)}_minus(t){return new n(this.x-t.x,this.y-t.y,this.z-t.z)}_dot(t){return this.x*t.x+this.y*t.y+this.z*t.z}_scale(t){return new n(t*this.x,t*this.y,t*this.z)}_linearInterpolation(t,e){return this._scale(1-e)._plus(t._scale(e))}asColor(){return new a(this.x,this.y,this.z)}length(){return Math.sqrt(this.x*this.x+this.y*this.y+this.z*this.z)}}var i=n;class r{constructor(t,e,s){this.r=t,this.g=e,this.b=s}asVector3(){return new i(this.r,this.g,this.b)}rescale(t=0,e=255,s=0,n=255,i=0,a=255){let h=e-t,o=n-s,c=a-i,l=parseInt((this.r-t)/h*255),u=parseInt((this.g-s)/o*255),x=parseInt((this.b-i)/c*255);return new r(l,u,x)}normalized(){return this.rescale(0,1,0,1,0,1)}}var a=r;var h=class{constructor(){}colorAtIntersection(t){return new a(0,0,0)}};var o=class{constructor(t=-2,e=2,s=-1,n=-1,i=0,r=5){this.xmin=t,this.xmax=e,this.ymin=s,this.ymax=n,this.zmin=i,this.zmax=r,this.objects=[],this.backgroundColor=new a(0,0,0)}getObjects(){return this.objects}generateRandomSpheres(t=5){for(var e=0;e