Advertisement
Guest User

mapBehavior

a guest
Feb 13th, 2016
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class mapBehavior extends Sup.Behavior {
  2.   content = this.actor.getChild("content");
  3.   terrainCamera                               // camera for rendering islands
  4.   mapSurface: THREE.Mesh;                     // plane to draw on
  5.   mapContentTexture: THREE.WebGLRenderTarget; // render target to draw with
  6.   mapContentRenderer: THREE.WebGLRenderer;    // renderer to render with
  7.   mapContentScene: THREE.Scene;               // scene to render in
  8.   mapContentSurface;                          // material to set the render to
  9.  
  10.   // this part is for incoming island data, it works and it's not part of the issue
  11.   recievedIsland = (mesh) => {
  12.     let content: any = this.actor.getChild("content");
  13.     //content.__inner.threeObject.add(mesh.clone());
  14.     this.mapContentScene.add(mesh.clone());
  15.     this.mapContentScene.updateMatrixWorld(true);
  16.   }
  17.  
  18.   awake() {
  19.     let map: any = this.actor;
  20.     let content: any = this.actor.getChild("content");
  21.     //let terrainCamera: any = this.actor.getParent().getChild("terrainCamera");
  22.     let alphaMapSprite: any = Sup.get("ui/map/mapAlphaMap");  // sprite with an alpha map
  23.     let alphaMap = alphaMapSprite.__inner.textures.map;       // alpha map (will be used later)
  24.     let mapShader: any = Sup.get("ui/map/mapShader");         // shader for the plane
  25.    
  26.     // make the new camera
  27.     let terrainCamera = new THREE.OrthographicCamera(-100, 100, -100, 100, 0.1, 1000);
  28.     terrainCamera.position.z = 100;
  29.     terrainCamera.updateMatrixWorld(true);
  30.    
  31.     // make other stuff
  32.     this.mapContentTexture = new THREE.WebGLRenderTarget(400, 280, { minFilter: THREE.LinearFilter, magFilter: THREE.NearestFilter, format: THREE.RGBFormat });
  33.     this.mapContentRenderer = new THREE.WebGLRenderer();
  34.     this.mapContentScene = new THREE.Scene();
  35.     this.mapContentSurface = new THREE.ShaderMaterial({
  36.       uniforms: {
  37.         tDiffuse: {type: "t", value: this.mapContentTexture}
  38.       },
  39.       vertexShader: "varying vec2 vUv; void main() { vUv = uv; gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );}",//mapShader.__inner.vertexShader.text,
  40.       fragmentShader: "uniform sampler2D tDiffuse; varying vec2 vUv; void main() { gl_FragColor = texture2D(tDiffuse, vUv);}",//mapShader.__inner.vertexShader.text
  41.       depthWrite: false
  42.     })
  43.    
  44.     this.mapContentRenderer.setClearColor(0xffff00); //debug purposes (in case the camera is facing nothing)
  45.    
  46.     // create the mesh
  47.     this.mapSurface = new THREE.Mesh(
  48.       new THREE.PlaneGeometry(400, 280),
  49.       //new THREE.MeshBasicMaterial({transparent: true, alphaMap: alphaMap})
  50.       this.mapContentSurface
  51.     );
  52.     content.__inner.threeObject.add(this.mapSurface);
  53.     this.terrainCamera = terrainCamera;
  54.    
  55.     //this.mapContentScene.add(this.terrainCamera);
  56.    
  57.     // add a sphere for debug purposes (in case the islands didn't get added or something)
  58.     this.mapContentScene.add(
  59.       new THREE.Mesh (
  60.         new THREE.SphereGeometry(5),
  61.         new THREE.MeshBasicMaterial()
  62.       )
  63.     );
  64.    
  65.     Sup.log(this.mapContentScene); // make sure all is well
  66.    
  67.     createdIsland.on("island", (mesh) => {this.recievedIsland(mesh)}); // unimportant for this
  68.   }
  69.  
  70.   update() {
  71.     this.terrainCamera.lookAt(0, 0, 0); // look at the center of the scene
  72.     this.mapContentRenderer.clear();    // clear the render target
  73.     this.mapContentRenderer.render(this.mapContentScene, this.terrainCamera, this.mapContentTexture, true); // RENDER EVERYTHING
  74.   }
  75. }
  76. Sup.registerBehavior(mapBehavior);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement