OSG粒子系统特效-----雨雪、爆炸、烟雾

这篇具有很好参考价值的文章主要介绍了OSG粒子系统特效-----雨雪、爆炸、烟雾。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

1、烟雾效果

OSG粒子系统特效-----雨雪、爆炸、烟雾,OSG入门及实战,OSG粒子特效,osg 雨雪、爆炸、烟雾飞机坠毁
OSG粒子系统特效-----雨雪、爆炸、烟雾,OSG入门及实战,OSG粒子特效,osg 雨雪、爆炸、烟雾
陨石坠落

源码:

// CMyOSGParticle.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

#include <iostream>
#include <windows.h>
#include <osgViewer/Viewer>

#include <osg/Node>
#include <osg/Geode>
#include <osg/Geometry>
#include <osg/Group>
#include <osg/Billboard>
#include <osg/Texture2D>
#include <osg/Image>
#include <osg/Vec3>
#include <osg/Vec2>
#include <osg/PositionAttitudeTransform>
#include <osg/MatrixTransform>
#include <osgDB/ReadFile>
#include <osgDB/WriteFile>
#include <osgUtil/Optimizer>

#include <osgViewer/ViewerEventHandlers> //事件监听
#include <osgGA/StateSetManipulator> //事件响应类,对渲染状态进行控制
#include <osgUtil/Simplifier> //简化几何体

#include <osgParticle/PrecipitationEffect>
#include <osgParticle/Particle>

#include <osgParticle/LinearInterpolator>
#include <osgParticle/ParticleSystem>
#include < osgParticle/RandomRateCounter>
#include <osgParticle/PointPlacer>
#include <osgParticle/RadialShooter>
#include <osgParticle/ModularEmitter>
#include <osgParticle/ParticleSystemUpdater>
#include < osgParticle/ModularProgram>


#pragma comment(lib, "OpenThreadsd.lib")
#pragma comment(lib, "osgd.lib")
#pragma comment(lib, "osgDBd.lib")
#pragma comment(lib, "osgUtild.lib")
#pragma comment(lib, "osgGAd.lib")
#pragma comment(lib, "osgViewerd.lib")
#pragma comment(lib, "osgTextd.lib")
#pragma comment(lib, "osgParticled.lib")

