Advertisement
here2share

$ minimalistic 3d game engine in javascript

Sep 15th, 2022 (edited)
966
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // minimalistic 3d game engine in javascript
  2.  
  3. // canvas element
  4. var canvas = document.createElement("canvas");
  5. var ctx = canvas.getContext("2d");
  6. canvas.width = 600;
  7. canvas.height = 600;
  8. document.body.appendChild(canvas);
  9.  
  10. // camera
  11. var camera = new Camera();
  12.  
  13. // scene
  14. var scene = new Scene();
  15.  
  16. // cube
  17. function cube(x, y, z, size, color) {
  18.     var cube = new GameObject();
  19.     cube.model = models.cube;
  20.     cube.position = new Vector3(x, y, z);
  21.     cube.scale = new Vector3(size, size, size);
  22.     cube.color = color;
  23.     scene.add(cube);
  24. }
  25.  
  26. // sphere
  27. function sphere(x, y, z, size, color) {
  28.     var sphere = new GameObject();
  29.     sphere.model = models.sphere;
  30.     sphere.position = new Vector3(x, y, z);
  31.     sphere.scale = new Vector3(size, size, size);
  32.     sphere.color = color;
  33.     scene.add(sphere);
  34. }
  35.  
  36. // pyramid
  37. function pyramid(x, y, z, size, color) {
  38.     var pyramid = new GameObject();
  39.     pyramid.model = models.pyramid;
  40.     pyramid.position = new Vector3(x, y, z);
  41.     pyramid.scale = new Vector3(size, size, size);
  42.     pyramid.color = color;
  43.     scene.add(pyramid);
  44. }
  45.  
  46. // cylinder
  47. function cylinder(x, y, z, size, color) {
  48.     var cylinder = new GameObject();
  49.     cylinder.model = models.cylinder;
  50.     cylinder.position = new Vector3(x, y, z);
  51.     cylinder.scale = new Vector3(size, size, size);
  52.     cylinder.color = color;
  53.     scene.add(cylinder);
  54. }
  55.  
  56. // cone
  57. function cone(x, y, z, size, color) {
  58.     var cone = new GameObject();
  59.     cone.model = models.cone;
  60.     cone.position = new Vector3(x, y, z);
  61.     cone.scale = new Vector3(size, size, size);
  62.     cone.color = color;
  63.     scene.add(cone);
  64. }
  65.  
  66. // torus
  67. function torus(x, y, z, size, color) {
  68.     var torus = new GameObject();
  69.     torus.model = models.torus;
  70.     torus.position = new Vector3(x, y, z);
  71.     torus.scale = new Vector3(size, size, size);
  72.     torus.color = color;
  73.     scene.add(torus);
  74. }
  75.  
  76. // terrain
  77. function terrain(x, y, z, size, color) {
  78.     var terrain = new GameObject();
  79.     terrain.model = models.terrain;
  80.     terrain.position = new Vector3(x, y, z);
  81.     terrain.scale = new Vector3(size, size, size);
  82.     terrain.color = color;
  83.     scene.add(terrain);
  84. }
  85.  
  86. // light
  87. function light(x, y, z, color) {
  88.     var light = new Light();
  89.     light.position = new Vector3(x, y, z);
  90.     light.color = color;
  91.     scene.add(light);
  92. }
  93.  
  94. // directional light
  95. function directional_light(x, y, z, color) {
  96.     var directional_light = new DirectionalLight();
  97.     directional_light.position = new Vector3(x, y, z);
  98.     directional_light.color = color;
  99.     scene.add(directional_light);
  100. }
  101.  
  102. // spot light
  103. function spot_light(x, y, z, color) {
  104.     var spot_light = new SpotLight();
  105.     spot_light.position = new Vector3(x, y, z);
  106.     spot_light.color = color;
  107.     scene.add(spot_light);
  108. }
  109.  
  110. // point light
  111. function point_light(x, y, z, color) {
  112.     var point_light = new PointLight();
  113.     point_light.position = new Vector3(x, y, z);
  114.     point_light.color = color;
  115.     scene.add(point_light);
  116. }
  117.  
  118. // ambient light
  119. function ambient_light(color) {
  120.     var ambient_light = new AmbientLight();
  121.     ambient_light.color = color;
  122.     scene.add(ambient_light);
  123. }
  124.  
  125. // fog
  126. function fog(color, density) {
  127.     var fog = new Fog();
  128.     fog.color = color;
  129.     fog.density = density;
  130.     scene.add(fog);
  131. }
  132.  
  133. // particle
  134. function particle(x, y, z, size, color) {
  135.     var particle = new Particle();
  136.     particle.position = new Vector3(x, y, z);
  137.     particle.scale = new Vector3(size, size, size);
  138.     particle.color = color;
  139.     scene.add(particle);
  140. }
  141.  
  142. // image
  143. function image(x, y, width, height, image) {
  144.     var image = new Image();
  145.     image.position = new Vector3(x, y, 0);
  146.     image.scale = new Vector3(width, height, 0);
  147.     image.image = image;
  148.     scene.add(image);
  149. }
  150.  
  151. // audio
  152. function audio(x, y, z, audio) {
  153.     var audio = new Audio();
  154.     audio.position = new Vector3(x, y, z);
  155.     audio.audio = audio;
  156.     scene.add(audio);
  157. }
  158.  
  159. // load 3d models
  160. var models = {};
  161. var model_names = ["cube", "sphere", "pyramid", "cylinder", "cone", "torus", "terrain", "particle"];
  162. var model_count = 0;
  163. for (var i = 0; i < model_names.length; i++) {
  164.     var model = new Model();
  165.     model.load(model_names[i], function() {
  166.         model_count++;
  167.         if (model_count == model_names.length) {
  168.             init();
  169.         }
  170.     });
  171.     models[model_names[i]] = model;
  172. }
  173.  
  174. // button
  175. function button(x, y, width, height, color, text, onclick) {
  176.     var button = new Button();
  177.     button.position = new Vector3(x, y, 0);
  178.     button.scale = new Vector3(width, height, 0);
  179.     button.color = color;
  180.     button.text = text;
  181.     button.onclick = onclick;
  182.     scene.add(button);
  183. }
  184.  
  185. // text
  186. function text(x, y, size, color, text) {
  187.     var text = new Text();
  188.     text.position = new Vector3(x, y, 0);
  189.     text.scale = new Vector3(size, size, 0);
  190.     text.color = color;
  191.     text.text = text;
  192.     scene.add(text);
  193. }
  194.  
  195. // input
  196. function input(x, y, width, height, color, text, onchange) {
  197.     var input = new Input();
  198.     input.position = new Vector3(x, y, 0);
  199.     input.scale = new Vector3(width, height, 0);
  200.     input.text = text;
  201.     input.onchange = onchange;
  202.     scene.add(input);
  203. }
  204.  
  205. // slider
  206. function slider(x, y, width, height, color, min, max, value, onchange) {
  207.     var slider = new Slider();
  208.     slider.position = new Vector3(x, y, 0);
  209.     slider.scale = new Vector3(width, height, 0);
  210.     slider.color = color;
  211.     slider.min = min;
  212.     slider.max = max;
  213.     slider.value = value;
  214.     slider.onchange = onchange;
  215.     scene.add(slider);
  216. }
  217.  
  218. // checkbox
  219. function checkbox(x, y, width, height, color, checked, onchange) {
  220.     var checkbox = new Checkbox();
  221.     checkbox.position = new Vector3(x, y, 0);
  222.     checkbox.scale = new Vector3(width, height, 0);
  223.     checkbox.checked = checked;
  224.     checkbox.onchange = onchange;
  225.     scene.add(checkbox);
  226. }
  227.  
  228. // radio
  229. function radio(x, y, width, height, color, checked, onchange) {
  230.     var radio = new Radio();
  231.     radio.position = new Vector3(x, y, 0);
  232.     radio.scale = new Vector3(width, height, 0);
  233.     radio.checked = checked;
  234.     radio.onchange = onchange;
  235.     scene.add(radio);
  236. }
  237.  
  238. // dropdown
  239. function dropdown(x, y, width, height, color, options, onchange) {
  240.     var dropdown = new Dropdown();
  241.     dropdown.position = new Vector3(x, y, 0);
  242.     dropdown.scale = new Vector3(width, height, 0);
  243.     dropdown.options = options;
  244.     dropdown.onchange = onchange;
  245.     scene.add(dropdown);
  246. }
  247.  
  248. // progressbar
  249. function progressbar(x, y, width, height, color, value) {
  250.     var progressbar = new Progressbar();
  251.     progressbar.position = new Vector3(x, y, 0);
  252.     progressbar.scale = new Vector3(width, height, 0);
  253.     progressbar.color = color;
  254.     progressbar.value = value;
  255.     scene.add(progressbar);
  256. }
  257.  
  258. // canvas
  259. function canvas(x, y, width, height, color) {
  260.     var canvas = new Canvas();
  261.     canvas.position = new Vector3(x, y, 0);
  262.     canvas.scale = new Vector3(width, height, 0);
  263.     canvas.color = color;
  264.     scene.add(canvas);
  265. }
  266.  
  267. // window
  268. function window(x, y, width, height, color, text) {
  269.     var window = new Window();
  270.     window.position = new Vector3(x, y, 0);
  271.     window.scale = new Vector3(width, height, 0);
  272.     window.color = color;
  273.     window.text = text;
  274.     scene.add(window);
  275. }
  276.  
  277. // panel
  278. function panel(x, y, width, height, color) {
  279.     var panel = new Panel();
  280.     panel.position = new Vector3(x, y, 0);
  281.     panel.scale = new Vector3(width, height, 0);
  282.     panel.color = color;
  283.     scene.add(panel);
  284. }
  285.  
  286. // scrollbar
  287. function scrollbar(x, y, width, height, color, vertical, value, onchange) {
  288.     var scrollbar = new Scrollbar();
  289.     scrollbar.position = new Vector3(x, y, 0);
  290.     scrollbar.scale = new Vector3(width, height, 0);
  291.     scrollbar.color = color;
  292.     scrollbar.vertical = vertical;
  293.     scrollbar.value = value;
  294.     scrollbar.onchange = onchange;
  295.     scene.add(scrollbar);
  296. }
  297.  
  298. // tab
  299. function tab(x, y, width, height, color, tabs) {
  300.     var tab = new Tab();
  301.     tab.position = new Vector3(x, y, 0);
  302.     tab.scale = new Vector3(width, height, 0);
  303.     tab.color = color;
  304.     tab.tabs = tabs;
  305.     scene.add(tab);
  306. }
  307.  
  308. // file load 3D models
  309. function load_model(name, callback) {
  310.     var model = new Model();
  311.     model.load(name, callback);
  312.     models[name] = model;
  313. }
  314.  
  315. // file load image
  316. function load_image(name, callback) {
  317.     var image = new Image();
  318.     image.load(name, callback);
  319.     images[name] = image;
  320. }
  321.  
  322. // file load audio
  323. function load_audio(name, callback) {
  324.     var audio = new Audio();
  325.     audio.load(name, callback);
  326.     audios[name] = audio;
  327. }
  328.  
  329. // file load font
  330. function load_font(name, callback) {
  331.     var font = new Font();
  332.     font.load(name, callback);
  333.     fonts[name] = font;
  334. }
  335.  
  336. // file load images
  337. var images = {};
  338. var image_names = [];
  339. var image_count = 0;
  340. for (var i = 0; i < image_names.length; i++) {
  341.     var image = new Image();
  342.     image.load(image_names[i], function() {
  343.         image_count++;
  344.         if (image_count == image_names.length) {
  345.             init();
  346.         }
  347.     });
  348.     images[image_names[i]] = image;
  349. }
  350.  
  351. // bind arrow keys to move player (and camera)
  352. function bind_arrow_keys() {
  353.     document.addEventListener("keydown", function(e) {
  354.         if (e.keyCode == 37) {
  355.             player.position.x -= 1;
  356.         }
  357.         if (e.keyCode == 38) {
  358.             player.position.y -= 1;
  359.         }
  360.         if (e.keyCode == 39) {
  361.             player.position.x += 1;
  362.         }
  363.         if (e.keyCode == 40) {
  364.             player.position.y += 1;
  365.         }
  366.     });
  367. }
  368.  
  369. // bind mouse click to shoot
  370. function bind_mouse_click() {
  371.     document.addEventListener("click", function(e) {
  372.         var bullet = new GameObject();
  373.         bullet.model = models.cube;
  374.         bullet.position = new Vector3(player.position.x, player.position.y, player.position.z);
  375.         bullet.velocity = new Vector3(e.clientX - canvas.width / 2, e.clientY - canvas.height / 2, 0);
  376.         scene.add(bullet);
  377.     });
  378. }
  379.  
  380. // bind mouse move to look around
  381. function bind_mouse_move() {
  382.     document.addEventListener("mousemove", function(e) {
  383.         camera.rotation.x = e.clientY / canvas.height * Math.PI;
  384.         camera.rotation.y = e.clientX / canvas.width * Math.PI;
  385.     });
  386. }
  387.  
  388. // bind mouse wheel to zoom
  389. function bind_mouse_wheel() {
  390.     document.addEventListener("wheel", function(e) {
  391.         camera.position.z += e.deltaY / 100;
  392.     });
  393. }
  394.  
  395. // bind spacebar to jump (or swim)
  396. function bind_spacebar() {
  397.     document.addEventListener("keydown", function(e) {
  398.         if (e.keyCode == 32) {
  399.             player.velocity.y = 1;
  400.         }
  401.     });
  402. }
  403.  
  404. // bind escape to pause
  405. function bind_escape() {
  406.     document.addEventListener("keydown", function(e) {
  407.         if (e.keyCode == 27) {
  408.             paused = !paused;
  409.         }
  410.     });
  411. }
  412.  
  413. // bind enter for action (or start)
  414. function bind_enter() {
  415.     document.addEventListener("keydown", function(e) {
  416.         if (e.keyCode == 13) {
  417.             started = true;
  418.         }
  419.     });
  420. }
  421.  
  422. // bind right mouse click for secondary action
  423. function bind_right_mouse_click() {
  424.     document.addEventListener("contextmenu", function(e) {
  425.         e.preventDefault();
  426.     });
  427.     document.addEventListener("mousedown", function(e) {
  428.         if (e.button == 2) {
  429.             secondary_action = true;
  430.         }
  431.     });
  432.     document.addEventListener("mouseup", function(e) {
  433.         if (e.button == 2) {
  434.             secondary_action = false;
  435.         }
  436.     });
  437. }
  438.  
  439. // display current score
  440. function display_score() {
  441.     var score = new Text();
  442.     score.position = new Vector3(0, 0, 0);
  443.     score.scale = new Vector3(0.1, 0.1, 0.1);
  444.     score.color = new Color(1, 1, 1);
  445.     score.text = "Score: " + player.score;
  446.     scene.add(score);
  447. }
  448.  
  449. // display health
  450. function display_health() {
  451.     var health = new Text();
  452.     health.position = new Vector3(0, 0.1, 0);
  453.     health.scale = new Vector3(0.1, 0.1, 0.1);
  454.     health.color = new Color(1, 1, 1);
  455.     health.text = "Health: " + player.health;
  456.     scene.add(health);
  457. }
  458.  
  459. // display ammo
  460. function display_ammo() {
  461.     var ammo = new Text();
  462.     ammo.position = new Vector3(0, 0.2, 0);
  463.     ammo.scale = new Vector3(0.1, 0.1, 0.1);
  464.     ammo.text = "Ammo: " + player.ammo;
  465.     scene.add(ammo);
  466. }
  467.  
  468. // display time
  469. function display_time() {
  470.     var time = new Text();
  471.     time.position = new Vector3(0, 0.3, 0);
  472.     time.scale = new Vector3(0.1, 0.1, 0.1);
  473.     time.color = new Color(1, 1, 1);
  474.     time.text = "Time: " + player.time;
  475.     scene.add(time);
  476. }
  477.  
  478. // display money
  479. function display_money() {
  480.     var money = new Text();
  481.     money.position = new Vector3(0, 0.5, 0);
  482.     money.text = "Money: " + player.money;
  483.     scene.add(money);
  484. }
  485.  
  486. // display fps
  487. function display_fps() {
  488.     var fps = new Text();
  489.     fps.position = new Vector3(0, 0.4, 0);
  490.     fps.scale = new Vector3(0.1, 0.1, 0.1);
  491.     fps.color = new Color(1, 1, 1);
  492.     fps.text = "FPS: " + player.fps;
  493.     scene.add(fps);
  494. }
  495.  
  496. // display (mini) map
  497. function display_map() {
  498.     var map = new Image();
  499.     map.position = new Vector3(0, 0, 0);
  500.     map.scale = new Vector3(1, 1, 0);
  501.     map.image = images.map;
  502.     scene.add(map);
  503. }
  504.  
  505. // display inventory
  506. function display_inventory() {
  507.     var inventory = new Image();
  508.     inventory.position = new Vector3(0, 0, 0);
  509.     inventory.scale = new Vector3(1, 1, 0);
  510.     inventory.image = images.inventory;
  511.     scene.add(inventory);
  512. }
  513.  
  514. // display pause menu
  515. function display_pause_menu() {
  516.     var pause_menu = new Image();
  517.     pause_menu.position = new Vector3(0, 0, 0);
  518.     pause_menu.scale = new Vector3(1, 1, 0);
  519.     pause_menu.image = images.pause_menu;
  520.     scene.add(pause_menu);
  521. }
  522.  
  523. // display settings menu
  524. function display_settings_menu() {
  525.     var settings_menu = new Image();
  526.     settings_menu.position = new Vector3(0, 0, 0);
  527.     settings_menu.scale = new Vector3(1, 1, 0);
  528.     settings_menu.image = images.settings_menu;
  529.     scene.add(settings_menu);
  530. }
  531.  
  532. // throw weapon
  533. function throw_weapon() {
  534.     var weapon = new GameObject();
  535.     weapon.model = models.cube;
  536.     weapon.position = new Vector3(player.position.x, player.position.y, player.position.z);
  537.     weapon.velocity = velocity.set
  538.     scene.add(weapon);
  539. }
  540.  
  541. // shoot weapon
  542. function shoot_weapon() {
  543.     var bullet = new GameObject();
  544.     bullet.model = models.cube;
  545.     bullet.position = new Vector3(player.position.x, player.position.y, player.position.z);
  546.     weapon.velocity = velocity.set
  547.     scene.add(bullet);
  548. }
  549.  
  550. // explosion
  551. function explosion(x, y, z) {
  552.     var explosion = new Particle();
  553.     explosion.position = new Vector3(x, y, z);
  554.     explosion.scale = new Vector3(0.1, 0.1, 0.1);
  555.     explosion.velocity = new Vector3(Math.random() * 2 - 1, Math.random() * 2 - 1, 0);
  556.     scene.add(explosion);
  557. }
  558.  
  559. // spawn enemy
  560. function spawn_enemy() {
  561.     var enemy = new GameObject();
  562.     enemy.model = models.cube;
  563.     enemy.position = new Vector3(Math.random() * 10 - 5, Math.random() * 10 - 5, 0);
  564.     scene.add(enemy);
  565. }
  566.  
  567. // spawn item
  568. function spawn_item() {
  569.     var item = new GameObject();
  570.     item.model = models.cube;
  571.     item.position = new Vector3(Math.random() * 10 - 5, Math.random() * 10 - 5, 0);
  572.     scene.add(item);
  573. }
  574.  
  575. // spawn powerup
  576. function spawn_powerup() {
  577.     var powerup = new GameObject();
  578.     powerup.model = models.cube;
  579.     powerup.position = new Vector3(Math.random() * 10 - 5, Math.random() * 10 - 5, 0);
  580.     scene.add(powerup);
  581. }
  582.  
  583. // display level
  584. function display_level() {
  585.     var level = new Text();
  586.     level.position = new Vector3(0, 0.6, 0);
  587.     level.text = "Level: " + player.level;
  588.     scene.add(level);
  589. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement