Bladeren bron

Start working on render engine.

Cixo Develop 9 maanden geleden
bovenliggende
commit
14abad8958
5 gewijzigde bestanden met toevoegingen van 106 en 3 verwijderingen
  1. 3 0
      assets/core.js
  2. 72 0
      assets/render-engine.js
  3. 15 3
      assets/scene.js
  4. 8 0
      index.html
  5. 8 0
      theme/scene.css

+ 3 - 0
assets/core.js

@@ -17,6 +17,9 @@ document.addEventListener("DOMContentLoaded", () => {
     const space = new scene();
     const controls = new scene_ui(space);
 
+    app.appendChild(space.canvas);
     app.appendChild(controls.box);
     app.appendChild(colors_changer);
+
+    space.run();
 });

+ 72 - 0
assets/render-engine.js

@@ -0,0 +1,72 @@
+import * as three from "three-js";
+
+class render_engine {
+    #renderer;
+    #scene;
+    #camera;
+    #canvas;
+    #actors;
+
+    constructor(canvas, context) {
+        if (!(canvas instanceof HTMLCanvasElement)) {
+            throw TypeError("Canvas must be an HTMLCanvasElement.");
+        }
+
+        if (!(context instanceof WebGL2RenderingContext)) {
+            throw TypeError("Context must be WebGL2 RenderingContext.");
+        }
+
+        this.#actors = {};
+        this.#canvas = canvas;
+        this.#scene = new three.Scene();
+        this.#camera = new three.PerspectiveCamera(75, 1, 0.1, 1000);
+
+        this.#renderer = new three.WebGLRenderer({ 
+            canvas: this.#canvas,
+            context: context
+        });
+   
+        this.#update_camera();
+        this.#resize_canvas();
+
+        window.addEventListener("resize", () => {
+            this.#resize_canvas();
+            this.#update_camera();
+        });
+    }
+
+    #resize_canvas() {
+        this.#canvas.width = window.innerWidth;
+        this.#canvas.height = window.innerHeight;
+
+        this.#renderer.setSize(window.innerWidth, window.innerHeight);
+    }
+
+    #update_camera() {
+        this.#camera.aspect = this.#canvas.width / this.#canvas.height;
+    }
+
+    run() {
+        this.#init();
+        this.#renderer.setAnimationLoop(() => { this.#render(); });
+    }
+
+    #init() {
+        const geometry = new three.BoxGeometry(1, 1, 1);
+        const material = new three.MeshBasicMaterial({ color: 0x004000 });
+        this.#actors.cube = new three.Mesh(geometry, material);
+
+        this.#scene.add(this.#actors.cube);
+
+        this.#camera.position.z = 5;
+    }
+
+    #render() {
+        this.#actors.cube.rotation.x += 0.01;
+        this.#actors.cube.rotation.y += 0.01;
+
+        this.#renderer.render(this.#scene, this.#camera);
+    }
+}
+
+export { render_engine };

+ 15 - 3
assets/scene.js

@@ -1,23 +1,35 @@
 import { coordinates } from "./coordinates.js";
+import { render_engine } from "./render-engine.js"
 
 class scene {
     #canvas;
-    #context;
     #position;
+    #engine;
 
     constructor() {
         this.#position = new coordinates();
         this.#canvas = document.createElement("canvas");
-        this.#context = this.#canvas.getContext("webgl");
+        const context = this.#canvas.getContext("webgl2");
 
-        if (!this.#context) {
+        if (!context) {
             throw new TypeError("Browser does not support WebGL.");
         }
+
+        this.#engine = new render_engine(this.#canvas, context);
+        this.#canvas.classList.add("space-render");
     }
 
     get position() {
         return this.#position;
     }
+
+    get canvas() {
+        return this.#canvas;
+    }
+
+    run() {
+        this.#engine.run();
+    }
 }
 
 export { scene };

+ 8 - 0
index.html

@@ -3,6 +3,14 @@
 <html>
     <head>
         <meta charset="UTF-8">
+        <script type="importmap">
+            {
+              "imports": {
+                "three-js": "https://cdn.jsdelivr.net/npm/[email protected]/build/three.module.js",
+                "three-js/addons/": "https://cdn.jsdelivr.net/npm/[email protected]/examples/jsm/"
+              }
+            }
+        </script>
         <script type="module" src="assets/core.js"></script>
         <link rel="stylesheet" type="text/css" href="theme/loader.css">
         <link rel="preconnect" href="https://fonts.googleapis.com">

+ 8 - 0
theme/scene.css

@@ -6,4 +6,12 @@
 
 #container-controls .container button {
     margin: 10px;
+}
+
+.space-render {
+    width: 100%;
+    height: 100%;
+    position: absolute;
+    top: 0px;
+    left: 0px;
 }