//创建火球(燃烧)
void createFireBall(osg::MatrixTransform* smokeNode)
{
	// 创建粒子对象,设置其属性并交由粒子系统使用。
	osgParticle::Particle particleTempalte;
	particleTempalte.setShape(osgParticle::Particle::QUAD);
	particleTempalte.setLifeTime(1.5); // 单位:秒
	particleTempalte.setSizeRange(osgParticle::rangef(3.0f, 1.0f)); // 单位:米
	particleTempalte.setAlphaRange(osgParticle::rangef(1, 0));
	particleTempalte.setColorRange(osgParticle::rangev4(osg::Vec4(1.0f, 0.2f, 0.0f, 1.0f),//0.1f,0.3f,0.4f,1.0f
		osg::Vec4(0.1f, 0.1f, 0.1f, 0)//0.95f,0.75f,0,1(1,1,1,1)
	));
	particleTempalte.setPosition(osg::Vec3(0.0f, 0.0f, 0.0f));
	particleTempalte.setVelocity(osg::Vec3(0.0f, 0.0f, 0.0f));
	particleTempalte.setMass(0.1f); //单位:千克
	particleTempalte.setRadius(0.2f);
	particleTempalte.setSizeInterpolator(new osgParticle::LinearInterpolator);
	particleTempalte.setAlphaInterpolator(new osgParticle::LinearInterpolator);
	particleTempalte.setColorInterpolator(new osgParticle::LinearInterpolator);
	// 创建并初始化粒子系统。
	osgParticle::ParticleSystem* particleSystem = new osgParticle::ParticleSystem;
	particleSystem->setDataVariance(osg::Node::STATIC);
	// 设置材质,是否放射粒子,以及是否使用光照。
	particleSystem->setDefaultAttributes("D:\\data\\Images\\smoke.rgb", true, false);
	osg::Geode* geode = new osg::Geode;
	geode->addDrawable(particleSystem);
	smokeNode->addChild(geode);
	//设置为粒子系统的缺省粒子对象。
	particleSystem->setDefaultParticleTemplate(particleTempalte);
	//获取放射极中缺省计数器的句柄,调整每帧增加的新粒子数目
	osgParticle::RandomRateCounter* particleGenerateRate = new osgParticle::RandomRateCounter();
	particleGenerateRate->setRateRange(30, 50);
	// 每秒新生成的粒子范围
	particleGenerateRate->setDataVariance(osg::Node::DYNAMIC);
	// 自定义一个放置器,这里创建并初始化一个点放置器
	osgParticle::PointPlacer* particlePlacer = new osgParticle::PointPlacer;
	particlePlacer->setCenter(osg::Vec3(0.0f, 0.0f, 0.0f));
	particlePlacer->setDataVariance(osg::Node::DYNAMIC);
	// 自定义一个弧度发射器
	osgParticle::RadialShooter* particleShooter = new osgParticle::RadialShooter;
	// 设置发射器的属性
	particleShooter->setDataVariance(osg::Node::DYNAMIC);
	particleShooter->setThetaRange(-0.1f, 0.1f);
	// 弧度值,与Z 轴夹角
	particleShooter->setPhiRange(-0.1f, 0.1f);
	particleShooter->setInitialSpeedRange(5, 7.5f);//单位:米/秒
												   //创建标准放射极对象
	osgParticle::ModularEmitter* emitter = new osgParticle::ModularEmitter;
	emitter->setDataVariance(osg::Node::DYNAMIC);
	emitter->setCullingActive(false);
	// 将放射极对象与粒子系统关联。
	emitter->setParticleSystem(particleSystem);
	// 设置计数器
	emitter->setCounter(particleGenerateRate);
	// 设置放置器
	emitter->setPlacer(particlePlacer);
	// 设置发射器
	emitter->setShooter(particleShooter);
	// 把放射极添加为变换节点
	smokeNode->addChild(emitter);
	// 添加更新器,以实现每帧的粒子管理。
	osgParticle::ParticleSystemUpdater* particleSystemUpdater = new osgParticle::ParticleSystemUpdater;
	// 将更新器与粒子系统对象关联。
	particleSystemUpdater->addParticleSystem(particleSystem);
	// 将更新器节点添加到场景中。
	smokeNode->addChild(particleSystemUpdater);
	// 创建标准编程器对象并与粒子系统相关联。
	osgParticle::ModularProgram* particleMoveProgram = new osgParticle::ModularProgram;
	particleMoveProgram->setParticleSystem(particleSystem);
	// 最后,将编程器添加到场景中。
	smokeNode->addChild(particleMoveProgram);
}

