Skip to main content
Commonmark migration
Source Link

#Update

Update

#Update

Update

added 1140 characters in body
Source Link

2017-12-31_19-55-32awdawDisplayNormal

you can use Replacement Shader to Displaying Mesh Normals

First attach this script to camera:

then use this shader in above script for Replacing shaders

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CaptureMe : MonoBehaviour {
    public RenderTexture RT;
    public Camera SecondCamera;//Second Camera Renders Normal that store to RenderTexture
    public Shader shader;//shader that you want pass result to it

    void Update(){
    }

    void OnRenderImage(RenderTexture source,RenderTexture target){

        int resWidth = Screen.width;
        int resHeight = Screen.height;

        RT = new RenderTexture(resWidth, resHeight, 24);
        SecondCamera.targetTexture = RT; //Create new renderTexture and assign to camera
        Texture2D screenShot = new Texture2D(resWidth, resHeight, TextureFormat.RGB24, false); //Create new texture

        SecondCamera.Render();

        RenderTexture.active = RT;
        screenShot.ReadPixels(new Rect(0, 0, resWidth, resHeight), 0, 0); //Apply pixels from camera onto Texture2D

        SecondCamera.targetTexture = null;
        RenderTexture.active = null; //Clean


        Material mat = new Material(shader);

        mat.SetTexture ("_NormalScene", RT);

        Graphics.Blit (source, target,mat);


        Destroy(RT); //Free memory 



    }
}

#Update

DisplayNormal

If you need better result you should be calculating the world normals.

Shader "Tutorial/DisplayNormal2"
{
    SubShader

    {
        Tags { "RenderType" = "Opaque" }
        Pass
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag

            #include "UnityCG.cginc"

            struct v2f

            {
                float4 uv : TEXCOORD0;
                float4 vertex : SV_POSITION;

            };
            v2f vert(appdata_base v)
            {
                v2f o;
                o.vertex = UnityObjectToClipPos(v.vertex);
                o.uv.xyz = UnityObjectToWorldNormal(v.normal);
                return o;
            }
            fixed4 frag (v2f i) : SV_Target
            {
                float3 c = i.uv.xyz;
                return float4(c, 1);
            }
            ENDCG
        }
    }
}

2017-12-31_19-55-32awdaw

attach this script to camera:

use this shader in above script for Replacing shaders

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CaptureMe : MonoBehaviour {
    public RenderTexture RT;
    public Camera SecondCamera;//Second Camera Renders Normal that store to RenderTexture
    public Shader shader;//shader that you want pass result to it

    void Update(){
    }

    void OnRenderImage(RenderTexture source,RenderTexture target){

        int resWidth = Screen.width;
        int resHeight = Screen.height;

        RT = new RenderTexture(resWidth, resHeight, 24);
        SecondCamera.targetTexture = RT; //Create new renderTexture and assign to camera
        Texture2D screenShot = new Texture2D(resWidth, resHeight, TextureFormat.RGB24, false); //Create new texture

        SecondCamera.Render();

        RenderTexture.active = RT;
        screenShot.ReadPixels(new Rect(0, 0, resWidth, resHeight), 0, 0); //Apply pixels from camera onto Texture2D

        SecondCamera.targetTexture = null;
        RenderTexture.active = null; //Clean


        Material mat = new Material(shader);

        mat.SetTexture ("_NormalScene", RT);

        Graphics.Blit (source, target,mat);


        Destroy(RT); //Free memory



    }
}

DisplayNormal

you can use Replacement Shader to Displaying Mesh Normals

First attach this script to camera:

then use this shader in above script for Replacing shaders

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CaptureMe : MonoBehaviour {
    public RenderTexture RT;
    public Camera SecondCamera;//Second Camera Renders Normal that store to RenderTexture
    public Shader shader;//shader that you want pass result to it

    void Update(){
    }

    void OnRenderImage(RenderTexture source,RenderTexture target){

        int resWidth = Screen.width;
        int resHeight = Screen.height;

        RT = new RenderTexture(resWidth, resHeight, 24);
        SecondCamera.targetTexture = RT; //Create new renderTexture and assign to camera
        Texture2D screenShot = new Texture2D(resWidth, resHeight, TextureFormat.RGB24, false); //Create new texture

        SecondCamera.Render();

        RenderTexture.active = RT;
        screenShot.ReadPixels(new Rect(0, 0, resWidth, resHeight), 0, 0); //Apply pixels from camera onto Texture2D

        SecondCamera.targetTexture = null;
        RenderTexture.active = null; //Clean


        Material mat = new Material(shader);

        mat.SetTexture ("_NormalScene", RT);

        Graphics.Blit (source, target,mat);


        Destroy(RT); //Free memory 



    }
}

