首页 > 其他分享 >Starling GodRay 效果实现

Starling GodRay 效果实现

时间:2023-01-05 18:33:30浏览次数:48  
标签:function frag 效果 Number GodRay Starling ._ var public


​Starling ‘God Ray’ Filter​



While cruising the internet today looking for interesting things to try out, I ran across this fun little ​​GPU Gem​​​ about creating a post-process volumetric lighting effect. With a little bit of work I quickly ported it over to an AGAL shader (you can see it ​​on Wonderfl here​​​). Then I figured what the hell, why not make it into a ​​Starling​​ filter.

I should say right off the bat that this won’t work in ‘baseline constrained’ mode (i.e. no mobile apps), but if you’re working on a web or desktop project, this filter will give you ​​an effect like this​​.

The entire filter is below. Enjoy.

/**
* Copyright (c) 2013 Devon O. Wolfgang
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

package starling.filters
{
import flash.display3D.Context3D;
import flash.display3D.Context3DBlendFactor;
import flash.display3D.Context3DProgramType;
import flash.display3D.Program3D;
import starling.textures.Texture;

/**
* Creates a 'God Rays' / fake volumetric light filter effect. Only use with Context3DProfile.BASELINE (not compatible with constrained profile)
* @author Devon O.
*/

public class GodRaysFilter extends FragmentFilter
{

private var shaderProgram:Program3D;

private var numSteps:int;

// lightx, lighty
private var lightPos:Vector.<Number> = Vector.<Number>( [ .5, .5, 1, 1 ]);

// numsamples, density, numsamples * density, 1 / numsamples * density
private var values1:Vector.<Number> = Vector.<Number>( [ 1, 1, 1, 1 ]);

// weight, decay, exposure
private var values2:Vector.<Number> = Vector.<Number>( [ 1, 1, 1, 1 ]);


private var _lightX:Number = 0;
private var _lightY:Number = 0;
private var _weight:Number = .50;
private var _decay:Number = .87;
private var _exposure:Number = .35;
private var _density:Number = 2.0;

public function GodRaysFilter(numSteps:int = 30)
{
this.numSteps = numSteps;
}

public override function dispose():void
{
if (this.shaderProgram) this.shaderProgram.dispose();
super.dispose();
}

protected override function createPrograms():void
{
var frag:String = "";

// Calculate vector from pixel to light source in screen space.
frag += "sub ft0.xy, v0.xy, fc0.xy \n";

// Divide by number of samples and scale by control factor.
frag += "mul ft0.xy, ft0.xy, fc1.ww \n";

// Store initial sample.
frag += "tex ft1, v0, fs0 <2d, clamp, linear, mipnone> \n";

// Set up illumination decay factor.
frag += "mov ft2.x, fc0.w \n";

// Store the texcoords
frag += "mov ft4.xy, v0.xy \n";

for (var i:int = 0; i < this.numSteps; i++)
{
// Step sample location along ray.
frag += "sub ft4.xy, ft4.xy, ft0.xy \n";

// Retrieve sample at new location.
frag += "tex ft3, ft4.xy, fs0 <2d, clamp, linear, mipnone> \n";

// Apply sample attenuation scale/decay factors.
frag += "mul ft2.y, ft2.x, fc2.x \n";
frag += "mul ft3.xyz, ft3.xyz, ft2.yyy \n";

// Accumulate combined color.
frag += "add ft1.xyz, ft1.xyz, ft3.xyz \n";

// Update exponential decay factor.
frag += "mul ft2.x, ft2.x, fc2.y \n";
}

// Output final color with a further scale control factor.
frag += "mul ft1.xyz, ft1.xyz, fc2.zzz \n";
frag += "mov oc, ft1";

this.shaderProgram = assembleAgal(frag);
}

protected override function activate(pass:int, context:Context3D, texture:Texture):void
{
// light position
this.lightPos[0] = this._lightX / texture.width;
this.lightPos[1] = this._lightY / texture.height;

// numsamples, density, numsamples * density, 1 / numsamples * density
this.values1[0] = this.numSteps;
this.values1[1] = this._density;
this.values1[2] = this.numSteps * values1[1];
this.values1[3] = 1 / values1[2];

// weight, decay, exposure
this.values2[0] = this._weight;
this.values2[1] = this._decay;
this.values2[2] = this._exposure;

context.setProgramConstantsFromVector(Context3DProgramType.FRAGMENT, 0, this.lightPos, 1 );
context.setProgramConstantsFromVector(Context3DProgramType.FRAGMENT, 1, this.values1, 1 );
context.setProgramConstantsFromVector(Context3DProgramType.FRAGMENT, 2, this.values2, 1 );

context.setProgram(this.shaderProgram);

}

public function set lightX(value:Number):void { this._lightX = value; }
public function get lightX():Number { return this._lightX; }

public function set lightY(value:Number):void { this._lightY = value; }
public function get lightY():Number { return this._lightY; }

public function set decay(value:Number):void { this._decay = value; }
public function get decay():Number { return this._decay; }

public function set exposure(value:Number):void { this._exposure = value; }
public function get exposure():Number { return this._exposure; }

public function set weight(value:Number):void { this._weight = value; }
public function get weight():Number { return this._weight; }

public function set density(value:Number):void { this._density = value; }
public function get density():Number { return this._density; }

 

标签:function,frag,效果,Number,GodRay,Starling,._,var,public
From: https://blog.51cto.com/kenkao/5991723

相关文章

