{"id":1450,"date":"2015-05-07T10:10:17","date_gmt":"2015-05-07T10:10:17","guid":{"rendered":"http:\/\/www.nikola-breznjak.com\/blog\/?p=1450"},"modified":"2015-08-03T06:11:22","modified_gmt":"2015-08-03T06:11:22","slug":"pluralsight-webgl-and-three-js-fundamentals-course-notes","status":"publish","type":"post","link":"https:\/\/nikola-breznjak.com\/blog\/javascript\/three-js\/pluralsight-webgl-and-three-js-fundamentals-course-notes\/","title":{"rendered":"Pluralsight WebGL and Three.js fundamentals course notes"},"content":{"rendered":"<p>Recently I bought a years worth subscription over at Pluralsight (here&#8217;s my <a href=\"http:\/\/www.pluralsight.com\/profile\/hitman666\">portfolio of passed courses<\/a>), and here I&#8217;ll be posting my notes from the courses that I attended. Please note that this is not meant to be as a sort of tutorial, just as the name says &#8211; notes for myself :). However, one could get some useful content from these posts as I&#8217;m noting things that catch my attention.<\/p>\n<p>Here are my\u00a0notes from the <a href=\"http:\/\/www.pluralsight.com\/courses\/webgl-threejs-fundamentals\">WebGL and Three.js fundamentals<\/a> course. I rated the course 4\/5 and all in all it&#8217;s a good introduction to <a href=\"http:\/\/en.wikipedia.org\/wiki\/WebGL\">WebGL<\/a> with <a href=\"http:\/\/threejs.org\/\">Three.js<\/a>.<\/p>\n<p><em>\/\/rant:\u00a0<\/em><em>I passed the exam with the whopping 100%, and actually I feel that since Pluralsight is\u00a0very famous and all that, that they could improve on the quality (it&#8217;s just too easy and does not go into detail) of these certification tests.<\/em><\/p>\n<pre class=\"lang:default decode:true\">var scene = new THREE.Scene(); \/\/acts like a container of all items<\/pre>\n<pre class=\"lang:default decode:true\">var renderer = new THREE.WebGLRenderer(); \/\/how our content will be displayed on the\u00a0page (WebGL, Canvas or\u00a0SVG renderers)<\/pre>\n<p>It uses a Cartesian cordinate system (x,y,z). If you don&#8217;t specify the position- it will be positioned at (0,0,0).<\/p>\n<p>It has a <strong>perspective<\/strong> and <strong>ortographic<\/strong> camera:<\/p>\n<pre class=\"lang:default decode:true\">camera = new THREE.PerspectiveCamera(\r\n35, =&gt;fov\r\nwindow.innerWidth \/ window.innerHeight,\r\n1, =&gt; near\r\n1000 =&gt; far planes\r\n);<\/pre>\n<p>You can use your dev tools console (Chrome) or Firebug (Firefox) and access items if you properly export the objects in the code\u00a0(<strong>return {scene: scene});\u00a0<\/strong>code snippet in the listing below):<\/p>\n<pre class=\"lang:default decode:true\">\/\/app.js file\r\nvar example = (function(){\r\n\r\n    \"use strict\";\r\n    \r\n    var scene=new THREE.Scene(),\r\n    renderer = window.WebGLRenderingContext ? new THREE.WebGLRenderer() : new THREE.CanvasRenderer(),\r\n    light= new THREE.AmbientLight(0xffffff),            \r\n    camera,        \r\n    box;\r\n\r\n    function initScene(){\r\n        renderer.setSize( window.innerWidth, window.innerHeight );\r\n        document.getElementById(\"webgl-container\").appendChild(renderer.domElement);\r\n\r\n        scene.add(light);\r\n                          \r\n        camera = new THREE.PerspectiveCamera(\r\n        35,\r\n        window.innerWidth \/ window.innerHeight,\r\n        1,\r\n        1000\r\n        );\r\n                            \r\n        camera.position.z= 100;            \r\n        scene.add( camera ); \r\n\r\n        box = new THREE.Mesh(\r\n        new THREE.BoxGeometry(20,20,20),\r\n        new THREE.MeshBasicMaterial({color: 0x00FF00})\r\n        );\r\n\r\n        box.name=\"box\";   \r\n\r\n        scene.add(box);\r\n\r\n        render();\r\n    }\r\n\r\n    function render(){\r\n        box.rotation.y +=0.01;\r\n        \r\n        renderer.render(scene, camera); \r\n        requestAnimationFrame(render);        \r\n    }\r\n\r\n    window.onload = initScene;\r\n    \r\n    return {\r\n        scene: scene\r\n    }\r\n\r\n})();<\/pre>\n<pre class=\"lang:default decode:true\">\/\/HTML file\r\n&lt;!DOCTYPE html&gt;\r\n\r\n&lt;html&gt;\r\n    &lt;head&gt;\r\n        &lt;title&gt;WebGL with three.js Fundamentals&lt;\/title&gt;       \r\n    &lt;\/head&gt;\r\n\r\n    &lt;body&gt;\r\n    \t&lt;div id=\"webgl-container\"&gt;&lt;\/div&gt;\r\n    \t\r\n    \t&lt;script src=\"scripts\/three.js\"&gt;&lt;\/script&gt;&lt;!--DL this from official site--&gt;\r\n    \t&lt;script src=\"scripts\/app.js\"&gt;&lt;\/script&gt;\r\n    &lt;\/body&gt;\r\n&lt;\/html&gt;<\/pre>\n<p>Accessing it from Chrome Console as mentioned above:<\/p>\n<pre class=\"lang:default decode:true\">var a = example.scene.getObjectByName('box');\r\na.position.x = 10;\r\na.position.set(0,2,2);\r\n<\/pre>\n<ul>\n<li>Degrees to radians formula:\u00a0<strong>radians = degrees *pi\/180<\/strong><\/li>\n<li><strong>Meshes <\/strong>&#8211; geometry + material<\/li>\n<li>use <a href=\"https:\/\/github.com\/mrdoob\/stats.js\/\">stats.js<\/a><\/li>\n<li>different existing controls:\u00a0FlyControls.js, OrbitControls.js<\/li>\n<li>Collision detection<\/li>\n<li><strong>Raycasting<\/strong> &#8211; returns objects that are on some other objects &#8220;ray&#8221; and Box3.<\/li>\n<li><strong>Physics<\/strong>: <a href=\"http:\/\/chandlerprall.github.io\/Physijs\/\">PhysiJS<\/a> is a wrapper for <a href=\"https:\/\/github.com\/kripken\/ammo.js\/\">Ammo.js<\/a> which is a port of C++ <a href=\"http:\/\/bulletphysics.org\/\">Bullet<\/a><\/li>\n<li>Supports gravity, mass property, friction, restitution (bounciness)<\/li>\n<\/ul>\n<p>Cool 3D frogger game made by the author: <a href=\"https:\/\/github.com\/alexmackey\/threeJsFrogger\">https:\/\/github.com\/alexmackey\/threeJsFrogger<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Recently I bought a years worth subscription over at Pluralsight (here&#8217;s my portfolio of passed courses), and here I&#8217;ll be posting my notes from the courses that I&hellip;<\/p>\n","protected":false},"author":1,"featured_media":1553,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[46,45],"tags":[],"class_list":["post-1450","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-pluralsight","category-three-js"],"_links":{"self":[{"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/posts\/1450","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/comments?post=1450"}],"version-history":[{"count":4,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/posts\/1450\/revisions"}],"predecessor-version":[{"id":1554,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/posts\/1450\/revisions\/1554"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/media\/1553"}],"wp:attachment":[{"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/media?parent=1450"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/categories?post=1450"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/tags?post=1450"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}