#Update

DisplayNormal

If you need better result you should be calculating the world normals.

Shader "Tutorial/DisplayNormal2"
{
    SubShader

    {
        Tags { "RenderType" = "Opaque" }
        Pass
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag

            #include "UnityCG.cginc"

            struct v2f

            {
                float4 uv : TEXCOORD0;
                float4 vertex : SV_POSITION;

            };
            v2f vert(appdata_base v)
            {
                v2f o;
                o.vertex = UnityObjectToClipPos(v.vertex);
                o.uv.xyz = UnityObjectToWorldNormal(v.normal);
                return o;
            }
            fixed4 frag (v2f i) : SV_Target
            {
                float3 c = i.uv.xyz;
                return float4(c, 1);
            }
            ENDCG
        }
    }
}
deleted 112 characters in body
Source Link
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CaptureMe : MonoBehaviour {
    public RenderTexture RT;
    public Camera SecondCamera;//Second Camera Renders Normal that store to RenderTexture
    public Shader shader;//shader that you want pass result to it

    void Update(){
    }

    void OnRenderImage(RenderTexture source,RenderTexture target){
        RenderTexture texture = new RenderTexture(Screen.width , Screen.height, 16, RenderTextureFormat.ARGB32);

        int resWidth = Screen.width;
        int resHeight = Screen.height;

        RT = new RenderTexture(resWidth, resHeight, 24);
        SecondCamera.targetTexture = RT; //Create new renderTexture and assign to camera
        Texture2D screenShot = new Texture2D(resWidth, resHeight, TextureFormat.RGB24, false); //Create new texture

        SecondCamera.Render();

        RenderTexture.active = RT;
        screenShot.ReadPixels(new Rect(0, 0, resWidth, resHeight), 0, 0); //Apply pixels from camera onto Texture2D

        SecondCamera.targetTexture = null;
        RenderTexture.active = null; //Clean


        Material mat = new Material(shader);

        mat.SetTexture ("_NormalScene", RT);

        Graphics.Blit (source, target,mat);


        Destroy(RT); //Free memory



    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CaptureMe : MonoBehaviour {
    public RenderTexture RT;
    public Camera SecondCamera;//Second Camera Renders Normal that store to RenderTexture
    public Shader shader;//shader that you want pass result to it

    void Update(){
    }

    void OnRenderImage(RenderTexture source,RenderTexture target){
        RenderTexture texture = new RenderTexture(Screen.width , Screen.height, 16, RenderTextureFormat.ARGB32);

        int resWidth = Screen.width;
        int resHeight = Screen.height;

        RT = new RenderTexture(resWidth, resHeight, 24);
        SecondCamera.targetTexture = RT; //Create new renderTexture and assign to camera
        Texture2D screenShot = new Texture2D(resWidth, resHeight, TextureFormat.RGB24, false); //Create new texture

        SecondCamera.Render();

        RenderTexture.active = RT;
        screenShot.ReadPixels(new Rect(0, 0, resWidth, resHeight), 0, 0); //Apply pixels from camera onto Texture2D

        SecondCamera.targetTexture = null;
        RenderTexture.active = null; //Clean


        Material mat = new Material(shader);

        mat.SetTexture ("_NormalScene", RT);

        Graphics.Blit (source, target,mat);


        Destroy(RT); //Free memory



    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CaptureMe : MonoBehaviour {
    public RenderTexture RT;
    public Camera SecondCamera;//Second Camera Renders Normal that store to RenderTexture
    public Shader shader;//shader that you want pass result to it

    void Update(){
    }

    void OnRenderImage(RenderTexture source,RenderTexture target){

        int resWidth = Screen.width;
        int resHeight = Screen.height;

        RT = new RenderTexture(resWidth, resHeight, 24);
        SecondCamera.targetTexture = RT; //Create new renderTexture and assign to camera
        Texture2D screenShot = new Texture2D(resWidth, resHeight, TextureFormat.RGB24, false); //Create new texture

        SecondCamera.Render();

        RenderTexture.active = RT;
        screenShot.ReadPixels(new Rect(0, 0, resWidth, resHeight), 0, 0); //Apply pixels from camera onto Texture2D

        SecondCamera.targetTexture = null;
        RenderTexture.active = null; //Clean


        Material mat = new Material(shader);

        mat.SetTexture ("_NormalScene", RT);

        Graphics.Blit (source, target,mat);


        Destroy(RT); //Free memory



    }
}
added 1544 characters in body
Source Link
Loading
Source Link
Loading