- Home /
How do I invert normals of a sphere???
Hey, I want to create an atmosphere shader. I found someone used "sphere with the normals inverted" and using X - Ray Shader. (http://answers.unity3d.com/questions/45654/atmospheric-shader-help.html?sort=oldest) And I really want to do the same :) So my question is, How do I do that? I am Using this X - Ray Shader if you have to know: ftp://www.photonworkshop.com/photonworkshop.com/HostedFiles/Mobile-XrayEffect.shader
You can do that from your 3D application directly or you could try this ReverseNormals
I already tried this. It doesn't invert xray shader tho...
heh. Already went thru all of this stuff in google. If i would find something what works then i wont post here.
Answer by robertbu · Aug 13, 2013 at 11:34 PM
Here is an editor script that will produce an inverted sphere. Put it in your Assets/Editor folder of your project. Then GameObject > Create Other > Inverted Sphere...
using UnityEngine;
using UnityEditor;
public class InvertedSphere : EditorWindow {
private string st = "1.0";
[MenuItem("GameObject/Create Other/Inverted Sphere...")]
public static void ShowWindow() {
EditorWindow.GetWindow(typeof(InvertedSphere));
}
public void OnGUI() {
GUILayout.Label("Enter sphere size:");
st = GUILayout.TextField (st);
float f;
if (!float.TryParse (st, out f))
f = 1.0f;
if (GUILayout.Button("Create Inverted Sphere")) {
CreateInvertedSphere(f);
}
}
private void CreateInvertedSphere(float size) {
GameObject go = GameObject.CreatePrimitive(PrimitiveType.Sphere);
MeshFilter mf = go.GetComponent<MeshFilter>();
Mesh mesh = mf.sharedMesh;
GameObject goNew = new GameObject();
goNew.name = "Inverted Sphere";
MeshFilter mfNew = goNew.AddComponent<MeshFilter>();
mfNew.sharedMesh = new Mesh();
//Scale the vertices;
Vector3[] vertices = mesh.vertices;
for (int i = 0; i < vertices.Length; i++)
vertices[i] = vertices[i] * size;
mfNew.sharedMesh.vertices = vertices;
// Reverse the triangles
int[] triangles = mesh.triangles;
for (int i = 0; i < triangles.Length; i += 3) {
int t = triangles[i];
triangles[i] = triangles[i+2];
triangles[i+2] = t;
}
mfNew.sharedMesh.triangles = triangles;
// Reverse the normals;
Vector3[] normals = mesh.normals;
for (int i = 0; i < normals.Length; i++)
normals[i] = -normals[i];
mfNew.sharedMesh.normals = normals;
mfNew.sharedMesh.uv = mesh.uv;
mfNew.sharedMesh.uv2 = mesh.uv2;
mfNew.sharedMesh.RecalculateBounds();
// Add the same material that the original sphere used
MeshRenderer mr = goNew.AddComponent<MeshRenderer>();
mr.sharedMaterial = go.renderer.sharedMaterial;
DestroyImmediate(go);
}
}
It is indeed inverting the sphere but still, it doesn't invert x - ray shader :/. Results after adding x - ray shader to InvertedSphere are exacly the same as without using InvertedSphere :/ Any Ideas why?
I'm running into the same problem, did you ever solve it?
Answer by YoungDeveloper · Aug 13, 2013 at 10:42 PM
Here is a shader for back face rendering.
Shader "Backfaced Bumped Diffuse" {
Properties {
_Color ("Main Color", Color) = (1,1,1,1)
_MainTex ("Base (RGB)", 2D) = "white" {}
_BumpMap ("Normalmap", 2D) = "bump" {}
}
SubShader {
Tags { "RenderType"="Opaque" }
LOD 300
Cull Off
CGPROGRAM
#pragma surface surf Lambert
sampler2D _MainTex;
sampler2D _BumpMap;
fixed4 _Color;
struct Input {
float2 uv_MainTex;
float2 uv_BumpMap;
};
void surf (Input IN, inout SurfaceOutput o) {
fixed4 c = tex2D(_MainTex, IN.uv_MainTex) * _Color;
o.Albedo = c.rgb;
o.Alpha = c.a;
o.Normal = UnpackNormal(tex2D(_BumpMap, IN.uv_BumpMap));
}
ENDCG
}
FallBack "Diffuse"
}
To Use: In Unity Editor Right Click > Create > Shader, Name it Backface Bumped Diffuse, Open the shader with monodevelop and paste the code above, Now change your materials that are having issues to "Backface Bumped Diffuse".
I see, but that still don't change mt X - Ray Script. Take a look:
So it supposed to looks like Halo, but only looks and Don't dissapear while camera is nearby. Hope you know what I mean.
Your answer
![](https://koobas.hobune.stream/wayback/20220613112140im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Metaball wiki script help 0 Answers
Applying a texture to a sphere 1 Answer
Cube meshes turn into spheres on Android export 0 Answers
Setting triangles failing 0 Answers
How do I guarantee a generated sphere's mesh is facing outward? 1 Answer