- Home /
Paint a transparent object at runtime
Hi guys!
I need to paint a texture on a transparent object at runtime.
On the wiki I've found a shader called "Dissolve with Texture" and, basically, I neeed to revert it.
// simple "dissolving" shader by genericuser (radware.wordpress.com)
// clips materials, using an image as guidance.
// use clouds or random noise as the slice guide for best results.
Shader "Custom Shaders/Dissolving" {
Properties {
_MainTex ("Texture (RGB)", 2D) = "white" {}
_SliceGuide ("Slice Guide (RGB)", 2D) = "white" {}
_SliceAmount ("Slice Amount", Range(0.0, 1.0)) = 0.5
}
SubShader {
Tags { "RenderType" = "Opaque" }
Cull Off
CGPROGRAM
//if you're not planning on using shadows, remove "addshadow" for better performance
#pragma surface surf Lambert addshadow
struct Input {
float2 uv_MainTex;
float2 uv_SliceGuide;
float _SliceAmount;
};
sampler2D _MainTex;
sampler2D _SliceGuide;
float _SliceAmount;
void surf (Input IN, inout SurfaceOutput o) {
clip(tex2D (_SliceGuide, IN.uv_SliceGuide).rgb - _SliceAmount);
o.Albedo = tex2D (_MainTex, IN.uv_MainTex).rgb;
}
ENDCG
}
Fallback "Diffuse"
}
Using this shader, at runtime I can "remove" some pixels clicking on a specific area of a mesh, but I need the opposite behaviour, that means to add part of a texture on an alpha object.
Here is the code I'm using to click on the mesh and select the area that must be painted
var hit : RaycastHit;
if (!Physics.Raycast (Camera.main.ScreenPointToRay(Input.mousePosition), hit)) return;
var renderer : Renderer = hit.collider.renderer;
var meshCollider = hit.collider as MeshCollider;
var tex : Texture2D = texture;
var pixelUV = hit.textureCoord;
pixelUV.x *= tex.width;
pixelUV.y *= tex.height;
tex.SetPixel(pixelUV.x, pixelUV.y, Color.black);
tex.Apply();
Can someone help me? I tried to modify the shader but I was not able to make it work as I need
Your answer
Follow this Question
Related Questions
Shader that does per Vert alpha with co 0 Answers
Z-priming alpha-per-vertex 0 Answers
Transparent terrain shader not working 2 Answers
Shader alpha depth problem? 1 Answer
Strange artifacts on Vertex Color Shader 0 Answers