Practical 01

by pancha00

HTML

<script type="importmap">
{
"imports": {
  "three": "https://unpkg.com/three@0.160.0/build/three.module.js",
  "three/addons/": "https://unpkg.com/three@0.160.0/examples/jsm/"
}
}
</script>
<canvas id="canvas" ></canvas>

CSS

body {
	  margin: 0;
    padding: 0;
}

canvas {
  display: block;
}

canvas {
  width: 100%;
  height:  100vh;
  display: block;
}

JavaScript

import * as THREE from 'three';
import { OrbitControls } from 'three/addons/controls/OrbitControls.js';

let scene, camera, mesh;
let controls;

init();

//Instrucciones: 
// 1) Agrega un PointLight justo arriba del cubo
// 2) Haz que el cubo rote en el update
// 3) Discute, cómo podemos hacer que el cubo gire con 
// respecto al centro del mundo
// 4) Cómo podemos hacer que la luz se mantenga arriba del cubo?
function createGeometry() {
    const geometry = new THREE.BoxGeometry();
    const material = new THREE.MeshLambertMaterial({ color: '#FF00FF' });
    mesh = new THREE.Mesh(geometry, material);
    mesh.position.set(3, 0, 0);
    scene.add(mesh);
}

function createCamera() {
    const aspect = canvas.clientWidth / canvas.clientHeight;
    camera = new THREE.PerspectiveCamera(60, aspect, 0.1, 100);
    camera.position.set(10, 10, 10);
    scene.add(camera);
}

function createLight() {
    //https://threejs.org/docs/?q=Point#PointLight
    const pointLight = new THREE.PointLight(0xffffff, 1, 0, 2);

}


/** Updates the elements of the scene before the call to render */
function update() {

}

/** Creates the scene and initalizes the elements in it **/
function init() {
    const canvas = document.querySelector('#canvas');

    scene = new THREE.Scene();
    scene.background = new THREE.Color('#aaa');

    createCamera();
    createGeometry();
    createLight();

    const renderer = new THREE.WebGLRenderer({ canvas, antialias: true });
    //renderer.setPixelRatio( window.devicePixelRatio );
    renderer.setSize(canvas.clientWidth, canvas.clientHeight);

    renderer.setAnimationLoop(() => {
        update();
        controls.update();
        renderer.render(scene, camera);
    });

    // Helpers (could be removed!)
    // ------------------------------------
    controls = new OrbitControls(camera, renderer.domElement);
    controls.update();

    const gridHelper = new...