Advertisement
KpoKec

ApplyOutline.js

Jul 30th, 2020
1,234
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var ApplyOutline = pc.createScript('applyOutline');
  2. ApplyOutline.attributes.add('camera', { type:'entity' });
  3. ApplyOutline.attributes.add('outlineLayerName', { type: 'string', default: "OutlineLayer"});
  4. ApplyOutline.attributes.add('color', { type: 'rgba' });
  5. ApplyOutline.attributes.add('blur', { type:'number', default: 1.0, min: 0.1, max: 5.0 });
  6. ApplyOutline.attributes.add('power', { type:'number', default: 1.0, min: 0.0, max: 5.0 });
  7.  
  8. // initialize code called once per entity
  9. ApplyOutline.prototype.initialize = function() {
  10.  
  11.     // --- variables
  12.     this.vec = new pc.Vec3();
  13.    
  14.     // --- execute
  15.     this.prepare();
  16.    
  17.     // --- events
  18.     window.addEventListener("resize", this.onResize.bind(this) );
  19.     this.app.on('Ermis:objectOutline:add', function(entity) {
  20.         if( entity && entity.model && entity.model.layers.indexOf(this.outlineLayer.id) === -1 ){
  21.             var layers = entity.model.layers.slice();
  22.             layers.push(this.outlineLayer.id);
  23.             entity.model.layers = layers;
  24.         }
  25.     }, this);
  26.    
  27.     this.app.on('Ermis:objectOutline:remove', function(entity) {
  28.         var index = entity.model.layers.indexOf(this.outlineLayer.id);
  29.         if( entity && entity.model && index > -1 ) {
  30.             var layers = entity.model.layers.slice();
  31.             layers.splice(index, 1);            
  32.             entity.model.layers = layers;
  33.         }        
  34.     }, this);
  35.     this.on('attr:color', function(value) {
  36.         this.color = value;
  37.         this.outline.color.copy(value);
  38.     }.bind(this), this);
  39. };
  40. ApplyOutline.prototype.postInitialize = function() {
  41. };
  42.  
  43. ApplyOutline.prototype.prepare = function() {
  44.     // create texture and render target for rendering into, including depth buffer
  45.     this.texture = new pc.Texture(this.app.graphicsDevice, {
  46.         width: this.app.graphicsDevice.width,
  47.         height: this.app.graphicsDevice.height,
  48.         format: pc.PIXELFORMAT_R8_G8_B8_A8,
  49.         mipmaps: true,
  50.         autoMipmap: true,
  51.         minFilter: pc.FILTER_LINEAR,
  52.         magFilter: pc.FILTER_LINEAR
  53.     });
  54.     this.renderTarget = new pc.RenderTarget(this.app.graphicsDevice, this.texture, { depth: true });
  55.  
  56.     // get layers
  57.     this.worldLayer = this.app.scene.layers.getLayerByName("World");    
  58.     this.outlineLayer = this.app.scene.layers.getLayerByName(this.outlineLayerName);    
  59.  
  60.     // set up layer to render to the render target
  61.     this.outlineLayer.renderTarget = this.renderTarget;
  62.    
  63.     // Create outline camera, which renders entities in outline layer
  64.     this.outlineCamera = new pc.Entity();
  65.     this.outlineCamera.addComponent("camera", {
  66.         clearColor: new pc.Color(0.0, 0.0, 0.0, 0.0),
  67.         layers: [this.outlineLayer.id],
  68.     });    
  69.     //this.app.root.addChild(this.outlineCamera);    
  70.     this.camera.addChild(this.outlineCamera);
  71.    
  72.     // instanciate outline post process effect
  73.     this.outline = new pc.OutlineEffect(this.app.graphicsDevice, this.thickness, this.blur);
  74.     this.outline.color = new pc.Color(0, 0, 1, 1);
  75.     this.outline.texture = this.texture;
  76.     this.entity.camera.postEffects.addEffect(this.outline);
  77. };
  78.  
  79. ApplyOutline.prototype.onResize = function(){
  80.     this.entity.camera.postEffects.removeEffect(this.outline);
  81.     this.app.scene.layers.remove(this.outlineLayer);
  82.     this.texture.destroy();
  83.     this.texture = new pc.Texture(this.app.graphicsDevice, {
  84.         width: this.app.graphicsDevice.width,
  85.         height: this.app.graphicsDevice.height,
  86.         format: pc.PIXELFORMAT_R8_G8_B8_A8,
  87.         autoMipmap: true,
  88.         minFilter: pc.FILTER_LINEAR,
  89.         magFilter: pc.FILTER_LINEAR
  90.     });
  91.     this.renderTarget.destroy();
  92.     this.renderTarget = new pc.RenderTarget(this.app.graphicsDevice, this.texture, { depth: true });
  93.     this.outlineLayer.renderTarget = this.renderTarget;
  94.     this.app.scene.layers.insert(this.outlineLayer, 0);
  95.     this.outline.texture = this.texture;
  96.     this.entity.camera.postEffects.addEffect(this.outline);
  97.     this.outline.color.copy(this.color);
  98. };
  99.  
  100. // update code called every frame
  101. ApplyOutline.prototype.update = function(dt) {
  102.     this.outlineCamera.camera.horizontalFov = this.app.graphicsDevice.width > this.app.graphicsDevice.height ? false : true;
  103.     // update color
  104.     //this.outline.color.copy(this.color);
  105.     //this.outline.power = this.power;
  106.     //this.outline.blur0 = this.blur;
  107.    
  108.     this.outline.set_color(this.color);
  109.     this.outline.set_power(this.power);
  110.     this.outline.set_blur(this.blur);
  111.  
  112.     //this.app.fire('debug:settext', "color: " + this.color);
  113.     //this.app.fire('debug:addtext', "blur0: " + this.blur);
  114.     //this.app.fire('debug:addtext', "power: " + this.power);
  115.  
  116. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement