Three.js කියන්නේ JavaScript වලින් 3D graphics කරන්න පුළුවන් ලාංකීයවම කියනවා නම්, “Browser එකේ 3D දේවල් ගහන්න හැකි පිස්සුවක්” 😅
පළමු episode එකේ අපි setup එක (Scene, Camera, Renderer) හැදුවා. දැන් අපි හැදුව Scene එකට cube එකක් දාමු, rotate කරමු, හා animate කරමු.
threejs-app/ ├── index.html └── script.js
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>Three.js Cube Example</title> <style> body { margin: 0; overflow: hidden; } canvas { display: block; } </style> </head> <body> <script type="module" src="script.js"></script> </body> </html>
// Three.js එක import කරනවා CDN එකෙන් import * as THREE from 'https://cdn.skypack.dev/three@0.152.2'; // ✅ Scene එකක් හදමු const scene = new THREE.Scene(); // ✅ Perspective camera එකක් හදමු const camera = new THREE.PerspectiveCamera( 75, // Field of View (angle) window.innerWidth / window.innerHeight, // aspect ratio 0.1, // near clipping plane 1000 // far clipping plane ); // ✅ Renderer එක canvas එකට ගහමු const renderer = new THREE.WebGLRenderer(); renderer.setSize(window.innerWidth, window.innerHeight); document.body.appendChild(renderer.domElement); // ✅ Box (Cube) Geometry එකක් හදමු const geometry = new THREE.BoxGeometry(1, 1, 1); // width, height, depth // ✅ Material එකක් – cube එකට color එකක් const material = new THREE.MeshStandardMaterial({ color: 0x0077ff }); // ✅ Mesh එක = Geometry + Material const cube = new THREE.Mesh(geometry, material); // ✅ Scene එකට cube එක එකතු කරමු scene.add(cube); // ✅ Light එකක් – otherwise object එක black වෙනවා const light = new THREE.DirectionalLight(0xffffff, 1); // white light light.position.set(2, 2, 5); // light එක position scene.add(light); // ✅ Camera එක zoom එක adjust කරමු camera.position.z = 5; // ✅ Animation function එකක් – cube එක rotate වෙන්න function animate() { requestAnimationFrame(animate); // frame by frame run කරන function එක // X සහ Y axis වලට cube එක little bit rotate වෙයි cube.rotation.x += 0.01; cube.rotation.y += 0.01; renderer.render(scene, camera); // scene එක draw කරනවා } animate();
THREE.MeshBasicMaterial()
– lighting බලන්නේ නෑ.THREE.MeshStandardMaterial()
– lighting effect බලනවා.StandardMaterial
දාලා නම් object එක totally black වෙනවා.renderer.setSize(window.innerWidth, window.innerHeight);
"Multiple Objects, Background Colors & Orbit Controls!" 🌈🌀
අපි cube එකට පාසලක් වගේ හත අටක් එක්ක ඒවා move කරන්න බලමු! Zoom, Rotate controls add කරන්නත් ඉගෙන ගමු.
Member since 2025-04-09 13:55:06
Comments
Please login to post a comment.
No comments yet. Be the first to comment!