Skip to content

Commit

Permalink
Merge pull request #135 from tier4/feat/smoke_simulator
Browse files Browse the repository at this point in the history
feat: smoke simulator
  • Loading branch information
mackierx111 authored Jun 21, 2023
2 parents ca2ab20 + 70515aa commit e896866
Show file tree
Hide file tree
Showing 16 changed files with 434 additions and 0 deletions.
98 changes: 98 additions & 0 deletions Assets/AWSIM/Prefabs/Environments/SmokeGenerator.prefab
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!1 &7448619282370533179
GameObject:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 7448619282370533180}
- component: {fileID: 7448619282370533181}
- component: {fileID: 7448619282370533178}
m_Layer: 0
m_Name: SmokeGenerator
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!4 &7448619282370533180
Transform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 7448619282370533179}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 2.1713438, y: 0.1, z: 0.90007114}
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: []
m_Father: {fileID: 0}
m_RootOrder: 0
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!23 &7448619282370533181
MeshRenderer:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 7448619282370533179}
m_Enabled: 1
m_CastShadows: 1
m_ReceiveShadows: 1
m_DynamicOccludee: 1
m_StaticShadowCaster: 0
m_MotionVectors: 1
m_LightProbeUsage: 1
m_ReflectionProbeUsage: 1
m_RayTracingMode: 2
m_RayTraceProcedural: 0
m_RenderingLayerMask: 257
m_RendererPriority: 0
m_Materials:
- {fileID: 2100000, guid: 73c176f402d2c2f4d929aa5da7585d17, type: 2}
m_StaticBatchInfo:
firstSubMesh: 0
subMeshCount: 0
m_StaticBatchRoot: {fileID: 0}
m_ProbeAnchor: {fileID: 0}
m_LightProbeVolumeOverride: {fileID: 0}
m_ScaleInLightmap: 1
m_ReceiveGI: 1
m_PreserveUVs: 0
m_IgnoreNormalsForChartDetection: 0
m_ImportantGI: 0
m_StitchLightmapSeams: 1
m_SelectedEditorRenderState: 3
m_MinimumChartSize: 4
m_AutoUVMaxDistance: 0.5
m_AutoUVMaxAngle: 89
m_LightmapParameters: {fileID: 0}
m_SortingLayerID: 0
m_SortingLayer: 0
m_SortingOrder: 0
m_AdditionalVertexStreams: {fileID: 0}
--- !u!114 &7448619282370533178
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 7448619282370533179}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 7f84f6b79d815f997a454929c6cac05e, type: 3}
m_Name:
m_EditorClassIdentifier:
maxParticle: 250
particleRangeRadius: 0.5
particleSize: 0.07
averageLifetime: 7.5
variationLifetime: 2.5
physics:
initialPlaneVelocity: 0.4
initialVerticalVelocity: -0.1
planeAcceleration: -0.04
verticalAcceleration: 0.04
7 changes: 7 additions & 0 deletions Assets/AWSIM/Prefabs/Environments/SmokeGenerator.prefab.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions Assets/AWSIM/Scripts/Environments/SmokeGenerator.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

84 changes: 84 additions & 0 deletions Assets/AWSIM/Scripts/Environments/SmokeGenerator/SmokeGenerator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;


[RequireComponent(typeof (MeshRenderer))]

