dug_deep/Assets/Render.cs

137 lines
No EOL
3.2 KiB
C#

using UnityEngine;
using System;
using System.Collections;
using System.Collections.Generic;
using ExtensionMethods;
[Serializable]
public class Render
{
Monolith mono;
public void Enable(Monolith mono)
{
this.mono = mono;
Camera.onPreCull -= DrawWithCamera;
Camera.onPreCull += DrawWithCamera;
}
public void Disable()
{
Camera.onPreCull -= DrawWithCamera;
}
public Mesh meshVoxelDebug, meshPieceDebug;
public Material matVoxelDebug, matPieceDebug, matEnemy, matPath;
void DrawWithCamera(Camera camera)
{
if (camera)
{
// GL.Clear(true, true, Color.gray);
Draw(camera, camera.transform.localToWorldMatrix * Matrix4x4.TRS(Vector3.forward * 10, Quaternion.identity, Vector3.one));
}
}
public Vector3 lerpPos;
void Draw(Camera camera, Matrix4x4 matrix)
{
Voxels();
// Draw Enemy
Graphics.DrawMesh(meshPieceDebug,
mono.enemy.pos,
Quaternion.identity,
matEnemy, 0
);
// Draw Piece
lerpPos = Vector3.Lerp(lerpPos, mono.piece.pos, Time.deltaTime * 24);
Graphics.DrawMesh(meshPieceDebug,
lerpPos,
Quaternion.identity,
matPieceDebug, 0
);
// Draw Path...(s)
// lets start constraining the movement to the generated level
// convert to instanced matrices and check for duplicates
// for (int i = 0; i < mono.dirs.Length; i++)
// {
// Paths(i, mono.piece.pos);
// }
for (int i = 0; i < mono.dirs.Length; i++)
{
if (mono.movePiece && !mono.Outside(mono.piece.pos + mono.dirs[i]))
{
Graphics.DrawMesh(meshPieceDebug,
mono.piece.pos + mono.dirs[i],
Quaternion.identity,
matPath, 0
);
}
}
}
void Paths(int dirIndex, Vector3Int pos, int patternIndex = 0)
{
for (int i = patternIndex; i < mono.piece.pattern.Length; i++)
{
pos += mono.dirs[dirIndex];
if (mono.Outside(pos))
{
return;
}
else
{
Graphics.DrawMesh(meshPieceDebug,
pos,
Quaternion.identity,
matPath, 0
);
}
if (i != patternIndex && mono.piece.pattern[i] != 0)
{
int toIndex = dirIndex.Rollover(mono.piece.pattern[i], mono.dirs.Length);
Paths(toIndex, pos, i);
Paths(toIndex.Rollover(2, mono.dirs.Length), pos, i);
Paths(toIndex.Rollover(3, mono.dirs.Length), pos, i);
Paths(toIndex.Rollover(5, mono.dirs.Length), pos, i);
return;
}
}
}
List<Matrix4x4> voxelM4 = new List<Matrix4x4>();
void Voxels()
{
voxelM4.Clear();
for (int i = 0; i < mono.voxels.Length; i++)
{
if (mono.voxels[i] != null)
{
for (int d = 0; d < mono.dirs.Length; d++)
{
if (mono.Outside(mono.voxels[i].pos + mono.dirs[d]))
{
Vector3 renderPos = mono.voxels[i].pos + (Vector3)mono.dirs[d] / 2;
Matrix4x4 m4 = new Matrix4x4();
m4.SetTRS(renderPos,
Quaternion.LookRotation(renderPos - mono.voxels[i].pos),
Vector3.one
);
voxelM4.Add(m4);
}
}
}
}
if (voxelM4.Count > 0)
{
Graphics.DrawMeshInstanced(meshVoxelDebug, 0, matVoxelDebug, voxelM4);
}
}
}