- Home /
Is it possible to transfer mesh to VRAM without rendering it?
Is it possible to transfer mesh to VRAM without rendering it? I want to generate some meshes procedurally, but I don't want to display them until they come into view. Is this possible?
It is a good practice to accept answers or at least upvote them.
Answer by valyard · Nov 08, 2012 at 07:10 PM
You could use DirectX 11 buffers which Unity 4 gladly supports. Something like this pseudocode:
public Material MyMaterial;
protected ComputeBuffer sourceBuffer;
protected void Start() {
sourceBuffer = new ComputeBuffer(<number of verteces>, <vertex size in bytes>, ComputeBufferType.Append);
sourceBuffer.SetData(<array with mesh data>);
}
protected void OnRenderObject() {
MyMaterial.SetPass(0);
MyMaterial.SetBuffer("pointBuffer", sourceBuffer);
MyMaterial.SetMatrix("MVP", Camera.main.projectionMatrix*Camera.main.worldToCameraMatrix*transform.localToWorldMatrix);
Graphics.DrawProcedural(MeshTopology.Points, ParticlesCount);
}
protected void OnDisable() {
sourceBuffer.release();
}
With shader which looks like this:
#pragma target 5.0
struct Vertex
{
float4 position;
float4 normal;
};
float4x4 MVP;
StructuredBuffer<Vertex> pointBuffer;
vs_out vert (uint id : SV_VertexID)
{
vs_out o;
float4 p = pointBuffer[id].position;
...
return o;
}
Refer to Unity 4 manual on buffers.
Awesome. I haven't downloaded the beta yet, but I'll check out the manual to see what I can find related to this!
Answer by Sakachi99 · Jul 01, 2013 at 07:55 PM
just a quick question with this:
i am using unity 4, and have a shadermodel 5 card.
but when i try the above idea, i get an error at vs_out vert (uint id : SV_VertexID)
it says that sv_vertexID is basically not recognized.
any idea how to get access to the vertexID?
is it only available in shadermodel 5?
Your answer
Follow this Question
Related Questions
How to texture walls in procedurally generated mesh? 1 Answer
Flat shading procedural generated mesh? 1 Answer
Triangles overlapping in mesh 0 Answers
Adding extra materials to a mesh programmatically 0 Answers
UV Mapping a Sphere at runtime 1 Answer