//创建浓烟
void createDarkSmoke(osg::MatrixTransform* smokeNode)
{
	// 创建粒子对象,设置其属性并交由粒子系统使用。
	osgParticle::Particle particleTempalte;
	particleTempalte.setShape(osgParticle::Particle::QUAD);
	particleTempalte.setLifeTime(10); // 单位:秒
	particleTempalte.setSizeRange(osgParticle::rangef(1.0f, 12.0f)); // 单位:米
	particleTempalte.setAlphaRange(osgParticle::rangef(1, 0));
	particleTempalte.setColorRange(osgParticle::rangev4(
		osg::Vec4(0.0f, 0.0f, 0.0f, 0.5f),//(0.1f,0.1f,0.1f,0.5f)
		osg::Vec4(0.5f, 0.5f, 0.5f, 1.5f)//0.95f,0.75f,0,1
	));
	particleTempalte.setPosition(osg::Vec3(0.0f, 0.0f, 0.0f));
	particleTempalte.setVelocity(osg::Vec3(0.0f, 0.0f, 0.0f));
	particleTempalte.setMass(0.1f); //单位:千克
	particleTempalte.setRadius(0.2f);
	particleTempalte.setSizeInterpolator(new osgParticle::LinearInterpolator);
	particleTempalte.setAlphaInterpolator(new osgParticle::LinearInterpolator);
	particleTempalte.setColorInterpolator(new osgParticle::LinearInterpolator);
	// 创建并初始化粒子系统。
	osgParticle::ParticleSystem* particleSystem = new osgParticle::ParticleSystem;
	particleSystem->setDataVariance(osg::Node::STATIC);
	// 设置材质,是否放射粒子,以及是否使用光照。
	particleSystem->setDefaultAttributes("D:\\data\\Images\\smoke.rgb", false, false);
	osg::Geode* geode = new osg::Geode;
	geode->addDrawable(particleSystem);
	smokeNode->addChild(geode);
	//设置为粒子系统的缺省粒子对象。
	particleSystem->setDefaultParticleTemplate(particleTempalte);
	//获取放射极中缺省计数器的句柄,调整每帧增加的新粒
	//子数目
	osgParticle::RandomRateCounter* particleGenerateRate = new osgParticle::RandomRateCounter();
	particleGenerateRate->setRateRange(30, 50);
	// 每秒新生成的粒子范围
	particleGenerateRate->setDataVariance(osg::Node::DYNAMIC);
	// 自定义一个放置器,这里我们创建并初始化一个点放置器
	osgParticle::PointPlacer* particlePlacer = new osgParticle::PointPlacer;
	particlePlacer->setCenter(osg::Vec3(0.0f, 0.0f, 0.05f));
	particlePlacer->setDataVariance(osg::Node::DYNAMIC);
	// 自定义一个弧度发射器
	osgParticle::RadialShooter* particleShooter = new osgParticle::RadialShooter;
	// 设置发射器的属性
	particleShooter->setDataVariance(osg::Node::DYNAMIC);
	particleShooter->setThetaRange(-0.1f, 0.1f);
	// 弧度值,与Z 轴夹角0.392699f
	particleShooter->setPhiRange(-0.1f, 0.1f);
	particleShooter->setInitialSpeedRange(10, 15);
	//单位:米/秒
	//创建标准放射极对象
	osgParticle::ModularEmitter* emitter = new osgParticle::
		ModularEmitter;
	emitter->setDataVariance(osg::Node::DYNAMIC);
	emitter->setCullingActive(false);
	// 将放射极对象与粒子系统关联。
	emitter->setParticleSystem(particleSystem);
	// 设置计数器
	emitter->setCounter(particleGenerateRate);
	// 设置放置器
	emitter->setPlacer(particlePlacer);
	// 设置发射器
	emitter->setShooter(particleShooter);
	// 把放射极添加为变换节点
	smokeNode->addChild(emitter);
	// 添加更新器,以实现每帧的粒子管理。
	osgParticle::ParticleSystemUpdater* particleSystemUpdater = new osgParticle::ParticleSystemUpdater;
	// 将更新器与粒子系统对象关联。
	particleSystemUpdater->addParticleSystem(particleSystem);
	// 将更新器节点添加到场景中。
	smokeNode->addChild(particleSystemUpdater);
	osgParticle::ModularProgram* particleMoveProgram = new osgParticle::ModularProgram;
	particleMoveProgram->setParticleSystem(particleSystem);
	// 最后,将编程器添加到场景中。
	smokeNode->addChild(particleMoveProgram);
}

