浏览代码

cloud mesh generator moved to utility, and asset

/feature-new-clouds
André McGrail 4 年前
当前提交
d00579aa
共有 5 个文件被更改,包括 1139 次插入45 次删除
  1. 72
      Assets/Scripts/Environment/CloudRenderer.cs
  2. 1001
      Assets/Objects/environment/Clouds/CloudMeshes.asset
  3. 8
      Assets/Objects/environment/Clouds/CloudMeshes.asset.meta
  4. 92
      Assets/Scripts/Utility/SpriteMeshGenerator.cs
  5. 11
      Assets/Scripts/Utility/SpriteMeshGenerator.cs.meta

72
Assets/Scripts/Environment/CloudRenderer.cs


using Unity.Collections;
using UnityEngine;
using UnityEngine.Rendering;
using Unity.Collections;
using Object = UnityEngine.Object;
[ExecuteAlways]
public Sprite[] sprites;
private Mesh[] cloudMeshes;
public Mesh[] cloudMeshes;
public Material cloudMaterial;
public ParticleSystem ps;

private void Start()
{
cloudMeshes = new Mesh[sprites.Length];
int i = 0;
foreach (var sprite in sprites)
{
// verts
var verts = new List<Vector3>();
sprite.vertices.ToList().ForEach(x => verts.Add(new Vector3(x.x, x.y, 0.0f)));
// triangles
var tris = new List<int>();
sprite.triangles.ToList().ForEach(x => tris.Add(x));
// normals
var normals = new Vector3[verts.Count];
for (var index = 0; index < normals.Length; index++)
{
normals[index] = Vector3.forward;
}
var mesh = new Mesh
{
name = sprite.name,
vertices = verts.ToArray(),
triangles = tris.ToArray(),
normals = normals.ToArray(),
uv = sprite.uv,
bounds = new Bounds(Vector3.zero, Vector3.one * 1000f)
};
cloudMeshes[i] = mesh;
i++;
}
}
particles = new NativeArray<ParticleSystem.Particle>(ps.main.maxParticles, Allocator.Persistent);
parts = new ParticleSystem.Particle[ps.main.maxParticles];
var main = ps.main;
particles = new NativeArray<ParticleSystem.Particle>(main.maxParticles, Allocator.Persistent);
parts = new ParticleSystem.Particle[main.maxParticles];
}
private void OnDisable()

private void RenderPipelineManagerOnbeginFrameRendering(ScriptableRenderContext arg1, Camera[] arg2)
{
var camPos = Camera.main.transform.position;
ps.GetParticles(parts);
var l = parts.ToList().OrderByDescending(x => Vector3.Distance(x.position * 64f, camPos));
parts = l.ToArray();
if (Camera.main != null)
{
var camPos = Camera.main.transform.position;
ps.GetParticles(parts);
var l = parts.ToList().OrderByDescending(x => Vector3.Distance(x.position * 64f, camPos));
parts = l.ToArray();
}
if (!cloudMaterial) return;
if (!cloudMaterial || cloudMeshes == null || cloudMeshes.Length == 0) return;
var camPos = camera.transform.position * (1f / 64f);

Random.InitState((int)particle.randomSeed);
Graphics.DrawMesh(cloudMeshes[Random.Range(0, cloudMeshes.Length)], Matrix4x4.TRS(particle.position, q, particle.startSize3D), cloudMaterial, LayerMask.NameToLayer("3DSkybox"), camera);
var mesh = cloudMeshes[Random.Range(0, cloudMeshes.Length)];
/*var m = Instantiate(mesh);
var colors = new Color[mesh.vertexCount];
var col = particle.GetCurrentColor(ps);
for (var index = 0; index < colors.Length; index++)
{
colors[index] = col;
}
m.SetColors(colors);
m.UploadMeshData(true);*/
Graphics.DrawMesh(mesh, Matrix4x4.TRS(particle.position, q, particle.startSize3D), cloudMaterial, LayerMask.NameToLayer("3DSkybox"), camera);
}
}
}

1001
Assets/Objects/environment/Clouds/CloudMeshes.asset
文件差异内容过多而无法显示
查看文件

8
Assets/Objects/environment/Clouds/CloudMeshes.asset.meta


fileFormatVersion: 2
guid: 8b7e5305947554a418af3eb802471592
NativeFormatImporter:
externalObjects: {}
mainObjectFileID: 0
userData:
assetBundleName:
assetBundleVariant:

92
Assets/Scripts/Utility/SpriteMeshGenerator.cs


using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEditor;
using UnityEngine;
using Object = UnityEngine.Object;
[CreateAssetMenu(fileName = "SpriteMeshGenerator.asset", menuName = "2D/Sprite Mesh Generator", order = 1)]
public class SpriteMeshGenerator : ScriptableObject
{
public Sprite[] sprites;
[SerializeField] private string hash;
private void OnValidate()
{
if (sprites != null && sprites.Length > 0)
{
Debug.Log("doing things");
var curHash = "";
foreach (var sprite in sprites)
{
if (AssetDatabase.TryGetGUIDAndLocalFileIdentifier(sprite, out var guid, out long _))
{
curHash += guid;
}
}
if (hash == curHash) return;
GenerateAndSaveMesh();
hash = curHash;
}
}
void GenerateAndSaveMesh()
{
try
{
AssetDatabase.StartAssetEditing();
CleanSubassets();
foreach (var sprite in sprites)
{
if(sprite == null) continue;
var mesh = GenerateMeshFromSprite(sprite);
AssetDatabase.AddObjectToAsset(mesh, this);
}
}
finally
{
AssetDatabase.StopAssetEditing();
}
}
public static Mesh GenerateMeshFromSprite(Sprite sprite)
{
// verts
var verts = new List<Vector3>();
sprite.vertices.ToList().ForEach(x => verts.Add(new Vector3(x.x, x.y, 0.0f)));
// triangles
var tris = new List<int>();
sprite.triangles.ToList().ForEach(x => tris.Add(x));
// normals
var normals = new Vector3[verts.Count];
for (var index = 0; index < normals.Length; index++)
{
normals[index] = Vector3.forward;
}
var mesh = new Mesh
{
name = sprite.name.Replace('.', '_'),
vertices = verts.ToArray(),
triangles = tris.ToArray(),
normals = normals.ToArray(),
colors32 = new Color32[verts.Count],
uv = sprite.uv
};
return mesh;
}
void CleanSubassets()
{
var subassets = AssetDatabase.LoadAllAssetsAtPath(AssetDatabase.GetAssetPath(this));
foreach (var t in subassets)
{
if(t == this) continue;
Debug.Log(t);
DestroyImmediate(t, true);
}
}
}

11
Assets/Scripts/Utility/SpriteMeshGenerator.cs.meta


fileFormatVersion: 2
guid: efd71620d53b04fe4b35207444f01def
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
正在加载...
取消
保存