Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proof of concept: Use Shader Graph for point cloud rendering #265

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 40 additions & 17 deletions Runtime/CesiumPointCloudRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ internal class CesiumPointCloudRenderer : MonoBehaviour

private Cesium3DTileInfo _tileInfo;

private Mesh _emptyMesh;

public Cesium3DTileInfo tileInfo
{
set => this._tileInfo = value;
Expand All @@ -51,7 +53,7 @@ void OnEnable()

this._pointCount = this._mesh.vertexCount;
this._pointMaterial = UnityEngine.Object.Instantiate(
Resources.Load<Material>("CesiumPointCloudShadingMaterial"));
Resources.Load<Material>("CesiumPointCloudTilesetMaterial"));

GraphicsBuffer sourceBuffer = this._mesh.GetVertexBuffer(0);

Expand All @@ -72,7 +74,7 @@ void OnEnable()

if (this._mesh.HasVertexAttribute(VertexAttribute.Color))
{
this._pointMaterial.EnableKeyword("HAS_POINT_COLORS");
this._pointMaterial.EnableKeyword("_HAS_POINT_COLORS");
}
else
{
Expand All @@ -94,14 +96,27 @@ void OnEnable()

if (this._mesh.HasVertexAttribute(VertexAttribute.Normal))
{
this._pointMaterial.EnableKeyword("HAS_POINT_NORMALS");
this._pointMaterial.EnableKeyword("_HAS_POINT_NORMALS");
}

if (XRSettings.stereoRenderingMode == XRSettings.StereoRenderingMode.SinglePassInstanced ||
XRSettings.stereoRenderingMode == XRSettings.StereoRenderingMode.SinglePassMultiview)
{
this._pointMaterial.EnableKeyword("INSTANCING_ON");
}

var layout = new VertexAttributeDescriptor[0];
this._emptyMesh = CesiumObjectPool.MeshPool.Get();

int[] indices = new int[this._pointCount * 6];
for (int i = 0; i < indices.Length; ++i)
{
indices[i] = i;
}

this._emptyMesh.SetVertexBufferParams(this._pointCount * 6, layout);
this._emptyMesh.indexFormat = IndexFormat.UInt32;
this._emptyMesh.SetIndices(indices, MeshTopology.Triangles, 0, false);
}

private float GetGeometricError(CesiumPointCloudShading pointCloudShading)
Expand Down Expand Up @@ -167,10 +182,17 @@ private void UpdateBounds()
positionsScratch[2] = localBounds.max;

this._bounds = GeometryUtility.CalculateBounds(positionsScratch, transformMatrix);
this._emptyMesh.bounds = this._mesh.bounds;
}