void WriteFireballAndSmoke()
{
	osg::ref_ptr<osg::Group> root = new osg::Group();
	osg::MatrixTransform* flightTransform = new osg::MatrixTransform();
	root->addChild(flightTransform);
	//引擎烟雾
	osg::MatrixTransform* fireballAndSmoke = new osg::MatrixTransform();
	createFireBall(fireballAndSmoke);

	fireballAndSmoke->setMatrix(osg::Matrix::rotate(-osg::PI / 2, 1, 0, 0) * osg::Matrix::translate(-8, -10, -3));
	flightTransform->addChild(fireballAndSmoke);
	createDarkSmoke(fireballAndSmoke);

	osgDB::writeNodeFile(*(root.get()), "D:\\data\\Images\\MyScene.osg");
}

void ReadFireballAndSmoke()
{
	osg::ref_ptr<osg::Group> root = new osg::Group();
	osg::Node* flightNode = osgDB::readNodeFile("D:\\data\\Images\\MyScene.osg");
	if (!flightNode)
	{
		return;
	}
	osg::MatrixTransform* flightTransform = new osg::MatrixTransform();
	flightTransform->addChild(flightNode);
	root->addChild(flightTransform);
	osgViewer::Viewer viewer;

	osgUtil::Simplifier simplifier(0.3f, 4.0f);
	osgUtil::Optimizer optimzer;

	optimzer.optimize(root.get());
	viewer.setSceneData(root.get());

	//添加一个事件句柄 相当于添加一个响应 响应鼠标或是键盘 响应L键(控制灯光开关)
	viewer.addEventHandler(new osgGA::StateSetManipulator(viewer.getCamera()->getOrCreateStateSet()));

	//窗口大小变化事件 添加窗口大小改变的句柄 这里响应的是F键
	viewer.addEventHandler(new osgViewer::WindowSizeHandler);

	//添加一些常用状态设置  添加常用的状态操作,这里会响应S键、W键等等 
	viewer.addEventHandler(new osgViewer::StatsHandler);
	viewer.realize();
	viewer.run();
}

//陨石坠落
//int main()
//{
//	WriteFireballAndSmoke();
//	ReadFireballAndSmoke();
//
//	return 0;
//}

int main()
{
	osg::ref_ptr<osg::Group> root = new osg::Group();
	osg::Node* flightNode = osgDB::readNodeFile("D:\\data\\cessna.osgt");//需要经模型放在项目根目录下(而不是在cpp所在目录)
	if (!flightNode)
	{
		return -1;
	}
	//飞机变换节点
	osg::MatrixTransform* flightTransform = new osg::MatrixTransform();
	flightTransform->addChild(flightNode);
	root->addChild(flightTransform);
	//引擎烟雾
	osg::MatrixTransform* fireballAndSmoke = new osg::MatrixTransform();
	createFireBall(fireballAndSmoke);

	fireballAndSmoke->setMatrix(osg::Matrix::rotate(-osg::PI / 2, 1, 0, 0) * osg::Matrix::translate(-8, -10, -3));
	flightTransform->addChild(fireballAndSmoke);
	createDarkSmoke(fireballAndSmoke);
	osgViewer::Viewer viewer;

	osgUtil::Simplifier simplifier(0.3f, 4.0f);
	osgUtil::Optimizer optimzer;

	optimzer.optimize(root.get());
	viewer.setSceneData(root.get());

	//添加一个事件句柄 相当于添加一个响应 响应鼠标或是键盘 响应L键(控制灯光开关)
	viewer.addEventHandler(new osgGA::StateSetManipulator(viewer.getCamera()->getOrCreateStateSet()));

	//窗口大小变化事件 添加窗口大小改变的句柄 这里响应的是F键
	viewer.addEventHandler(new osgViewer::WindowSizeHandler);

	//添加一些常用状态设置  添加常用的状态操作,这里会响应S键、W键等等 
	viewer.addEventHandler(new osgViewer::StatsHandler);

	viewer.realize();
	viewer.run();

	return 0;
}

2、爆炸效果

#include <osgParticle/ExplosionDebrisEffect>
#include <osgParticle/ExplosionEffect>
#include <osgParticle/SmokeEffect>
#include <osgParticle/FireEffect>
#include <osgUtil/Optimizer>

