2012-04-05 3 views
1

지우개 도구처럼 만들려고합니다. 사용자가 손가락을 텍스처로 드래그하면 해당 위치에서 텍스처가 투명 해집니다. 나는 아래의 코드를 사용하여 이것을 시도했다. (나는 온라인 예제를 발견하고 조금 수정했다.)하지만 너무 느리다. 배경 텍스처를 그릴 때 브러쉬 텍스처를 사용하고 있습니다.이 문제의 또 다른 해결책이 있습니까? 어쩌면 일부 셰이더를 사용하면 더 빨라지 겠지만 어떻게해야할지 모르겠다. 어떤 도움Unity3d -이 코드가 iPhone에서 너무 느립니다.

void Start() 
{ 
    stencilUV = new Color[stencil.width * stencil.height]; 
    tex = (Texture2D)Instantiate(paintMaterial.mainTexture); 
} 

void Update() 
{ 
    RaycastHit hit; 
    if (Input.touchCount == 0) return; 
    //if (!Input.GetMouseButton(0)) return; 
    if (Physics.Raycast(mainCamera.ScreenPointToRay(Input.mousePosition), out hit)) 
    { 
     pixelUV = hit.textureCoord; 
     pixelUV.x *= tex.width; 
     pixelUV.y *= tex.height; 

     CreateStencil((int)pixelUV.x, (int)pixelUV.y, stencil); 
    } 
} 

void CreateStencil(int x, int y, Texture2D texture) 
{ 
    paintMaterial.mainTexture = tex; 
    for (int xPix = 0; xPix<texture.width; xPix++) 
    { 
     for (int yPix=0;yPix<texture.height; yPix++) 
     { 
      stencilUV[i] = tex.GetPixel((x - texture.width/2) + xPix, 
       (y - texture.height/2) + yPix); 
      stencilUV[i].a = 0;//1-color.a; 
      i++; 
     } 
    } 
    i=0; 
    tex.SetPixels(x-texture.width/2, y-texture.height/2, texture.width, texture.height, stencilUV); 
    tex.Apply(); 
} 

답변

2

에 대한

덕분에 잘 아는 한, Update() 기능은 매 프레임마다 호출됩니다. 따라서 코드를 최적화해야하는 곳입니다. CreateStencil() 함수는 텍스처에있는 모든 픽셀을 반복하고 for 루프 내에서 tex.GetPixel(..)을 호출 할 때 꽤 비쌉니다. 즉, 화면을 터치하면 앱이 모든 프레임을이 프레임에서 모든 픽셀에 대해 실행하려고 시도합니다. 텍스처 - 확실히 갈 길이 아닙니다. Start() 또는 Awake() 함수의 다른 배열에 픽셀 정보를 버퍼링하고 Update() 내부의 필요한 부분 만 처리하려고합니다. iPhone에서 원활한 앱 실행을 위해서는 값 비싼 오퍼레이션을 필수적으로 배제해야합니다.

관련 문제