private void DestroyResources()
{
if (this._emptyMesh != null)
{
CesiumObjectPool.MeshPool.Release(this._emptyMesh);
this._emptyMesh = null;
}

if (this._meshVertexBuffer != null)
{
this._meshVertexBuffer.Release();
Expand All @@ -196,14 +218,14 @@ private void UpdateMaterial()
this._pointMaterial.SetVector("_attenuationParameters", this._attenuationParameters);
this._pointMaterial.SetVector("_constantColor", this._constantColor);

if (this._tileInfo.isTranslucent || this._constantColor.w < 1.0f)
{
this._pointMaterial.SetOverrideTag("RenderType", "Transparent");
this._pointMaterial.renderQueue = (int)RenderQueue.Transparent;
this._pointMaterial.SetInt("_SrcBlend", (int)BlendMode.SrcAlpha);
this._pointMaterial.SetInt("_DstBlend", (int)BlendMode.OneMinusSrcAlpha);
}
else
//if (this._tileInfo.isTranslucent || this._constantColor.w < 1.0f)
//{
// this._pointMaterial.SetOverrideTag("RenderType", "Transparent");
// this._pointMaterial.renderQueue = (int)RenderQueue.Transparent;
// this._pointMaterial.SetInt("_SrcBlend", (int)BlendMode.SrcAlpha);
// this._pointMaterial.SetInt("_DstBlend", (int)BlendMode.OneMinusSrcAlpha);
//}
//else
{
this._pointMaterial.SetInt("_SrcBlend", (int)BlendMode.One);
this._pointMaterial.SetInt("_DstBlend", (int)BlendMode.Zero);
Expand All @@ -216,12 +238,13 @@ private void DrawPointsWithAttenuation()
this.UpdateAttenuationParameters();
this.UpdateMaterial();

Graphics.DrawProcedural(
this._pointMaterial,
this._bounds,
MeshTopology.Triangles,
this._pointCount * 6,
1);
Graphics.DrawMesh(this._emptyMesh, this.gameObject.transform.localToWorldMatrix, this._pointMaterial, 0);
//Graphics.DrawProcedural(
// this._pointMaterial,
// this._bounds,
// MeshTopology.Triangles,
// this._pointCount * 6,
// 1);
}

void Update()
Expand Down
221 changes: 221 additions & 0 deletions Runtime/Resources/CesiumPointCloudTilesetMaterial.mat
Original file line number Diff line number Diff line change
@@ -0,0 +1,221 @@
%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!114 &-4552124577082293056
MonoBehaviour:
m_ObjectHideFlags: 11
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 0}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: d0353a89b1f911e48b9e16bdc9f2e058, type: 3}
m_Name:
m_EditorClassIdentifier:
version: 5
--- !u!114 &-4425681635612486665
MonoBehaviour:
m_ObjectHideFlags: 11
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 0}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 639247ca83abc874e893eb93af2b5e44, type: 3}
m_Name:
m_EditorClassIdentifier:
version: 0
--- !u!21 &2100000
Material:
serializedVersion: 8
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_Name: CesiumPointCloudTilesetMaterial
m_Shader: {fileID: -6465566751694194690, guid: 1f9910ca3bd0f48428a1c4d37895439e,
type: 3}
m_ValidKeywords: []
m_InvalidKeywords:
- _DOUBLESIDED_ON
m_LightmapFlags: 4
m_EnableInstancingVariants: 0
m_DoubleSidedGI: 1
m_CustomRenderQueue: 2000
stringTagMap:
MotionVector: User
disabledShaderPasses:
- TransparentDepthPrepass
- TransparentDepthPostpass
- TransparentBackface
- RayTracingPrepass
- MOTIONVECTORS
m_SavedProperties:
serializedVersion: 3
m_TexEnvs:
- _baseColorTexture:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _emissiveTexture:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _metallicRoughnessTexture:
m_Texture: {fileID: 2800000, guid: ce97cc92c5d962f4585b2ac5527d723e, type: 3}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _normalMapTexture:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _normalTexture:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _occlusionTexture:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _overlay0Texture:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _overlay1Texture:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _overlay2Texture:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- unity_Lightmaps:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- unity_LightmapsInd:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- unity_ShadowMasks:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
m_Ints: []
m_Floats:
- _AddPrecomputedVelocity: 0
- _AlphaClip: 0
- _AlphaCutoffEnable: 0
- _AlphaDstBlend: 0
- _AlphaSrcBlend: 1
- _AlphaToMask: 0
- _AlphaToMaskInspectorValue: 0
- _BUILTIN_QueueControl: 1
- _BUILTIN_QueueOffset: 0
- _Blend: 0
- _BlendMode: 0
- _CastShadows: 1
- _ConservativeDepthOffsetEnable: 0
- _Cull: 2
- _CullMode: 0
- _CullModeForward: 0
- _DepthOffsetEnable: 0
- _DoubleSidedEnable: 1
- _DoubleSidedGIMode: 0
- _DoubleSidedNormalMode: 2
- _DstBlend: 0
- _EnableBlendModePreserveSpecularLighting: 1
- _EnableFogOnTransparent: 1
- _HASBASECOLOR: 0
- _HASEMISSIVE: 0
- _HASMETALLICROUGHNESS: 0
- _HASNORMALMAP: 0
- _HASOCCLUSION: 0
- _HAS_BASE_COLOR: 0
- _HAS_EMISSIVE: 0
- _HAS_METALLIC_ROUGHNESS: 0
- _HAS_NORMAL_MAP: 0
- _HAS_NORMAL_TEXTURE: 0
- _HAS_OCCLUSION: 0
- _HAS_POINT_COLORS: 0
- _HAS_POINT_NORMALS: 0
- _OVERLAYCOUNT: 0
- _OVERLAY_COUNT: 0
- _OpaqueCullMode: 2
- _QueueControl: 0
- _QueueOffset: 0
- _RayTracing: 0
- _ReceiveShadows: 1
- _ReceivesSSR: 1
- _ReceivesSSRTransparent: 0
- _RefractionModel: 0
- _RenderQueueType: 1
- _RequireSplitLighting: 0
- _SrcBlend: 1
- _StencilRef: 0
- _StencilRefDepth: 0
- _StencilRefDistortionVec: 4
- _StencilRefGBuffer: 2
- _StencilRefMV: 32
- _StencilWriteMask: 6
- _StencilWriteMaskDepth: 8
- _StencilWriteMaskDistortionVec: 4
- _StencilWriteMaskGBuffer: 14
- _StencilWriteMaskMV: 42
- _SupportDecals: 1
- _Surface: 0
- _SurfaceType: 0
- _TESTING: 1
- _TransparentBackfaceEnable: 0
- _TransparentCullMode: 2
- _TransparentDepthPostpassEnable: 0
- _TransparentDepthPrepassEnable: 0
- _TransparentSortPriority: 0
- _TransparentWritingMotionVec: 0
- _TransparentZWrite: 0
- _UseShadowThreshold: 0
- _WorkflowMode: 1
- _ZTest: 4
- _ZTestDepthEqualForOpaque: 3
- _ZTestGBuffer: 4
- _ZTestTransparent: 4
- _ZWrite: 1
- _ZWriteControl: 0
- _baseColorTextureCoordinateIndex: 0
- _emissiveTextureCoordinateIndex: 0
- _metallicRoughnessTextureCoordinateIndex: 0
- _normalMapScale: 0
- _normalMapTextureCoordinateIndex: 0
- _normalTextureCoordinateIndex: 0
- _occlusionStrength: 0
- _occlusionTextureCoordinateIndex: 0
- _overlay0TextureCoordinateIndex: 0
- _overlay1TextureCoordinateIndex: 0
- _overlay2TextureCoordinateIndex: 0
m_Colors:
- _DoubleSidedConstants: {r: 1, g: 1, b: -1, a: 0}
- _EmissionColor: {r: 1, g: 1, b: 1, a: 1}
- _baseColorFactor: {r: 1, g: 1, b: 1, a: 1}
- _emissiveFactor: {r: 0, g: 0, b: 0, a: 0}
- _metallicRoughnessFactor: {r: 0, g: 0, b: 0, a: 0}
- _overlay0TranslationAndScale: {r: 0, g: 0, b: 1, a: 1}
- _overlay1TranslationAndScale: {r: 0, g: 0, b: 1, a: 1}
- _overlay2TranslationAndScale: {r: 0, g: 0, b: 1, a: 1}
m_BuildTextureStacks: []
--- !u!114 &1361552703489516110
MonoBehaviour:
m_ObjectHideFlags: 11
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 0}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: da692e001514ec24dbc4cca1949ff7e8, type: 3}
m_Name:
m_EditorClassIdentifier:
version: 12
hdPluginSubTargetMaterialVersions:
m_Keys: []
m_Values:
8 changes: 8 additions & 0 deletions Runtime/Resources/CesiumPointCloudTilesetMaterial.mat.meta

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

Loading