//创建爆炸效果
osg::ref_ptr<osg::Node> createExplode()
{
	osg::ref_ptr<osg::Group> explode = new osg::Group();
	//风向
	osg::Vec3 wind(1.0f, 0.0f, 0.0f);
	osg::Vec3 position(0.0f, 0.0f, -1.0f);
	//爆炸模拟,10.0f为缩放比,默认为1.0f,不缩放
	osg::ref_ptr<osgParticle::ExplosionEffect> explosion = new osgParticle::ExplosionEffect(position, 10.0f);
	osg::ref_ptr<osgParticle::ExplosionDebrisEffect> explosionDebri =
		new osgParticle::ExplosionDebrisEffect(position, 10.0f);
	//烟模拟
	osg::ref_ptr<osgParticle::SmokeEffect> smoke = new osgParticle::SmokeEffect(position, 10.0f);
	//火焰模拟
	osg::ref_ptr<osgParticle::FireEffect> fire = new osgParticle::FireEffect(position, 10.0f);
	//设置风向
	explosion->setWind(wind);
	explosionDebri->setWind(wind);
	smoke->setWind(wind);
	fire->setWind(wind);

	//添加子节点
	explode->addChild(explosion.get());
	explode->addChild(explosionDebri.get());
	explode->addChild(smoke.get());
	explode->addChild(fire.get());
	return explode.get();
}

int main()
{
	osg::ref_ptr<osgViewer::Viewer> viewer = new osgViewer::Viewer();
	osg::ref_ptr<osg::Group> root = new osg::Group();
	//添加爆炸效果
	root->addChild(createExplode());
	//优化场景数据
	osgUtil::Optimizer optimizer;
	optimizer.optimize(root.get());
	//设置场景数据
	viewer->setSceneData(root.get());
	//初始化并创建窗口
	viewer->realize();
	//viewer->setUpViewInWindow(200, 200, 800, 800);
	viewer->run();
}

3、雨雪效果

OSG粒子系统特效-----雨雪、爆炸、烟雾,OSG入门及实战,OSG粒子特效,osg 雨雪、爆炸、烟雾
源码:文章来源地址https://www.toymoban.com/news/detail-723800.html

#include <osgParticle/PrecipitationEffect>
void main() 
{
	osgViewer::Viewer viewer;

	//设置雪花类
	osg::ref_ptr<osgParticle::PrecipitationEffect> precipitationEffect = new osgParticle::PrecipitationEffect;

	//设置雪花浓度
	precipitationEffect->snow(0.5);

	//设置雪花颜色 
	precipitationEffect->setParticleColor(osg::Vec4(1, 1, 1, 1));

	//设置风向
	precipitationEffect->setWind(osg::Vec3(2, 0, 0));

	osg::ref_ptr <osg::Group> pRoot = new osg::Group();

	//把雪花加入到场景结点
	pRoot->addChild(precipitationEffect.get());
	osg::ref_ptr<osg::Node> glider = osgDB::readNodeFile("D:\\data\\cessna.osgt");
	pRoot->addChild(glider.get());
	viewer.setSceneData(pRoot);

	//添加一个事件句柄 相当于添加一个响应 响应鼠标或是键盘 响应L键(控制灯光开关)
	viewer.addEventHandler(new osgGA::StateSetManipulator(viewer.getCamera()->getOrCreateStateSet()));

	//窗口大小变化事件 添加窗口大小改变的句柄 这里响应的是F键
	viewer.addEventHandler(new osgViewer::WindowSizeHandler);

	//添加一些常用状态设置  添加常用的状态操作,这里会响应S键、W键等等 
	viewer.addEventHandler(new osgViewer::StatsHandler);

	viewer.realize();
	viewer.run();
}

