TA159

Notas, resueltos y trabajos practicos de la materia Sistemas Gráficos
Index Commits Files Refs Submodules README LICENSE
commit 34b252a70c040809264409a9dab62f8236283ac6
parent 9f346a5d41cf607acaa984e2a6267db52739f27f
Author: mjkloeckner <martin.cachari@gmail.com>
Date:   Sun,  2 Jun 2024 02:12:47 -0300

add `createInstancedTrees` function

TODO: implement tree leaves generation which is missing

Diffstat:
Mtp/src/trees.js | 50++++++++++++++++++++++++++++++++++++++++++++++++--
1 file changed, 48 insertions(+), 2 deletions(-)
diff --git a/tp/src/trees.js b/tp/src/trees.js
@@ -19,7 +19,7 @@ function setupThreeJs() {
     container.appendChild(renderer.domElement);
 
     camera = new THREE.PerspectiveCamera(35, window.innerWidth / window.innerHeight, 0.1, 1000);
-    camera.position.set(-40, 50, 30);
+    camera.position.set(-50, 60, 50);
     camera.lookAt(0, 0, 0);
 
     const controls = new OrbitControls(camera, renderer.domElement);
@@ -44,6 +44,48 @@ function setupThreeJs() {
     onResize();
 }
 
+function createInstancedTrees(count) {
+    console.log('Generating `' + count + '` instances of tree');
+
+    const treeLogGeometry    = new THREE.CylinderGeometry(0.35, 0.35, 4, 40, 40);
+    //const treeLeavesGeometry = new THREE.SphereGeometry(1.75,40,40);
+
+    treeLogGeometry.translate(0, 2, 0);
+
+    const instancedTreeLogGeometry = new THREE.InstancedBufferGeometry();
+    instancedTreeLogGeometry.copy(treeLogGeometry);
+
+    const treeLogMaterial = new THREE.MeshPhongMaterial({color: 0x7c3f00});
+    const instancedTreeLogs = new THREE.InstancedMesh(instancedTreeLogGeometry, treeLogMaterial, count);
+
+    const rotMatrix = new THREE.Matrix4();
+    const translationMatrix = new THREE.Matrix4();
+    const matrix = new THREE.Matrix4();
+
+    //let origin = new THREE.Vector3();
+    const RANGE = 50 - 4/2;
+
+    for (let i = 0; i < count; i++) {
+        let position = new THREE.Vector3(
+            (Math.random() - 0.5) * RANGE,
+            0,
+            (Math.random() - 0.5) * RANGE
+        );
+
+        translationMatrix.makeTranslation(position);
+
+        //rotMatrix.lookAt(0, 0, new THREE.Vector3(0, 1, 0));
+        matrix.identity();
+        matrix.makeScale(1, 0.5 + Math.random()*2, 1);
+        //matrix.premultiply(rotMatrix);
+        matrix.premultiply(translationMatrix);
+
+        instancedTreeLogs.setMatrixAt(i, matrix);
+    }
+
+    return instancedTreeLogs;
+}
+
 function buildScene() {
     console.log('Building scene');
 
@@ -54,6 +96,10 @@ function buildScene() {
     terrain.rotateX(Math.PI/2);
     terrain.position.set(0, 0, 0);
     scene.add(terrain);
+
+    console.log('Generating trees');
+    const trees = createInstancedTrees(20);
+    scene.add(trees);
 }
 
 function createMenu() {
@@ -68,7 +114,7 @@ function mainLoop() {
 function main() {
     setupThreeJs();
     buildScene();
-    createMenu();
+    //createMenu();
     mainLoop();
 }