/// <summary>
/// Smoke Generator class.
/// </summary>
[System.Serializable]
public class SmokeGenerator : MonoBehaviour
{
[Tooltip("Specifies the maximum number of smoke particles in a scene.")]
[SerializeField]
[Range(1, 1000)]
private int maxParticle = 250;
[Tooltip("Specifies the radius of a circular region where particles are randomly generated in [m].")]
[SerializeField]
[Range(0.01f, 3.0f)]
private float particleRangeRadius = 2.0f;
[Tooltip("Specifies the size of a smoke particle in [m].")]
[SerializeField]
[Range(0.005f, 0.1f)]
private float particleSize = 0.07f;
[Tooltip("Specifies the average lifetime of a particle in [s].")]
[SerializeField]
[Range(5.0f, 15.0f)]
private float averageLifetime = 7.5f;
[Tooltip("Specifies the variation range of lifetime of a particle in [s].")]
[SerializeField]
[Range(0.0f, 5.0f)]
private float variationLifetime = 2.5f;

[SerializeField]
private SmokeParticlePhysics physics;

// Start is called before the first frame update
public void Start()
{
for (int i = 0; i < maxParticle; i++)
this.CreateSmokeParticle();

print(GetComponent<MeshRenderer>().material);
}

// Update is called once per frame
public void Update()
{
if (this.transform.childCount < maxParticle)
this.CreateSmokeParticle();
}

private void CreateSmokeParticle()
{
float angleRad = Random.Range(0.0f, (float)System.Math.PI*2.0f);
float radius = Random.Range(0.0f, particleRangeRadius);
SmokeParticle.Create(gameObject, particleSize, radius, angleRad);
}

/// <summary>
/// Returns particle size.
/// </summary>
public float GetParticleSize()
{
return this.particleSize;
}

/// <summary>
/// Returns initial velocity and acceleration of particle.
/// </summary>
public float[] GetVelAcc()
{
return new float[4] {this.physics.initialPlaneVelocity, this.physics.initialVerticalVelocity, this.physics.planeAcceleration, this.physics.verticalAcceleration};
}

/// <summary>
/// Returns lifetime of particle.
/// </summary>
public double GetLifetime()
{
return (double)(averageLifetime + Random.Range(-variationLifetime, variationLifetime));
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

130 changes: 130 additions & 0 deletions Assets/AWSIM/Scripts/Environments/SmokeGenerator/SmokeParticle.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
using UnityEngine;

[RequireComponent(typeof (MeshFilter))]
[RequireComponent(typeof (MeshRenderer))]

/// <summary>
/// Smoke Particle class.
/// </summary>
public class SmokeParticle : MonoBehaviour
{
private SmokeGenerator parentComp;
private double lifeTime;
private Vector3 velocity = new Vector3(0.0f, -0.05f, 0.0f);
private Vector3 acceleration = new Vector3(0.0f, 0.05f, 0.0f);

void Start()
{
this.CreateCube();
this.lifeTime = parentComp.GetLifetime();

Material mat = this.parentComp.GetComponent<MeshRenderer>().material;
MeshRenderer rend = GetComponent<MeshRenderer>();
if (rend != null)
rend.material = mat;
}

void Update()
{
this.velocity += this.acceleration * Time.deltaTime;
Vector3 displacement = this.velocity * Time.deltaTime;
this.transform.position += displacement;

this.lifeTime -= Time.deltaTime;
if (lifeTime <= 0.0)
Destroy(gameObject);
}

private void CreateCube ()
{
float size = this.parentComp.GetParticleSize();

Vector3[] vertices = {
new Vector3 (0, 0, 0),
new Vector3 (size, 0, 0),
new Vector3 (size, size, 0),
new Vector3 (0, size, 0),
new Vector3 (0, size, size),
new Vector3 (size, size, size),
new Vector3 (size, 0, size),
new Vector3 (0, 0, size),
};

int[] triangles = {
0, 2, 1,
0, 3, 2,
2, 3, 4,
2, 4, 5,
1, 2, 5,
1, 5, 6,
0, 7, 4,
0, 4, 3,
5, 4, 7,
5, 7, 6,
0, 6, 7,
0, 1, 6
};

Mesh mesh = GetComponent<MeshFilter>().mesh;
mesh.Clear ();
mesh.vertices = vertices;
mesh.triangles = triangles;
mesh.Optimize ();
mesh.RecalculateNormals ();
}

/// <summary>
/// Gets and sets the SmokeGenerator component of the parent GameObject.
/// </summary>
/// <param name="gameObject">Parent GameObject of the Smoke Particle.</param>
public void SetParentComp(GameObject gameObject)
{
this.parentComp = gameObject.GetComponentInParent<SmokeGenerator>();
}

/// <summary>
/// Sets the initial velocity and acceleration of Smoke Particle.
/// </summary>
/// <param name="angleRad">Angular location of Smoke Particle in radians.</param>
public void SetVelAcc(float angleRad)
{
float[] velAcc = this.parentComp.GetVelAcc();
float velPlane = velAcc[0];
float velY = velAcc[1];
float accPlane = velAcc[2];
float accY = velAcc[3];

float velX = velPlane * (float)System.Math.Cos(angleRad);
float velZ = velPlane * (float)System.Math.Sin(angleRad);
this.velocity = new Vector3(velX, velY, velZ);

float accX = accPlane * (float)System.Math.Cos(angleRad);
float accZ = accPlane * (float)System.Math.Sin(angleRad);
this.acceleration = new Vector3(accX, accY, accZ);
}

/// <summary>
/// Creates a new Smoke Particle GameObject of specified properties.
/// </summary>
/// <param name="gameObject">Parent GameObject of the Smoke Particle.</param>
/// <param name="particle_size">Edge length of the Smoke Particle in [m].</param>
/// <param name="radius">Radius of a circle which defines the region where the Smoke Particle is generated in.</param>
/// <param name="angleRad">Angular location of the Smoke Particle to be generated at.</param>
public static void Create(GameObject gameObject, float particle_size, float radius, float angleRad)
{
GameObject particle = new GameObject("Particle");
particle.transform.parent = gameObject.transform;

float x = radius * (float)System.Math.Cos(angleRad);
float z = radius * (float)System.Math.Sin(angleRad);
float y = Random.Range(0.0f, 1.5f);
particle.transform.position = gameObject.transform.position + new Vector3(x, y, z);

particle.AddComponent(typeof(MeshFilter));
particle.AddComponent(typeof(MeshRenderer));

particle.AddComponent<SmokeParticle>();
particle.GetComponent<SmokeParticle>().SetParentComp(gameObject);
particle.GetComponent<SmokeParticle>().SetVelAcc(angleRad);
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System;
using UnityEngine;

/// <summary>
/// Struct for storing Smoke Particle class-related parameters.
/// </summary>
[Serializable]
public struct SmokeParticlePhysics
{
[Tooltip("Specifies the initial velocity of a smoke particle in the x-z plane in [m/s].")]
[Range(0.0f, 0.5f)]
public float initialPlaneVelocity;
[Tooltip("Specifies the vertical velocity of a smoke particle in [m/s].")]
[Range(-0.5f, 3.0f)]
public float initialVerticalVelocity;

[Tooltip("Specifies the acceleration of a smoke particle in the x-z plane in [m/s^2].")]
[Range(-0.1f, 0.1f)]
public float planeAcceleration;
[Tooltip("Specifies the vertical acceleration of a smoke particle in [m/s^2].")]
[Range(-0.1f, 0.1f)]
public float verticalAcceleration;
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit e896866

Please sign in to comment.