Search Unity

How To Combine Texture And Color With Cubemap Reflections

Discussion in 'Shaders' started by ippdev, Oct 22, 2014.

  1. ippdev

    ippdev

    Joined:
    Feb 7, 2010
    Posts:
    3,853
    I am learning how to build shaders. I have nice reflections from the cube map and wanted to add color and texture maps to blend with the reflections. The slots are there and I have hammered away trying various code insertions from various wikis, blogs and examples from this forum but am having a time getting my head around the calls you can make and the syntax. What i have tried so far gave me hot pink..ouch..or did nothing at all. Here is what I have so far that did not throw errors or hot pink. Texture slot accepts textures unlike other shaders I tried and can change the diffuse to enhance the texture..which is a goal of this shader. Also..is this as optimized as it could be for mobile. I really want to get this under my thumb and understand it if anybody could be of help. How do i combine the outputs?

    Code (CSharp):
    1. Shader "GLSLMobileDiffuseReflections" {
    2.    Properties {
    3.       _Cube("Reflection Map", Cube) = "" {}
    4.       _Color ("Diffuse Material Color", Color) = (1,1,1,1)
    5.       _MainTex ("Base (RGB)", 2D) = "white" { }
    6.    }
    7.    SubShader {
    8.       Pass {  
    9.          GLSLPROGRAM
    10.          // User-specified uniforms
    11.          uniform vec4 _Color; // shader property specified by users
    12.          uniform samplerCube _Cube;  
    13.          // The following built-in uniforms
    14.          // are also defined in "UnityCG.glslinc",
    15.          // i.e. one could #include "UnityCG.glslinc"
    16.          uniform vec3 _WorldSpaceCameraPos;
    17.             // camera position in world space
    18.          uniform mat4 _Object2World; // model matrix
    19.          uniform mat4 _World2Object; // inverse model matrix
    20.          // Varyings        
    21.          varying vec3 normalDirection;
    22.          varying vec3 viewDirection;
    23.          #ifdef VERTEX
    24.          void main()
    25.          {          
    26.             mat4 modelMatrix = _Object2World;
    27.             mat4 modelMatrixInverse = _World2Object; // unity_Scale.w
    28.                // is unnecessary because we normalize vectors
    29.             normalDirection = normalize(vec3(
    30.                vec4(gl_Normal, 0.0) * modelMatrixInverse));
    31.             viewDirection = vec3(modelMatrix * gl_Vertex
    32.                - vec4(_WorldSpaceCameraPos, 1.0));
    33.             gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
    34.          }
    35.          #endif
    36.          #ifdef FRAGMENT
    37.          void main()
    38.          {
    39.             vec3 reflectedDirection =
    40.                reflect(viewDirection, normalize(normalDirection));
    41.             gl_FragColor = textureCube(_Cube, reflectedDirection);
    42.          }
    43.          #endif
    44.          ENDGLSL
    45.       }
    46.    }
    47. }
    48.  
     
  2. showoff

    showoff

    Joined:
    Apr 28, 2009
    Posts:
    273