Skip to content

Commit

Permalink
Dirty prototype of VFX support
Browse files Browse the repository at this point in the history
  • Loading branch information
keijiro committed Jan 29, 2020
1 parent 1d1fd63 commit 070ba37
Show file tree
Hide file tree
Showing 14 changed files with 6,589 additions and 13 deletions.
37 changes: 37 additions & 0 deletions Assets/Color.renderTexture
@@ -0,0 +1,37 @@
%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!84 &8400000
RenderTexture:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_Name: Color
m_ImageContentsHash:
serializedVersion: 2
Hash: 00000000000000000000000000000000
m_ForcedFallbackFormat: 4
m_DownscaleFallback: 0
serializedVersion: 3
m_Width: 256
m_Height: 256
m_AntiAliasing: 1
m_MipCount: -1
m_DepthFormat: 2
m_ColorFormat: 52
m_MipMap: 0
m_GenerateMips: 1
m_SRGB: 0
m_UseDynamicScale: 0
m_BindMS: 0
m_EnableCompatibleFormat: 1
m_TextureSettings:
serializedVersion: 2
m_FilterMode: 0
m_Aniso: 0
m_MipBias: 0
m_WrapU: 1
m_WrapV: 1
m_WrapW: 1
m_Dimension: 2
m_VolumeDepth: 1
8 changes: 8 additions & 0 deletions Assets/Color.renderTexture.meta

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

74 changes: 74 additions & 0 deletions Assets/Converter.shader
@@ -0,0 +1,74 @@
Shader "Hidden/Converter"
{
CGINCLUDE

#include "UnityCG.cginc"

Buffer<float> _VertexArray;
Buffer<float> _UVArray;
float2 _TextureSize;
uint _VertexCount;

void Vertex(
float4 vertex : POSITION,
float2 uv : TEXCOORD0,
out float4 outPosition : SV_Position,
out float2 outUV : TEXCOORD0
)
{
outPosition = UnityObjectToClipPos(vertex);
outUV = uv;
}

void Fragment(
float4 position : SV_Position,
float2 uv : TEXCOORD0,
out float4 outColor : SV_Target
)
{
uint2 uvi = uv * _TextureSize;
uint index = uvi.x + uvi.y * _TextureSize.x;

float x = _VertexArray[index * 3 + 0];
float y = _VertexArray[index * 3 + 1];
float z = _VertexArray[index * 3 + 2];

outColor = float4(x, y, z, index < _VertexCount);
}

void Fragment2(
float4 position : SV_Position,
float2 uv : TEXCOORD0,
out float4 outColor : SV_Target
)
{
uint2 uvi = uv * _TextureSize;
uint index = uvi.x + uvi.y * _TextureSize.x;

float u = _UVArray[index * 2 + 0];
float v = _UVArray[index * 2 + 1];

outColor = float4(u, v, 0, index < _VertexCount);
}

ENDCG

SubShader
{
Cull Off ZWrite Off ZTest Always
Pass
{
CGPROGRAM
#pragma vertex Vertex
#pragma fragment Fragment
ENDCG
}
Pass
{
CGPROGRAM
#pragma vertex Vertex
#pragma fragment Fragment2
ENDCG
}
}
}
9 changes: 9 additions & 0 deletions Assets/Converter.shader.meta

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

80 changes: 70 additions & 10 deletions Assets/GeometryRenderer4DS.cs
Expand Up @@ -12,6 +12,10 @@ public sealed class GeometryRenderer4DS : MonoBehaviour, ITimeControl, IProperty
[SerializeField] string _fileName = null;
[SerializeField] Material _material = null;
[SerializeField] float _time = 0;
[SerializeField] RenderTexture _positionMap = null;
[SerializeField] RenderTexture _uvMap = null;
[SerializeField] RenderTexture _colorMap = null;
[SerializeField] Shader _bakeShader = null;

DataSource4DS _dataSource;
int _totalFrames;
Expand All @@ -22,13 +26,39 @@ public sealed class GeometryRenderer4DS : MonoBehaviour, ITimeControl, IProperty
Texture2D _texture;
MaterialPropertyBlock _props;

Material _bakeMaterial;

(
NativeArray<Vector3> vertex,
NativeArray<Vector3> normal,
NativeArray<Vector2> uv,
NativeArray<int> index,
NativeArray<byte> texture
)
_buffer;

