WebTexture.js 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. "atomic component";
  2. const BROWSER_WIDTH = 1024;
  3. const BROWSER_HEIGHT = 1024;
  4. const BROWSER_URL = "http://www.atomicgameengine.com/";
  5. // First create a web texture and set filtering mode
  6. var webTexture = new WebView.WebTexture2D();
  7. var texture2D = webTexture.texture2D;
  8. texture2D.filterMode = Atomic.TextureFilterMode.FILTER_TRILINEAR;
  9. // Setup a simple material for the web texture
  10. var webMaterial = new Atomic.Material();
  11. webMaterial.setTechnique(0, Atomic.cache.getResource("Technique", "Techniques/DiffEmissive.xml"));
  12. webMaterial.setTexture(Atomic.TextureUnit.TU_EMISSIVE, texture2D);
  13. webMaterial.setShaderParameter("MatEmissiveColor", "1 1 1 1");
  14. // Create web client with pluggable handlers
  15. var webClient = new WebView.WebClient();
  16. // Set our render handler to be the WebTexture2D we created above
  17. webClient.webRenderHandler = webTexture;
  18. // Create the browser!
  19. webClient.createBrowser(BROWSER_URL, BROWSER_WIDTH, BROWSER_HEIGHT);
  20. exports.component = function(self) {
  21. var camera = self.node.scene.getComponent("Camera", true);
  22. var octree = self.node.scene.getComponent("Octree", true);
  23. // assign the web material to our model component
  24. var model = self.node.getComponent("StaticModel");
  25. model.setMaterial(webMaterial);
  26. var hm = true;
  27. // update function
  28. self.update = function(timeStep) {
  29. // 3D web texture interaction
  30. var mousePos = Atomic.input.getMousePosition();
  31. if (Atomic.input.getKeyPress(Atomic.KEY_L)) {
  32. hm = !hm;
  33. }
  34. if (!hm)
  35. return;
  36. // normalize x/y
  37. mousePos[0] /= Atomic.graphics.width;
  38. mousePos[1] /= Atomic.graphics.height;
  39. // calculate the screen ray at the mouse point
  40. var ray = camera.getScreenRay(mousePos[0], mousePos[1]);
  41. var result = octree.rayCastSingle(ray, Atomic.RayQueryLevel.RAY_TRIANGLE_UV, Atomic.M_INFINITY, Atomic.DRAWABLE_GEOMETRY);
  42. if (result) {
  43. var uv = result.textureUV;
  44. var x = uv[0] * BROWSER_WIDTH;
  45. var y = uv[1] * BROWSER_WIDTH;
  46. webClient.sendMouseMoveEvent(x, y, 0);
  47. if (Atomic.input.getMouseButtonPress(Atomic.MOUSEB_LEFT)) {
  48. webClient.sendMousePressEvent(x, y);
  49. }
  50. }
  51. if (Atomic.input.mouseMoveWheel) {
  52. webClient.sendMouseWheelEvent(0, 0, 0, 0, -Atomic.input.mouseMoveWheel);
  53. }
  54. };
  55. };