到了这里,关于OSG粒子系统特效-----雨雪、爆炸、烟雾的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • Unity火焰特效与爆炸特效的制作

          通过对Unity3D游戏制作一段时间的学习,我认识到在游戏中粒子效果是一个比较重要的环节,可以帮助增强游戏的画面效果和音效效果。同时,我也对制作粒子效果缺乏经验,这次制作火焰粒子效果与爆炸粒子效果也是我在粒子效果制作的一次尝试。 flame 效果: 属性展

    2024年01月25日
    浏览(37)
  • html 3D 倒计时爆炸特效

    下面是代码: style.css代码为: 有两个js代码  dat.gui.min.js为: index.js为: 下面是运行效果(这是3D动态的):

    2024年01月24日
    浏览(33)
  • OSG三维渲染引擎编程学习之八十五:“第八章:OSG文字” 之 “8.4 文字特效实例”

    目录 第八章 OSG文字 8.4 文字特效实例       适当的文字信息对于显示场景信息是非常重要的。在OSG中,osgText提供了向场景中添加文字的强大功能,由于有第三方插件FreeType的支撑,可完全支持TrueType字体。       TrueType是由AppleComputer公司和Microsoft公司联合提出的一种新型数

    2024年02月11日
    浏览(33)
  • 【Unity】超简单特效 - 烟雾

    前言: 各式各样的制造工坊常常会出现在任意类型的游戏中,铁匠铺、车间、工业建筑等等,那么如何快速且简单的实现一款可复用的烟雾特效呢,先在脑海中想象一下我们生活里常见的烟雾吧。 初步实现: 在经过简单的想象以后,我们可以总结烟雾有以下一些普遍规律:

    2024年02月05日
    浏览(33)
  • 第五节 利用Ogre 2.3实现雨,雪,爆炸,飞机喷气尾焰等粒子效果

    本节主要学习如何使用 Ogre2.3 加载粒子效果。为了学习方便,直接将官方粒子模块 Sample_ParticleFX 单独拿出来编译,学习如何实现粒子效果。 如果参考官方示例建议用最新版的 Ogre 2.3.1 。否则找不到有粒子效果的示例。不要用官网 Ogre2.3 script 脚本编译源码! Ogre 1.x 中关于实现

    2024年02月09日
    浏览(28)
  • html 粒子效果文字特效

    有两个代码如下: index.html index2.html 下面是运行效果视频: 20240124_21:16:06_1 代码可以直接复制 如果有啥问题可以问我看到一定会回复大家,如果大家喜欢可以作者点赞和关注 大家的支持是我创作下去的最大动力!

    2024年01月25日
    浏览(33)
  • 27.CSS粒子特效

    2024年02月10日
    浏览(26)
  • 14. unity粒子特效--速度、粒子环绕、力(重力、阻力、风力等)、噪音

    1. 渐变速度(Velocity over Time) 可以分别指定x,y,z三个方向的速度,最终的速度是三个方向的速度合成,点击右侧的下拉三角,也可以使用曲线进行速度的控制调节,如下图所示: 2. 粒子环绕 粒子的环绕就是让粒子绕着某一个轴进行转动,实现这个效果可以使用一个圆形发射

    2024年02月04日
    浏览(35)
  • html Canvas粒子文字特效

    代码有点长,下面是代码: 有两个css代码  style.css代码: normalize.css  js代码: 运行效果: 20240120_090246 代码可以直接复制 如果有啥问题可以问我看到一定会回复大家,如果大家喜欢可以作者点赞和关注 大家的支持是我创作下去的最大动力!

    2024年01月24日
    浏览(29)
  • Unity 粒子特效遮罩(ParticleMask)

    1.需求: 游戏中粒子特效能实现非常好的效果,但是由于粒子特效是独立的系统,Unity自带的Mask普通的遮罩,遮不住粒子特效。 2.实现原理: 通过shader把超出范围的粒子纹理(Texture),改成透明颜色,以实现遮住粒子特效的功能。 3.关键Shader代码 4.效果演示: 5.接口易用性:

    2024年02月06日
    浏览(34)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包