Guest User

Untitled

a guest
Nov 10th, 2017
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #pragma strict
  2.  
  3. //@HideInInspector
  4. var verts:Vector3[]= new Vector3[0];
  5. //@HideInInspector
  6. var tris:int[]= new int[0];
  7. //@HideInInspector
  8. var uvs:Vector2[] = new Vector2[0];
  9. var size:int;
  10. var tilesetSize:float;//4,8,16
  11. var epsilon: float;
  12. var emptyObject:Transform;
  13.  
  14. function Start ()
  15. {
  16.     CreateRectangularStarterRoom();
  17. }
  18.  
  19. function Update ()
  20. {
  21.    
  22. }
  23.  
  24. var vertsTier:int;
  25. var trisTier:int;
  26.  
  27. function CreateRectangularStarterRoom()
  28. {
  29.     var roomSize:Vector3=Vector3(Mathf.RoundToInt(Random.Range(6,12)),Mathf.RoundToInt(Random.Range(2,2)),Mathf.RoundToInt(Random.Range(6,12)));
  30.     print("roomSize= "+roomSize);
  31.     var numberOfDoors:int=Random.Range(4,5);
  32.     var numberOfVerts:int=(roomSize.x*roomSize.z*2*4)+(roomSize.x*roomSize.y*2*4)+(roomSize.z*roomSize.y*2*4);//-(numberOfDoors*2*4);
  33.     verts=new Vector3[numberOfVerts];
  34.     var numberOfTris:int=(roomSize.x*roomSize.z*2*2*3)+(roomSize.x*roomSize.y*2*2*3)+(roomSize.z*roomSize.y*2*2*3);//-(numberOfDoors*2*2*3);
  35.     tris=new int[numberOfTris];
  36.     uvs=new Vector2[numberOfVerts];
  37.     var doorLocations:Vector3[]=new Vector3[numberOfDoors];
  38.     var doorsSet:int;
  39.     while(doorsSet<numberOfDoors)
  40.     {
  41.         var farOrNear:int=Random.Range(0,2);
  42.         var xorz:int=Random.Range(0,2);
  43.         var newDoor:Vector3=new Vector3();
  44.         if(xorz==0) //along x axis
  45.         {
  46.             var doNotSetA:boolean=false;
  47.             newDoor=Vector3(Mathf.RoundToInt(Random.Range(1,roomSize.x-2)),0,Mathf.RoundToInt(Random.Range(62,64)));
  48.             for(var i=0;i<doorsSet;i++)
  49.             {
  50.                 if(doorLocations[i].z==newDoor.z)
  51.                 {
  52.                     print("1A= "+newDoor);
  53.                     if(Mathf.Abs(doorLocations[i].x-newDoor.x)<=1)
  54.                     {
  55.                         print("2A= "+newDoor);
  56.                         doNotSetA=true;
  57.                     }
  58.                 }
  59.             }
  60.             if(!doNotSetA)
  61.             {
  62.                 doorLocations[doorsSet]=newDoor;
  63.                 print("door "+doorsSet+" "+doorLocations[doorsSet]);
  64.                 doorsSet++;
  65.             }
  66.         }
  67.         else
  68.         {
  69.             var doNotSetB:boolean=false;
  70.             newDoor=Vector3(Mathf.RoundToInt(Random.Range(62,64)),0,Mathf.RoundToInt(Random.Range(1,roomSize.z-2)));
  71.             for(var j=0;j<doorsSet;j++)
  72.             {
  73.                 if(doorLocations[j].x==newDoor.x)
  74.                 {
  75.                     print("1B= "+newDoor);
  76.                     if(Mathf.Abs(doorLocations[j].z-newDoor.z)<=1)
  77.                     {
  78.                         print("2B= "+newDoor);
  79.                         doNotSetB=true;
  80.                     }
  81.                 }
  82.             }
  83.         if(!doNotSetB)
  84.             {
  85.                 doorLocations[doorsSet]=newDoor;
  86.                 print("door "+doorsSet+" "+doorLocations[doorsSet]);
  87.                 doorsSet++;
  88.             }
  89.         }
  90.     }
  91.     CreateWall(0,Mathf.FloorToInt(roomSize.x*roomSize.z*4),size,0,0,0,size,0,0,0,0,[4,17,0],[2,1,0,3,2,0],[true,false,false,false,false,true],Mathf.FloorToInt(roomSize.x));
  92.     CreateWall(vertsTier,Mathf.FloorToInt(vertsTier+roomSize.x*roomSize.z*4),size,0,0,0,size,0,0,roomSize.y,0,[0,16,0],[0,1,2,0,2,3],[true,false,false,false,false,true],Mathf.FloorToInt(roomSize.x));
  93.     var xStart1:int=vertsTier;
  94.     CreateWall(vertsTier,Mathf.FloorToInt(vertsTier+roomSize.x*roomSize.y*4),0,size,size,0,0,0,0,0,0,[6,4,0],[0,1,2,0,2,3],[true,false,false,false,true,false],Mathf.FloorToInt(roomSize.x));
  95.     var xStart2:int=vertsTier;
  96.     CreateWall(vertsTier,Mathf.FloorToInt(vertsTier+roomSize.x*roomSize.y*4),0,size,size,0,0,0,0,0,roomSize.z,[6,4,0],[2,1,0,3,2,0],[true,false,false,false,true,false],Mathf.FloorToInt(roomSize.x));
  97.     var zStart1:int=vertsTier;
  98.     CreateWall(vertsTier,Mathf.FloorToInt(vertsTier+roomSize.y*roomSize.z*4),0,0,0,size,0,size,0,0,0,[6,4,1],[0,1,2,0,2,3],[false,false,true,false,true,false],Mathf.FloorToInt(roomSize.z));
  99.     var zStart2:int=vertsTier;
  100.     CreateWall(vertsTier,Mathf.FloorToInt(vertsTier+roomSize.y*roomSize.z*4),0,0,0,size,0,size,roomSize.x,0,0,[6,4,1],[2,1,0,3,2,0],[false,false,true,false,true,false],Mathf.FloorToInt(roomSize.z));
  101.    
  102.     var hallwayStarts:Vector3[]=new Vector3[doorLocations.length];
  103.     for(var z=0;z<doorLocations.length;z++)//make doors
  104.     {
  105.         if(doorLocations[z].x>0&&doorLocations[z].x<60)
  106.         {
  107.             if(doorLocations[z].z==62)
  108.             {
  109.                 hallwayStarts[z]=verts[xStart1+doorLocations[z].x*4];
  110.                 print("hallwayStarts[z]= "+hallwayStarts[z]);
  111.                 verts[xStart1+doorLocations[z].x*4]=Vector3.zero;
  112.                 verts[xStart1+doorLocations[z].x*4+1]=Vector3.zero;
  113.                 verts[xStart1+doorLocations[z].x*4+2]=Vector3.zero;
  114.                 verts[xStart1+doorLocations[z].x*4+3]=Vector3.zero;
  115.                 /* verts[xStart1+doorLocations[z].x*4+roomSize.x*4]=Vector3.zero;
  116.                 verts[xStart1+doorLocations[z].x*4+roomSize.x*4+1]=Vector3.zero;
  117.                 verts[xStart1+doorLocations[z].x*4+roomSize.x*4+2]=Vector3.zero;
  118.                 verts[xStart1+doorLocations[z].x*4+roomSize.x*4+3]=Vector3.zero; */
  119.             }
  120.             if(doorLocations[z].z==63)
  121.             {
  122.                 hallwayStarts[z]=verts[xStart2+doorLocations[z].x*4];
  123.                 verts[xStart2+doorLocations[z].x*4]=Vector3.zero;
  124.                 verts[xStart2+doorLocations[z].x*4+1]=Vector3.zero;
  125.                 verts[xStart2+doorLocations[z].x*4+2]=Vector3.zero;
  126.                 verts[xStart2+doorLocations[z].x*4+3]=Vector3.zero;
  127.                 /* verts[xStart2+doorLocations[z].x*4+roomSize.x*4]=Vector3.zero;
  128.                 verts[xStart2+doorLocations[z].x*4+roomSize.x*4+1]=Vector3.zero;
  129.                 verts[xStart2+doorLocations[z].x*4+roomSize.x*4+2]=Vector3.zero;
  130.                 verts[xStart2+doorLocations[z].x*4+roomSize.x*4+3]=Vector3.zero; */
  131.                
  132.             }
  133.         }
  134.         if(doorLocations[z].z>0&&doorLocations[z].z<60)
  135.         {
  136.             if(doorLocations[z].x==62)
  137.             {
  138.                 hallwayStarts[z]=verts[zStart1+doorLocations[z].z*4];
  139.                 verts[zStart1+doorLocations[z].z*4]=Vector3.zero;
  140.                 verts[zStart1+doorLocations[z].z*4+1]=Vector3.zero;
  141.                 verts[zStart1+doorLocations[z].z*4+2]=Vector3.zero;
  142.                 verts[zStart1+doorLocations[z].z*4+3]=Vector3.zero;
  143.                 /* verts[zStart1+doorLocations[z].z*4+roomSize.z*4]=Vector3.zero;
  144.                 verts[zStart1+doorLocations[z].z*4+roomSize.z*4+1]=Vector3.zero;
  145.                 verts[zStart1+doorLocations[z].z*4+roomSize.z*4+2]=Vector3.zero;
  146.                 verts[zStart1+doorLocations[z].z*4+roomSize.z*4+3]=Vector3.zero; */
  147.                
  148.             }
  149.             if(doorLocations[z].x==63)
  150.             {
  151.                 hallwayStarts[z]=verts[zStart2+doorLocations[z].z*4];
  152.                 verts[zStart2+doorLocations[z].z*4]=Vector3.zero;
  153.                 verts[zStart2+doorLocations[z].z*4+1]=Vector3.zero;
  154.                 verts[zStart2+doorLocations[z].z*4+2]=Vector3.zero;
  155.                 verts[zStart2+doorLocations[z].z*4+3]=Vector3.zero;
  156.                 /* verts[zStart2+doorLocations[z].z*4+roomSize.z*4]=Vector3.zero;
  157.                 verts[zStart2+doorLocations[z].z*4+roomSize.z*4+1]=Vector3.zero;
  158.                 verts[zStart2+doorLocations[z].z*4+roomSize.z*4+2]=Vector3.zero;
  159.                 verts[zStart2+doorLocations[z].z*4+roomSize.z*4+3]=Vector3.zero; */
  160.                
  161.             }
  162.         }
  163.     }
  164.        
  165.     var newMesh:Mesh=transform.GetComponent(MeshFilter).mesh;
  166.     newMesh.Clear();
  167.     newMesh.vertices=verts;
  168.     newMesh.triangles=tris;
  169.     newMesh.uv=uvs;
  170.     newMesh.RecalculateNormals();
  171.     transform.GetComponent(MeshCollider).sharedMesh=newMesh;
  172.     for(var a=0;a<doorLocations.length;a++)
  173.     {
  174.         if(doorLocations[a].z==62)
  175.         {
  176.             CreateHallway(hallwayStarts[a],0);
  177.         }
  178.         if(doorLocations[a].z==63)
  179.         {
  180.             CreateHallway(hallwayStarts[a],1);
  181.         }
  182.         if(doorLocations[a].x==62)
  183.         {
  184.            
  185.         }
  186.         if(doorLocations[a].x==63)
  187.         {
  188.            
  189.         }
  190.     }
  191. }
  192. var slow:boolean;
  193. function CreateHallway(hallStart:Vector3,dir:int)
  194. {
  195.     print("hallStart= "+hallStart+" dir= "+dir);
  196.     var hall:Transform=Instantiate(emptyObject,Vector3(0,0,0),Quaternion.identity);
  197.     var hallLength:int=Random.Range(2,8);
  198.     var numberOfVerts:int=(hallLength*16);
  199.     verts=new Vector3[numberOfVerts];
  200.     var numberOfTris:int=(hallLength*8*3);
  201.     tris=new int[numberOfTris];
  202.     uvs=new Vector2[numberOfVerts];
  203.     vertsTier=0;
  204.     trisTier=0;
  205.     switch (dir)
  206.     {
  207.         case 0:
  208.         CreateWall(0,Mathf.FloorToInt(hallLength*4),size,0,0,0,size,0,hallStart.x/size,hallStart.y/size,hallStart.z/size-hallLength,[4,17,0],[2,1,0,3,2,0],[true,false,false,false,false,true],1);
  209.         print("side hall starts at "+vertsTier);
  210.         CreateWall(vertsTier,Mathf.FloorToInt(vertsTier+hallLength*4),0,0,size,0,size,0,hallStart.x/size+1,hallStart.y/size,hallStart.z/size-hallLength,[6,4,0],[0,1,2,0,2,3],[false,true,false,false,false,true],1);
  211.         print("side hall ends at "+vertsTier);
  212.         break;
  213.         case 1:
  214.         CreateWall(0,Mathf.FloorToInt(hallLength*4),size,0,0,0,size,0,hallStart.x/size,hallStart.y/size,hallStart.z/size,[4,17,0],[2,1,0,3,2,0],[true,false,false,false,false,true],1);
  215.         break;
  216.     }
  217.  
  218.    
  219.     var newMesh:Mesh=hall.GetComponent(MeshFilter).mesh;
  220.     newMesh.Clear();
  221.     newMesh.vertices=verts;
  222.     newMesh.triangles=tris;
  223.     newMesh.uv=uvs;
  224.     newMesh.RecalculateNormals();
  225.     hall.GetComponent(MeshCollider).sharedMesh=newMesh;
  226.     print("hallways done");
  227. }
  228.  
  229. function CreateWall(vertStart:int,vertEnd:int,incXa:int,incXb:int,incYa:int,incYb:int,incZa:int,incZb:int,xStart:int,yStart:int,zStart:int,uv:int[],trisIncrease:int[],along:boolean[],width:int)
  230. {
  231.     var uvX:Vector4;
  232.     var uvY:Vector4;
  233.     if(uv[2]==0)//bottom left clockwise
  234.     {
  235.         uvX=Vector4((uv[0]+0)/tilesetSize+epsilon,(uv[0]+0)/tilesetSize+epsilon,(uv[0]+1)/tilesetSize-epsilon,(uv[0]+1)/tilesetSize-epsilon);
  236.         uvY=Vector4((uv[1]+0)/tilesetSize+epsilon,(uv[1]+1)/tilesetSize-epsilon,(uv[1]+1)/tilesetSize-epsilon,(uv[1]+0)/tilesetSize+epsilon);
  237.     }
  238.     if(uv[2]==1)//top left clockwise
  239.     {
  240.         uvX=Vector4((uv[0]+0)/tilesetSize+epsilon,(uv[0]+1)/tilesetSize-epsilon,(uv[0]+1)/tilesetSize-epsilon,(uv[0]+0)/tilesetSize+epsilon);
  241.         uvY=Vector4((uv[1]+1)/tilesetSize-epsilon,(uv[1]+1)/tilesetSize-epsilon,(uv[1]+0)/tilesetSize+epsilon,(uv[1]+0)/tilesetSize+epsilon);
  242.     }
  243.     if(uv[2]==2)//top right clockwise
  244.     {
  245.         uvX=Vector4((uv[0]+1)/tilesetSize-epsilon,(uv[0]+1)/tilesetSize-epsilon,(uv[0]+0)/tilesetSize+epsilon,(uv[0]+0)/tilesetSize+epsilon);
  246.         uvY=Vector4((uv[1]+1)/tilesetSize-epsilon,(uv[1]+0)/tilesetSize+epsilon,(uv[1]+0)/tilesetSize+epsilon,(uv[1]+1)/tilesetSize-epsilon);
  247.     }
  248.     if(uv[2]==3)//bottom right clockwise
  249.     {
  250.         uvX=Vector4((uv[0]+1)/tilesetSize-epsilon,(uv[0]+0)/tilesetSize+epsilon,(uv[0]+0)/tilesetSize+epsilon,(uv[0]+1)/tilesetSize-epsilon);
  251.         uvY=Vector4((uv[1]+0)/tilesetSize+epsilon,(uv[1]+0)/tilesetSize+epsilon,(uv[1]+1)/tilesetSize-epsilon,(uv[1]+1)/tilesetSize-epsilon);
  252.     }
  253.     var xTier:int;
  254.     var yTier:int;
  255.     var zTier:int;
  256.     for(var i=vertStart;i<vertEnd;i+=4)
  257.     {
  258.         verts[i+0]=Vector3(xTier*size+xStart*size+incXa,            yTier*size+yStart*size,                 zTier*size+zStart*size);
  259.         verts[i+1]=Vector3(xTier*size+xStart*size+incXa+incXb,      yTier*size+yStart*size+incYb,           zTier*size+zStart*size+incZa);
  260.         verts[i+2]=Vector3(xTier*size+xStart*size+incXb,            yTier*size+yStart*size+incYa+incYb,     zTier*size+zStart*size+incZa+incZb);
  261.         verts[i+3]=Vector3(xTier*size+xStart*size,                  yTier*size+yStart*size+incYa,           zTier*size+zStart*size+incZb);
  262.        
  263.         uvs[i+0]=Vector2(uvX.w,uvY.w);
  264.         uvs[i+1]=Vector2(uvX.x,uvY.x);
  265.         uvs[i+2]=Vector2(uvX.y,uvY.y);
  266.         uvs[i+3]=Vector2(uvX.z,uvY.z);
  267.        
  268.         tris[trisTier+0]=i+trisIncrease[0];         tris[trisTier+1]=i+trisIncrease[1];     tris[trisTier+2]=i+trisIncrease[2];
  269.         tris[trisTier+3]=i+trisIncrease[3];         tris[trisTier+4]=i+trisIncrease[4];     tris[trisTier+5]=i+trisIncrease[5];
  270.        
  271.         trisTier+=6;
  272.         vertsTier+=4;
  273.         if(along[0])
  274.         {
  275.             xTier++;
  276.             if(xTier>=width)
  277.             {
  278.                 xTier=0;
  279.                 if(along[4])
  280.                 {
  281.                     yTier++;
  282.                 }
  283.                 if(along[5])
  284.                 {
  285.                     zTier++;
  286.                 }
  287.             }
  288.         }
  289.         if(along[1])
  290.         {
  291.             yTier++;
  292.             if(yTier>=width)
  293.             {
  294.                 yTier=0;
  295.                 if(along[3])
  296.                 {
  297.                     xTier++;
  298.                 }
  299.                 if(along[5])
  300.                 {
  301.                     zTier++;
  302.                 }
  303.             }
  304.         }
  305.         if(along[2])
  306.         {
  307.             zTier++;
  308.             if(zTier>=width)
  309.             {
  310.                 zTier=0;
  311.                 if(along[3])
  312.                 {
  313.                     xTier++;
  314.                 }
  315.                 if(along[4])
  316.                 {
  317.                     yTier++;
  318.                 }
  319.             }
  320.         }
  321.     }
  322. }
Advertisement
Add Comment
Please, Sign In to add comment