Game Engine for 3D games with pre-rendered background in the browser https://victorgiers.com/painted_world/v0.1/painted_world.html
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

painted_world.js 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. 'use strict';
  2. var ctxSettings = {};
  3. ctxSettings.alpha = true;
  4. ctxSettings.preserveDrawingBuffer = true;
  5. var app = new v3d.App('container', ctxSettings, null);
  6. app.clearBkgOnLoad = true;
  7. app.renderer.setClearColor(0x000000, 0);
  8. app.renderCallbacks.push(renderCb);
  9. app.compileCallbacks.push(compileCb);
  10. var pause = false, loading = false;
  11. function compileCb(){
  12. dof(0.0,200, 0.025, 1);
  13. bloom(7.5, 0.2, 0.4);
  14. }
  15. var controllerMatrix;
  16. app.loadScene('PlayerCharacter.gltf', function() {
  17. app.run();
  18. //app.enableControls();
  19. controllerCam = app.scene.children.find(controllerCam => controllerCam.name == 'ControllerCam');
  20. playerCharacter = app.scene.children.find(playerCharacter => playerCharacter.name == "PlayerRig");
  21. switchField('s1', 'f1', 'sp1');
  22. mainMenu = false;
  23. });
  24. $('document').ready(function(){
  25. // switchField('s1', 'f1', 'sp1');
  26. // mainMenu = false;
  27. });
  28. //var movingLeft, movingRight, movingUp, movingDown;
  29. let mainMenu = true;
  30. var playerCharacter;
  31. var controllerCam;
  32. var upDirector;
  33. var groundPlane;
  34. var fieldScene, fieldCollider, fieldPortals, fieldCamera;
  35. var movementSpeed = 0.05;
  36. let PI = 3.1416;
  37. //var finishedLoading = false;
  38. var playerPosition = new v3d.Vector3();
  39. var nextPosition = new v3d.Vector3();
  40. var colliderBoxes = [];
  41. var portalBoxes = [];
  42. function collides(vector){
  43. /*
  44. for(var i = 0; i < portalBoxes.length; i++){
  45. if(portalBoxes[i].containsPoint(vector)){
  46. var target = fieldPortals[i].userData.v3d.customProps.target.split(/[$-/:-?{-~!"^_`\[\]]/gi);
  47. console.log(target[0] + " " + target[1] + " " + target[2]);
  48. if(!loading){
  49. switchField(target[0], target[1], target[2]);
  50. return false;
  51. }
  52. }
  53. }
  54. */
  55. var collides = false;
  56. colliderBoxes.forEach(function(box){
  57. if(box.containsPoint(vector)){
  58. collides = true;
  59. }
  60. });
  61. return collides;
  62. }
  63. function renderCb(){
  64. if(playerCharacter != undefined){
  65. playerPosition.set(playerCharacter.position.x, playerCharacter.position.y-1.75, playerCharacter.position.z);
  66. //if(finishedLoading){
  67. for(var i = 0; i < portalBoxes.length; i++){
  68. if(portalBoxes[i].containsPoint(playerPosition) && !loading){
  69. loading = true;
  70. var target = fieldPortals[i].userData.v3d.customProps.target.split(/[$-/:-?{-~!"^_`\[\]]/gi);
  71. console.log(target[0] + " " + target[1] + " " + target[2]);
  72. switchField(target[0], target[1], target[2]);
  73. //finishedLoading = false;
  74. //return false;
  75. }
  76. }
  77. }
  78. }
  79. function switchField(scene, field, spawnpoint) {
  80. //pause = true;
  81. if(app.controls != undefined){
  82. app.controls.dispose();
  83. //app.unload(app.camera);
  84. }
  85. //controllerCam.position.set(0,1.75,0);
  86. //controllerCam.applyMatrix4(controllerMatrix);
  87. //playerCharacter
  88. var fieldName = scene + field;
  89. var fieldURI = "scenes/" + fieldName + "/" + fieldName + ".gltf";
  90. var bgimageURI = "scenes/" + fieldName + "/" + fieldName + "_bg.png";
  91. if(fieldScene != undefined){
  92. app.setCamera(controllerCam);
  93. app.unload(fieldScene);
  94. }
  95. app.appendScene(fieldURI, function(){
  96. $("#container").css("background-image", "url(" + bgimageURI + ")");
  97. $('#overlay').hide();
  98. fieldScene = app.scene.children.find(fieldScene => fieldScene.name == 'Scene');
  99. groundPlane = fieldScene.children.find(groundPlane => groundPlane.name == 'Ground');
  100. console.log(fieldScene);
  101. fieldCollider = fieldScene.children.filter(fieldCollider => fieldCollider.groupNames.includes('Collider'));
  102. colliderBoxes = [];
  103. for (let i = 0; i < fieldCollider.length; i++) {
  104. colliderBoxes.push(new v3d.Box3());
  105. colliderBoxes[i].copy(fieldCollider[i].geometry.boundingBox).applyMatrix4(fieldCollider[i].matrixWorld);
  106. }
  107. fieldPortals = fieldScene.children.filter(fieldPortals => fieldPortals.groupNames.includes('Portals'));
  108. portalBoxes = [];
  109. for (let i = 0; i < fieldPortals.length; i++) {
  110. portalBoxes.push(new v3d.Box3());
  111. portalBoxes[i].copy(fieldPortals[i].geometry.boundingBox).applyMatrix4(fieldPortals[i].matrixWorld);
  112. }
  113. fieldCamera = fieldScene.children.find(fieldCamera => fieldCamera.name == 'Camera');
  114. app.setCamera(fieldCamera);
  115. let spawnPoint = fieldScene.children.find(spawnPoint => spawnPoint.name == scene + "_" + field + "_" + spawnpoint);
  116. //alles superkacke hier :D
  117. controllerCam.position.set(0,1.75,0);
  118. controllerCam.setRotationFromMatrix(spawnPoint.matrix);
  119. playerCharacter.setRotationFromMatrix(spawnPoint.matrix);
  120. controllerCam.applyMatrix4(spawnPoint.matrix);
  121. app.enableControls();
  122. app.controls.object = controllerCam;
  123. loading = false;
  124. }, fieldLoadProgress, fieldLoadError, true, true);
  125. }
  126. function fieldLoadProgress(progress){
  127. //console.log(progress);
  128. $('#overlay').show().css('background-color', 'rgba(0,0,0,' + progress / 100 + ')');
  129. }
  130. function fieldLoadError(error){
  131. console.log("Error loading Field, dumping: " + error);
  132. }
  133. var cube;
  134. document.addEventListener('keydown', function(event){
  135. if(mainMenu){
  136. /*
  137. if(event.key == 'Enter'){
  138. switchField('s1', 'f1', 'sp1');
  139. mainMenu = false;
  140. }
  141. */
  142. } else {
  143. /*
  144. if(event.key == 'Enter'){
  145. switchField('s1', 'f1', 'sp1');
  146. }
  147. if(event.key == 'p'){
  148. pause = !pause;
  149. }
  150. if(event.key == '1'){
  151. switchField('s1', 'f1', 'sp1');
  152. }
  153. if(event.key == '2'){
  154. switchField('s1', 'f2', 'sp1');
  155. }
  156. if(event.key == '3'){
  157. switchField('s1', 'f3', 'sp1');
  158. }
  159. if(event.key == '4'){
  160. switchField('s1', 'f4', 'sp1');
  161. }
  162. if(event.key == '5'){
  163. switchField('s1', 'f5', 'sp1');
  164. }
  165. */
  166. if(event.key == 'ArrowUp'){
  167. playerCharacter.rotation.y = PI * 2;
  168. }
  169. if(event.key == 'ArrowDown'){
  170. playerCharacter.rotation.y = PI;
  171. }
  172. if(event.key == 'ArrowLeft'){
  173. playerCharacter.rotation.y = PI / 2;
  174. }
  175. if(event.key == 'ArrowRight'){
  176. playerCharacter.rotation.y = PI + PI / 2;
  177. }
  178. }
  179. });
  180. document.addEventListener('keyup', function(event){
  181. });
  182. function dof(focus, aperture, maxblur, depthLeakThreshold) {
  183. app.enablePostprocessing([{
  184. type: 'dof',
  185. focus: focus,
  186. aperture: aperture,
  187. maxblur: maxblur,
  188. depthLeakThreshold: depthLeakThreshold
  189. }]);
  190. }
  191. function bloom(threshold, strength, radius) {
  192. app.enablePostprocessing([{
  193. type: 'bloom',
  194. threshold: threshold,
  195. strength: strength,
  196. radius: radius
  197. }]);
  198. }