rparams | Unity 用于渲染原语的参数。 |
topology | 基本拓扑(例如三角形或直线)。 |
vertexCount | 每个实例的顶点数。 |
instanceCount | 要渲染的实例数。 |
使用 GPU 实例化和自定义着色器渲染非索引化原语。
渲染具有特定拓扑的给定数量的实例和原语。此方法需要自定义着色器,以便使用 SV_VertexID
语义获取或计算顶点数据。要访问实例 ID,请使用 SV_InstanceID
语义。
其他资源: RenderPrimitivesIndexed。
以下示例使用该函数渲染网格的 10 个实例。关联材质必须使用以下自定义着色器
using UnityEngine;
public class ExampleClass : MonoBehaviour { public Material material; public Mesh mesh;
GraphicsBuffer meshTriangles; GraphicsBuffer meshPositions;
void Start() { // note: remember to check "Read/Write" on the mesh asset to get access to the geometry data meshTriangles = new GraphicsBuffer(GraphicsBuffer.Target.Structured, mesh.triangles.Length, sizeof(int)); meshTriangles.SetData(mesh.triangles); meshPositions = new GraphicsBuffer(GraphicsBuffer.Target.Structured, mesh.vertices.Length, 3 * sizeof(float)); meshPositions.SetData(mesh.vertices); }
void OnDestroy() { meshTriangles?.Dispose(); meshTriangles = null; meshPositions?.Dispose(); meshPositions = null; }
void Update() { RenderParams rp = new RenderParams(material); rp.worldBounds = new Bounds(Vector3.zero, 10000*Vector3.one); // use tighter bounds rp.matProps = new MaterialPropertyBlock(); rp.matProps.SetBuffer("_Triangles", meshTriangles); rp.matProps.SetBuffer("_Positions", meshPositions); rp.matProps.SetInt("_StartIndex", (int)mesh.GetIndexStart(0)); rp.matProps.SetInt("_BaseVertexIndex", (int)mesh.GetBaseVertex(0)); rp.matProps.SetMatrix("_ObjectToWorld", Matrix4x4.Translate(new Vector3(-4.5f, 0, 0))); rp.matProps.SetFloat("_NumInstances", 10.0f); Graphics.RenderPrimitives(rp, MeshTopology.Triangles, (int)mesh.GetIndexCount(0), 10); } }
对上述 C# 示例代码使用以下示例着色器
Shader "ExampleShader" { SubShader { Pass { CGPROGRAM #pragma vertex vert #pragma fragment frag
#include "UnityCG.cginc"
struct v2f { float4 pos : SV_POSITION; float4 color : COLOR0; };
StructuredBuffer<int> _Triangles; StructuredBuffer<float3> _Positions; uniform uint _StartIndex; uniform uint _BaseVertexIndex; uniform float4x4 _ObjectToWorld; uniform float _NumInstances;
v2f vert(uint vertexID: SV_VertexID, uint instanceID : SV_InstanceID) { v2f o; float3 pos = _Positions[_Triangles[vertexID + _StartIndex] + _BaseVertexIndex]; float4 wpos = mul(_ObjectToWorld, float4(pos + float3(instanceID, 0, 0), 1.0f)); o.pos = mul(UNITY_MATRIX_VP, wpos); o.color = float4(instanceID / _NumInstances, 0.0f, 0.0f, 0.0f); return o; }
float4 frag(v2f i) : SV_Target { return i.color; } ENDCG } } }
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.