- Home /
Unity 4.0 mesh duplication possible?
I need a code to duplicate mesh without original mesh disappearing. is it possible? how?
Update: Full Code
#pragma strict
var Test : Transform;
var Nyil : Transform;
var NyilStorage = new Array();
var hit : RaycastHit;
var curhit : RaycastHit;
var hSliderOldValue : int[] = new int[10] ;
var hSliderValue : int[] = new int[10] ;
var Vertices : Vector3[];
private var a = new Array("Models","Morph","Vertices");
private var b = new Array("Number");
private var c = new Array("Id","Mod","DirX" ,"DirY" ,"DirZ","Target");
var LD : String[,,] = new String[0,0,0];
var MousePos : Vector2;
var ScreenPosV : Vector2;
var SelNyil : int;
function Start () {
XML.OpenXML("Modules/" + "Modul" );
LD = XML.ReadXMLAdv(a,b,c);
TestNewObj();
}
function Update () {
SelectObject();
}
function OnGUI(){
//print(hit.collider);
if (GUI.Button(Rect(0,0,50,20),"press") ){
Vertices = (hit.collider as MeshCollider).sharedMesh.vertices;
print((hit.collider as MeshCollider).sharedMesh.vertices[0]);
for(var i = 1;i<LD.GetLength(1);i++){
Vertices[ parseInt(LD[0,i,0]) ] = Vertices[ parseInt(LD[0,i,0]) ] + Vector3.up ;
print(i);
}
(hit.collider as MeshCollider).sharedMesh.vertices = Vertices;
}
MorphSlider();
if (GUI.changed){
for(i = 0;i<hSliderValue.length;i++){
if(hSliderOldValue[i] != hSliderValue[i]){
hSliderOldValue[i] = hSliderValue[i];
NyilRePos(i);
//print("Changed " + i);
}
}
}
}
function SelectObject(){
if(Input.GetMouseButtonUp(0)){
if (Physics.Raycast (camera.ScreenPointToRay(Input.mousePosition), curhit)){
// print(curhit.collider.name + " " + (curhit.collider as MeshCollider).sharedMesh );
if (curhit.collider != null || (curhit.collider as MeshCollider).sharedMesh != null ){
if(hit.collider != curhit.collider && curhit.collider.name != "Arrow(Clone)"){
hit=curhit;
NyilDestroy();
NyilInit();
}
}
}
}
if(Input.GetMouseButton(0)){
if(Input.GetMouseButtonDown(0)){
SelNyil = -1;
if (Physics.Raycast (camera.ScreenPointToRay(Input.mousePosition), curhit)){
print("ar");
if( curhit.collider.name == "Arrow(Clone)"){
var screenPosA : Vector3 = camera.WorldToScreenPoint (curhit.transform.position);
var screenPosB : Vector3 = camera.WorldToScreenPoint (hit.transform.position);
ScreenPosV = (Vector2( screenPosA.x,screenPosA.y ) - Vector2( screenPosB.x, screenPosB.y )).normalized ;
MousePos = Vector2(Input.mousePosition.x,Input.mousePosition.y) ;
SelNyil = (curhit.transform.GetComponent("Destroy") as Destroy).Count ;
//print(ScreenPosV + " mp: " + MousePos);
//print("a");
}
}}
// print(SelNyil);
if(SelNyil != -1){
var MousePosCurr : Vector2 = Vector2(Input.mousePosition.x,Input.mousePosition.y) ;
var NewSliderPos : int = hSliderOldValue[SelNyil] + Mathf.CeilToInt( ((MousePosCurr.x - MousePos.x) /30 * ScreenPosV.x ) + ( (MousePosCurr.y - MousePos.y) /30 * ScreenPosV.y) );
hSliderValue[SelNyil] = (NewSliderPos < 0) ? 0 : NewSliderPos ;
NyilRePos(SelNyil);
}
}
if(Input.GetMouseButtonUp(0)){
if(SelNyil != -1){
hSliderOldValue[SelNyil] = hSliderValue[SelNyil];
}
}
}
function MorphSlider(){
for( var i = 0;i<LD.GetLength(0) ;i++){
hSliderValue[i] = GUI.HorizontalSlider(Rect(0,0 + i*22,80,20), hSliderValue[i], 0, 10 );
GUI.Label(Rect(85,0 +i*22,80,20),hSliderValue[i] + " Morph"+ i.ToString() );
}
}
function NyilInit(){
for(var i =0;i<LD.GetLength(0);i++){
var pos = Vector3( parseInt(LD[i,0,2]) , parseInt(LD[i,0,3]) , parseInt(LD[i,0,4]) );
NyilStorage.Add( Instantiate(Nyil,hit.collider.transform.position + pos, Quaternion.LookRotation(pos*-1 )) );
((NyilStorage[NyilStorage.length-1] as Transform).GetComponent("Destroy") as Destroy).SetCount(i);
}
}
function NyilDestroy(){
for (var value : Transform in NyilStorage) {
(value.GetComponent("Destroy") as Destroy).SelfDestroy();
}
NyilStorage.Clear();
}
function NyilRePos(i){
//print("value " + i +"pos: " + Vector3( parseInt(LD[i,0,2]) , parseInt(LD[i,0,3]) , parseInt(LD[i,0,4])) * (hSliderOldValue[i]+1) );
(NyilStorage[i] as Transform).position = hit.collider.transform.position + Vector3( parseInt(LD[i,0,2]) , parseInt(LD[i,0,3]) , parseInt(LD[i,0,4])) * (hSliderValue[i]+1);
}
function TestNewObj(){
var m = new Mesh();
m.name = "Scripted_Plane_New_Mesh";
// var MeshSource : Mesh = Test.GetComponent(MeshFilter).mesh;
/*
var ver : Vector3[] = Test.GetComponent(MeshFilter).mesh.vertices;
var tri : int[] = Test.GetComponent(MeshFilter).mesh.triangles;
var uv : Vector2[] = Test.GetComponent(MeshFilter).mesh.uv;
var norm : Vector3[] = Test.GetComponent(MeshFilter).mesh.normals;
m.vertices = ver;
m.triangles = tri;
m.uv = uv;
m.normals = norm;
*/
var obj : GameObject = new GameObject("New_Plane_Fom_Script", MeshRenderer, MeshFilter, MeshCollider);
obj.GetComponent(MeshFilter).mesh = Instantiate(Test.GetComponent(MeshFilter).mesh);//m;
obj.GetComponent(MeshCollider).mesh = Instantiate(Test.GetComponent(MeshFilter).mesh);// m;
obj.GetComponent("Renderer").renderer.material = Resources.Load("Materials/Default2") as Material;
// MeshFilter mf = GetComponent<MeshFilter>();
// mf.mesh = (Mesh)Instantiate(mf.mesh);
}
Thanks, but this is not a duplication. Prefabs use the same mesh, and i need a mesh copy not instancing one mesh several times.
update
var Test : Transform;
function TestNewObj(){
var m = new $$anonymous$$esh();
m.name = "Scripted_Plane_New_$$anonymous$$esh";
// var $$anonymous$$eshSource : $$anonymous$$esh = Test.GetComponent($$anonymous$$eshFilter).mesh;
var ver : Vector3[] = Test.GetComponent($$anonymous$$eshFilter).mesh.vertices;
var tri : int[] = Test.GetComponent($$anonymous$$eshFilter).mesh.triangles;
var uv : Vector2[] = Test.GetComponent($$anonymous$$eshFilter).mesh.uv;
var norm : Vector3[] = Test.GetComponent($$anonymous$$eshFilter).mesh.normals;
m.vertices = ver;
m.triangles = tri;
m.uv = uv;
m.normals = norm;
var obj : GameObject = new GameObject("New_Plane_Fom_Script", $$anonymous$$eshRenderer, $$anonymous$$eshFilter, $$anonymous$$eshCollider);
obj.GetComponent($$anonymous$$eshFilter).mesh = m;
obj.GetComponent($$anonymous$$eshCollider).mesh = m;
obj.GetComponent("Renderer").renderer.material = Resources.Load("$$anonymous$$aterials/Default2") as $$anonymous$$aterial;
}
this is my last try (original mesh disapper)
Use comments to comment...
What are you tryng to do? And what is instatiation different than copying? Its not definitely linked, it has its own properties.
no, unity 4.0 upgrade , all instantiated object shared one mesh without own properties. you can only change size, rotation and position, but if you change vertex data , all clone mesh vertex data change, AND your prefabs mesh AND imported mesh vertex data change also.
Did you ever find a solution to this? I have the exact same issue
Answer by robertbu · Jan 13, 2013 at 07:54 PM
Use Instantiate():
MeshFilter mf = GetComponent<MeshFilter>();
mf.mesh = (Mesh)Instantiate(mf.mesh);
not working. Original prefab mesh filter error ( type mismatch) so if i try to create a mesh copy, original mesh disappearing or if i change clone mesh data, original vertex data change also.
This is exactly same code as i posted before. Casting mesh and saving reference into variable definately makes no changes.
You still havent told us what it is you want to do.
Ok i want to create a Starship creator with a morphable component. Example : youtube video
I stored build elements in a simple prefab with loaded fbx mesh. I need a copy of my selected prefab mesh data which i can morphing(change vertex data without original mesh data modifying).
First problem: Simple prefab instantiate. If i modifying vertex structure all clone and original vertex data modifying too.
Sencond Problem: If i try creating a clear mesh and copy all vertex, triangle, uv information from my donor/original prefab mesh, original prefab mesh data = null , and also modifying the original vertex data.
i hope this is answer for your question:) And sorry for my bad english, this is realy not my language.
I might be wrong, but this seems to bo done exclusively by rotation/translation/scale. Just bunch of composite components with carefully positioned pivots and scripting.
Note I testing the Instantiate() code above with a GameObject (not a Prefab) before posting, and it worked fine. Perhaps you need create a single GameObject from the prefab first that you use as a basis for your other ships. In my test, I saved off the original mesh, modified the Instantiated mesh, and then a few seconds later the code restored the original mesh.
Answer by alwynd · Oct 13, 2013 at 03:12 PM
Hi , you should be able to do this with a simply one liner, clone the mesh, and assign it back to the shared mesh, this will effectively break the instance, and you can make any changes to the vertices, and it will only affect that instance, of the skinnedmeshrenderer.
myRenderer.sharedMesh = (Mesh) Instantiate( myRenderer.sharedMesh );