WebTexture.js 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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/Diff.xml"));
  12. webMaterial.setTexture(Atomic.TextureUnit.TU_DIFFUSE, texture2D);
  13. // Create web client with pluggable handlers
  14. var webClient = new WebView.WebClient();
  15. // Set our render handler to be the WebTexture2D we created above
  16. webClient.webRenderHandler = webTexture;
  17. // Create the browser!
  18. webClient.createBrowser(BROWSER_URL, BROWSER_WIDTH, BROWSER_HEIGHT);
  19. exports.component = function(self) {
  20. var camera = self.node.scene.getComponent("Camera", true);
  21. var octree = self.node.scene.getComponent("Octree", true);
  22. // assign the web material to our model component
  23. var model = self.node.getComponent("StaticModel");
  24. model.setMaterial(webMaterial);
  25. // update function
  26. self.update = function(timeStep) {
  27. // 3D web texture interaction
  28. var mousePos = Atomic.input.getMousePosition();
  29. // normalize x/y
  30. mousePos[0] /= Atomic.graphics.width;
  31. mousePos[1] /= Atomic.graphics.height;
  32. // calculate the screen ray at the mouse point
  33. var ray = camera.getScreenRay(mousePos[0], mousePos[1]);
  34. var result = octree.rayCastSingle(ray, Atomic.RayQueryLevel.RAY_TRIANGLE_UV, Atomic.M_INFINITY, Atomic.DRAWABLE_GEOMETRY);
  35. if (result) {
  36. var uv = result.textureUV;
  37. var x = uv[0] * BROWSER_WIDTH;
  38. var y = uv[1] * BROWSER_WIDTH;
  39. webClient.sendMouseMoveEvent(x, y, 0);
  40. if (Atomic.input.getMouseButtonPress(Atomic.MOUSEB_LEFT)) {
  41. webClient.sendMousePressEvent(x, y);
  42. }
  43. }
  44. if (Atomic.input.mouseMoveWheel) {
  45. webClient.sendMouseWheelEvent(0, 0, 0, 0, -Atomic.input.mouseMoveWheel);
  46. }
  47. };
  48. };