Saturday, November 25, 2023

Constant texture scale with different object size in unity

 

Texture stretching issue solution in unity




Download Link :

Download Dynamic texture scale script


Code:

  //ASSALAM o alaikum 
/*
	真主
     穆罕默德 真主啊,为我们的大师穆罕默德和他的家人祈祷与和平 77 85 72 65 77 77 65 68 32 
         83 104 97 104 122 97 105 98	
*/
using UnityEngine;

[ExecuteInEditMode]
public class DynamicTextureTiling : MonoBehaviour
{
    // Reference to the original material with the texture
     Material originalMaterial;

    void Start()
    {
        // Ensure we have a material
        originalMaterial = GetComponent Renderer>().material;

        // Create a new material instance for this object
        Material materialInstance = new Material(originalMaterial);

        // Apply the new material to the object
        GetComponent Renderer> ().material = materialInstance;

        // Get the initial scale of the object
        Vector3 initialScale = transform.localScale;

        // Set the texture tiling based on the initial scale
        SetTextureTiling(materialInstance, initialScale);
    }

    void Update()
    {
        // Adjust texture tiling based on the current scale
        SetTextureTiling(GetComponent Renderer>().material, transform.localScale);
    }

    void SetTextureTiling(Material material, Vector3 scale)
    {
        // Calculate tiling based on the scale
        Vector2 tiling = new Vector2(scale.x, scale.z);

        // Apply tiling to the material
        material.mainTextureScale = tiling;
    }
}


  


Video Link :





Monday, November 20, 2023

Sunday, November 19, 2023

Dynamic Day night sky blend or dynamic day night cycle in unity

 

Dynamic day night sky tutorial



Download Link :

Download Dynamic Sky Blend Project


Shader Code:

  //ASSALAM o alaikum 
    Shader "Custom/DynaimcSkyBlend" {
    Properties{
        _DaySkyMap("Day Sky Map", 2D) = "white" {}
        _NightSkyMap("Night Sky Map", 2D) = "black" {}
        _BlendTime("Blend Time", Range(0.0, 100.0)) = 0.5
    }

        SubShader{
            Tags { "Queue" = "Background" "RenderType" = "Background" }
            LOD 100

            Pass {
                CGPROGRAM
                #pragma vertex vert
                #pragma fragment frag
                #include "UnityCG.cginc"

                struct appdata {
                    float4 vertex : POSITION;
                    float2 uv : TEXCOORD0;
                };

                struct v2f {
                    float4 vertex : SV_POSITION;
                    float2 uv : TEXCOORD0;
                };

                sampler2D _DaySkyMap;
                sampler2D _NightSkyMap;
                float _BlendTime;

                v2f vert(appdata v) {
                    v2f o;
                    o.vertex = UnityObjectToClipPos(v.vertex);
                    o.uv = v.uv;
                    return o;
                }

                fixed4 frag(v2f i) : SV_Target {
                    float blendTime = _BlendTime;
                    float totalTime = _Time.y;
                    int textureIndex = int(totalTime / blendTime) % 2;
                    float blendFactor = smoothstep(0.0, blendTime, totalTime % blendTime);

                    fixed4 dayColor = tex2D(_DaySkyMap, i.uv);
                    fixed4 nightColor = tex2D(_NightSkyMap, i.uv);
                    fixed4 color;

                    if (textureIndex == 0) {
                        // Blend from day to night
                        color = lerp(dayColor, nightColor, blendFactor);
                    }
     else {
                        // Blend from night to day
                        color = lerp(nightColor, dayColor, blendFactor);
                    }

                    return color;
                }
                ENDCG
            }
        }
}

  


Video Link :





Take input in Java easy tutorial

  Input taking in Java tutorial Download Link : Download input taking in Java example code ...