(
ComputeBuffer vertex,
ComputeBuffer normal,
ComputeBuffer uv,
ComputeBuffer index
)
_bakeBuffer;

void OnDisable()
{
if (_buffer.vertex .IsCreated) _buffer.vertex .Dispose();
if (_buffer.normal .IsCreated) _buffer.normal .Dispose();
if (_buffer.uv .IsCreated) _buffer.uv .Dispose();
if (_buffer.index .IsCreated) _buffer.index .Dispose();
if (_buffer.texture.IsCreated) _buffer.texture.Dispose();

_bakeBuffer.vertex?.Dispose();
_bakeBuffer.normal?.Dispose();
_bakeBuffer.uv ?.Dispose();
_bakeBuffer.index ?.Dispose();

_bakeBuffer = (null, null, null, null);
}

void OnDestroy()
Expand Down Expand Up @@ -56,16 +86,16 @@ void OnDestroy()
DestroyImmediate(_texture);
_texture = null;
}
}

(
NativeArray<Vector3> vertex,
NativeArray<Vector3> normal,
NativeArray<Vector2> uv,
NativeArray<int> index,
NativeArray<byte> texture
)
_buffer;
if (_bakeMaterial != null)
{
if (Application.isPlaying)
Destroy(_bakeMaterial);
else
DestroyImmediate(_bakeMaterial);
_bakeMaterial = null;
}
}

unsafe void Update()
{
Expand Down Expand Up @@ -98,6 +128,17 @@ unsafe void Update()
_buffer.texture = new NativeArray< byte>(texsize, Allocator.Persistent);
}

if (_bakeBuffer.vertex == null)
{
var vcount = _dataSource.MaxVertices;
var tcount = _dataSource.MaxTriangles;

_bakeBuffer.vertex = new ComputeBuffer(vcount * 3, sizeof(float));
_bakeBuffer.normal = new ComputeBuffer(vcount * 3, sizeof(float));
_bakeBuffer.uv = new ComputeBuffer(vcount * 2, sizeof(float));
_bakeBuffer.index = new ComputeBuffer(tcount * 3, sizeof(int));
}

if (_mesh == null)
{
_mesh = new Mesh();
Expand All @@ -120,6 +161,10 @@ unsafe void Update()

if (_props == null) _props = new MaterialPropertyBlock();

if (_bakeMaterial == null)
_bakeMaterial =
new Material(_bakeShader){ hideFlags = HideFlags.DontSave };

if (_dataSource != null)
{
var frame = Mathf.Clamp((int)(_time * _frameRate), 0, _totalFrames - 1);
Expand All @@ -140,23 +185,38 @@ unsafe void Update()
_lastFrame, ref vertexCount, ref indexCount
);

_mesh.SetVertices(_buffer.vertex); _mesh.SetNormals(_buffer.normal);
_bakeMaterial.SetInt("_VertexCount", vertexCount);

_mesh.SetVertices(_buffer.vertex);
_mesh.SetNormals(_buffer.normal);
_mesh.SetUVs(0, _buffer.uv);
_mesh.SetIndices(_buffer.index, MeshTopology.Triangles, 0);

_texture.LoadRawTextureData(_buffer.texture);
_texture.Apply();

_bakeBuffer.vertex.SetData(_buffer.vertex);
_bakeBuffer.uv.SetData(_buffer.uv);

_lastFrame = frame;
}
}

_props.SetTexture("_MainTex", _texture);

/*
Graphics.DrawMesh(
_mesh, transform.localToWorldMatrix,
_material, 0, null, 0, _props
);
*/

_bakeMaterial.SetVector("_TextureSize", new Vector2(_positionMap.width, _positionMap.height));
_bakeMaterial.SetBuffer("_VertexArray", _bakeBuffer.vertex);
_bakeMaterial.SetBuffer("_UVArray", _bakeBuffer.uv);
Graphics.Blit(null, _positionMap, _bakeMaterial, 0);
Graphics.Blit(null, _uvMap, _bakeMaterial, 1);
Graphics.Blit(_texture, _colorMap);
}

#region ITimeControl implementation
Expand Down

0 comments on commit 070ba37

Please sign in to comment.