<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Prateek Sharma on Medium]]></title>
        <description><![CDATA[Stories by Prateek Sharma on Medium]]></description>
        <link>https://medium.com/@prateek0987.ps?source=rss-7a812e6d6f5a------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/2*JP-vXv0sbmZn24qL3qwgoQ.jpeg</url>
            <title>Stories by Prateek Sharma on Medium</title>
            <link>https://medium.com/@prateek0987.ps?source=rss-7a812e6d6f5a------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 02 Dec 2026 21:33:21 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@prateek0987.ps/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[The Making of “Night Landscape”: Animating a Basic 3D Scene with Three.js]]></title>
            <link>https://medium.com/swlh/the-making-of-night-landscape-animating-a-basic-3d-scene-with-three-js-435594d5aa8f?source=rss-7a812e6d6f5a------2</link>
            <guid isPermaLink="false">https://medium.com/p/435594d5aa8f</guid>
            <category><![CDATA[threejs]]></category>
            <category><![CDATA[3d]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[tweenmax]]></category>
            <category><![CDATA[css]]></category>
            <dc:creator><![CDATA[Prateek Sharma]]></dc:creator>
            <pubDate>Mon, 11 May 2020 19:51:39 GMT</pubDate>
            <atom:updated>2020-05-12T16:40:14.545Z</atom:updated>
            <content:encoded><![CDATA[<p>A tutorial that explores the basics of creating animated 3D scenes using Three.js.</p><p>Today we are going to make a simple night landscape scene using three.js, a 3D library that makes WebGL simpler. WebGL is a pretty unknown world for many developers because of the complexity and syntax of GLSL. But With Three.js, 3D in the browser becomes very easy to implement.</p><p>Beyond the scope of this tutorial is the entire scene, but you can download it and check out the code, it contains interesting additional part of custom texturing with shaders.</p><p>This is my first article so if they’re a problem please feel free to tell me I will improve that in my next tutorials series. <strong>Let’s get started now.</strong></p><h3>Setup basic HTML and CSS</h3><p>This tutorial uses mainly the Three.js library, which makes WebGL easy to use. Check out the <a href="http://threejs.org/">website</a> and <a href="https://github.com/mrdoob/three.js/">GitHub repo</a> to get some more info on it.</p><p>We also use <a href="https://github.com/mrdoob/three.js/blob/dev/src/loaders/ObjectLoader.js">OBJloader.js</a> and <a href="https://github.com/mrdoob/three.js/blob/dev/examples/js/loaders/MTLLoader.js">MTLLoader.js</a> to import the blender model into our seen.</p><p>The first thing to do is to import the library in your HTML header:</p><pre>&lt;script type=”text/javascript” src=”js/main_step1.js”&gt;&lt;/script&gt;</pre><pre>&lt;script src=”./threejs-loader/OBJLoader.js”&gt;&lt;/script&gt;</pre><pre>&lt;script src=”./threejs-loader/MTLLoader.js”&gt;&lt;/script&gt;</pre><p>Then you need to add a container element in the HTML to hold the rendered scene:</p><pre>&lt;div id=&quot;night&quot;&gt;&lt;/div&gt;</pre><p>You can simply style it like the following to make it fill the entire scene:</p><pre>.night {<br>position: absolute;<br>width: 100%;<br>height: 100%;<br>overflow: hidden;<br>background: -webkit-linear-gradient(#e4e0ba, #f7d9aa);<br>background: linear-gradient(#16092e, #7E87DE);<br>}</pre><h3>Setup JavaScript</h3><p>Three.js is easy to learn if you have prior knowledge of javascript before.</p><h3>The structure of the Code</h3><p>All the main functions we need to create are put into the <strong>init </strong>function:</p><pre>window.addEventListener(&#39;load&#39;, init, false);</pre><pre>function init() {<br>	<em>// set up the scene, the camera and the renderer</em><br>	createScene();</pre><pre>	<em>// add the lights</em><br>	createLights();</pre><pre>	<em>// add the objects</em><br>	createLand();<br>	createStar();<br>	createMoon();</pre><pre>	<em>// start a loop that will update the objects&#39; positions </em><br>	<em>// and render the scene on each frame</em><br>	loop();<br>}</pre><h3>Setting up the Scene</h3><p>To create a Three.js project, we’ll need at least the following:</p><ol><li><strong>A scene:</strong> consider this as the stage where every object needs to be added in order to be rendered</li><li><strong>A camera:</strong> in this case, we will use a perspective camera, but it could also be an <a href="https://en.wikipedia.org/wiki/Orthographic_projection">orthographic</a> camera.</li><li><strong>A renderer</strong> that will display all the scenes using WebGL.</li><li><strong>One or more objects</strong> to render, in our case, we will create a plane, a sea and a sky (a few clouds)</li><li><strong>One or more lights:</strong> there is also different types of lights available. In this project, we will mainly use a hemisphere light for the atmosphere and a directional light for the shadows.</li></ol><pre>var scene,<br>camera, fieldOfView, aspectRatio, nearPlane, farPlane, HEIGHT, WIDTH,renderer, container;</pre><pre>function createScene() {<br>	<em>// Get the width and the height of the screen,</em><br>	<em>// use them to set up the aspect ratio of the camera </em><br>	<em>// and the size of the renderer.</em><br>	HEIGHT = window.innerHeight;<br>	WIDTH = window.innerWidth;</pre><pre>	<em>// Create the scene</em><br>	scene = new THREE.Scene();</pre><pre>	<em>// Add a fog effect to the scene; same color as the</em><br>	<em>// background color used in the style sheet</em><br>	scene.fog = new THREE.Fog(0xf7d9aa, 100, 950);<br>	<br>	<em>// Create the camera</em><br>	aspectRatio = WIDTH / HEIGHT;<br>	fieldOfView = 60;<br>	nearPlane = 1;<br>	farPlane = 10000;<br>	camera = new THREE.PerspectiveCamera(<br>		fieldOfView,<br>		aspectRatio,<br>		nearPlane,<br>		farPlane<br>		);<br>	<br>	<em>// Set the position of the camera</em><br>	camera.position.x = 0;<br>	camera.position.z = 200;<br>	camera.position.y = 100;<br>	<br>	<em>// Create the renderer</em><br>	renderer = new THREE.WebGLRenderer({ <br>		<em>// Allow transparency to show the gradient background</em><br>		<em>// we defined in the CSS</em><br>		alpha: true, </pre><pre>		<em>// Activate the anti-aliasing; this is less performant,</em><br>		<em>// but, as our project is low-poly based, it should be fine :)</em><br>		antialias: true <br>	});</pre><pre>	<em>// Define the size of the renderer; in this case,</em><br>	<em>// it will fill the entire screen</em><br>	renderer.setSize(WIDTH, HEIGHT);<br>	<br>	<em>// Enable shadow rendering</em><br>	renderer.shadowMap.enabled = true;<br>	<br>	<em>// Add the DOM element of the renderer to the </em><br>	<em>// container we created in the HTML</em><br>	container = document.getElementById(&#39;world&#39;);<br>	container.appendChild(renderer.domElement);<br>	<br>	<em>// Listen to the screen: if the user resizes it</em><br>	<em>// we have to update the camera and the renderer size</em><br>	window.addEventListener(&#39;resize&#39;, handleWindowResize, false);<br>}</pre><p>As the screen size can change, we need to update the renderer size and the camera aspect ratio:</p><pre>function handleWindowResize() {<br>	<em>// update height and width of the renderer and the camera</em><br>	HEIGHT = window.innerHeight;<br>	WIDTH = window.innerWidth;<br>	renderer.setSize(WIDTH, HEIGHT);<br>	camera.aspect = WIDTH / HEIGHT;<br>	camera.updateProjectionMatrix();<br>}</pre><h3>Setup Lights</h3><p>Lightning is certainly one of the trickiest parts when it comes to setting up a scene. The lights will set the mood of the whole scene and must be determined carefully. At this step of the project, we will just try to make the lightning good enough to make the objects visible.</p><pre>var hemisphereLight, shadowLight;</pre><pre>function createLights() {<br>	<em>// A hemisphere light is a gradient colored light; </em><br>	<em>// the first parameter is the sky color, the second parameter is the ground color, </em><br>	<em>// the third parameter is the intensity of the light</em><br>	hemisphereLight = new THREE.HemisphereLight(0xaaaaaa,0x000000, .9)<br>	<br>	<em>// A directional light shines from a specific direction. </em><br>	<em>// It acts like the sun, that means that all the rays produced are parallel. </em><br>	shadowLight = new THREE.DirectionalLight(0xffffff, .9);</pre><pre>	<em>// Set the direction of the light  </em><br>	shadowLight.position.set(150, 350, 350);<br>	<br>	<em>// Allow shadow casting </em><br>	shadowLight.castShadow = true;</pre><pre>	<em>// define the visible area of the projected shadow</em><br>	shadowLight.shadow.camera.left = -400;<br>	shadowLight.shadow.camera.right = 400;<br>	shadowLight.shadow.camera.top = 400;<br>	shadowLight.shadow.camera.bottom = -400;<br>	shadowLight.shadow.camera.near = 1;<br>	shadowLight.shadow.camera.far = 1000;</pre><pre>	<em>// define the resolution of the shadow; the higher the better, </em><br>	<em>// but also the more expensive and less performant</em><br>	shadowLight.shadow.mapSize.width = 2048;<br>	shadowLight.shadow.mapSize.height = 2048;<br>	<br>	<em>// to activate the lights, just add them to the scene</em><br>	scene.add(hemisphereLight);  <br>	scene.add(shadowLight);<br>}</pre><h3>Creating Object with Three.js</h3><h3>A simple Cylinder for land:</h3><p>Let’s start with creating land for our scene as it is the easiest object to make we will first make a cylinder geometric and then we rotate it to look like a surface and for mountains land will take the vertices from the surface of our cylinder and randomly change the position of the vertices to look like mountains.</p><pre>Land = function(){</pre><pre>//Creating Cylinder geom.</pre><pre>var geom = new THREE.CylinderGeometry(80,80,1000,40,10);</pre><pre>//Rotating Geomtery to X-axis</pre><pre>geom.applyMatrix(new THREE.Matrix4().makeRotationZ(-Math.PI/2));</pre><pre>geom.mergeVertices();</pre><pre>var l = geom.vertices.length;</pre><pre>this.lands = [];</pre><pre>for (var i=0; i&lt;l; i++){</pre><pre>// get each vertex</pre><pre>var v = geom.vertices[i];</pre><pre>// store some data associated to it</pre><pre>this.lands.push({y:v.y,</pre><pre>x:v.x,</pre><pre>z:v.z,</pre><pre>// a random angle</pre><pre>ang:Math.random()*Math.PI*2,</pre><pre>// a random distance</pre><pre>amp:5 + Math.random()*25,</pre><pre>// a random speed between 0.016 and 0.048 radians / frame</pre><pre>speed:0.016 + Math.random()*0.032</pre><pre>});</pre><pre>};</pre><pre>// create the material</pre><pre>var mat = new THREE.MeshBasicMaterial({</pre><pre>color:Colors.blue,</pre><pre>transparent:false,</pre><pre>opacity:1,</pre><pre>flatShading:THREE.FlatShading,</pre><pre>});</pre><pre>this.mesh = new THREE.Mesh(geom, mat);</pre><pre>this.mesh.receiveShadow = true;</pre><pre>}</pre><p>So, I’m warping up this article now thinking for writing this in parts, so lets summaries what we learn in this part 1 of creating night scene in three.js</p><ol><li>create a Scene</li><li>create light</li><li>Setup our Init and loop functions</li><li>create a geometry</li><li>create a material</li><li>pass them into a mesh</li><li>add the mesh to our scene</li></ol><h3>Final Render scene</h3><p><a href="https://vimeo.com/user115252273/review/417334797/a31694ce90">Night Scene in Three.js</a></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*LirL2Ls1pIx2gCBzIiSrYw.png" /><figcaption>Final Render Scene</figcaption></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=435594d5aa8f" width="1" height="1" alt=""><hr><p><a href="https://medium.com/swlh/the-making-of-night-landscape-animating-a-basic-3d-scene-with-three-js-435594d5aa8f">The Making of “Night Landscape”: Animating a Basic 3D Scene with Three.js</a> was originally published in <a href="https://medium.com/swlh">The Startup</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>