  • Starling常见问题解决办法
    ​​Starling常见问题解决办法​​来自:​​智慧+毅力=无所不能​​ 1、Android设备上阻止用户按下后退后的行为侦听按键事件//阻止后退行为view.stage.addEventL......
  • Starling浅尝
      starling笔记:基于Stage3Dg开发出来的一个可以使用GPU加速2D应用程序的框架。是一个渲染框架!特色:直观,轻量,免费。Starling与Sparrow框架很相近。驱动关系:GPU-->OpenGL/E......
  • 为什么谷歌外链没效果?谷歌拒绝外链权重能恢复吗
    站内内容,GPB外链,页面打开速度,目前被认为是Google排名优化的三大利器,实际上,谷歌的算法截至目前,还是外链算法,付费外链的地位越来越重要,特别是软文,投稿和GPB外链。推荐阅读《​......
  • 噩梦系列篇之Player受伤功能及伤害效果
    下面设定player的受伤数值变化及伤害效果显示;添加一个Health脚本。。。下面看脚本的内容;让我们再次Coding起来;usingUnityEngine;usingSystem.Collections;publicclasshe......
  • 6.鼠标滑动变大变小效果
    有时候我们在前端需要一种效果就是鼠标滑入然后商品变大或者变小的效果。显示为选中的效果在电商里面使用很频繁代码如下<!DOCTYPEhtml><htmllang="en"><head> <meta......
  • JavaScript 自执行函数防止冲突全局作用域变量 - 在线客服源码实现弹窗效果JavaScript
    当我在实现在线客服源码弹窗效果JavaScriptSDK时,对外公开的SDK代码就是使用的自执行函数的形式。使用自执行函数来实现JavaScriptSDK有以下好处:封装代码:自执行函数......
  • springboot整合mybatis以及bootstrap实现分页效果
      搜索后分页  pom依赖<?xmlversion="1.0"encoding="UTF-8"?><projectxmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.or......
  • Vue2 element-ui组件二封-表单组件-效果展示
    vue2已经落后了?不着急,vue3的也在写的过程中,只是发出来vue2的一些组件系列说明:>编写原因vue2在很多人眼里已经快过时了,而我一直想写一些总结,但是从两年前......
  • 在线客服系统实现消息声音提醒效果 - 在线客服系统源码
    在在线客服系统中实现消息声音提醒效果可以带来许多好处,包括:改善用户体验:通知声音可以帮助提醒用户有新消息,鼓励他们及时回复,提高整体用户体验。提高生产率:通过提醒......
  • 【Web】CDN加速效果浅析
     1.什么是CDN?CDN的全称是ContentDeliveryNetwork,即内容分发网络。其目的是通过在现有的Internet中增加一层新的CACHE(缓存)层,将网站的内容发布到最接近用户的网络"边缘"......